1 // clang-format off
2 /* ----------------------------------------------------------------------
3    LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
4    https://www.lammps.org/, Sandia National Laboratories
5    Steve Plimpton, sjplimp@sandia.gov
6 
7    Copyright (2003) Sandia Corporation.  Under the terms of Contract
8    DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains
9    certain rights in this software.  This software is distributed under
10    the GNU General Public License.
11 
12    See the README file in the top-level LAMMPS directory.
13 ------------------------------------------------------------------------- */
14 
15 /* ----------------------------------------------------------------------
16    Contributing author: Trung Nguyen (Northwestern)
17 ------------------------------------------------------------------------- */
18 
19 #include "pair_lj_expand_coul_long_gpu.h"
20 
21 #include "atom.h"
22 #include "domain.h"
23 #include "error.h"
24 #include "force.h"
25 #include "gpu_extra.h"
26 #include "kspace.h"
27 #include "neigh_list.h"
28 #include "neigh_request.h"
29 #include "neighbor.h"
30 #include "suffix.h"
31 
32 #include <cmath>
33 
34 #define EWALD_F   1.12837917
35 #define EWALD_P   0.3275911
36 #define A1        0.254829592
37 #define A2       -0.284496736
38 #define A3        1.421413741
39 #define A4       -1.453152027
40 #define A5        1.061405429
41 
42 using namespace LAMMPS_NS;
43 
44 // External functions from cuda library for atom decomposition
45 
46 int ljecl_gpu_init(const int ntypes, double **cutsq, double **host_lj1,
47                    double **host_lj2, double **host_lj3, double **host_lj4,
48                    double **offset, double **shift, double *special_lj,
49                    const int nlocal, const int nall, const int max_nbors,
50                    const int maxspecial, const double cell_size,
51                    int &gpu_mode, FILE *screen, double **host_cut_ljsq,
52                    double host_cut_coulsq, double *host_special_coul,
53                    const double qqrd2e, const double g_ewald);
54 int ljecl_gpu_reinit(const int ntypes, double **cutsq, double **host_lj1,
55                      double **host_lj2, double **host_lj3, double **host_lj4,
56                      double **offset, double **shift, double **host_lj_cutsq);
57 void ljecl_gpu_clear();
58 int ** ljecl_gpu_compute_n(const int ago, const int inum,
59                            const int nall, double **host_x, int *host_type,
60                            double *sublo, double *subhi, tagint *tag,
61                            int **nspecial, tagint **special, const bool eflag,
62                            const bool vflag, const bool eatom, const bool vatom,
63                            int &host_start, int **ilist, int **jnum,
64                            const double cpu_time, bool &success, double *host_q,
65                            double *boxlo, double *prd);
66 void ljecl_gpu_compute(const int ago, const int inum, const int nall,
67                        double **host_x, int *host_type, int *ilist, int *numj,
68                        int **firstneigh, const bool eflag, const bool vflag,
69                        const bool eatom, const bool vatom, int &host_start,
70                        const double cpu_time, bool &success, double *host_q,
71                        const int nlocal, double *boxlo, double *prd);
72 double ljecl_gpu_bytes();
73 
74 /* ---------------------------------------------------------------------- */
75 
PairLJExpandCoulLongGPU(LAMMPS * lmp)76 PairLJExpandCoulLongGPU::PairLJExpandCoulLongGPU(LAMMPS *lmp) :
77   PairLJExpandCoulLong(lmp), gpu_mode(GPU_FORCE)
78 {
79   respa_enable = 0;
80   cpu_time = 0.0;
81   suffix_flag |= Suffix::GPU;
82   GPU_EXTRA::gpu_ready(lmp->modify, lmp->error);
83 }
84 
85 /* ----------------------------------------------------------------------
86    free all arrays
87 ------------------------------------------------------------------------- */
88 
~PairLJExpandCoulLongGPU()89 PairLJExpandCoulLongGPU::~PairLJExpandCoulLongGPU()
90 {
91   ljecl_gpu_clear();
92 }
93 
94 /* ---------------------------------------------------------------------- */
95 
compute(int eflag,int vflag)96 void PairLJExpandCoulLongGPU::compute(int eflag, int vflag)
97 {
98   ev_init(eflag,vflag);
99 
100   int nall = atom->nlocal + atom->nghost;
101   int inum, host_start;
102 
103   bool success = true;
104   int *ilist, *numneigh, **firstneigh;
105   if (gpu_mode != GPU_FORCE) {
106     double sublo[3],subhi[3];
107     if (domain->triclinic == 0) {
108       sublo[0] = domain->sublo[0];
109       sublo[1] = domain->sublo[1];
110       sublo[2] = domain->sublo[2];
111       subhi[0] = domain->subhi[0];
112       subhi[1] = domain->subhi[1];
113       subhi[2] = domain->subhi[2];
114     } else {
115       domain->bbox(domain->sublo_lamda,domain->subhi_lamda,sublo,subhi);
116     }
117     inum = atom->nlocal;
118     firstneigh = ljecl_gpu_compute_n(neighbor->ago, inum, nall, atom->x,
119                                     atom->type, sublo, subhi,
120                                     atom->tag, atom->nspecial, atom->special,
121                                     eflag, vflag, eflag_atom, vflag_atom,
122                                     host_start, &ilist, &numneigh, cpu_time,
123                                     success, atom->q, domain->boxlo,
124                                     domain->prd);
125   } else {
126     inum = list->inum;
127     ilist = list->ilist;
128     numneigh = list->numneigh;
129     firstneigh = list->firstneigh;
130     ljecl_gpu_compute(neighbor->ago, inum, nall, atom->x, atom->type,
131                      ilist, numneigh, firstneigh, eflag, vflag, eflag_atom,
132                      vflag_atom, host_start, cpu_time, success, atom->q,
133                      atom->nlocal, domain->boxlo, domain->prd);
134   }
135   if (!success)
136     error->one(FLERR,"Insufficient memory on accelerator");
137 
138   if (host_start<inum) {
139     cpu_time = MPI_Wtime();
140     cpu_compute(host_start, inum, eflag, vflag, ilist, numneigh, firstneigh);
141     cpu_time = MPI_Wtime() - cpu_time;
142   }
143 }
144 
145 /* ----------------------------------------------------------------------
146    init specific to this pair style
147 ------------------------------------------------------------------------- */
148 
init_style()149 void PairLJExpandCoulLongGPU::init_style()
150 {
151   cut_respa = nullptr;
152 
153   if (!atom->q_flag)
154     error->all(FLERR,"Pair style lj/cut/coul/long/gpu requires atom attribute q");
155   if (force->newton_pair)
156     error->all(FLERR,"Pair style lj/cut/coul/long/gpu requires newton pair off");
157 
158   // Repeat cutsq calculation because done after call to init_style
159   double maxcut = -1.0;
160   double cut;
161   for (int i = 1; i <= atom->ntypes; i++) {
162     for (int j = i; j <= atom->ntypes; j++) {
163       if (setflag[i][j] != 0 || (setflag[i][i] != 0 && setflag[j][j] != 0)) {
164         cut = init_one(i,j);
165         cut *= cut;
166         if (cut > maxcut)
167           maxcut = cut;
168         cutsq[i][j] = cutsq[j][i] = cut;
169       } else
170         cutsq[i][j] = cutsq[j][i] = 0.0;
171     }
172   }
173   double cell_size = sqrt(maxcut) + neighbor->skin;
174 
175   cut_coulsq = cut_coul * cut_coul;
176 
177   // insure use of KSpace long-range solver, set g_ewald
178 
179   if (force->kspace == nullptr)
180     error->all(FLERR,"Pair style requires a KSpace style");
181   g_ewald = force->kspace->g_ewald;
182 
183   // setup force tables
184 
185   if (ncoultablebits) init_tables(cut_coul,cut_respa);
186 
187   int maxspecial=0;
188   if (atom->molecular != Atom::ATOMIC)
189     maxspecial=atom->maxspecial;
190   int mnf = 5e-2 * neighbor->oneatom;
191   int success = ljecl_gpu_init(atom->ntypes+1, cutsq, lj1, lj2, lj3, lj4,
192                               offset, shift, force->special_lj, atom->nlocal,
193                               atom->nlocal+atom->nghost, mnf, maxspecial,
194                               cell_size, gpu_mode, screen, cut_ljsq, cut_coulsq,
195                               force->special_coul, force->qqrd2e, g_ewald);
196   GPU_EXTRA::check_flag(success,error,world);
197 
198   if (gpu_mode == GPU_FORCE) {
199     int irequest = neighbor->request(this,instance_me);
200     neighbor->requests[irequest]->half = 0;
201     neighbor->requests[irequest]->full = 1;
202   }
203 }
204 
205 /* ---------------------------------------------------------------------- */
206 
reinit()207 void PairLJExpandCoulLongGPU::reinit()
208 {
209   Pair::reinit();
210 
211   ljecl_gpu_reinit(atom->ntypes+1, cutsq, lj1, lj2, lj3, lj4, offset, shift, cut_ljsq);
212 }
213 
214 /* ---------------------------------------------------------------------- */
215 
memory_usage()216 double PairLJExpandCoulLongGPU::memory_usage()
217 {
218   double bytes = Pair::memory_usage();
219   return bytes + ljecl_gpu_bytes();
220 }
221 
222 /* ---------------------------------------------------------------------- */
223 
cpu_compute(int start,int inum,int eflag,int,int * ilist,int * numneigh,int ** firstneigh)224 void PairLJExpandCoulLongGPU::cpu_compute(int start, int inum, int eflag,
225                                        int /* vflag */, int *ilist,
226                                        int *numneigh, int **firstneigh)
227 {
228   int i,j,ii,jj,jnum,itype,jtype,itable;
229   double qtmp,xtmp,ytmp,ztmp,delx,dely,delz,evdwl,ecoul,fpair;
230   double fraction,table;
231   double r,r2inv,r6inv,forcecoul,forcelj,factor_coul,factor_lj;
232   double grij,expm2,prefactor,t,erfc;
233   double rsq,rshift,rshiftsq,rshift2inv;
234 
235   int *jlist;
236 
237   evdwl = ecoul = 0.0;
238 
239   double **x = atom->x;
240   double **f = atom->f;
241   double *q = atom->q;
242   int *type = atom->type;
243   double *special_coul = force->special_coul;
244   double *special_lj = force->special_lj;
245   double qqrd2e = force->qqrd2e;
246 
247   // loop over neighbors of my atoms
248 
249   for (ii = start; ii < inum; ii++) {
250     i = ilist[ii];
251     qtmp = q[i];
252     xtmp = x[i][0];
253     ytmp = x[i][1];
254     ztmp = x[i][2];
255     itype = type[i];
256     jlist = firstneigh[i];
257     jnum = numneigh[i];
258 
259     for (jj = 0; jj < jnum; jj++) {
260       j = jlist[jj];
261       factor_lj = special_lj[sbmask(j)];
262       factor_coul = special_coul[sbmask(j)];
263       j &= NEIGHMASK;
264 
265       delx = xtmp - x[j][0];
266       dely = ytmp - x[j][1];
267       delz = ztmp - x[j][2];
268       rsq = delx*delx + dely*dely + delz*delz;
269       jtype = type[j];
270 
271       if (rsq < cutsq[itype][jtype]) {
272         r2inv = 1.0/rsq;
273 
274         if (rsq < cut_coulsq) {
275           if (!ncoultablebits || rsq <= tabinnersq) {
276             r = sqrt(rsq);
277             grij = g_ewald * r;
278             expm2 = exp(-grij*grij);
279             t = 1.0 / (1.0 + EWALD_P*grij);
280             erfc = t * (A1+t*(A2+t*(A3+t*(A4+t*A5)))) * expm2;
281             prefactor = qqrd2e * qtmp*q[j]/r;
282             forcecoul = prefactor * (erfc + EWALD_F*grij*expm2);
283             if (factor_coul < 1.0) forcecoul -= (1.0-factor_coul)*prefactor;
284           } else {
285             union_int_float_t rsq_lookup;
286             rsq_lookup.f = rsq;
287             itable = rsq_lookup.i & ncoulmask;
288             itable >>= ncoulshiftbits;
289             fraction = (rsq_lookup.f - rtable[itable]) * drtable[itable];
290             table = ftable[itable] + fraction*dftable[itable];
291             forcecoul = qtmp*q[j] * table;
292             if (factor_coul < 1.0) {
293               table = ctable[itable] + fraction*dctable[itable];
294               prefactor = qtmp*q[j] * table;
295               forcecoul -= (1.0-factor_coul)*prefactor;
296             }
297           }
298         } else forcecoul = 0.0;
299 
300         if (rsq < cut_ljsq[itype][jtype]) {
301           r = sqrt(rsq);
302           rshift = r - shift[itype][jtype];
303           rshiftsq = rshift*rshift;
304           rshift2inv = 1.0/rshiftsq;
305           r6inv = rshift2inv*rshift2inv*rshift2inv;
306           forcelj = r6inv * (lj1[itype][jtype]*r6inv - lj2[itype][jtype]);
307           forcelj = factor_lj*forcelj/rshift/r;
308         } else forcelj = 0.0;
309 
310         fpair = forcecoul*r2inv + forcelj;
311 
312         f[i][0] += delx*fpair;
313         f[i][1] += dely*fpair;
314         f[i][2] += delz*fpair;
315 
316         if (eflag) {
317           if (rsq < cut_coulsq) {
318             if (!ncoultablebits || rsq <= tabinnersq)
319               ecoul = prefactor*erfc;
320             else {
321               table = etable[itable] + fraction*detable[itable];
322               ecoul = qtmp*q[j] * table;
323             }
324             if (factor_coul < 1.0) ecoul -= (1.0-factor_coul)*prefactor;
325           } else ecoul = 0.0;
326 
327           if (rsq < cut_ljsq[itype][jtype]) {
328             evdwl = r6inv*(lj3[itype][jtype]*r6inv-lj4[itype][jtype]) -
329               offset[itype][jtype];
330             evdwl *= factor_lj;
331           } else evdwl = 0.0;
332         }
333 
334         if (evflag) ev_tally_full(i,evdwl,ecoul,fpair,delx,dely,delz);
335       }
336     }
337   }
338 }
339