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 Dac Nguyen (ORNL)
17 ------------------------------------------------------------------------- */
18 
19 #include "pair_lj_gromacs_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 "neigh_list.h"
27 #include "neigh_request.h"
28 #include "neighbor.h"
29 #include "suffix.h"
30 
31 #include <cmath>
32 
33 using namespace LAMMPS_NS;
34 
35 // External functions from cuda library for atom decomposition
36 
37 int ljgrm_gpu_init(const int ntypes, double **cutsq, double **host_lj1,
38                    double **host_lj2, double **host_lj3, double **host_lj4,
39                    double *special_lj, const int inum, const int nall,
40                    const int max_nbors, const int maxspecial,
41                    const double cell_size, int &gpu_mode, FILE *screen,
42                    double **host_ljsw1, double **host_ljsw2,
43                    double **host_ljsw3, double **host_ljsw4,
44                    double **host_ljsw5, double **cut_inner,
45                    double **cut_innersq);
46 void ljgrm_gpu_clear();
47 int ** ljgrm_gpu_compute_n(const int ago, const int inum_full, const int nall,
48                            double **host_x, int *host_type, double *sublo,
49                            double *subhi, tagint *tag, int **nspecial,
50                            tagint **special, const bool eflag, const bool vflag,
51                            const bool eatom, const bool vatom, int &host_start,
52                            int **ilist, int **jnum, const double cpu_time,
53                            bool &success);
54 void ljgrm_gpu_compute(const int ago, const int inum_full, const int nall,
55                        double **host_x, int *host_type, int *ilist, int *numj,
56                        int **firstneigh, const bool eflag, const bool vflag,
57                        const bool eatom, const bool vatom, int &host_start,
58                        const double cpu_time, bool &success);
59 double ljgrm_gpu_bytes();
60 
61 /* ---------------------------------------------------------------------- */
62 
PairLJGromacsGPU(LAMMPS * lmp)63 PairLJGromacsGPU::PairLJGromacsGPU(LAMMPS *lmp) :
64   PairLJGromacs(lmp), gpu_mode(GPU_FORCE)
65 {
66   respa_enable = 0;
67   reinitflag = 0;
68   cpu_time = 0.0;
69   suffix_flag |= Suffix::GPU;
70   GPU_EXTRA::gpu_ready(lmp->modify, lmp->error);
71 }
72 
73 /* ----------------------------------------------------------------------
74    free all arrays
75 ------------------------------------------------------------------------- */
76 
~PairLJGromacsGPU()77 PairLJGromacsGPU::~PairLJGromacsGPU()
78 {
79   ljgrm_gpu_clear();
80 }
81 
82 /* ---------------------------------------------------------------------- */
83 
compute(int eflag,int vflag)84 void PairLJGromacsGPU::compute(int eflag, int vflag)
85 {
86   ev_init(eflag,vflag);
87 
88   int nall = atom->nlocal + atom->nghost;
89   int inum, host_start;
90 
91   bool success = true;
92   int *ilist, *numneigh, **firstneigh;
93   if (gpu_mode != GPU_FORCE) {
94     double sublo[3],subhi[3];
95     if (domain->triclinic == 0) {
96       sublo[0] = domain->sublo[0];
97       sublo[1] = domain->sublo[1];
98       sublo[2] = domain->sublo[2];
99       subhi[0] = domain->subhi[0];
100       subhi[1] = domain->subhi[1];
101       subhi[2] = domain->subhi[2];
102     } else {
103       domain->bbox(domain->sublo_lamda,domain->subhi_lamda,sublo,subhi);
104     }
105     inum = atom->nlocal;
106     firstneigh = ljgrm_gpu_compute_n(neighbor->ago, inum, nall,
107                                      atom->x, atom->type, sublo,
108                                      subhi, atom->tag, atom->nspecial,
109                                      atom->special, eflag, vflag, eflag_atom,
110                                      vflag_atom, host_start, &ilist,
111                                      &numneigh, cpu_time, success);
112   } else {
113     inum = list->inum;
114     ilist = list->ilist;
115     numneigh = list->numneigh;
116     firstneigh = list->firstneigh;
117     ljgrm_gpu_compute(neighbor->ago, inum, nall, atom->x, atom->type,
118                       ilist, numneigh, firstneigh, eflag, vflag, eflag_atom,
119                       vflag_atom, host_start, cpu_time, success);
120   }
121   if (!success)
122     error->one(FLERR,"Insufficient memory on accelerator");
123 
124   if (host_start<inum) {
125     cpu_time = MPI_Wtime();
126     cpu_compute(host_start, inum, eflag, vflag, ilist, numneigh, firstneigh);
127     cpu_time = MPI_Wtime() - cpu_time;
128   }
129 }
130 
131 /* ----------------------------------------------------------------------
132    init specific to this pair style
133 ------------------------------------------------------------------------- */
134 
init_style()135 void PairLJGromacsGPU::init_style()
136 {
137   if (force->newton_pair)
138     error->all(FLERR,"Pair style lj/gromacs/gpu requires newton pair off");
139 
140   // Repeat cutsq calculation because done after call to init_style
141   double maxcut = -1.0;
142   double mcut;
143   for (int i = 1; i <= atom->ntypes; i++) {
144     for (int j = i; j <= atom->ntypes; j++) {
145       if (setflag[i][j] != 0 || (setflag[i][i] != 0 && setflag[j][j] != 0)) {
146         mcut = init_one(i,j);
147         mcut *= mcut;
148         if (mcut > maxcut)
149           maxcut = mcut;
150         cutsq[i][j] = cutsq[j][i] = mcut;
151       } else
152         cutsq[i][j] = cutsq[j][i] = 0.0;
153     }
154   }
155   double cell_size = sqrt(maxcut) + neighbor->skin;
156 
157   int maxspecial=0;
158   if (atom->molecular != Atom::ATOMIC)
159     maxspecial=atom->maxspecial;
160 
161   int mnf = 5e-2 * neighbor->oneatom;
162   int success = ljgrm_gpu_init(atom->ntypes+1, cutsq, lj1, lj2, lj3, lj4,
163                                                  force->special_lj, atom->nlocal,
164                                atom->nlocal+atom->nghost, mnf, maxspecial,
165                                cell_size, gpu_mode, screen, ljsw1, ljsw2,
166                                ljsw3, ljsw4, ljsw5, cut_inner, cut_inner_sq);
167   GPU_EXTRA::check_flag(success,error,world);
168 
169   if (gpu_mode == GPU_FORCE) {
170     int irequest = neighbor->request(this,instance_me);
171     neighbor->requests[irequest]->half = 0;
172     neighbor->requests[irequest]->full = 1;
173   }
174 }
175 
176 /* ---------------------------------------------------------------------- */
177 
memory_usage()178 double PairLJGromacsGPU::memory_usage()
179 {
180   double bytes = Pair::memory_usage();
181   return bytes + ljgrm_gpu_bytes();
182 }
183 
184 /* ---------------------------------------------------------------------- */
185 
cpu_compute(int start,int inum,int eflag,int,int * ilist,int * numneigh,int ** firstneigh)186 void PairLJGromacsGPU::cpu_compute(int start, int inum, int eflag,
187                                    int /* vflag */, int *ilist,
188                                    int *numneigh, int **firstneigh)
189 {
190   int i,j,ii,jj,jnum,itype,jtype;
191   double xtmp,ytmp,ztmp,delx,dely,delz,evdwl,fpair;
192   double rsq,r2inv,r6inv,forcelj,factor_lj;
193   double r,t,fswitch,eswitch;
194   int *jlist;
195 
196   double **x = atom->x;
197   double **f = atom->f;
198   int *type = atom->type;
199   double *special_lj = force->special_lj;
200 
201   // loop over neighbors of my atoms
202 
203   for (ii = start; ii < inum; ii++) {
204     i = ilist[ii];
205     xtmp = x[i][0];
206     ytmp = x[i][1];
207     ztmp = x[i][2];
208     itype = type[i];
209     jlist = firstneigh[i];
210     jnum = numneigh[i];
211 
212     for (jj = 0; jj < jnum; jj++) {
213       j = jlist[jj];
214       factor_lj = special_lj[sbmask(j)];
215       j &= NEIGHMASK;
216 
217       delx = xtmp - x[j][0];
218       dely = ytmp - x[j][1];
219       delz = ztmp - x[j][2];
220       rsq = delx*delx + dely*dely + delz*delz;
221       jtype = type[j];
222 
223       if (rsq < cutsq[itype][jtype]) {
224         r2inv = 1.0/rsq;
225               r6inv = r2inv*r2inv*r2inv;
226         forcelj = r6inv * (lj1[itype][jtype]*r6inv - lj2[itype][jtype]);
227               if (rsq > cut_inner_sq[itype][jtype]) {
228           r = sqrt(rsq);
229           t = r - cut_inner[itype][jtype];
230                  fswitch = r*t*t*(ljsw1[itype][jtype] + ljsw2[itype][jtype]*t);
231                 forcelj += fswitch;
232         }
233               fpair = factor_lj*forcelj * r2inv;
234 
235               f[i][0] += delx*fpair;
236               f[i][1] += dely*fpair;
237               f[i][2] += delz*fpair;
238 
239                if (eflag) {
240                 evdwl = r6inv * (lj3[itype][jtype]*r6inv - lj4[itype][jtype]);
241                 evdwl += ljsw5[itype][jtype];
242           if (rsq > cut_inner_sq[itype][jtype]) {
243             eswitch = t*t*t*(ljsw3[itype][jtype] + ljsw4[itype][jtype]*t);
244             evdwl += eswitch;
245           }
246                 evdwl *= factor_lj;
247               }
248 
249               if (evflag) ev_tally_full(i,evdwl,0.0,fpair,delx,dely,delz);
250       }
251     }
252   }
253 }
254