Blame view

stim/cuda/arraymath/array_cart2polar.cuh 1.46 KB
13fe3c84   Laila Saadatifard   update the stimli...
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
  #ifndef STIM_CUDA_ARRAY_CART2POLAR_H
  #define STIM_CUDA_ARRAY_CART2POLAR_H
  
  namespace stim{
  	namespace cuda{
  		template<typename T>
  		__global__ void cuda_cart2polar(T* a, unsigned int N){
  
  			
  			//calculate the 1D index for this thread
  			int i = blockIdx.x * blockDim.x + threadIdx.x;
  
  			if(i < N){
  				float x = a[i * 2 + 0];
  				float y = a[i * 2 + 1];
  				float theta = atan2( y, x ) ;
  				float r = sqrt(x * x + y * y);
  				a[i * 2 + 0] = theta;
  				a[i * 2 + 1] = r;
  			}
  		}
  
  
  		template<typename T>
  		void gpu_cart2polar(T* gpuGrad, unsigned int N){
  
  			//get the maximum number of threads per block for the CUDA device
  			int threads = stim::maxThreadsPerBlock();
  
  			//calculate the number of blocks
  			int blocks = N / threads + (N % threads == 0 ? 0:1);
  
  			//call the kernel to do the multiplication
  			cuda_cart2polar <<< blocks, threads >>>(gpuGrad, N);
  
  		}
  
  
  		template<typename T>
  		void cpu_cart2polar(T* a, unsigned int N){
  
  			//calculate the number of bytes in the array
  			unsigned int bytes = N * sizeof(T) * 2;
  
  			//allocate memory on the GPU for the array
  			T* gpuA;
  			HANDLE_ERROR( cudaMalloc(&gpuA, bytes) );
  
  			//copy the array to the GPU
  			HANDLE_ERROR( cudaMemcpy(gpuA, a, bytes, cudaMemcpyHostToDevice) );
  
  			//call the GPU version of this function
  			gpu_cart2polar<T>(gpuA, N);
  
  			//copy the array back to the CPU
  			HANDLE_ERROR( cudaMemcpy(a, gpuA, bytes, cudaMemcpyDeviceToHost) );
  
  			//free allocated memory
  			cudaFree(gpuA);
  
  		}
  
  	}
  }
  
  #endif