13fe3c84
Laila Saadatifard
update the stimli...
|
1
2
3
4
5
6
|
#ifndef STIM_CUDA_ARRAY_CART2POLAR_H
#define STIM_CUDA_ARRAY_CART2POLAR_H
namespace stim{
namespace cuda{
template<typename T>
|
276f9b23
David Mayerich
forced matching t...
|
7
|
__global__ void cuda_cart2polar(T* a, size_t x, size_t y, float rotation){
|
13fe3c84
Laila Saadatifard
update the stimli...
|
8
9
|
|
bf731970
Laila Saadatifard
fix some bugs in ...
|
10
|
// calculate the 2D coordinates for this current thread.
|
276f9b23
David Mayerich
forced matching t...
|
11
12
|
size_t xi = blockIdx.x * blockDim.x + threadIdx.x;
size_t yi = blockIdx.y * blockDim.y + threadIdx.y;
|
bf731970
Laila Saadatifard
fix some bugs in ...
|
13
|
// convert 2D coordinates to 1D
|
276f9b23
David Mayerich
forced matching t...
|
14
|
size_t i = yi * x + xi;
|
bf731970
Laila Saadatifard
fix some bugs in ...
|
15
16
17
18
19
20
21
22
|
if(xi >= x|| yi >= y) return;
float xl = a[i * 2 + 0];
float yl = a[i * 2 + 1];
float theta = atan2( yl, xl ) ;
float r = sqrt(xl * xl + yl * yl);
|
84eff8b1
Pavel Govyadinov
Merged only the n...
|
23
|
a[i * 2 + 0] = theta + rotation;
|
13fe3c84
Laila Saadatifard
update the stimli...
|
24
|
a[i * 2 + 1] = r;
|
bf731970
Laila Saadatifard
fix some bugs in ...
|
25
|
|
13fe3c84
Laila Saadatifard
update the stimli...
|
26
27
28
29
|
}
template<typename T>
|
276f9b23
David Mayerich
forced matching t...
|
30
|
void gpu_cart2polar(T* gpuGrad, size_t x, size_t y, float rotation = 0){
|
13fe3c84
Laila Saadatifard
update the stimli...
|
31
|
|
bf731970
Laila Saadatifard
fix some bugs in ...
|
32
33
|
unsigned int max_threads = stim::maxThreadsPerBlock();
dim3 threads(max_threads, 1);
|
276f9b23
David Mayerich
forced matching t...
|
34
|
dim3 blocks(((unsigned int)x/threads.x) + ((unsigned int)x %threads.x == 0 ? 0:1) , (unsigned int)y);
|
bf731970
Laila Saadatifard
fix some bugs in ...
|
35
|
|
13fe3c84
Laila Saadatifard
update the stimli...
|
36
|
//call the kernel to do the multiplication
|
84eff8b1
Pavel Govyadinov
Merged only the n...
|
37
|
cuda_cart2polar <<< blocks, threads >>>(gpuGrad, x, y, rotation);
|
13fe3c84
Laila Saadatifard
update the stimli...
|
38
39
40
41
42
|
}
template<typename T>
|
276f9b23
David Mayerich
forced matching t...
|
43
|
void cpu_cart2polar(T* a, size_t x, size_t y){
|
13fe3c84
Laila Saadatifard
update the stimli...
|
44
45
|
//calculate the number of bytes in the array
|
276f9b23
David Mayerich
forced matching t...
|
46
47
|
size_t N = x *y;
size_t bytes = N * sizeof(T) * 2;
|
13fe3c84
Laila Saadatifard
update the stimli...
|
48
49
50
51
52
53
54
55
56
|
//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
|
bf731970
Laila Saadatifard
fix some bugs in ...
|
57
|
gpu_cart2polar<T>(gpuA, x, y);
|
13fe3c84
Laila Saadatifard
update the stimli...
|
58
59
60
61
62
63
64
65
66
67
68
69
|
//copy the array back to the CPU
HANDLE_ERROR( cudaMemcpy(a, gpuA, bytes, cudaMemcpyDeviceToHost) );
//free allocated memory
cudaFree(gpuA);
}
}
}
|
84eff8b1
Pavel Govyadinov
Merged only the n...
|
70
|
#endif
|