define _USE_MATH_DEFINES include cuda_runtime include device_launch_pa

  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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#define _USE_MATH_DEFINES
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <cuda.h>
#include <cuda_runtime_api.h>
#include <stdio.h>
#include <complex>
#include "cuComplex.h"
#include "cublas.h"
#include <math.h>
#define eps 1e-15
#define el 0.5772156649015329
#define N 5
#define h M_PI * 2 /( N - 1 )
static cuFloatComplex cii = make_cuFloatComplex(0.0, 1.0);
static cuFloatComplex cone = make_cuFloatComplex(1.0, 0.0);
static cuFloatComplex czero = make_cuFloatComplex(0.0, 0.0);
float getArgFromComplex(cuFloatComplex z){
float absZ = sqrt(z.x * z.x + z.y * z.y);
return acos(z.x / absZ);
}
float getArgFromSin(cuFloatComplex z){
float absZ = sqrt(z.x * z.x + z.y * z.y);
return asin(z.y / absZ);
}
float cosCudaComplex(cuFloatComplex z){
float cosArg = getArgFromComplex(z);
float absZ = sqrt(z.x * z.x + z.y * z.y);
return cos(cosArg);
}
float sinCudaComplex(cuFloatComplex z){
float sinArg = getArgFromSin(z);
float absZ = sqrt(z.x * z.x + z.y * z.y);
return sin(sinArg);
}
cuFloatComplex cudaFloatComplexMult(float number, cuFloatComplex complex)
{
cuFloatComplex returnValue = make_cuFloatComplex(complex.x * number, complex.y * number);
return returnValue;
}
__device__ cuFloatComplex cudaFloatComplexMultGPU(float number, cuFloatComplex complex)
{
cuFloatComplex returnValue = make_cuFloatComplex(complex.x * number, complex.y * number);
return returnValue;
}
cuFloatComplex cudaFloatComplexDiv(cuFloatComplex complex, float number)
{
cuFloatComplex returnValue = make_cuFloatComplex(complex.x / number, complex.y / number);
return returnValue;
}
cuFloatComplex cudaComplexPow(cuFloatComplex z, int n){
float arg = getArgFromComplex(z);
cuFloatComplex returnValue = make_cuFloatComplex(cos(n*arg), sin(n*arg));
float absZ = sqrt(z.x * z.x + z.y * z.y);
float absZpow = pow(absZ, n);
return cudaFloatComplexMult(absZpow, returnValue);
}
cuFloatComplex logCuda(cuFloatComplex z, int k){
float F;
if (z.x >= 0 && z.y >= 0)F = 0;
if (z.x < 0 && z.y >= 0)F = M_PI;
if (z.x >= 0 && z.y < 0)F = M_PI;
if (z.x < 0 && z.y < 0)F = 0;
float f = atan(z.y / z.x) + F;
cuFloatComplex returnValue = make_cuFloatComplex((z.x) + z.y * (f + 2 * M_PI*k) + el, 0);
return returnValue;
}
cuFloatComplex besselh01(cuFloatComplex z)
{
cuFloatComplex z1, z2, cr, cp, cs, cp0, cq0, ct1, cu;
cuFloatComplex cj0, cy0;
float a0, w0;
int k, kz;
static float a[] = {
-7.03125e-2,
0.112152099609375,
-0.5725014209747314,
6.074042001273483,
-1.100171402692467e2,
3.038090510922384e3,
-1.188384262567832e5,
6.252951493434797e6,
-4.259392165047669e8,
3.646840080706556e10,
-3.833534661393944e12,
4.854014686852901e14,
-7.286857349377656e16,
1.279721941975975e19 };
static float b[] = {
7.32421875e-2,
-0.2271080017089844,
1.727727502584457,
-2.438052969955606e1,
5.513358961220206e2,
-1.825775547429318e4,
8.328593040162893e5,
-5.006958953198893e7,
3.836255180230433e9,
-3.649010818849833e11,
4.218971570284096e13,
-5.827244631566907e15,
9.476288099260110e17,
-1.792162323051699e20 };
a0 = cuCabsf(z);
z2 = cuCmulf(z, z);
z1 = z;
if (a0 == 0.0) {
cj0 = cone;
cy0 = make_cuFloatComplex(-1e308, 0);
return make_cuFloatComplex(0, 0);
}
if (z.x < 0.0)z1 = make_cuFloatComplex(-z.x, -z.y);
if (a0 <= 12.0) {
cj0 = cone;
cr = cone;
for (k = 1; k <= 40; k++) {
cr = cuCmulf(cr, cudaFloatComplexDiv(cudaFloatComplexMult(-0.25, z2), (float)(k*k)));
cj0 = cuCaddf(cj0, cr);
if (cuCabsf(cr) < cuCabsf(cj0)*eps) break;
}
w0 = 0.0;
cr = cone;
cs = czero;
for (k = 1; k <= 40; k++) {
w0 += 1.0 / k;
cr = cuCmulf(cr, cudaFloatComplexDiv(cudaFloatComplexMult(-0.25, z2), (float)(k*k)));
cp = cudaFloatComplexMult(w0, cr);
cs = cuCaddf(cs, cp);
if (cuCabsf(cp) < cuCabsf(cs)*eps) break;
}
cy0 = cudaFloatComplexMult(M_2_PI, cuCdivf(cuCmulf((logCuda(cudaFloatComplexMult(0.5, z1), 1)), cj0), cs));
}
else {
if (a0 >= 50.0) kz = 8; // can be changed to 10
else if (a0 >= 35.0) kz = 10; // " " " 12
else kz = 12; // " " "
ct1 = make_cuFloatComplex(z1.x - M_PI_4, z1.y);
cp0 = cone;
for (k = 0; k<kz; k++) {
cp0 = cuCaddf(cp0, cudaFloatComplexMult(a[k], cudaComplexPow(z1, -2.0*k - 2.0)));
}
cq0 = cudaFloatComplexMult(-1 / 0.125, z1);
for (k = 0; k<kz; k++) {
cq0 = cuCaddf(cq0, cudaFloatComplexMult(b[k], cudaComplexPow(z1, -2.0*k - 3.0)));
}
/////////////////
cu = cudaComplexPow(cudaFloatComplexMult(1 / M_2_PI, z1), 1 / 2);
cj0 = cuCmulf(cu, cuCsubf(cudaFloatComplexMult(cosCudaComplex(ct1), cp0), cudaFloatComplexMult(sinCudaComplex(ct1), cq0)));
cy0 = cuCmulf(cu, cuCaddf(cudaFloatComplexMult(sinCudaComplex(ct1), cp0), cudaFloatComplexMult(cosCudaComplex(ct1), cq0)));
}
if (z.x < 0.0) {
if (z.y < 0.0) {
cy0 = cuCsubf(cy0, cuCmulf(cudaFloatComplexMult(2.0, cii), cj0));
// cy0 -= 2.0*cii*cj0;
}
else if (z.y > 0.0) {
cy0 = cuCaddf(cy0, cuCmulf(cudaFloatComplexMult(2.0, cii), cj0));
// cy0 += 2.0*cii*cj0;
}
}
return (cuCaddf(cj0, cuCmulf(cii, cy0)));
}
__global__ void CudaHankelFunction(float *matrix, cuFloatComplex *cudaArray)
{
const int N2 = 5;
double h2 = M_PI * 2 / (N2 - 1);
int indexX = blockIdx.x * blockDim.x + threadIdx.x;
int indexY = blockIdx.y * blockDim.y + threadIdx.y;
float *buffer = new float[N2 * N2];
cuFloatComplex hi = make_cuFloatComplex(1.7, -2.7);
for (int i = 1; i < N2; i++){
float ti = h2*i;
float x1 = cos(ti);
float x2 = sin(ti);
for (int j = 1; j < N2; j++){
float tj = h2 *j;
float y1 = cos(tj);
float y2 = sin(tj);
buffer[indexX * N2 + indexY] = sqrt(powf((x1 - y1), 2) + powf((x2 - y2), 2));
}
}
__syncthreads();
for (int i = 0; i < N2; i++){
for (int j = 0; j < N2; j++)
{
cudaArray[indexX * N2 + indexY] = cudaFloatComplexMultGPU(buffer[indexX * N2 + indexY], hi);
}
}
}
int main()
{
/////////////////////////// CPU PART //////////////////////////////////////////////
cuFloatComplex hi = make_cuFloatComplex(1.7, -2.7);
cuFloatComplex cudaArrayCPU[N][N];
//float h = M_PI * 2 / N;
float matrixCPU[N][N];
for (int i = 1; i < N; i++){
float ti = h*i;
float x1 = cos(ti);
float x2 = sin(ti);
for (int j = 0; j < N; j++){
float tj = h *j;
float y1 = cos(tj);
float y2 = sin(tj);
matrixCPU[i][j] = sqrt(pow((x1 - y1), 2) + pow((x2 - y2), 2));
printf("%f ", matrixCPU[i][j]);
}
printf("\n");
}
for (int i = 0; i < N; i++){
for (int j = 0; j < N; j++){
cudaArrayCPU[i][j] = cudaFloatComplexMult(matrixCPU[i][j], hi);
}
}
cuFloatComplex resultArrayCPU[N][N];
for (int i = 0; i < N; i++){
for (int j = 0; j < N; j++){
resultArrayCPU[i][j] = besselh01(cudaArrayCPU[i][j]);
printf("%d + %di \n", resultArrayCPU[i][j].x, resultArrayCPU[i][j].y);
}
}
////////////////////////// GPU PART ////////////////////////////////////////
cuFloatComplex cudaArray[N * N];
float matrix[N * N];
cuFloatComplex resultArray[N * N];
// allocate host memory
int numBytesCudaArray = N * N * sizeof(cuFloatComplex);
int numBytesMatrix = N * N * sizeof(float);
int numBytesResultArray = N * N * sizeof(cuFloatComplex);
//threads
int threads = 16;
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
{
matrix[i *N + j] = 0;
cudaArray[i * N + j].x = 0;
cudaArray[i * N + j].y = 0;
}
// allocate device memory
cuFloatComplex *devCudaResult = NULL;
cuFloatComplex *devCudaArray = NULL;
float *devMatrix = NULL;
cudaMalloc((void**)&devCudaArray, numBytesCudaArray);
cudaMalloc((void**)&devMatrix, numBytesMatrix);
cudaMalloc((void**)&devCudaResult, numBytesResultArray);
dim3 cudaThreads = dim3(threads);
dim3 cudaBlocks = dim3(1);
// create cuda event handles
cudaEvent_t start, stop;
float gpuTime = 0.0f;
cudaEventCreate(&start);
cudaEventCreate(&stop);
// asynchronously issue work to the GPU (all to stream 0)
cudaEventRecord(start, 0);
cudaMemcpy(devCudaArray, cudaArray, numBytesCudaArray, cudaMemcpyHostToDevice);
cudaMemcpy(devMatrix, matrix, numBytesMatrix, cudaMemcpyHostToDevice);
//launch kernel
CudaHankelFunction << <cudaBlocks, cudaThreads >> >(devMatrix, devCudaArray);
cudaMemcpy(cudaArray, devCudaArray, numBytesCudaArray, cudaMemcpyDeviceToHost);
cudaMemcpy(matrix, devMatrix, numBytesMatrix, cudaMemcpyDeviceToHost);
for (int i = 0; i < N; i++){
for (int j = 0; j < N; j++){
resultArray[i * N + j] = besselh01(cudaArray[i * N + j]);// can't use this function on GPU, so it is here.
}
}
cudaEventRecord(stop, 0);
cudaEventSynchronize(stop);
cudaEventElapsedTime(&gpuTime, start, stop);
// print the cpu and gpu times
printf("time spent executing by the GPU: %f millseconds\n", gpuTime);
// check the output for correctness
printf("--------------------------------------------------------------\n");
for (int i = 0; i < N; i++){
for (int j = 0; j < N; j++)
{
printf("%d + %di \n", resultArray[i * N + j].x, resultArray[i * N + j].y);
}
// release resources
cudaEventDestroy(start);
cudaEventDestroy(stop);
//free cuda memory
cudaFree(devCudaArray);
cudaFree(devMatrix);
float errorCount = 0;
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
errorCount += abs(resultArrayCPU[i][j].x - resultArray[i * N + j].x);
}
}
printf("error count %d \n", errorCount);
return 0;
}
}