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