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