sphere.cu
4.14 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "sphere.h"
#include "rts/math/legendre.h"
__global__ void gpuScalarUsp(bsComplex* Usp, bsComplex* h, bsComplex* B, int Nl, int rR, int thetaR)
{
//get the current coordinate in the plane slice
int ir = blockIdx.x * blockDim.x + threadIdx.x;
int itheta = blockIdx.y * blockDim.y + threadIdx.y;
//make sure that the thread indices are in-bounds
if(itheta >= thetaR || ir >= rR) return;
int i = itheta * rR + ir;
//ptype dr = (rmax - a) / (rR - 1);
ptype dtheta = (PI) / (thetaR - 1);
//comptue the current angle and distance
//ptype r = dr * ir + a;
ptype theta = dtheta * itheta;
ptype cos_theta = cos(theta);
//initialize the Legendre polynomial
ptype P[2];
rts::init_legendre<ptype>(cos_theta, P[0], P[1]);
//initialize the result
bsComplex Us((ptype)0, (ptype)0);
//for each order l
for(int l=0; l <= Nl; l++)
{
if(l == 0)
{
Us += B[l] * h[ir * (Nl+1) + l] * P[0];
//Us += P[0];
}
else
{
if(l > 1)
{
rts::shift_legendre<ptype>(l, cos_theta, P[0], P[1]);
}
Us += B[l] * h[ir * (Nl+1) + l] * P[1];
//Us += P[1];
}
}
Usp[i] = Us;
//Usp[i] = h[ir * (Nl+1)];
//Usp[i] = ir;
}
__global__ void gpuScalarUip(bsComplex* Uip, bsComplex* j, bsComplex* A, int Nl, int aR, int thetaR)
{
//get the current coordinate in the plane slice
int ia = blockIdx.x * blockDim.x + threadIdx.x;
int itheta = blockIdx.y * blockDim.y + threadIdx.y;
//make sure that the thread indices are in-bounds
if(itheta >= thetaR || ia >= aR) return;
int i = itheta * aR + ia;
ptype dtheta = (PI) / (thetaR - 1);
//comptue the current angle and distance
ptype theta = dtheta * itheta;
ptype cos_theta = cos(theta);
//initialize the Legendre polynomial
ptype P[2];
rts::init_legendre<ptype>(cos_theta, P[0], P[1]);
//initialize the result
bsComplex Ui((ptype)0, (ptype)0);
//for each order l
for(int l=0; l <= Nl; l++)
{
if(l == 0)
{
Ui += A[l] * j[ia * (Nl+1) + l] * P[0];
}
else
{
if(l > 1)
{
rts::shift_legendre<ptype>(l, cos_theta, P[0], P[1]);
}
Ui += A[l] * j[ia * (Nl+1) + l] * P[1];
}
}
Uip[i] = Ui;
}
void sphere::scalarUsp(bsComplex* h, int rR, int thetaR)
{
//copy the hankel function to the GPU
bsComplex* gpu_h;
HANDLE_ERROR( cudaMalloc( (void**)&gpu_h, sizeof(bsComplex) * (Nl + 1) * rR ) );
HANDLE_ERROR( cudaMemcpy( gpu_h, h, sizeof(bsComplex) * (Nl + 1) * rR, cudaMemcpyHostToDevice ) );
//allocate memory for the scattering coefficients
bsComplex* gpuB;
HANDLE_ERROR(cudaMalloc((void**) &gpuB, (Nl+1) * sizeof(bsComplex)));
//copy the scattering coefficients to the GPU
HANDLE_ERROR(cudaMemcpy(gpuB, &B[0], (Nl+1) * sizeof(bsComplex), cudaMemcpyHostToDevice));
//create one thread for each pixel of the field slice
dim3 dimBlock(SQRT_BLOCK, SQRT_BLOCK);
dim3 dimGrid((Usp.R[0] + SQRT_BLOCK -1)/SQRT_BLOCK, (Usp.R[1] + SQRT_BLOCK - 1)/SQRT_BLOCK);
gpuScalarUsp<<<dimGrid, dimBlock>>>(Usp.x_hat, gpu_h, gpuB, Nl, rR, thetaR);
//free memory
cudaFree(gpu_h);
cudaFree(gpuB);
}
void sphere::scalarUip(bsComplex* j, int rR, int thetaR)
{
//copy the bessel and hankel LUTs to the GPU
bsComplex* gpu_j;
HANDLE_ERROR( cudaMalloc( (void**)&gpu_j, sizeof(bsComplex) * (Nl + 1) * rR ) );
HANDLE_ERROR( cudaMemcpy( gpu_j, j, sizeof(bsComplex) * (Nl + 1) * rR, cudaMemcpyHostToDevice ) );
//allocate memory for the scattering coefficients
bsComplex* gpuA;
HANDLE_ERROR(cudaMalloc((void**) &gpuA, (Nl+1) * sizeof(bsComplex)));
//copy the scattering coefficients to the GPU
HANDLE_ERROR(cudaMemcpy(gpuA, &A[0], (Nl+1) * sizeof(bsComplex), cudaMemcpyHostToDevice));
//create one thread for each pixel of the field slice
dim3 dimBlock(SQRT_BLOCK, SQRT_BLOCK);
dim3 dimGrid((Uip.R[0] + SQRT_BLOCK -1)/SQRT_BLOCK, (Uip.R[1] + SQRT_BLOCK - 1)/SQRT_BLOCK);
gpuScalarUip<<<dimGrid, dimBlock>>>(Uip.x_hat, gpu_j, gpuA, Nl, rR, thetaR);
//free memory
cudaFree(gpu_j);
cudaFree(gpuA);
}