Blame view

stim/cuda/testKernel.cuh 3.02 KB
84eff8b1   Pavel Govyadinov   Merged only the n...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
  #include <assert.h>
  #include <cuda.h>
  #include <cuda_runtime.h>
  #include <stdio.h>
  #include <stim/visualization/colormap.h>
  #include <sstream>
  #include <stim/math/vector.h>
  #include <stim/cuda/cudatools/devices.h>
  #include <stim/cuda/cudatools/threads.h>
  #include <stim/cuda/cuda_texture.cuh>
  	stim::cuda::cuda_texture tx;  //texture object.
  	float* print;
  	
  	///Initialization function, allocates the memory and passes the necessary
  	///handles from OpenGL and Cuda.
  	///@param DIM_Y			--integer controlling how much memory to allocate.
  	void initArray()
  	{
  			cudaMalloc( (void**) &print, 216*16*sizeof(float));     ///temporary
  	}
  
  	///Deinit function that frees the memery used and releases the texture resource
  	///back to OpenGL.
  	void cleanUP()
  	{
  			cudaFree(print);         ///temporary
  	}  
5de3a9c2   Pavel Govyadinov   CHECKPOINT: befo...
28
29
30
31
32
33
34
35
36
37
38
  
          __device__
          float templ(int x)                                                                      
          {
                  if(x < 16/6 || x > 16*5/6 || (x > 16*2/6 && x < 16*4/6)){
                          return 1.0;
                  }else{
                          return 0.0;
                  }
  
          }
84eff8b1   Pavel Govyadinov   Merged only the n...
39
40
41
42
43
44
45
46
47
48
49
50
51
52
  		
  	///Find the difference of the given set of samples and the template
  	///using cuda acceleration.
  	///@param stim::cuda::cuda_texture t	--stim texture that holds all the references
  	///					  to the data.
  	///@param float* result			--a pointer to the memory that stores the result.
  	__global__
  	//void get_diff (float *result)
  	void get_diff (cudaTextureObject_t texIn, float *print)
  	{       
  		int x   = threadIdx.x + blockIdx.x * blockDim.x;
  		int y   = threadIdx.y + blockIdx.y * blockDim.y;
  		int idx = y*16+x;
  
59781ee3   Pavel Govyadinov   fixed a stask bug...
53
  		float valIn             = tex2D<unsigned char>(texIn, x, y);
5de3a9c2   Pavel Govyadinov   CHECKPOINT: befo...
54
55
56
  		float templa		= templ(x);
  		//print[idx]              = abs(valIn);             ///temporary
  		print[idx]              = abs(templa);             ///temporary
84eff8b1   Pavel Govyadinov   Merged only the n...
57
58
59
60
61
62
63
64
65
66
  
  	}
  
  
  	///External access-point to the cuda function
  	///@param GLuint texbufferID 	--GLtexture (most be contained in a framebuffer object)
  	///				  that holds the data that will be handed to cuda.
  	///@param GLenum texType	--either GL_TEXTURE_1D, GL_TEXTURE_2D or GL_TEXTURE_3D
  	///				  may work with other gl texture types, but untested.
  	///@param DIM_Y, the number of samples in the template.
84eff8b1   Pavel Govyadinov   Merged only the n...
67
68
69
70
71
72
73
74
75
76
77
78
  	void test(GLint texbufferID, GLenum texType)
  	{
  
  		//Bind the Texture in GL and allow access to cuda.
  		tx.MapCudaTexture(texbufferID, texType);
  
  		//initialize the return arrays.
  
  		initArray();
  		
  		int x = 16;
  		int y = 27*8;
1306fd96   Pavel Govyadinov   minor bug fixes i...
79
  		y = 8* 1089; 
84eff8b1   Pavel Govyadinov   Merged only the n...
80
  		int max_threads = stim::maxThreadsPerBlock();
1306fd96   Pavel Govyadinov   minor bug fixes i...
81
82
83
84
85
86
  		//dim3 threads(max_threads, 1);
  		//dim3 blocks(x / threads.x + 1, y);	
  		dim3 numBlocks(1, 1089);
  		dim3 threadsPerBlock(16, 8);
  		//dim3 numBlocks(2, 2);
  		//dim3 threadsPerBlock(8, 108);
84eff8b1   Pavel Govyadinov   Merged only the n...
87
88
89
90
91
92
93
94
  
  
  //		get_diff <<< blocks, threads >>> (tx.getTexture(), print);
  		get_diff <<< numBlocks, threadsPerBlock >>> (tx.getTexture(), print);
  
  		cudaDeviceSynchronize();
  		stringstream name;      //for debugging
  		name << "FromTex.bmp";
5de3a9c2   Pavel Govyadinov   CHECKPOINT: befo...
95
  		stim::gpu2image<float>(print, name.str(),16,1089*8,0,1.0);
84eff8b1   Pavel Govyadinov   Merged only the n...
96
97
98
99
  	  
  		tx.UnmapCudaTexture();
  		cleanUP();
  	}