Blame view

stim/cuda/ivote/local_max.cuh 2.81 KB
13fe3c84   Laila Saadatifard   update the stimli...
1
2
3
  #ifndef STIM_CUDA_LOCAL_MAX_H
  #define STIM_CUDA_LOCAL_MAX_H
  
13fe3c84   Laila Saadatifard   update the stimli...
4
5
  # include <iostream>
  # include <cuda.h>
96f9b10f   Laila Saadatifard   change the header...
6
  #include <stim/cuda/cudatools.h>
13fe3c84   Laila Saadatifard   update the stimli...
7
8
9
10
  
  namespace stim{
  	namespace cuda{
  
13fe3c84   Laila Saadatifard   update the stimli...
11
12
  		// this kernel calculates the local maximum for finding the cell centers
  		template<typename T>
c01958ad   Pavel Govyadinov   merged the two st...
13
  		__global__ void cuda_local_max(T* gpuCenters, T* gpuVote, T final_t, int conn, int x, int y){
13fe3c84   Laila Saadatifard   update the stimli...
14
15
  
  			// calculate the 2D coordinates for this current thread.
c01958ad   Pavel Govyadinov   merged the two st...
16
17
  			int xi = blockIdx.x * blockDim.x + threadIdx.x;
  			int yi = blockIdx.y * blockDim.y + threadIdx.y;
84ca9bba   Laila Saadatifard   fix some bugs in ...
18
19
20
  			
  			if(xi >= x || yi >= y)
  				return;
bf731970   Laila Saadatifard   fix some bugs in ...
21
  						
13fe3c84   Laila Saadatifard   update the stimli...
22
  			// convert 2D coordinates to 1D
c01958ad   Pavel Govyadinov   merged the two st...
23
  			int i = yi * x + xi;			
84ca9bba   Laila Saadatifard   fix some bugs in ...
24
  			
84ca9bba   Laila Saadatifard   fix some bugs in ...
25
26
27
28
29
  			gpuCenters[i] = 0;		//initialize the value at this location to zero
  			
  			T val = gpuVote[i];
  			
  			//compare to the threshold
c01958ad   Pavel Govyadinov   merged the two st...
30
  			if(val < final_t) return;
84ca9bba   Laila Saadatifard   fix some bugs in ...
31
  			
11cd127f   Laila Saadatifard   Leila's ivote pro...
32
33
34
35
36
  			//define an array to store indices with same vote value
  			/*int * IdxEq;
  			IdxEq = new int  [2*conn];
  			int n = 0;*/
  			
84ca9bba   Laila Saadatifard   fix some bugs in ...
37
38
39
40
41
42
  			for(int xl = xi - conn; xl < xi + conn; xl++){
  				for(int yl = yi - conn; yl < yi + conn; yl++){
  					if(xl >= 0 && xl < x && yl >= 0 && yl < y){
  						int il = yl * x + xl;
  						if(gpuVote[il] > val){							
  							return;
11cd127f   Laila Saadatifard   Leila's ivote pro...
43
44
45
46
47
48
49
50
51
  							}
  						if (gpuVote[il] == val){
  							/*IdxEq[n] = il;
  							n = n+1;*/
  							 if( il > i){
  								 return;
  							}
  						}
  					}							
84ca9bba   Laila Saadatifard   fix some bugs in ...
52
53
  				}
  			}
11cd127f   Laila Saadatifard   Leila's ivote pro...
54
55
56
57
58
  			/*if (n!=0){
  				if(IdxEq[n/2] !=i){
  					return;
  				}
  			}	*/	
c01958ad   Pavel Govyadinov   merged the two st...
59
  			gpuCenters[i] = 1;
13fe3c84   Laila Saadatifard   update the stimli...
60
  		}
bf731970   Laila Saadatifard   fix some bugs in ...
61
  		
13fe3c84   Laila Saadatifard   update the stimli...
62
  		template<typename T>
c01958ad   Pavel Govyadinov   merged the two st...
63
  		void gpu_local_max(T* gpuCenters, T* gpuVote, T final_t, unsigned int conn, unsigned int x, unsigned int y){
13fe3c84   Laila Saadatifard   update the stimli...
64
  
13fe3c84   Laila Saadatifard   update the stimli...
65
  			unsigned int max_threads = stim::maxThreadsPerBlock();
03428452   Laila Saadatifard   update the ivote ...
66
67
  			/*dim3 threads(max_threads, 1);
  			dim3 blocks(x/threads.x + (x %threads.x == 0 ? 0:1) , y);*/
c01958ad   Pavel Govyadinov   merged the two st...
68
69
  			dim3 threads( sqrt(max_threads), sqrt(max_threads) );
  			dim3 blocks(x/threads.x + 1, y/threads.y + 1);
13fe3c84   Laila Saadatifard   update the stimli...
70
  			
13fe3c84   Laila Saadatifard   update the stimli...
71
  			//call the kernel to find the local maximum.
c01958ad   Pavel Govyadinov   merged the two st...
72
  			cuda_local_max <<< blocks, threads >>>(gpuCenters, gpuVote, final_t, conn, x, y);
13fe3c84   Laila Saadatifard   update the stimli...
73
74
  		}
  
13fe3c84   Laila Saadatifard   update the stimli...
75
  		template<typename T>
c01958ad   Pavel Govyadinov   merged the two st...
76
  		void cpu_local_max(T* cpuCenters, T* cpuVote, T final_t, unsigned int conn, unsigned int x, unsigned int y){
13fe3c84   Laila Saadatifard   update the stimli...
77
  		
13fe3c84   Laila Saadatifard   update the stimli...
78
79
80
81
82
83
84
  			//calculate the number of bytes in the array
  			unsigned int bytes = x * y * sizeof(T);
  
  			// allocate space on the GPU for the detected cell centes
  			T* gpuCenters;
  			cudaMalloc(&gpuCenters, bytes);		
  
13fe3c84   Laila Saadatifard   update the stimli...
85
86
87
88
  			//allocate space on the GPU for the input Vote Image
  			T* gpuVote;
  			cudaMalloc(&gpuVote, bytes);		
  
13fe3c84   Laila Saadatifard   update the stimli...
89
90
  			//copy the Vote image data to the GPU
  			HANDLE_ERROR(cudaMemcpy(gpuVote, cpuVote, bytes, cudaMemcpyHostToDevice));
bf731970   Laila Saadatifard   fix some bugs in ...
91
  						
13fe3c84   Laila Saadatifard   update the stimli...
92
  			//call the GPU version of the local max function
c01958ad   Pavel Govyadinov   merged the two st...
93
  			gpu_local_max<T>(gpuCenters, gpuVote, final_t, conn, x, y);
bf731970   Laila Saadatifard   fix some bugs in ...
94
  							
13fe3c84   Laila Saadatifard   update the stimli...
95
96
  			//copy the cell centers data to the CPU
  			cudaMemcpy(cpuCenters, gpuCenters, bytes, cudaMemcpyDeviceToHost) ;
bf731970   Laila Saadatifard   fix some bugs in ...
97
  						
13fe3c84   Laila Saadatifard   update the stimli...
98
99
100
  			//free allocated memory
  			cudaFree(gpuCenters);
  			cudaFree(gpuVote);
13fe3c84   Laila Saadatifard   update the stimli...
101
102
103
104
105
106
107
108
  		}
  		
  	}
  }
  
  
  
  #endif