-
Notifications
You must be signed in to change notification settings - Fork 1
/
pair_style.cu
328 lines (251 loc) · 11 KB
/
pair_style.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#include "globals.h"
#include "pair_style.h"
__global__ void d_prepareDensity(int, float*, cufftComplex*, int);
__global__ void d_prepareChargeDensity(float*, cufftComplex*, int);
__global__ void d_prepareForceKSpace(cufftComplex*, cufftComplex*,
cufftComplex*, const int, const int, const int);
__global__ void d_accumulateGridForce(cufftComplex*, float*, float*,
const int, const int);
__global__ void d_multiplyComplex(cufftComplex*, cufftComplex*,
cufftComplex*, int);
__global__ void d_prepareIntegrand(cufftComplex*, int, float*, float*, int);
__global__ void d_extractForceComp(cufftComplex*, cufftComplex*, const int, const int, const int);
__global__ void d_initVirial(float*, const float*, const float*, const float*,
const float*, const int, const int, const int*, const int);
__global__ void d_extractVirialCompR2C(cufftComplex*, const float*, const int,
const int, const int);
__global__ void d_insertVirialCompC2C(cufftComplex*, const cufftComplex*, const int,
const int, const int);
__global__ void d_prepareVirial(const cufftComplex*, const cufftComplex*,
cufftComplex*, const int, const int, const int, const int);
__global__ void d_divideByDimension(cufftComplex*, const int);
__global__ void d_multiply_cufftCpx_scalar(cufftComplex*, float, int);
float reduce_device_float(float*, const int, const int);
//bool checknan_cpx(cufftComplex*, int);
void write_lammps_traj(void);
void cuda_collect_x(void);
void cuda_collect_rho(void);
void write_kspace_cudaComplex(const char*, cufftComplex*);
// Constructor, allocates memory for:
// potential, forces, virial contribution
// in both r- and k-space
void PairStyle::Initialize_PairStyle(int alloc_size, int typ_A, int typ_B) {
allocated = true;
int n_off_diag = Dim + (Dim * Dim - Dim) / 2;
size = alloc_size;
this->u = (float*)calloc(alloc_size, sizeof(float));
this->f = (float**)calloc(Dim, sizeof(float*));
this->vir = (float**)calloc(n_off_diag, sizeof(float*));
this->u_k = (complex<float>*) calloc(alloc_size, sizeof(complex<float>));
this->f_k = (complex<float>**) calloc(Dim, sizeof(complex<float>*));
this->vir_k = (complex<float>**) calloc(n_off_diag, sizeof(complex<float>*));
for (int i = 0; i < Dim; i++) {
this->f[i] = (float*)calloc(alloc_size, sizeof(float));
this->f_k[i] = (complex<float>*) calloc(alloc_size, sizeof(complex<float>));
}
total_vir = (float*)calloc(n_off_diag, sizeof(float));
// Vir stores the diagonal plus the off-diagonal terms
// The term in parenthesis (Dim*Dim-Dim) will always be even
for (int i = 0; i < n_off_diag; i++) {
this->vir[i] = (float*)calloc(alloc_size, sizeof(float));
this->vir_k[i] = (complex<float>*) calloc(alloc_size, sizeof(complex<float>));
}
cudaMalloc(&this->d_u, M * sizeof(float));
cudaMalloc(&this->d_f, Dim * M * sizeof(float));
cudaMalloc(&this->d_vir, n_P_comps * M * sizeof(float));
device_mem_use += M * (Dim + 1 + n_P_comps) * sizeof(float);
cudaMalloc(&this->d_u_k, M * sizeof(cufftComplex));
cudaMalloc(&this->d_f_k, Dim * M * sizeof(cufftComplex));
cudaMalloc(&this->d_vir_k, n_P_comps * M * sizeof(cufftComplex));
device_mem_use += M * (Dim + 1 + n_P_comps) * sizeof(cufftComplex);
type1 = typ_A;
type2 = typ_B;
}
// Calculates forces on rho1, rho2 for this pairstyle
void PairStyle::CalcForces() {
/////////////////////////
// rho2 acting on rho1 //
/////////////////////////
// fft rho2
d_prepareDensity<<<M_Grid, M_Block>>>(type2, d_all_rho, d_cpx1, M);
cudaReturn = cudaGetLastError();
if (cudaReturn != cudaSuccess) {
char cherror[90];
sprintf(cherror, "Cuda failed with error \"%s\" while d_prepareDensity ran\n",
cudaGetErrorString(cudaReturn));
die(cherror);
}
cufftExecC2C(fftplan, d_cpx1, d_cpx2, CUFFT_FORWARD);
cudaReturn = cudaGetLastError();
if (cudaReturn != cudaSuccess) {
char cherror[90];
sprintf(cherror, "Cuda failed with error \"%s\" while cufftExec1 ran\n", cudaGetErrorString(cudaReturn));
die(cherror);
}
for (int j = 0; j < Dim; j++) {
// d_cpx1 = d_cpx2 * d_f_k
d_prepareForceKSpace<<<M_Grid, M_Block>>>(this->d_f_k,
d_cpx2, d_cpx1, j, Dim, M);
cudaReturn = cudaGetLastError();
if (cudaReturn != cudaSuccess) {
char cherror[90];
sprintf(cherror, "Cuda failed with error \"%s\" while d_prepareForceKSpace in j=%d ran\n", cudaGetErrorString(cudaReturn), j);
die(cherror);
}
// back to real space, in-place transform
cufftExecC2C(fftplan, d_cpx1, d_cpx1, CUFFT_INVERSE);
cudaReturn = cudaGetLastError();
if (cudaReturn != cudaSuccess) {
char cherror[90];
sprintf(cherror, "Cuda failed with error \"%s\" while cufft2 in j=%d ran\n", cudaGetErrorString(cudaReturn), j);
die(cherror);
}
// Accumulate the forces on type 1
if (j == 0)
d_accumulateGridForce<<<M_Grid, M_Block>>>(d_cpx1,
d_all_rho, d_all_fx, type1, M);
if (j == 1)
d_accumulateGridForce<<<M_Grid, M_Block>>>(d_cpx1,
d_all_rho, d_all_fy, type1, M);
if (j == 2)
d_accumulateGridForce<<<M_Grid, M_Block>>>(d_cpx1,
d_all_rho, d_all_fz, type1, M);
cudaReturn = cudaGetLastError();
if (cudaReturn != cudaSuccess) {
char cherror[90];
sprintf(cherror, "Cuda failed with error \"%s\" while d_accumulateGridForce in j=%d ran\n",
cudaGetErrorString(cudaReturn), j);
die(cherror);
}
}
// fft rho1
d_prepareDensity<<<M_Grid, M_Block>>> (type1, d_all_rho, d_cpx1, M);
cufftExecC2C(fftplan, d_cpx1, d_cpx2, CUFFT_FORWARD);
cudaReturn = cudaGetLastError();
if (cudaReturn != cudaSuccess) {
char cherror[90];
sprintf(cherror, "Cuda failed with error \"%s\" while cufftExec ran\n", cudaGetErrorString(cudaReturn));
die(cherror);
}
for (int j = 0; j < Dim; j++) {
// d_cpx1 = d_cpx2 * d_f_k
d_prepareForceKSpace<<<M_Grid, M_Block>>>(this->d_f_k,
d_cpx2, d_cpx1, j, Dim, M);
// back to real space, in-place transform
cufftExecC2C(fftplan, d_cpx1, d_cpx1, CUFFT_INVERSE);
// Accumulate the forces on type 2
if (j == 0)
d_accumulateGridForce<<<M_Grid, M_Block>>>(d_cpx1,
d_all_rho, d_all_fx, type2, M);
if (j == 1)
d_accumulateGridForce<<<M_Grid, M_Block>>>(d_cpx1,
d_all_rho, d_all_fy, type2, M);
if (j == 2)
d_accumulateGridForce<<<M_Grid, M_Block>>>(d_cpx1,
d_all_rho, d_all_fz, type2, M);
}// j=0:Dim
/*cudaMemcpy(tmp, d_all_fx, M * sizeof(float), cudaMemcpyDeviceToHost);
write_grid_data("fx0.dat", tmp);
cudaMemcpy(tmp, d_all_fy, M * sizeof(float), cudaMemcpyDeviceToHost);
write_grid_data("fy0.dat", tmp);
cuda_collect_rho();
for (int i = 0; i < ntypes; i++) {
char nm[30];
sprintf(nm, "rho%d.dat", i);
write_grid_data(nm, Components[i].rho);
}
if ( type1 == 0 && type2 == 1 )
exit(1);*/
}
// Calculates the energy involved in this potential as
// energy = \int dr rho1(r) \int dr' u(r-r') rho2(r')
// The convolution theorem is used to efficiently evaluate
// the integral over r'
float PairStyle::CalcEnergy() {
// fft rho2
d_prepareDensity<<<M_Grid, M_Block>>>(type2, d_all_rho, d_cpx1, M);
cufftExecC2C(fftplan, d_cpx1, d_cpx2, CUFFT_FORWARD);
// Multiply by d_u_k
d_multiplyComplex<<<M_Grid, M_Block>>>(this->d_u_k, d_cpx2,
d_cpx1, M);
// Back to real space
cufftExecC2C(fftplan, d_cpx1, d_cpx1, CUFFT_INVERSE);
d_prepareIntegrand<<<M_Grid, M_Block>>>(d_cpx1, type1, d_all_rho,
d_tmp, M);
// Copy the integrand back to host for integration
// This should be replaced with on-device integration at some point
cudaMemcpy(tmp, d_tmp, M * sizeof(float), cudaMemcpyDeviceToHost);
this->energy = integ_trapPBC(tmp);
//float temp = reduce_device_float(d_tmp, threads, M);
//cout << "host: " << this->energy << " device: " << temp << endl;
//die("here!");
return this->energy;
}
void PairStyle::CalcVirial()
{
// fft rho2
d_prepareDensity<<<M_Grid, M_Block>>>(this->type2, d_all_rho, d_cpx1, M);
cufftExecC2C(fftplan, d_cpx1, d_cpx1, CUFFT_FORWARD);
d_divideByDimension<<<M_Grid, M_Block>>>(d_cpx1, M);
for (int j = 0; j < n_P_comps; j++) {
d_prepareVirial<<<M_Grid, M_Block>>>(this->d_vir_k, d_cpx1, d_cpx2,
j, Dim, n_P_comps, M);
// back to real space, in-place transform
cufftExecC2C(fftplan, d_cpx2, d_cpx2, CUFFT_INVERSE);
d_prepareIntegrand<<<M_Grid, M_Block>>>(d_cpx2, this->type1, d_all_rho,
d_tmp, M);
// Copy the integrand back to host for integration
// This should be replaced with on-device integration at some point
cudaMemcpy(tmp, d_tmp, M * sizeof(float), cudaMemcpyDeviceToHost);
this->total_vir[j] = integ_trapPBC(tmp) / V / float(Dim);
}
}
void PairStyle::InitializeVirial()
{
d_initVirial<<<M_Grid, M_Block>>>(this->d_vir, this->d_f,
d_L, d_Lh, d_dx, Dim, n_P_comps, d_Nx, M);
for (int j = 0; j < n_P_comps; j++) {
d_extractVirialCompR2C<<<M_Grid, M_Block>>>(d_cpx1, this->d_vir, j, n_P_comps, M);
cufftExecC2C(fftplan, d_cpx1, d_cpx1, CUFFT_FORWARD);
d_divideByDimension<<<M_Grid, M_Block>>>(d_cpx1, M);
d_insertVirialCompC2C<<<M_Grid, M_Block>>>(this->d_vir_k, d_cpx1, j, n_P_comps, M);
}
}
PairStyle::~PairStyle() {
//printf("PS here for some reason!\n"); fflush(stdout);
if (allocated){
int n_off_diag = Dim + (Dim * Dim - Dim) / 2;
free(this->u);
free(this->u_k);
for (int i = 0; i < Dim; i++) {
free(this->f[i]);
free(this->f_k[i]);
}
for (int i = 0; i < n_off_diag; i++) {
free(this->vir[i]);
free(this->vir_k[i]);
}
free(this->f);
free(this->f_k);
free(this->vir);
free(this->vir_k);
}
}
PairStyle::PairStyle() {
}
void PairStyle::Update() {
if (!ramp) return;
// The factor of 1000x is to attempt to cancel some float
// precision errors if chi is adjusted slowly over the course
// of a long simulation. It cancels since the ratio is passed
// as the argument
float delta_A = (1000.0f * (initial_prefactor - final_prefactor)) / float(max_steps);
float cur_A = 1000.0f * initial_prefactor + delta_A * float(step);
float new_A = cur_A + delta_A;
cufftComplex temp_cpx;
cudaMemcpy(&temp_cpx, this->d_u_k, sizeof(cufftComplex), cudaMemcpyDeviceToHost);
cur_A = temp_cpx.x * 1000.0f;
d_multiply_cufftCpx_scalar << <M_Grid, M_Block >> > (this->d_u_k, new_A / cur_A, M);
d_multiply_cufftCpx_scalar << <M_Grid * Dim, M_Block >> > (this->d_f_k,
new_A / cur_A, M * Dim);
}