Blame view

stim/cuda/ivote/vote_atomic.cuh 3.67 KB
11cd127f   Laila Saadatifard   Leila's ivote pro...
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
  #ifndef STIM_CUDA_VOTE_ATOMIC_H
  #define STIM_CUDA_VOTE_ATOMIC_H
  
  # include <iostream>
  # include <cuda.h>
  #include <stim/cuda/cudatools.h>
  #include <stim/cuda/sharedmem.cuh>
  #include "cpyToshare.cuh"
  
  namespace stim{
  	namespace cuda{
  
  		// this kernel calculates the vote value by adding up the gradient magnitudes of every voter that this pixel is located in their voting area
  		template<typename T>
  		__global__ void cuda_vote(T* gpuVote, T* gpuGrad, T* gpuTable, T phi, int rmax, int x, int y){
  
  			
  			// calculate the 2D coordinates for this current thread.
  			int xi = blockIdx.x * blockDim.x + threadIdx.x;
  			int yi = blockIdx.y * blockDim.y + threadIdx.y;
  			// convert 2D coordinates to 1D
  			int i = yi * x + xi;
  
  			// calculate the voting direction based on the grtadient direction
  			float theta = gpuGrad[2*i];
  			//calculate the amount of vote for the voter
  			float mag = gpuGrad[2*i + 1];
  						
  			// compute the size of window which will be checked for finding the proper voters for this pixel
  			int x_table = 2*rmax +1;
  			int rmax_sq = rmax * rmax;
  			if(xi < x && yi < y){
  			//for every line (along y)
  				for(int yr = -rmax; yr <= rmax; yr++){					
  					for(int xr = -rmax; xr <= rmax; xr++){
  						if ((yi+yr)>=0 && (yi+yr)<y && (xi+xr)>=0 && (xi+xr)<x){
  					
  							//find the location of the current pixel in the atan2 table
  							unsigned int ind_t = (rmax - yr) * x_table + rmax - xr;
  
  							// calculate the angle between the voter and the current pixel in x and y directions
  							float atan_angle = gpuTable[ind_t];
  						
  							// check if the current pixel is located in the voting area of this voter.
  							if (((xr * xr + yr *yr)< rmax_sq) && (abs(atan_angle - theta) <phi)){
  								// calculate the 1D index for the current pixel in global memory
  								unsigned int ind_g = (yi+yr)*x + (xi+xr);
  								atomicAdd(&gpuVote[ind_g], mag);
  							
  								}
  						}
  					}
  				}	
  			}
  		}
  
  		template<typename T>
  		void gpu_vote(T* gpuVote, T* gpuGrad, T* gpuTable, T phi, unsigned int rmax, unsigned int x, unsigned int y){
  
  							
  			unsigned int max_threads = stim::maxThreadsPerBlock();
  			dim3 threads(max_threads, 1);
  			dim3 blocks(x/threads.x + (x %threads.x == 0 ? 0:1) , y);
  					
  			// specify  share memory
  			//unsigned int share_bytes = (2*rmax + threads.x)*(1)*2*4;
  			
  			//call the kernel to do the voting
  			cuda_vote <<< blocks, threads>>>(gpuVote, gpuGrad, gpuTable, phi, rmax, x , y);
  
  		}
  
  
  		template<typename T>
  		void cpu_vote(T* cpuVote, T* cpuGrad,T* cpuTable, T phi, unsigned int rmax, unsigned int x, unsigned int y){
  
  			//calculate the number of bytes in the array
  			unsigned int bytes = x * y * sizeof(T);
  
  			//calculate the number of bytes in the atan2 table
  			unsigned int bytes_table = (2*rmax+1) * (2*rmax+1) * sizeof(T);
  
  			//allocate space on the GPU for the Vote Image
  			T* gpuVote;
  			cudaMalloc(&gpuVote, bytes);		
  
  			//allocate space on the GPU for the input Gradient image
  			T* gpuGrad;
  			HANDLE_ERROR(cudaMalloc(&gpuGrad, bytes*2));
  
  			//copy the Gradient Magnitude data to the GPU
  			HANDLE_ERROR(cudaMemcpy(gpuGrad, cpuGrad, bytes*2, cudaMemcpyHostToDevice));
  
  			//allocate space on the GPU for the atan2 table
  			T* gpuTable;
  			HANDLE_ERROR(cudaMalloc(&gpuTable, bytes_table));
  
  			//copy the atan2 values to the GPU
  			HANDLE_ERROR(cudaMemcpy(gpuTable, cpuTable, bytes_table, cudaMemcpyHostToDevice));
  						
  			//call the GPU version of the vote calculation function
  			gpu_vote<T>(gpuVote, gpuGrad, gpuTable, phi, rmax, x , y);
  							
  			//copy the Vote Data back to the CPU
  			cudaMemcpy(cpuVote, gpuVote, bytes, cudaMemcpyDeviceToHost) ;
  
  			//free allocated memory
  			cudaFree(gpuTable);
  			cudaFree(gpuVote);
  			cudaFree(gpuGrad);
  		}
  		
  	}
  }
  
  #endif