#ifndef STIM_CUDA_VOTE_THRESHOLD_GLOBAL_H #define STIM_CUDA_VOTE_THRESHOLD_GLOBAL_H # include # include #include #include #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 __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)=0 && (x_v+xr) 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 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(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