-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fft_lattice.cu
197 lines (137 loc) · 3.59 KB
/
Fft_lattice.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
#include "Fft_lattice.h"
#include <cufft.h>
#include <helper_cuda.h>
extern cufftHandle planr2c;
extern cufftHandle planc2r;
__global__ void create_lattice_kernel(float *d_latticevol,uint NX, uint NY, uint NZ, uint size)
{
int x = blockIdx.x * blockDim.x + threadIdx.x;
int y = blockIdx.y * blockDim.y + threadIdx.y;
int z = blockIdx.z * blockDim.z + threadIdx.z;
uint index = x + (y * NX) + (z * (NX * NY));
float a;
if((x < NX) && (y < NY ) && ( z < NZ))
{
float xx = (x * 1.0)/NX;
float yy = (y * 1.0)/NY;
float zz = (z * 1.0)/NZ;
a = cosf(6.28 * xx)*sinf(6.28 * yy) + cosf(6.28 * yy) * sinf(6.28 * zz) + cosf(6.28* zz) * sinf(6.28 * xx);
d_latticevol[index] = a;
__syncthreads();
}
};
void Fft_lattice::create_lattice(float *d_latticevol, uint NX, uint NY, uint NZ, uint size)
{
dim3 grids(ceil((NX)/float(16)),ceil((NY)/float(8)),ceil((NZ)/float(8)));
dim3 tids(16,8,8);
create_lattice_kernel<<<grids,tids>>>(d_latticevol,NX,NY,NZ,size);
cudaDeviceSynchronize();
}
void normalise_bufferr(float *dataone, float *datatwo, size_t size)
{
float *h_B;
h_B = (float *)malloc((size) * sizeof(*dataone));
cudaMemcpy(h_B, dataone, (size) * sizeof(*dataone), cudaMemcpyHostToHost);
float a,b;
for (int i=0;i<size;i++)
{
if(i==0)
{
a = h_B[i];
b = h_B[i];
}
a=min(a,h_B[i]);
b =max(b,h_B[i]);
}
for(int i=0;i<size;i++)
{
h_B[i] = (h_B[i] - a)/(b-a);
}
cudaMemcpy(datatwo, h_B, (size) * sizeof(*h_B), cudaMemcpyHostToHost);
free(h_B);
}
void Fft_lattice::fft_func(float2 *fft_data)
{
checkCudaErrors(cufftExecR2C(planr2c, (cufftReal *)fft_data, (cufftComplex *)fft_data));
}
void Fft_lattice::ifft_func(float2 *fft_data)
{
checkCudaErrors(cufftExecC2R(planc2r, (cufftComplex *)fft_data, (cufftReal *)fft_data));
}
__global__ void fft_scalar_kernel(float2 *fft_data_compute,float scalar_val,int size)
{
int tx = blockIdx.x * blockDim.x + threadIdx.x;
float2 a ;
if(tx < size)
{
a = fft_data_compute[tx];
a.x /= scalar_val;
a.y /= scalar_val;
fft_data_compute[tx] = a;
__syncthreads();
}
};
void Fft_lattice::fft_scalar(float2 *fft_data_compute,float scalar_val,int size)
{
dim3 grids(ceil((size)/float(1024)),1,1);
dim3 tids(1024,1,1);
fft_scalar_kernel<<<grids,tids>>>(fft_data_compute,scalar_val,size);
cudaDeviceSynchronize();
}
__global__ void fft_fill_kernel(float2 *fft_compute, float2 *fft_compute_fill,int Nx, int Ny , int Nz ,size_t size, uint Nx2)
{
int tx = blockIdx.x * blockDim.x + threadIdx.x;
if(tx < Nx2*Ny*Nz)
{
int z = tx/(Nx2*Ny);
int y = (tx%(Nx2*Ny))/Nx2;
int x = (tx%(Nx2*Ny))%Nx2;
int e,f,g;
if((x == 0) && (y == 0) && (z == 0))
{
fft_compute_fill[0] = fft_compute[0];
}
else
{
if(x == 0)
{
e = 0;
}
else if(x > 0)
{
e = Nx - x;
}
if( y == 0)
{
f = 0;
}
else if(y > 0)
{
f = Nx - y;
}
if(z == 0)
{
g = 0;
}
else if(z > 0)
{
g = Nx - z;
}
int indd = x + y *Nx2 + z * (Nx2 *Ny);
int indd1 = x + y *Nx + z * (Nx*Ny);
int indd2 = e + f *Nx + g * (Nx*Ny);
fft_compute_fill[indd1] = fft_compute[indd];
fft_compute_fill[indd2] = fft_compute[indd];
fft_compute_fill[indd2].y *= -1;
}
}
}
void Fft_lattice::fft_fill(float2 *fft_compute, float2 *fft_compute_fill,int Nx, int Ny , int Nz)
{
uint Nx2 = floor(Nx/2.0) +1;
size_t size = Nx2*Ny*Nz;
dim3 grids(ceil((size)/float(1024)),1,1);
dim3 tids(1024,1,1);
fft_fill_kernel<<<grids,tids>>>(fft_compute,fft_compute_fill,Nx,Ny,Nz,size,Nx2);
cudaDeviceSynchronize();
}