1 /***************************************************************************
2                             lj_coul_long_ext.cpp
3                              -------------------
4                             W. Michael Brown (ORNL)
5 
6   Functions for LAMMPS access to lj/cut/coul/long acceleration routines.
7 
8  __________________________________________________________________________
9     This file is part of the LAMMPS Accelerator Library (LAMMPS_AL)
10  __________________________________________________________________________
11 
12     begin                :
13     email                : brownw@ornl.gov
14  ***************************************************************************/
15 
16 #include <iostream>
17 #include <cassert>
18 #include <cmath>
19 
20 #include "lal_lj_coul_long.h"
21 
22 using namespace std;
23 using namespace LAMMPS_AL;
24 
25 static LJCoulLong<PRECISION,ACC_PRECISION> LJCLMF;
26 
27 // ---------------------------------------------------------------------------
28 // Allocate memory on host and device and copy constants to device
29 // ---------------------------------------------------------------------------
ljcl_gpu_init(const int ntypes,double ** cutsq,double ** host_lj1,double ** host_lj2,double ** host_lj3,double ** host_lj4,double ** offset,double * special_lj,const int inum,const int nall,const int max_nbors,const int maxspecial,const double cell_size,int & gpu_mode,FILE * screen,double ** host_cut_ljsq,double host_cut_coulsq,double * host_special_coul,const double qqrd2e,const double g_ewald)30 int ljcl_gpu_init(const int ntypes, double **cutsq, double **host_lj1,
31                   double **host_lj2, double **host_lj3, double **host_lj4,
32                   double **offset, double *special_lj, const int inum,
33                   const int nall, const int max_nbors, const int maxspecial,
34                   const double cell_size, int &gpu_mode, FILE *screen,
35                   double **host_cut_ljsq, double host_cut_coulsq,
36                   double *host_special_coul, const double qqrd2e,
37                   const double g_ewald) {
38   LJCLMF.clear();
39   gpu_mode=LJCLMF.device->gpu_mode();
40   double gpu_split=LJCLMF.device->particle_split();
41   int first_gpu=LJCLMF.device->first_device();
42   int last_gpu=LJCLMF.device->last_device();
43   int world_me=LJCLMF.device->world_me();
44   int gpu_rank=LJCLMF.device->gpu_rank();
45   int procs_per_gpu=LJCLMF.device->procs_per_gpu();
46 
47   LJCLMF.device->init_message(screen,"lj/cut/coul/long",first_gpu,last_gpu);
48 
49   bool message=false;
50   if (LJCLMF.device->replica_me()==0 && screen)
51     message=true;
52 
53   if (message) {
54     fprintf(screen,"Initializing Device and compiling on process 0...");
55     fflush(screen);
56   }
57 
58   int init_ok=0;
59   if (world_me==0)
60     init_ok=LJCLMF.init(ntypes, cutsq, host_lj1, host_lj2, host_lj3, host_lj4,
61                         offset, special_lj, inum, nall, max_nbors, maxspecial,
62                         cell_size, gpu_split, screen, host_cut_ljsq,
63                         host_cut_coulsq, host_special_coul, qqrd2e, g_ewald);
64 
65   LJCLMF.device->world_barrier();
66   if (message)
67     fprintf(screen,"Done.\n");
68 
69   for (int i=0; i<procs_per_gpu; i++) {
70     if (message) {
71       if (last_gpu-first_gpu==0)
72         fprintf(screen,"Initializing Device %d on core %d...",first_gpu,i);
73       else
74         fprintf(screen,"Initializing Devices %d-%d on core %d...",first_gpu,
75                 last_gpu,i);
76       fflush(screen);
77     }
78     if (gpu_rank==i && world_me!=0)
79       init_ok=LJCLMF.init(ntypes, cutsq, host_lj1, host_lj2, host_lj3, host_lj4,
80                           offset, special_lj, inum, nall, max_nbors, maxspecial,
81                           cell_size, gpu_split, screen, host_cut_ljsq,
82                           host_cut_coulsq, host_special_coul, qqrd2e, g_ewald);
83 
84     LJCLMF.device->gpu_barrier();
85     if (message)
86       fprintf(screen,"Done.\n");
87   }
88   if (message)
89     fprintf(screen,"\n");
90 
91   if (init_ok==0)
92     LJCLMF.estimate_gpu_overhead();
93   return init_ok;
94 }
95 
96 // ---------------------------------------------------------------------------
97 // Copy updated coeffs from host to device
98 // ---------------------------------------------------------------------------
ljcl_gpu_reinit(const int ntypes,double ** cutsq,double ** host_lj1,double ** host_lj2,double ** host_lj3,double ** host_lj4,double ** offset,double ** host_cut_ljsq)99 void ljcl_gpu_reinit(const int ntypes, double **cutsq, double **host_lj1,
100                     double **host_lj2, double **host_lj3, double **host_lj4,
101                     double **offset, double **host_cut_ljsq) {
102   int world_me=LJCLMF.device->world_me();
103   int gpu_rank=LJCLMF.device->gpu_rank();
104   int procs_per_gpu=LJCLMF.device->procs_per_gpu();
105 
106   if (world_me==0)
107     LJCLMF.reinit(ntypes, cutsq, host_lj1, host_lj2, host_lj3, host_lj4,
108                   offset, host_cut_ljsq);
109   LJCLMF.device->world_barrier();
110 
111   for (int i=0; i<procs_per_gpu; i++) {
112     if (gpu_rank==i && world_me!=0)
113       LJCLMF.reinit(ntypes, cutsq, host_lj1, host_lj2, host_lj3, host_lj4,
114                     offset, host_cut_ljsq);
115     LJCLMF.device->gpu_barrier();
116   }
117 }
118 
ljcl_gpu_clear()119 void ljcl_gpu_clear() {
120   LJCLMF.clear();
121 }
122 
ljcl_gpu_compute_n(const int ago,const int inum_full,const int nall,double ** host_x,int * host_type,double * sublo,double * subhi,tagint * tag,int ** nspecial,tagint ** special,const bool eflag,const bool vflag,const bool eatom,const bool vatom,int & host_start,int ** ilist,int ** jnum,const double cpu_time,bool & success,double * host_q,double * boxlo,double * prd)123 int** ljcl_gpu_compute_n(const int ago, const int inum_full,
124                          const int nall, double **host_x, int *host_type,
125                          double *sublo, double *subhi, tagint *tag, int **nspecial,
126                          tagint **special, const bool eflag, const bool vflag,
127                          const bool eatom, const bool vatom, int &host_start,
128                          int **ilist, int **jnum,  const double cpu_time,
129                          bool &success, double *host_q, double *boxlo,
130                          double *prd) {
131   return LJCLMF.compute(ago, inum_full, nall, host_x, host_type, sublo,
132                         subhi, tag, nspecial, special, eflag, vflag, eatom,
133                         vatom, host_start, ilist, jnum, cpu_time, success,
134                         host_q, boxlo, prd);
135 }
136 
ljcl_gpu_compute(const int ago,const int inum_full,const int nall,double ** host_x,int * host_type,int * ilist,int * numj,int ** firstneigh,const bool eflag,const bool vflag,const bool eatom,const bool vatom,int & host_start,const double cpu_time,bool & success,double * host_q,const int nlocal,double * boxlo,double * prd)137 void ljcl_gpu_compute(const int ago, const int inum_full, const int nall,
138                       double **host_x, int *host_type, int *ilist, int *numj,
139                       int **firstneigh, const bool eflag, const bool vflag,
140                       const bool eatom, const bool vatom, int &host_start,
141                       const double cpu_time, bool &success, double *host_q,
142                       const int nlocal, double *boxlo, double *prd) {
143   LJCLMF.compute(ago,inum_full,nall,host_x,host_type,ilist,numj,
144                 firstneigh,eflag,vflag,eatom,vatom,host_start,cpu_time,success,
145                 host_q,nlocal,boxlo,prd);
146 }
147 
ljcl_gpu_bytes()148 double ljcl_gpu_bytes() {
149   return LJCLMF.host_memory_usage();
150 }
151 
152 
153