1 /***************************************************************************
2                                   cg_cmm.h
3                              -------------------
4                             W. Michael Brown (ORNL)
5 
6   Functions for LAMMPS access to lj/sdk pair 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 <math.h>
19 
20 #include "lal_cg_cmm.h"
21 
22 using namespace std;
23 using namespace LAMMPS_AL;
24 
25 static CGCMM<PRECISION,ACC_PRECISION> CMMMF;
26 
27 // ---------------------------------------------------------------------------
28 // Allocate memory on host and device and copy constants to device
29 // ---------------------------------------------------------------------------
cmm_gpu_init(const int ntypes,double ** cutsq,int ** cg_types,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)30 int cmm_gpu_init(const int ntypes, double **cutsq, int **cg_types,
31                  double **host_lj1, double **host_lj2, double **host_lj3,
32                  double **host_lj4, double **offset, double *special_lj,
33                  const int inum, const int nall, const int max_nbors,
34                  const int maxspecial, const double cell_size, int &gpu_mode,
35                  FILE *screen) {
36   CMMMF.clear();
37   gpu_mode=CMMMF.device->gpu_mode();
38   double gpu_split=CMMMF.device->particle_split();
39   int first_gpu=CMMMF.device->first_device();
40   int last_gpu=CMMMF.device->last_device();
41   int world_me=CMMMF.device->world_me();
42   int gpu_rank=CMMMF.device->gpu_rank();
43   int procs_per_gpu=CMMMF.device->procs_per_gpu();
44 
45   CMMMF.device->init_message(screen,"lj/sdk",first_gpu,last_gpu);
46 
47   bool message=false;
48   if (CMMMF.device->replica_me()==0 && screen)
49     message=true;
50 
51   if (message) {
52     fprintf(screen,"Initializing GPU and compiling on process 0...");
53     fflush(screen);
54   }
55 
56   int init_ok=0;
57   if (world_me==0)
58     init_ok=CMMMF.init(ntypes,cutsq,cg_types,host_lj1,host_lj2,host_lj3,
59                        host_lj4, offset, special_lj, inum, nall, 300,
60                        maxspecial, cell_size, gpu_split, screen);
61 
62   CMMMF.device->world_barrier();
63   if (message)
64     fprintf(screen,"Done.\n");
65 
66   for (int i=0; i<procs_per_gpu; i++) {
67     if (message) {
68       if (last_gpu-first_gpu==0)
69         fprintf(screen,"Initializing GPU %d on core %d...",first_gpu,i);
70       else
71         fprintf(screen,"Initializing GPUs %d-%d on core %d...",first_gpu,
72                 last_gpu,i);
73       fflush(screen);
74     }
75     if (gpu_rank==i && world_me!=0)
76       init_ok=CMMMF.init(ntypes,cutsq,cg_types,host_lj1,host_lj2,host_lj3,
77                          host_lj4, offset, special_lj, inum, nall, 300,
78                          maxspecial, cell_size, gpu_split, screen);
79 
80     CMMMF.device->gpu_barrier();
81     if (message)
82       fprintf(screen,"Done.\n");
83   }
84   if (message)
85     fprintf(screen,"\n");
86 
87   if (init_ok==0)
88     CMMMF.estimate_gpu_overhead();
89   return init_ok;
90 }
91 
cmm_gpu_clear()92 void cmm_gpu_clear() {
93   CMMMF.clear();
94 }
95 
cmm_gpu_compute_n(const int ago,const int inum_full,const int nall,double ** host_x,int * host_type,double * sublo,double * subhi,int * tag,int ** nspecial,int ** 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)96 int** cmm_gpu_compute_n(const int ago, const int inum_full,
97                         const int nall, double **host_x, int *host_type,
98                         double *sublo, double *subhi, int *tag, int **nspecial,
99                         int **special, const bool eflag, const bool vflag,
100                         const bool eatom, const bool vatom, int &host_start,
101                         int **ilist, int **jnum, const double cpu_time,
102                         bool &success) {
103   return CMMMF.compute(ago, inum_full, nall, host_x, host_type, sublo,
104                        subhi, tag, nspecial, special, eflag, vflag, eatom,
105                        vatom, host_start, ilist, jnum, cpu_time, success);
106 }
107 
cmm_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)108 void cmm_gpu_compute(const int ago, const int inum_full, const int nall,
109                      double **host_x, int *host_type, int *ilist, int *numj,
110                      int **firstneigh, const bool eflag, const bool vflag,
111                      const bool eatom, const bool vatom, int &host_start,
112                      const double cpu_time, bool &success) {
113   CMMMF.compute(ago,inum_full,nall,host_x,host_type,ilist,numj,
114                 firstneigh,eflag,vflag,eatom,vatom,host_start,cpu_time,success);
115 }
116 
cmm_gpu_bytes()117 double cmm_gpu_bytes() {
118   return CMMMF.host_memory_usage();
119 }
120 
121 
122