vote_threshold_global.cuh 3.67 KB
#ifndef STIM_CUDA_VOTE_THRESHOLD_GLOBAL_H
#define STIM_CUDA_VOTE_THRESHOLD_GLOBAL_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* gpuTh, T* gpuTable, T phi, int rmax, int th_size, int x, int y){

						
			// calculate the x coordinate for this current thread.
			int xi = blockIdx.x * blockDim.x + threadIdx.x;
			
			// calculate the voting direction based on the grtadient direction
			float theta = gpuTh[3*xi];
			//find the gradient magnitude for the current voter
			float mag = gpuTh[3*xi + 1];
			//calculate the position and x, y coordinations of this voter in the original image
			unsigned int i_v = gpuTh[3*xi+2];
			unsigned int y_v = i_v/x;
			unsigned int x_v = i_v - (y_v*x);
			
			// 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 < th_size){
				for(int yr = -rmax; yr <= rmax; yr++){					
					for(int xr = -rmax; xr <= rmax; xr++){
						if ((y_v+yr)>=0 && (y_v+yr)<y && (x_v+xr)>=0 && (x_v+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 = (y_v+yr)*x + (x_v+xr);
								atomicAdd(&gpuVote[ind_g], mag);
							
								}
						}
					}
				}	
			}
		}

		template<typename T>
		void gpu_vote(T* gpuVote, T* gpuTh, T* gpuTable, T phi, unsigned int rmax, unsigned int th_size, unsigned int x, unsigned int y){

							
			unsigned int max_threads = stim::maxThreadsPerBlock();
			dim3 threads(max_threads);
			dim3 blocks(th_size/threads.x + 1);
			
			//call the kernel to do the voting
			cuda_vote <<< blocks, threads>>>(gpuVote, gpuTh, gpuTable, phi, rmax, th_size, 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