Blame view

cudaKK.h 4.53 KB
52a5fe9d   dmayerich   Added double supp...
1
  __device__ double g(double v0, double v1)
da3d4e0e   dmayerich   Initial commit.
2
3
4
5
  {
  	return (v0 + v1)*log(abs(v0+v1)) + (v0-v1)*log(abs(v0-v1));
  }
  
52a5fe9d   dmayerich   Added double supp...
6
  __device__ double hfin(double v0, double v1, double dv)
da3d4e0e   dmayerich   Initial commit.
7
  {
52a5fe9d   dmayerich   Added double supp...
8
9
10
11
  	double e = 0.001;
  	double t0 = g(v0+e, v1-dv)/dv;
  	double t1 = 2*g(v0+e, v1)/dv;
  	double t2 = g(v0+e, v1+dv)/dv;
da3d4e0e   dmayerich   Initial commit.
12
13
14
15
  
  	return -1.0/PI * (t0 - t1 + t2);
  }
  
52a5fe9d   dmayerich   Added double supp...
16
  __global__ void devKramersKronig(double* gpuN, double* gpuK, int numVals, double nuStart, double nuEnd, double nOffset)
da3d4e0e   dmayerich   Initial commit.
17
18
19
20
  {
  	int i = blockIdx.x * blockDim.x + threadIdx.x;
  
  	if(i >= numVals) return;
52a5fe9d   dmayerich   Added double supp...
21
  	double nuDelta = (nuEnd - nuStart)/(numVals - 1);
da3d4e0e   dmayerich   Initial commit.
22
  
52a5fe9d   dmayerich   Added double supp...
23
24
25
26
  	double nu = nuStart + i*nuDelta;
  	double n = 0.0;
  	double jNu;
  	double jK;
da3d4e0e   dmayerich   Initial commit.
27
28
29
30
31
32
33
34
35
36
37
  	for(int j=1; j<numVals-1; j++)
  	{
  		jNu = nuStart + j*nuDelta;
  		jK = gpuK[j];
  		n += hfin(nu, jNu, nuDelta) * jK;
  	}
  	gpuN[i] = n + nOffset;
  
  
  }
  
52a5fe9d   dmayerich   Added double supp...
38
  void cudaKramersKronig(double* cpuN, double* cpuK, int nVals, double nuStart, double nuEnd, double nOffset)
da3d4e0e   dmayerich   Initial commit.
39
  {
52a5fe9d   dmayerich   Added double supp...
40
41
42
43
44
  	double* gpuK;
  	HANDLE_ERROR(cudaMalloc(&gpuK, sizeof(double)*nVals));
  	HANDLE_ERROR(cudaMemcpy(gpuK, cpuK, sizeof(double)*nVals, cudaMemcpyHostToDevice));
  	double* gpuN;
  	HANDLE_ERROR(cudaMalloc(&gpuN, sizeof(double)*nVals));
da3d4e0e   dmayerich   Initial commit.
45
46
47
48
49
  
  	dim3 block(BLOCK_SIZE*BLOCK_SIZE);
  	dim3 grid(nVals/block.x + 1);
  	devKramersKronig<<<grid, block>>>(gpuN, gpuK, nVals, nuStart, nuEnd, nOffset);
  
52a5fe9d   dmayerich   Added double supp...
50
  	HANDLE_ERROR(cudaMemcpy(cpuN, gpuN, sizeof(double)*nVals, cudaMemcpyDeviceToHost));
da3d4e0e   dmayerich   Initial commit.
51
52
53
54
55
56
  
  	//free resources
  	HANDLE_ERROR(cudaFree(gpuK));
  	HANDLE_ERROR(cudaFree(gpuN));
  }
  
52a5fe9d   dmayerich   Added double supp...
57
58
  __global__ void devComputeSpectrum(double* I, double2* B, double* alpha, int Nl, 
  								   int nSamples, double oThetaI, double oThetaO, double cThetaI, double cThetaO)
da3d4e0e   dmayerich   Initial commit.
59
60
61
62
  {
  	int i = blockIdx.x * blockDim.x + threadIdx.x;
  
  	//compute the delta-theta value
52a5fe9d   dmayerich   Added double supp...
63
  	double dTheta = (oThetaO - oThetaI)/nSamples;
da3d4e0e   dmayerich   Initial commit.
64
65
  
  	//allocate space for the Legendre polynomials
52a5fe9d   dmayerich   Added double supp...
66
67
68
69
70
71
72
73
74
75
  	double Ptheta[2];	
  
  	double cosTheta, theta;
  	cuDoubleComplex Us;
  	cuDoubleComplex UsSample;
  	cuDoubleComplex U;
  	//cuComplex Ui;
  	//Ui.x = 2*PI;
  	//Ui.y = 0.0;
  	cuDoubleComplex numer;
da3d4e0e   dmayerich   Initial commit.
76
  	numer.x = 0.0;
52a5fe9d   dmayerich   Added double supp...
77
78
79
  	cuDoubleComplex exp_numer;
  	cuDoubleComplex iL;
  	cuDoubleComplex imag;
da3d4e0e   dmayerich   Initial commit.
80
  	imag.x = 0.0; imag.y = 1.0;
52a5fe9d   dmayerich   Added double supp...
81
82
83
84
85
86
  	double realFac;
  	cuDoubleComplex complexFac;
  	double PlTheta;
  	double Isum = 0.0;
  	//float maxVal = 0;
  	//float val;
da3d4e0e   dmayerich   Initial commit.
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
  	for(int iTheta = 0; iTheta < nSamples; iTheta++)
  	{
  		//calculate theta
  		theta = iTheta * dTheta + oThetaI;
  		cosTheta = cos(theta);
  
  		//initialize the theta Legendre polynomial
  		Ptheta[0] = 1.0;
  		Ptheta[1] = cosTheta;
  
  		//initialize the scattered field
  		Us.x = Us.y = 0.0;
  		iL.x = 1.0;
  		iL.y = 0.0;
  		for(int l = 0; l<Nl; l++)
  		{
  			//compute the theta legendre polynomial
  			if(l == 0)
  				PlTheta = Ptheta[0];
  			else if(l == 1)
  				PlTheta = Ptheta[1];
  			else
  			{
  				PlTheta = ((2*l - 1)*cosTheta*Ptheta[1] - (l - 1)*Ptheta[0])/l;
  				Ptheta[0] = Ptheta[1];
  				Ptheta[1] = PlTheta;
  			}
  
  			//compute the real components of the scattered field
  			realFac = alpha[l] * PlTheta;
  
  			//compute the complex components of the scattered field
  			numer.x = 0.0;
  			numer.y = -(l*PI)/2.0;
  			exp_numer = cExp(numer);
  
  			complexFac = cMult(B[Nl * i + l], exp_numer);
  			complexFac = cMult(complexFac, iL);
  			
  
  			//combine the real and complex components
  			UsSample = cMult(complexFac, realFac);
  			Us = cAdd(Us, UsSample);
  
  			//increment the imaginary exponent i^l
  			iL = cMult(iL, imag);
  
52a5fe9d   dmayerich   Added double supp...
134
  		
da3d4e0e   dmayerich   Initial commit.
135
136
137
138
139
140
141
142
143
144
145
146
147
  		}
  
  		//sum the scattered and incident fields
  		if(theta >= cThetaI && theta <= cThetaO)
  			U = cAdd(Us, 2*PI);
  		else
  			U = Us;
  		Isum += (U.x*U.x + U.y*U.y) * sin(theta) * 2 * PI * dTheta;
  	}
  
  	I[i] = Isum;
  }
  
52a5fe9d   dmayerich   Added double supp...
148
149
  void cudaComputeSpectrum(double* cpuI, double* cpuB, double* cpuAlpha,
  						 int Nl, int nLambda, double oThetaI, double oThetaO, double cThetaI, double cThetaO, int nSamples)
da3d4e0e   dmayerich   Initial commit.
150
151
  {
  	//copy everything to the GPU
52a5fe9d   dmayerich   Added double supp...
152
153
154
  	double2* gpuB;
  	HANDLE_ERROR(cudaMalloc(&gpuB, sizeof(double2) * nLambda * Nl));
  	HANDLE_ERROR(cudaMemcpy(gpuB, cpuB, sizeof(double2) * nLambda * Nl, cudaMemcpyHostToDevice));
da3d4e0e   dmayerich   Initial commit.
155
  
52a5fe9d   dmayerich   Added double supp...
156
157
158
  	double* gpuAlpha;
  	HANDLE_ERROR(cudaMalloc(&gpuAlpha, sizeof(double) * Nl));
  	HANDLE_ERROR(cudaMemcpy(gpuAlpha, cpuAlpha, sizeof(double) * Nl, cudaMemcpyHostToDevice));
da3d4e0e   dmayerich   Initial commit.
159
  
52a5fe9d   dmayerich   Added double supp...
160
161
  	double* gpuI;
  	HANDLE_ERROR(cudaMalloc(&gpuI, sizeof(double) * nLambda));
da3d4e0e   dmayerich   Initial commit.
162
163
164
165
166
167
168
  	
  
  	//call the kernel to compute the spectrum
  	dim3 block(BLOCK_SIZE*BLOCK_SIZE);
  	dim3 grid(nLambda/block.x + 1);
  
  	//devComputeSpectrum
52a5fe9d   dmayerich   Added double supp...
169
  	devComputeSpectrum<<<grid, block>>>(gpuI, (double2*)gpuB, gpuAlpha, Nl,
da3d4e0e   dmayerich   Initial commit.
170
171
  										nSamples, oThetaI, oThetaO, cThetaI, cThetaO);
  
52a5fe9d   dmayerich   Added double supp...
172
  	HANDLE_ERROR(cudaMemcpy(cpuI, gpuI, sizeof(double) * nLambda, cudaMemcpyDeviceToHost));
da3d4e0e   dmayerich   Initial commit.
173
174
175
176
177
178
179
180
181
  
  	HANDLE_ERROR(cudaFree(gpuB));
  	HANDLE_ERROR(cudaFree(gpuAlpha));
  	HANDLE_ERROR(cudaFree(gpuI));
  
  	
  
  
  }