-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCUDAfft2.0.cu
307 lines (248 loc) · 7.04 KB
/
CUDAfft2.0.cu
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
//Compile with: nvcc CUDAfft2.0.cu -I/home/phyd57/N_Body1/9.2/include -L/home/phyd57/N_Body1/9.2/lib64 -lcufft -o CUDAfftcu2.out -I/usr/local/dislin -ldislin
#include <cuda_runtime.h>
#include <device_launch_parameters.h>
#include <cufft.h>
#include <stdlib.h>
#include<stdio.h>
#include <iostream>
#include "dislin.h"
#define N 256 // N is the sidelength of the image -> N^3 pixels in entire image
#define block_size_x 2
#define block_size_y 2
#define block_size_z 2
float den_array[N][N][N];
float grav_po[N][N][N];
float image[N/2][N/2];
__global__ void real2complex(cufftComplex *c, float *a, int n);
__global__ void complex2real_scaled(float *a, cufftComplex *c, float scale, int n);
__global__ void solve_poisson(cufftComplex *c, float *k_xyz, int n);
void FFT_poisson(float den_array[N][N][N], float grav_po[N][N][N])
{
int x, y, z, i;
float *k_xyz, *den;
k_xyz = (float *)malloc(sizeof(float)*N);
den = (float *)malloc(sizeof(float)*N*N*N);
float *k_xyz_d, *den_d;
cufftComplex *den_complex_d;
cudaMalloc((void **)&k_xyz_d, sizeof(float) * N);
cudaMalloc((void **)&den_d, sizeof(float) * N * N * N);
cudaMalloc((void **)&den_complex_d, sizeof(cufftComplex) * N * N * N);
#pragma omp for
for (x = 0; x < N; x++)
for (y = 0; y < N; y++)
for (z = 0; z < N; z++)
den[x + y*N + z*N*N] = den_array[x][y][z];
float* den_inital = (float *)malloc(sizeof(float) * N * N * N);
for (i = 0; i < N * N; i++)
den_inital[i] = den[i];
for (i = 0; i < N; i++)
{
if (i < N/2)
{
k_xyz[i] = i;
}
else
{
k_xyz[i] = i-N;
}
}
cudaMemcpy(k_xyz_d, k_xyz, sizeof(float)*N, cudaMemcpyHostToDevice);
cudaMemcpy(den_d, den, sizeof(float)*N*N*N, cudaMemcpyHostToDevice);
cufftHandle plan;
cufftPlan3d(&plan,N,N,N,CUFFT_C2C);
/* Compute the execution configuration, block_size_x*block_size_y*block_size_z = number of threads */
dim3 dimBlock(block_size_x, block_size_y, block_size_z);
dim3 dimGrid(N/dimBlock.x, N/dimBlock.y, N/dimBlock.z);
/* Handle N not multiple of block_size_x, block_size_y, or block_size_y */
if (N % block_size_x != 0) dimGrid.x += 1;
if (N % block_size_y != 0) dimGrid.y += 1;
if (N % block_size_z != 0) dimGrid.z += 1;
real2complex<<<dimGrid, dimBlock>>>(den_complex_d, den_d, N);
cufftExecC2C(plan, den_complex_d, den_complex_d, CUFFT_FORWARD);
solve_poisson<<<dimGrid, dimBlock>>>(den_complex_d, k_xyz_d, N);
cufftExecC2C(plan, den_complex_d, den_complex_d, CUFFT_INVERSE);
float scale = 1.0f / (N*N*N);
complex2real_scaled<<<dimGrid, dimBlock>>>(den_d, den_complex_d, scale, N);
cudaMemcpy(den, den_d, sizeof(float)*N*N*N, cudaMemcpyDeviceToHost);
#pragma omp for
for (x = 0; x < N; x++)
for (y = 0; y < N; y++)
for (z = 0; z < N; z++)
grav_po[x][y][z] = den[x + y*N + z*N*N];
/* Destroy plan and clean up memory on device*/
cudaFree(k_xyz);
cudaFree(den);
cudaFree(den_inital);
cufftDestroy(plan);
cudaFree(den_complex_d);
cudaFree(den);
cudaFree(k_xyz_d);
}
void make_image(float array[N][N][N], const char *output_name)
{
int x, y, z;
float Max = -500.0, Min = 500.0;
#pragma omp for
for (x = 0; x < N/2; x++)
for (y = 0; y < N/2; y++)
image[x][y] = 0.0;
#pragma omp for
for (x = 0; x < N/2; x++)
for (y = 0; y < N/2; y++)
for (z = 0; z < N/2; z++)
image[x][y] += array[x+N/4][y+N/4][z+N/4];
#pragma omp for
for (x = 0; x < N/2; x++)
{
for (y = 0; y < N/2; y++)
{
if (image[x][y] > Max)
{
Max = image[x][y];
}
if (image[x][y] < Min)
{
Min = image[x][y];
}
}
}
metafl("PNG");
setfil(output_name);
//metafl("CONS");
disini();
pagera();
hwfont();
titlin("Potential map", 4);
//titlin("anthing below", 2)
name("X [kP]", "x");
name("Y [kP]", "y");
name("Potential in Z", "z");
intax() ;
autres(N/2,N/2);
axspos(300,1850);
ax3len(1600,1600,1600);
labdig(6, "Z");
graf3(-N/4, N/4, -N/4, N/40, -N/4, N/4, -N/4, N/40, Min, Max, Min, (Max-Min)/10);
crvmat((float *)image, N/2, N/2 , 1, 1);
height(50);
title();
disfin();
}
int main()
{
int i, j, k;
#pragma omp parallel for
for (i = 0; i < 256; i ++)
{
for (j = 0; j < 256; j++)
{
for (k = 0; k < 256; k++)
{
den_array[i][j][k] = 0.0;
grav_po[i][j][k] = 0.0;
}
}
}
den_array[128][128][128] = 500.0;
den_array[128][158][128] = 250.0;
den_array[128][98][128] = 250.0;
den_array[158][128][128] = 250.0;
den_array[98][128][128] = 250.0;
#pragma omp parallel for
for (i = 143; i > 113; i --)
{
for (j = 113; j < 143; j++)
{
printf("%.1f,", den_array[j][i][128]);
}
printf("\n");
}
FFT_poisson(den_array, grav_po);
printf("z = 127:\n");
#pragma omp parallel for
for (i = 132; i > 124; i --)
{
for (j = 123; j < 133; j++)
{
printf("%f,", grav_po[j][i][127]);
}
printf("\n");
}
printf("\n\n");
printf("z = 128:\n");
#pragma omp parallel for
for (i = 132; i > 124; i --)
{
for (j = 123; j < 133; j++)
{
printf("%f,", grav_po[j][i][128]);
}
printf("\n");
}
printf("\n\n");
printf("z = 129:\n");
#pragma omp parallel for
for (j = 132; j > 124; j --)
{
for (i = 123; i < 133; i++)
{
printf("%f,", grav_po[i][j][129]);
}
printf("\n");
}
printf("\n%f\n", grav_po[0][128][128]);
printf("%f\n", grav_po[255][128][128]);
printf("%f\n", grav_po[128][0][128]);
printf("%f\n", grav_po[128][255][128]);
//print the 8 corners.
printf("\n%f\n", grav_po[0][0][0]);
printf("%f\n", grav_po[0][0][255]);
printf("%f\n", grav_po[0][255][255]);
printf("%f\n", grav_po[0][255][0]);
printf("\n%f\n", grav_po[255][0][255]);
printf("%f\n", grav_po[255][0][0]);
printf("%f\n", grav_po[255][255][0]);
printf("%f\n", grav_po[255][255][255]);
make_image(grav_po, "final_test.png");
}
__global__ void real2complex(cufftComplex *c, float *a, int n)
{
/* compute idx, idy, and idz, the location of the element in the original NxNxN array */
int idx = blockIdx.x * blockDim.x + threadIdx.x;
int idy = blockIdx.y * blockDim.y + threadIdx.y;
int idz = blockIdx.z * blockDim.z + threadIdx.z;
if (idx < n && idy < n && idz < n)
{
int index = idx + idy*n + idz*n*n;
c[index].x = a[index];
c[index].y = 0.0f;
}
}
__global__ void complex2real_scaled(float *a, cufftComplex *c, float scale, int n)
{
/* compute idx and idy, the location of the element in the original NxN array */
int idx = blockIdx.x * blockDim.x + threadIdx.x;
int idy = blockIdx.y * blockDim.y + threadIdx.y;
int idz = blockIdx.z * blockDim.z + threadIdx.z;
if (idx < n && idy < n && idz < n)
{
int index = idx + idy*n + idz*n*n;
a[index] = scale * c[index].x;
}
}
__global__ void solve_poisson(cufftComplex *c, float *k_xyz, int n)
{
/* compute idx and idy, the location of the element in the original NxN array */
int idx = blockIdx.x * blockDim.x + threadIdx.x;
int idy = blockIdx.y * blockDim.y + threadIdx.y;
int idz = blockIdx.z * blockDim.z + threadIdx.z;
if (idx < n && idy < n && idz < n)
{
int index = idx + idy*n + idz*n*n;
float scale = -(k_xyz[idx]*k_xyz[idx] + k_xyz[idy]*k_xyz[idy] + k_xyz[idz]*k_xyz[idz]) + 0.00001f;
if (idx == 0 && idy == 0 && idz == 0) scale = 1.0f;
scale = 1.0f / scale;
c[index].x *= scale;
c[index].y *= scale;
}
}