-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathforce_lj.cpp
More file actions
446 lines (358 loc) · 12.6 KB
/
force_lj.cpp
File metadata and controls
446 lines (358 loc) · 12.6 KB
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/* ----------------------------------------------------------------------
miniMD is a simple, parallel molecular dynamics (MD) code. miniMD is
an MD microapplication in the Mantevo project at Sandia National
Laboratories ( http://www.mantevo.org ). The primary
authors of miniMD are Steve Plimpton (sjplimp@sandia.gov) , Paul Crozier
(pscrozi@sandia.gov) and Christian Trott (crtrott@sandia.gov).
Copyright (2008) Sandia Corporation. Under the terms of Contract
DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains
certain rights in this software. This library is free software; you
can redistribute it and/or modify it under the terms of the GNU Lesser
General Public License as published by the Free Software Foundation;
either version 3 of the License, or (at your option) any later
version.
This library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this software; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA. See also: http://www.gnu.org/licenses/lgpl.txt .
For questions, contact Paul S. Crozier (pscrozi@sandia.gov) or
Christian Trott (crtrott@sandia.gov).
Please read the accompanying README and LICENSE files.
---------------------------------------------------------------------- */
#include "stdio.h"
#include "math.h"
#include "force_lj.h"
#include "openmp.h"
#ifndef VECTORLENGTH
#define VECTORLENGTH 4
#endif
ForceLJ::ForceLJ()
{
cutforce = 0.0;
cutforcesq = 0.0;
use_oldcompute = 0;
reneigh = 1;
style = FORCELJ;
epsilon = 1.0;
sigma6 = 1.0;
sigma = 1.0;
}
ForceLJ::~ForceLJ() {}
void ForceLJ::setup(Atom &atom)
{
cutforcesq = cutforce * cutforce;
}
void ForceLJ::compute(Atom &atom, Neighbor &neighbor, Comm &comm, int me)
{
eng_vdwl = 0;
virial = 0;
if(evflag) {
if(use_oldcompute)
return compute_original<1>(atom, neighbor, me);
if(neighbor.halfneigh) {
if(neighbor.ghost_newton) {
if(threads->omp_num_threads > 1)
return compute_halfneigh_threaded<1, 1>(atom, neighbor, me);
else
return compute_halfneigh<1, 1>(atom, neighbor, me);
} else {
if(threads->omp_num_threads > 1)
return compute_halfneigh_threaded<1, 0>(atom, neighbor, me);
else
return compute_halfneigh<1, 0>(atom, neighbor, me);
}
} else return compute_fullneigh<1>(atom, neighbor, me);
} else {
if(use_oldcompute)
return compute_original<0>(atom, neighbor, me);
if(neighbor.halfneigh) {
if(neighbor.ghost_newton) {
if(threads->omp_num_threads > 1)
return compute_halfneigh_threaded<0, 1>(atom, neighbor, me);
else
return compute_halfneigh<0, 1>(atom, neighbor, me);
} else {
if(threads->omp_num_threads > 1)
return compute_halfneigh_threaded<0, 0>(atom, neighbor, me);
else
return compute_halfneigh<0, 0>(atom, neighbor, me);
}
} else return compute_fullneigh<0>(atom, neighbor, me);
}
}
//original version of force compute in miniMD
// -MPI only
// -not vectorizable
template<int EVFLAG>
void ForceLJ::compute_original(Atom &atom, Neighbor &neighbor, int me)
{
int i, j, k, nlocal, nall, numneigh;
MMD_float xtmp, ytmp, ztmp, delx, dely, delz, rsq;
MMD_float sr2, sr6, force;
int* neighs;
MMD_float** x, **f;
nlocal = atom.nlocal;
nall = atom.nlocal + atom.nghost;
x = atom.x;
f = atom.f;
eng_vdwl = 0;
virial = 0;
// clear force on own and ghost atoms
for(i = 0; i < nall; i++) {
f[i][0] = 0.0;
f[i][1] = 0.0;
f[i][2] = 0.0;
}
// loop over all neighbors of my atoms
// store force on both atoms i and j
for(i = 0; i < nlocal; i++) {
neighs = &neighbor.neighbors[i * neighbor.maxneighs];
numneigh = neighbor.numneigh[i];
xtmp = x[i][0];
ytmp = x[i][1];
ztmp = x[i][2];
for(k = 0; k < numneigh; k++) {
j = neighs[k];
delx = xtmp - x[j][0];
dely = ytmp - x[j][1];
delz = ztmp - x[j][2];
rsq = delx * delx + dely * dely + delz * delz;
if(rsq < cutforcesq) {
sr2 = 1.0 / rsq;
sr6 = sr2 * sr2 * sr2 * sigma6;
force = 48.0 * sr6 * (sr6 - 0.5) * sr2 * epsilon;
f[i][0] += delx * force;
f[i][1] += dely * force;
f[i][2] += delz * force;
f[j][0] -= delx * force;
f[j][1] -= dely * force;
f[j][2] -= delz * force;
if(EVFLAG) {
eng_vdwl += (4.0 * sr6 * (sr6 - 1.0)) * epsilon;
virial += (delx * delx + dely * dely + delz * delz) * force;
}
}
}
}
}
//optimised version of compute
// -MPI only
// -use temporary variable for summing up fi
// -enables vectorization by:
// -getting rid of 2d pointers
// -use pragma simd to force vectorization of inner loop
template<int EVFLAG, int GHOST_NEWTON>
void ForceLJ::compute_halfneigh(Atom &atom, Neighbor &neighbor, int me)
{
int* neighs;
int tid = omp_get_thread_num();
const int nlocal = atom.nlocal;
const int nall = atom.nlocal + atom.nghost;
MMD_float* x = &atom.x[0][0];
MMD_float* f = &atom.f[0][0];
// clear force on own and ghost atoms
for(int i = 0; i < nall; i++) {
f[i * PAD + 0] = 0.0;
f[i * PAD + 1] = 0.0;
f[i * PAD + 2] = 0.0;
}
// loop over all neighbors of my atoms
// store force on both atoms i and j
MMD_float t_energy = 0;
MMD_float t_virial = 0;
for(int i = 0; i < nlocal; i++) {
neighs = &neighbor.neighbors[i * neighbor.maxneighs];
const int numneighs = neighbor.numneigh[i];
const MMD_float xtmp = x[i * PAD + 0];
const MMD_float ytmp = x[i * PAD + 1];
const MMD_float ztmp = x[i * PAD + 2];
MMD_float fix = 0.0;
MMD_float fiy = 0.0;
MMD_float fiz = 0.0;
#ifdef USE_SIMD
#pragma simd reduction (+: fix,fiy,fiz)
#endif
for(int k = 0; k < numneighs; k++) {
const int j = neighs[k];
const MMD_float delx = xtmp - x[j * PAD + 0];
const MMD_float dely = ytmp - x[j * PAD + 1];
const MMD_float delz = ztmp - x[j * PAD + 2];
const MMD_float rsq = delx * delx + dely * dely + delz * delz;
if(rsq < cutforcesq) {
const MMD_float sr2 = 1.0 / rsq;
const MMD_float sr6 = sr2 * sr2 * sr2 * sigma6;
const MMD_float force = 48.0 * sr6 * (sr6 - 0.5) * sr2 * epsilon;
fix += delx * force;
fiy += dely * force;
fiz += delz * force;
if(GHOST_NEWTON || j < nlocal) {
f[j * PAD + 0] -= delx * force;
f[j * PAD + 1] -= dely * force;
f[j * PAD + 2] -= delz * force;
}
if(EVFLAG) {
const MMD_float scale = (GHOST_NEWTON || j < nlocal) ? 1.0 : 0.5;
t_energy += scale * (4.0 * sr6 * (sr6 - 1.0)) * epsilon;
t_virial += scale * (delx * delx + dely * dely + delz * delz) * force;
}
}
}
f[i * PAD + 0] += fix;
f[i * PAD + 1] += fiy;
f[i * PAD + 2] += fiz;
}
eng_vdwl += t_energy;
virial += t_virial;
}
//optimised version of compute
// -MPI + OpenMP (atomics for fj update)
// -use temporary variable for summing up fi
// -enables vectorization by:
// -getting rid of 2d pointers
// -use pragma simd to force vectorization of inner loop (not currently supported due to OpenMP atomics
template<int EVFLAG, int GHOST_NEWTON>
void ForceLJ::compute_halfneigh_threaded(Atom &atom, Neighbor &neighbor, int me)
{
int nlocal, nall;
int* neighs;
MMD_float* x, *f;
int tid = omp_get_thread_num();
MMD_float t_eng_vdwl = 0;
MMD_float t_virial = 0;
nlocal = atom.nlocal;
nall = atom.nlocal + atom.nghost;
x = &atom.x[0][0];
f = &atom.f[0][0];
// clear force on own and ghost atoms
for(int i = 0; i < nall; i++) {
f[i * PAD + 0] = 0.0;
f[i * PAD + 1] = 0.0;
f[i * PAD + 2] = 0.0;
}
// loop over all neighbors of my atoms
// store force on both atoms i and j
for(int i = 0; i < nlocal; i++) {
neighs = &neighbor.neighbors[i * neighbor.maxneighs];
const int numneighs = neighbor.numneigh[i];
const MMD_float xtmp = x[i * PAD + 0];
const MMD_float ytmp = x[i * PAD + 1];
const MMD_float ztmp = x[i * PAD + 2];
MMD_float fix = 0.0;
MMD_float fiy = 0.0;
MMD_float fiz = 0.0;
for(int k = 0; k < numneighs; k++) {
const int j = neighs[k];
const MMD_float delx = xtmp - x[j * PAD + 0];
const MMD_float dely = ytmp - x[j * PAD + 1];
const MMD_float delz = ztmp - x[j * PAD + 2];
const MMD_float rsq = delx * delx + dely * dely + delz * delz;
if(rsq < cutforcesq) {
const MMD_float sr2 = 1.0 / rsq;
const MMD_float sr6 = sr2 * sr2 * sr2 * sigma6;
const MMD_float force = 48.0 * sr6 * (sr6 - 0.5) * sr2 * epsilon;
fix += delx * force;
fiy += dely * force;
fiz += delz * force;
if(GHOST_NEWTON || j < nlocal) {
f[j * PAD + 0] -= delx * force;
f[j * PAD + 1] -= dely * force;
f[j * PAD + 2] -= delz * force;
}
if(EVFLAG) {
const MMD_float scale = (GHOST_NEWTON || j < nlocal) ? 1.0 : 0.5;
t_eng_vdwl += scale * (4.0 * sr6 * (sr6 - 1.0)) * epsilon;
t_virial += scale * (delx * delx + dely * dely + delz * delz) * force;
}
}
}
f[i * PAD + 0] += fix;
f[i * PAD + 1] += fiy;
f[i * PAD + 2] += fiz;
}
eng_vdwl += t_eng_vdwl;
virial += t_virial;
}
//optimised version of compute
// -MPI + OpenMP (using full neighborlists)
// -gets rid of fj update (read/write to memory)
// -use temporary variable for summing up fi
// -enables vectorization by:
// -get rid of 2d pointers
// -use pragma simd to force vectorization of inner loop
template<int EVFLAG>
void ForceLJ::compute_fullneigh(Atom &atom, Neighbor &neighbor, int me)
{
//int tid = omp_get_thread_num();
const int nlocal = atom.nlocal;
const int nall = atom.nlocal + atom.nghost;
const MMD_float* const restrict x = atom.d_x; //&atom.x[0][0];
//MMD_float* const restrict f = &atom.f[0][0];
MMD_float* const restrict f = atom.d_f;
const int* const restrict neighbors = neighbor.d_neighbors;
const int* const restrict numneigh = neighbor.d_numneigh;
const int maxneighs = neighbor.maxneighs;
const MMD_float sigma6_ = sigma6;
const MMD_float epsilon_ = epsilon;
const MMD_float cutforcesq_ = cutforcesq;
const int nmax = neighbor.nmax;
// clear force on own and ghost atoms
#pragma acc data deviceptr(x,neighbors,numneigh,f) //copyout(f[0:nall*3])
{
MMD_float t_eng_vdwl = 0;
MMD_float t_virial = 0;
#pragma acc kernels
for(int i = 0; i < nlocal; i++) {
f[i * PAD + 0] = 0.0;
f[i * PAD + 1] = 0.0;
f[i * PAD + 2] = 0.0;
}
// loop over all neighbors of my atoms
// store force on atom i
#pragma acc kernels
for(int i = 0; i < nlocal; i++) {
const int* const neighs = &neighbors[i * DS0(nmax,maxneighs)];
const int numneighs = numneigh[i];
const MMD_float xtmp = x[i * PAD + 0];
const MMD_float ytmp = x[i * PAD + 1];
const MMD_float ztmp = x[i * PAD + 2];
MMD_float fix = 0;
MMD_float fiy = 0;
MMD_float fiz = 0;
//pragma simd forces vectorization (ignoring the performance objections of the compiler)
//also give hint to use certain vectorlength for MIC, Sandy Bridge and WESTMERE this should be be 8 here
//give hint to compiler that fix, fiy and fiz are used for reduction only
for(int k = 0; k < numneighs; k++) {
const int j = neighs[k*DS1(nmax,maxneighs)];
const MMD_float delx = xtmp - x[j * PAD + 0];
const MMD_float dely = ytmp - x[j * PAD + 1];
const MMD_float delz = ztmp - x[j * PAD + 2];
const MMD_float rsq = delx * delx + dely * dely + delz * delz;
if(rsq < cutforcesq_) {
const MMD_float sr2 = 1.0 / rsq;
const MMD_float sr6 = sr2 * sr2 * sr2 * sigma6_;
const MMD_float force = 48.0 * sr6 * (sr6 - 0.5) * sr2 * epsilon_;
fix += delx * force;
fiy += dely * force;
fiz += delz * force;
#ifdef ENABLE_EV_CALCULATION //crashes with PGI 13.9
if(EVFLAG) {
t_eng_vdwl += sr6 * (sr6 - 1.0) * epsilon;
t_virial += (delx * delx + dely * dely + delz * delz) * force;
}
#endif //ENABLE_EV_CALCULATION
}
}
f[i * PAD + 0] += fix;
f[i * PAD + 1] += fiy;
f[i * PAD + 2] += fiz;
}
t_eng_vdwl *= 4.0;
t_virial *= 0.5;
eng_vdwl += t_eng_vdwl;
virial += t_virial;
}
}