david_update_dir_global.cuh 6.04 KB
#ifndef STIM_CUDA_UPDATE_DIR_GLOBALD_H
#define STIM_CUDA_UPDATE_DIR_GLOBAL_H

# include <iostream>
# include <cuda.h>
#include <stim/cuda/cudatools.h>
#include <stim/cuda/sharedmem.cuh>
#include <math.h>
#include "cpyToshare.cuh" 

#define RMAX_TEST	8

namespace stim{
	namespace cuda{
	
		// this kernel calculates the voting direction for the next iteration based on the angle between the location of this voter and the maximum vote value in its voting area.
		template<typename T>
		__global__ void cuda_update_dir(T* gpuDir, T* gpuVote, T* gpuGrad, T* gpuTable, T phi, int rmax,  int x,  int y){
			extern __shared__ T atan2_table[];
			
			//calculate the start point for this block
			//int bxi = blockIdx.x * blockDim.x;

			stim::cuda::sharedMemcpy(atan2_table, gpuTable, (2 * rmax + 1) * (2 * rmax + 1), threadIdx.x, blockDim.x);

			__syncthreads();
			
			// calculate the 2D coordinates for this current thread.
			//int xi = bxi + threadIdx.x;
			int xi = blockIdx.x * blockDim.x + threadIdx.x;
			int yi = blockIdx.y * blockDim.y + threadIdx.y;
			if(xi >= x || yi >= y) return;													//if the index is outside of the image, terminate the kernel

			int i = yi * x + xi;												// convert 2D coordinates to 1D
			
			float theta = gpuGrad[2*i];											// calculate the voting direction based on the grtadient direction - global memory fetch			
			gpuDir[i] = 0;														//initialize the vote direction to zero			
			float max = 0;														// define a local variable to maximum value of the vote image in the voting area for this voter
			int id_x = 0;														// define two local variables for the x and y position of the maximum
			int id_y = 0;
			
			int x_table = 2*rmax +1;											// compute the size of window which will be checked for finding the voting area for this voter
			int rmax_sq = rmax * rmax;
			int tx_rmax = threadIdx.x + rmax;
			float atan_angle;
			float vote_c;
			unsigned int ind_t;
			for(int yr = -rmax; yr <= rmax; yr++){					//for each counter in the y direction
				if (yi+yr >= 0 && yi + yr < y){									//if the counter exists (we aren't looking outside of the image)
					for(int xr = -rmax; xr <= rmax; xr++){					//for each counter in the x direction
						if((xr * xr + yr *yr)< rmax_sq){								//if the counter is within range of the voter

							ind_t = (rmax - yr) * x_table + rmax - xr;		//calculate the index to the atan2 table							
							atan_angle = atan2_table[ind_t];								//retrieve the direction vector from the table						

							//atan_angle = atan2((float)yr, (float)xr);
							
							if (abs(atan_angle - theta) <phi){							// check if the current pixel is located in the voting angle of this voter.				
								vote_c = gpuVote[(yi+yr)*x + (xi+xr)];			// find the vote value for the current counter						
								if(vote_c>max) {								// compare the vote value of this pixel with the max value to find the maxima and its index.
									max = vote_c;
									id_x =  xr;
									id_y =  yr;
								}
							}
						}
					}
				}
			}
							
			unsigned int ind_m = (rmax - id_y) * x_table + (rmax - id_x);
			float new_angle = gpuTable[ind_m];

			if(xi < x && yi < y)
				gpuDir[i] = new_angle;
		}										//end kernel

		// this kernel updates the gradient direction by the calculated voting direction.
		template<typename T>
		__global__ void cuda_update_grad(T* gpuGrad, T* gpuDir, 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;
			
			//update the gradient image with the vote direction
			gpuGrad[2*i] = gpuDir[i];
		}
		
		template<typename T>
		void gpu_update_dir(T* gpuVote, T* gpuGrad, T* gpuTable, 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);

			unsigned int max_threads = stim::maxThreadsPerBlock();

			dim3 threads(sqrt(max_threads), sqrt(max_threads));
			dim3 blocks(x/threads.x + 1, y/threads.y + 1);

			
			
			// allocate space on the GPU for the updated vote direction
			T* gpuDir;
			cudaMalloc(&gpuDir, bytes);	

			size_t shared_mem = sizeof(T) * std::pow((2 * rmax + 1), 2);
			std::cout<<"Shared memory for atan2 table: "<<shared_mem<<std::endl;

			//call the kernel to calculate the new voting direction
			cuda_update_dir <<< blocks, threads, shared_mem>>>(gpuDir, gpuVote, gpuGrad, gpuTable, phi, rmax, x , y);

			//call the kernel to update the gradient direction
			cuda_update_grad <<< blocks, threads >>>(gpuGrad, gpuDir, x , y);
			
			//free allocated memory
			cudaFree(gpuDir);

		}
		
		template<typename T>
		void cpu_update_dir(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);

			//copy the input vote image to the GPU
			HANDLE_ERROR(cudaMemcpy(gpuVote, cpuVote, bytes, cudaMemcpyHostToDevice));	

			//allocate space on the GPU for the input Gradient image
			T* gpuGrad;
			HANDLE_ERROR(cudaMalloc(&gpuGrad, bytes*2));

			//copy the Gradient 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 update direction function
			gpu_update_dir<T>(gpuVote, gpuGrad, gpuTable, phi, rmax, x , y);
							
			//copy the new gradient image back to the CPU
			cudaMemcpy(cpuGrad, gpuGrad, bytes*2, cudaMemcpyDeviceToHost) ;

			//free allocated memory
			cudaFree(gpuTable);
			cudaFree(gpuVote);
			cudaFree(gpuGrad);
		}
		
	}
}

#endif