Blame view

stim/cuda/cost.h 4.08 KB
7e099e80   Pavel Govyadinov   "lots of stuff, t...
1
2
3
4
  #include <assert.h>
  #include <cuda.h>
  #include <cuda_runtime.h>
  #include <cublas_v2.h>
7e099e80   Pavel Govyadinov   "lots of stuff, t...
5
6
  #include <stdio.h>
  #include "../visualization/colormap.h"
42145f38   Pavel Govyadinov   Fixed the issues ...
7
  #include <sstream>
7e099e80   Pavel Govyadinov   "lots of stuff, t...
8
  
556c4e15   Pavel Govyadinov   Changed the handl...
9
10
  
  ///Cost function that works with the gl-spider class to find index of the item with min-cost.
7e099e80   Pavel Govyadinov   "lots of stuff, t...
11
  typedef unsigned char uchar;
32c433c7   Pavel Govyadinov   recovered the maj...
12
  texture<uchar, cudaTextureType2D, cudaReadModeElementType> texIn;
7e099e80   Pavel Govyadinov   "lots of stuff, t...
13
14
15
  float *result;
  float* v_dif;
  cudaArray* srcArray;
4f5b240a   Pavel Govyadinov   minor change to i...
16
  bool testing = false;
7e099e80   Pavel Govyadinov   "lots of stuff, t...
17
18
19
20
21
22
23
24
25
26
  
  inline void checkCUDAerrors(const char *msg)
  {
  	cudaError_t err = cudaGetLastError();
  	if (cudaSuccess != err){
  		fprintf(stderr, "Cuda error: %s: %s.\n", msg, cudaGetErrorString(err) );
  		exit(1);
  		}
  }
  
556c4e15   Pavel Govyadinov   Changed the handl...
27
28
29
  ///Finds the sum of all the pixes in a gives template element.
  ///Returns the abosolute value.
  ///@param *diff, a pointer to the memory block that holds the pixel-differences.
7e099e80   Pavel Govyadinov   "lots of stuff, t...
30
31
32
33
34
35
36
  float get_sum(float *diff)
  {
  
  	cublasStatus_t ret;
  	cublasHandle_t handle;
  	ret = cublasCreate(&handle);
  	
a9f956be   Pavel Govyadinov   Fixed the cost fu...
37
  	ret = cublasSetVector(20*10, sizeof(*diff), diff, 1, v_dif, 1);
7e099e80   Pavel Govyadinov   "lots of stuff, t...
38
  	float out;
a9f956be   Pavel Govyadinov   Fixed the cost fu...
39
  	ret = cublasSasum(handle, 20*10, v_dif, 1, &out);
7e099e80   Pavel Govyadinov   "lots of stuff, t...
40
  	cublasDestroy(handle);
7e099e80   Pavel Govyadinov   "lots of stuff, t...
41
42
43
  	return out;
  }
  
556c4e15   Pavel Govyadinov   Changed the handl...
44
45
46
  ///A virtual representation of a uniform template.
  ///Returns the value of the template pixel.
  ///@param x, location of a pixel.
7e099e80   Pavel Govyadinov   "lots of stuff, t...
47
48
  __device__ float Template(int x)
  {
a9f956be   Pavel Govyadinov   Fixed the cost fu...
49
  	if(x < 20/6 || x > 20*5/6 || (x > 20*2/6 && x < 20*4/6)){
7e099e80   Pavel Govyadinov   "lots of stuff, t...
50
51
52
53
54
55
56
  		return 1.0;
  	}else{
  		return 0.0;
  	}
  
  }
  
556c4e15   Pavel Govyadinov   Changed the handl...
57
58
59
  ///Find the difference of the given set of samples and the template
  ///using cuda acceleration.
  ///@param *result, a pointer to the memory that stores the result.
7e099e80   Pavel Govyadinov   "lots of stuff, t...
60
61
62
63
64
65
  __global__
  void get_diff (float *result)
  {	
  	//cuPrintf("Hello");
  	int x 	= threadIdx.x + blockIdx.x * blockDim.x;
  	int y 	= threadIdx.y + blockIdx.y * blockDim.y;
5e7c7581   Pavel Govyadinov   Debugging build f...
66
  	int idx = y*20+x;	
7e099e80   Pavel Govyadinov   "lots of stuff, t...
67
  
a9f956be   Pavel Govyadinov   Fixed the cost fu...
68
  	float valIn		= tex2D(texIn, x, y)/255.0;
7e099e80   Pavel Govyadinov   "lots of stuff, t...
69
  	float valTemp		= Template(x);
556c4e15   Pavel Govyadinov   Changed the handl...
70
  	result[idx]		= abs(valIn-valTemp);
7e099e80   Pavel Govyadinov   "lots of stuff, t...
71
72
73
74
  }
  
  
  
556c4e15   Pavel Govyadinov   Changed the handl...
75
76
77
78
  ///Initialization function, allocates the memory and passes the necessary
  ///handles from OpenGL and Cuda.
  ///@param src, cudaGraphicsResource that handles the shared OpenGL/Cuda Texture
  ///@param DIM_Y, integer controlling how much memory to allocate.
5e7c7581   Pavel Govyadinov   Debugging build f...
79
  void initArray(cudaGraphicsResource_t src, int DIM_Y)
7e099e80   Pavel Govyadinov   "lots of stuff, t...
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
  {
  	//cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc<uchar> ();
  	//cudaMallocArray(&result, &channelDesc, DIM_X, DIM_Y, 0);
  	//HANDLE_ERROR(
  	//	cudaGraphicsGLRegisterImage(&src,
  	//		fboID,
  	//		GL_TEXTURE_2D,
  	HANDLE_ERROR(
  		cudaGraphicsMapResources(1, &src)	
  	);
  	HANDLE_ERROR(
  		cudaGraphicsSubResourceGetMappedArray(&srcArray, src,0,0)
  		);
  	HANDLE_ERROR(
  		cudaBindTextureToArray(texIn, srcArray)
  		);
5e7c7581   Pavel Govyadinov   Debugging build f...
96
  	cudaMalloc( (void**) &result, 20*DIM_Y*sizeof(float));
7e099e80   Pavel Govyadinov   "lots of stuff, t...
97
          checkCUDAerrors("Memory Allocation Issue 1");	
a9f956be   Pavel Govyadinov   Fixed the cost fu...
98
  	cudaMalloc((void **) &v_dif, 20*10*sizeof(float));
7e099e80   Pavel Govyadinov   "lots of stuff, t...
99
100
101
102
103
          checkCUDAerrors("Memory Allocation Issue 2");	
  	//HANDLE_ERROR(
  	//	cudaBindTextureToArray(texIn, ptr, &channelDesc)
  	//	    );		
  }
556c4e15   Pavel Govyadinov   Changed the handl...
104
105
106
  ///Deinit function that frees the memery used and releases the texture resource
  ///back to OpenGL.
  ///@param src, cudaGraphicsResource that handles the shared OpenGL/Cuda Texture
7e099e80   Pavel Govyadinov   "lots of stuff, t...
107
108
109
110
111
112
113
114
115
116
117
  void cleanUP(cudaGraphicsResource_t src)
  {
  	HANDLE_ERROR(
  		cudaUnbindTexture(texIn)
  	);
  	HANDLE_ERROR(
  		cudaFree(result)
  	);
  	HANDLE_ERROR(
  		cudaGraphicsUnmapResources(1,&src)
  	);
a9f956be   Pavel Govyadinov   Fixed the cost fu...
118
119
120
  	HANDLE_ERROR(
  		cudaFree(v_dif)
  	);
7e099e80   Pavel Govyadinov   "lots of stuff, t...
121
  }
556c4e15   Pavel Govyadinov   Changed the handl...
122
123
124
125
  ///External access-point to the cuda function
  ///@param src, cudaGraphicsResource that handles the shared OpenGL/Cuda Texture
  ///@param DIM_Y, the number of samples in the template.
  ///@inter temporary paramenter that tracks the number of times cost.h was called.
7e099e80   Pavel Govyadinov   "lots of stuff, t...
126
  extern "C"
5e7c7581   Pavel Govyadinov   Debugging build f...
127
  int get_cost(cudaGraphicsResource_t src, int inter, int DIM_Y)
7e099e80   Pavel Govyadinov   "lots of stuff, t...
128
  {
5e7c7581   Pavel Govyadinov   Debugging build f...
129
  	float output[DIM_Y];
a9f956be   Pavel Govyadinov   Fixed the cost fu...
130
131
  	float mini = 10000000000000000.0;
  	int idx;
556c4e15   Pavel Govyadinov   Changed the handl...
132
  	stringstream name;	//for debugging
5e7c7581   Pavel Govyadinov   Debugging build f...
133
134
  	initArray(src, DIM_Y*10);
  	dim3 grid(20, DIM_Y*10);
7e099e80   Pavel Govyadinov   "lots of stuff, t...
135
  	dim3 block(1, 1);
7e099e80   Pavel Govyadinov   "lots of stuff, t...
136
  	get_diff <<< grid, block >>> (result);
5e7c7581   Pavel Govyadinov   Debugging build f...
137
138
  	stim::gpu2image<float>(result, "test.bmp", 20,DIM_Y*10,0,1);
  	for (int i = 0; i < DIM_Y; i++){
a9f956be   Pavel Govyadinov   Fixed the cost fu...
139
  		output[i] = get_sum(result+(20*10*i));
42145f38   Pavel Govyadinov   Fixed the issues ...
140
  		if(output[i] <= mini){
a9f956be   Pavel Govyadinov   Fixed the cost fu...
141
142
  			mini = output[i];
  			idx = i;
385d2447   Pavel Govyadinov   Checkpoint: Conve...
143
  		}
42145f38   Pavel Govyadinov   Fixed the issues ...
144
145
146
147
  	}	
  	name << "sample_" << inter << "_" << idx << ".bmp";
  	output[idx] = get_sum(result+(20*10*idx));
  	stim::gpu2image<float>(v_dif, name.str(), 20,10,0,1);
7e099e80   Pavel Govyadinov   "lots of stuff, t...
148
  	cleanUP(src);
a9f956be   Pavel Govyadinov   Fixed the cost fu...
149
  	return idx;
7e099e80   Pavel Govyadinov   "lots of stuff, t...
150
  }