diff --git a/lib/gpu/lal_born_coul_wolf_cs.cpp b/lib/gpu/lal_born_coul_wolf_cs.cpp new file mode 100644 index 0000000000..bdb1c31e55 --- /dev/null +++ b/lib/gpu/lal_born_coul_wolf_cs.cpp @@ -0,0 +1,97 @@ +/*************************************************************************** + born_coul_wolf_cs.cpp + ------------------- + Trung Dac Nguyen (Northwestern) + + Class for acceleration of the born/coul/wolf/cs pair style. + + __________________________________________________________________________ + This file is part of the LAMMPS Accelerator Library (LAMMPS_AL) + __________________________________________________________________________ + + begin : + email : ndactrung@gmail.com + ***************************************************************************/ + +#ifdef USE_OPENCL +#include "born_coul_wolf_cs_cl.h" +#elif defined(USE_CUDART) +const char *born_coul_wolf_cs=0; +#else +#include "born_coul_wolf_cs_cubin.h" +#endif + +#include "lal_born_coul_wolf_cs.h" +#include +using namespace LAMMPS_AL; +#define BornCoulWolfCST BornCoulWolfCS + +extern Device device; + +template +int BornCoulWolfCST::init(const int ntypes, double **host_cutsq, double **host_rhoinv, + double **host_born1, double **host_born2, double **host_born3, + double **host_a, double **host_c, double **host_d, + double **host_sigma, double **host_offset, + double *host_special_lj, const int nlocal, + const int nall, const int max_nbors, + const int maxspecial, const double cell_size, + const double gpu_split, FILE *_screen, + double **host_cut_ljsq, const double host_cut_coulsq, + double *host_special_coul, const double qqrd2e, + const double alf, const double e_shift, const double f_shift) { + int success; + success=this->init_atomic(nlocal,nall,max_nbors,maxspecial,cell_size,gpu_split, + _screen,born_coul_wolf_cs,"k_born_coul_wolf_cs"); + if (success!=0) + return success; + + // If atom type constants fit in shared memory use fast kernel + int lj_types=ntypes; + this->shared_types=false; + int max_shared_types=this->device->max_shared_types(); + if (lj_types<=max_shared_types && this->_block_size>=max_shared_types) { + lj_types=max_shared_types; + this->shared_types=true; + } + this->_lj_types=lj_types; + + // Allocate a host write buffer for data initialization + UCL_H_Vec host_write(lj_types*lj_types*32,*(this->ucl_device), + UCL_WRITE_ONLY); + + for (int i=0; icoeff1.alloc(lj_types*lj_types,*(this->ucl_device),UCL_READ_ONLY); + this->atom->type_pack4(ntypes,lj_types,this->coeff1,host_write,host_rhoinv, + host_born1,host_born2,host_born3); + + this->coeff2.alloc(lj_types*lj_types,*(this->ucl_device),UCL_READ_ONLY); + this->atom->type_pack4(ntypes,lj_types,this->coeff2,host_write,host_a,host_c, + host_d,host_offset); + + this->cutsq_sigma.alloc(lj_types*lj_types,*(this->ucl_device),UCL_READ_ONLY); + this->atom->type_pack4(ntypes,lj_types,this->cutsq_sigma,host_write,host_cutsq, + host_cut_ljsq,host_sigma); + + this->sp_lj.alloc(8,*(this->ucl_device),UCL_READ_ONLY); + for (int i=0; i<4; i++) { + host_write[i]=host_special_lj[i]; + host_write[i+4]=host_special_coul[i]; + } + ucl_copy(this->sp_lj,host_write,8,false); + + this->_cut_coulsq=host_cut_coulsq; + this->_qqrd2e=qqrd2e; + this->_alf=alf; + this->_e_shift=e_shift; + this->_f_shift=f_shift; + + this->_allocated=true; + this->_max_bytes=this->coeff1.row_bytes()+this->coeff2.row_bytes() + +this->cutsq_sigma.row_bytes()+this->sp_lj.row_bytes(); + return 0; +} + +template class BornCoulWolfCS; diff --git a/lib/gpu/lal_born_coul_wolf_cs.cu b/lib/gpu/lal_born_coul_wolf_cs.cu new file mode 100644 index 0000000000..847387bfe8 --- /dev/null +++ b/lib/gpu/lal_born_coul_wolf_cs.cu @@ -0,0 +1,306 @@ +// ************************************************************************** +// born_coul_wolf_cs.cu +// ------------------- +// Trung Dac Nguyen (Northwestern) +// +// Device code for acceleration of the born/coul/wolf/cs pair style +// +// __________________________________________________________________________ +// This file is part of the LAMMPS Accelerator Library (LAMMPS_AL) +// __________________________________________________________________________ +// +// begin : +// email : ndactrung@gmail.com +// ***************************************************************************/ + +#ifdef NV_KERNEL + +#include "lal_aux_fun1.h" +#ifndef _DOUBLE_DOUBLE +texture pos_tex; +texture q_tex; +#else +texture pos_tex; +texture q_tex; +#endif + +#else +#define pos_tex x_ +#define q_tex q_ +#endif + +#define EPSILON (acctyp)(1.0e-20) +#define MY_PIS (acctyp)1.77245385090551602729 + +__kernel void k_born_coul_wolf_cs(const __global numtyp4 *restrict x_, + const __global numtyp4 *restrict coeff1, + const __global numtyp4 *restrict coeff2, + const int lj_types, + const __global numtyp *restrict sp_lj_in, + const __global int *dev_nbor, + const __global int *dev_packed, + __global acctyp4 *restrict ans, + __global acctyp *restrict engv, + const int eflag, const int vflag, const int inum, + const int nbor_pitch, + const __global numtyp *restrict q_, + const __global numtyp4 *restrict cutsq_sigma, + const numtyp cut_coulsq, const numtyp qqrd2e, + const numtyp alf, const numtyp e_shift, + const numtyp f_shift, const int t_per_atom) { + int tid, ii, offset; + atom_info(t_per_atom,ii,tid,offset); + + __local numtyp sp_lj[8]; + sp_lj[0]=sp_lj_in[0]; + sp_lj[1]=sp_lj_in[1]; + sp_lj[2]=sp_lj_in[2]; + sp_lj[3]=sp_lj_in[3]; + sp_lj[4]=sp_lj_in[4]; + sp_lj[5]=sp_lj_in[5]; + sp_lj[6]=sp_lj_in[6]; + sp_lj[7]=sp_lj_in[7]; + + acctyp energy=(acctyp)0; + acctyp e_coul=(acctyp)0; + acctyp4 f; + f.x=(acctyp)0; f.y=(acctyp)0; f.z=(acctyp)0; + acctyp virial[6]; + for (int i=0; i<6; i++) + virial[i]=(acctyp)0; + + if (ii0) { + acctyp e_self = -((acctyp)0.5*e_shift + alf/MY_PIS) * + qtmp*qtmp*qqrd2e/(acctyp)t_per_atom; + e_coul += (acctyp)2.0*e_self; + } + + for ( ; nbor0) { + if (rsq < cut_coulsq) { + acctyp e=v_sh; + if (factor_coul < (numtyp)1.0) e -= ((numtyp)1.0-factor_coul)*prefactor; + e_coul += e; + } + if (rsq < cutsq_sigma[mtype].y) { + numtyp e=coeff2[mtype].x*rexp - coeff2[mtype].y*r6inv + + coeff2[mtype].z*r2inv*r6inv; + energy+=factor_lj*(e-coeff2[mtype].w); + } + } + if (vflag>0) { + virial[0] += delx*delx*force; + virial[1] += dely*dely*force; + virial[2] += delz*delz*force; + virial[3] += delx*dely*force; + virial[4] += delx*delz*force; + virial[5] += dely*delz*force; + } + } + + } // for nbor + store_answers_q(f,energy,e_coul,virial,ii,inum,tid,t_per_atom,offset,eflag, + vflag,ans,engv); + } // if ii +} + +__kernel void k_born_coul_wolf_cs_fast(const __global numtyp4 *restrict x_, + const __global numtyp4 *restrict coeff1_in, + const __global numtyp4 *restrict coeff2_in, + const __global numtyp *restrict sp_lj_in, + const __global int *dev_nbor, + const __global int *dev_packed, + __global acctyp4 *restrict ans, + __global acctyp *restrict engv, + const int eflag, const int vflag, const int inum, + const int nbor_pitch, + const __global numtyp *restrict q_, + const __global numtyp4 *restrict cutsq_sigma, + const numtyp cut_coulsq, const numtyp qqrd2e, + const numtyp alf, const numtyp e_shift, + const numtyp f_shift, const int t_per_atom) { + int tid, ii, offset; + atom_info(t_per_atom,ii,tid,offset); + + __local numtyp4 coeff1[MAX_SHARED_TYPES*MAX_SHARED_TYPES]; + __local numtyp4 coeff2[MAX_SHARED_TYPES*MAX_SHARED_TYPES]; + __local numtyp sp_lj[8]; + if (tid<8) + sp_lj[tid]=sp_lj_in[tid]; + if (tid0) + coeff2[tid]=coeff2_in[tid]; + } + + acctyp energy=(acctyp)0; + acctyp e_coul=(acctyp)0; + acctyp4 f; + f.x=(acctyp)0; f.y=(acctyp)0; f.z=(acctyp)0; + acctyp virial[6]; + for (int i=0; i<6; i++) + virial[i]=(acctyp)0; + + __syncthreads(); + + if (ii0) { + acctyp e_self = -((acctyp)0.5*e_shift + alf/MY_PIS) * + qtmp*qtmp*qqrd2e/(acctyp)t_per_atom; + e_coul += (acctyp)2.0*e_self; + } + + for ( ; nbor0) { + if (rsq < cut_coulsq) { + acctyp e=v_sh; + if (factor_coul < (numtyp)1.0) e -= ((numtyp)1.0-factor_coul)*prefactor; + e_coul += e; + } + if (rsq < cutsq_sigma[mtype].y) { + numtyp e=coeff2[mtype].x*rexp - coeff2[mtype].y*r6inv + + coeff2[mtype].z*r2inv*r6inv; + energy+=factor_lj*(e-coeff2[mtype].w); + } + } + if (vflag>0) { + virial[0] += delx*delx*force; + virial[1] += dely*dely*force; + virial[2] += delz*delz*force; + virial[3] += delx*dely*force; + virial[4] += delx*delz*force; + virial[5] += dely*delz*force; + } + } + + } // for nbor + store_answers_q(f,energy,e_coul,virial,ii,inum,tid,t_per_atom,offset,eflag, + vflag,ans,engv); + } // if ii +} + diff --git a/lib/gpu/lal_born_coul_wolf_cs.h b/lib/gpu/lal_born_coul_wolf_cs.h new file mode 100644 index 0000000000..1d9de0a457 --- /dev/null +++ b/lib/gpu/lal_born_coul_wolf_cs.h @@ -0,0 +1,54 @@ +/*************************************************************************** + born_coul_wolf_cs.h + ------------------- + Trung Dac Nguyen (Northwestern) + + Class for acceleration of the born/coul/wolf/cs pair style. + + __________________________________________________________________________ + This file is part of the LAMMPS Accelerator Library (LAMMPS_AL) + __________________________________________________________________________ + + begin : + email : ndactrung@gmail.com + ***************************************************************************/ + +#ifndef LAL_BORN_COUL_WOLF_CS_H +#define LAL_BORN_COUL_WOLF_CS_H + +#include "lal_born_coul_wolf.h" + +namespace LAMMPS_AL { + +template +class BornCoulWolfCS : public BornCoulWolf { + public: + BornCoulWolfCS() {} + ~BornCoulWolfCS() {} + + /// Clear any previous data and set up for a new LAMMPS run + /** \param max_nbors initial number of rows in the neighbor matrix + * \param cell_size cutoff + skin + * \param gpu_split fraction of particles handled by device + * + * Returns: + * - 0 if successfull + * - -1 if fix gpu not found + * - -3 if there is an out of memory error + * - -4 if the GPU library was not compiled for GPU + * - -5 Double precision is not supported on card **/ + int init(const int ntypes, double **host_cutsq, double **host_rhoinv, + double **host_born1, double **host_born2, double **host_born3, + double **host_a, double **host_c, double **host_d, + double **host_sigma, double **host_offset, double *host_special_lj, + const int nlocal, const int nall, const int max_nbors, + const int maxspecial, const double cell_size, + const double gpu_split, FILE *screen, double **host_cut_ljsq, + const double host_cut_coulsq, double *host_special_coul, + const double qqrd2e, const double alf, const double e_shift, + const double f_shift); +}; + +} + +#endif diff --git a/lib/gpu/lal_born_coul_wolf_cs_ext.cpp b/lib/gpu/lal_born_coul_wolf_cs_ext.cpp new file mode 100644 index 0000000000..e2211644af --- /dev/null +++ b/lib/gpu/lal_born_coul_wolf_cs_ext.cpp @@ -0,0 +1,134 @@ +/*************************************************************************** + born_coul_wolf_cs_ext.cpp + ------------------- + Trung Dac Nguyen (Northwestern) + + Functions for LAMMPS access to born/coul/wolf/cs acceleration routines. + + __________________________________________________________________________ + This file is part of the LAMMPS Accelerator Library (LAMMPS_AL) + __________________________________________________________________________ + + begin : + email : ndactrung@gmail.com + ***************************************************************************/ + +#include +#include +#include + +#include "lal_born_coul_wolf_cs.h" + +using namespace std; +using namespace LAMMPS_AL; + +static BornCoulWolfCS BornCWCST; + +// --------------------------------------------------------------------------- +// Allocate memory on host and device and copy constants to device +// --------------------------------------------------------------------------- +int borncwcs_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, + double **host_cut_ljsq, double host_cut_coulsq, + double *host_special_coul, const double qqrd2e, + const double alf, const double e_shift, const double f_shift) { + BornCWCST.clear(); + gpu_mode=BornCWCST.device->gpu_mode(); + double gpu_split=BornCWCST.device->particle_split(); + int first_gpu=BornCWCST.device->first_device(); + int last_gpu=BornCWCST.device->last_device(); + int world_me=BornCWCST.device->world_me(); + int gpu_rank=BornCWCST.device->gpu_rank(); + int procs_per_gpu=BornCWCST.device->procs_per_gpu(); + + BornCWCST.device->init_message(screen,"born/coul/wolf/cs",first_gpu,last_gpu); + + bool message=false; + if (BornCWCST.device->replica_me()==0 && screen) + message=true; + + if (message) { + fprintf(screen,"Initializing Device and compiling on process 0..."); + fflush(screen); + } + + int init_ok=0; + if (world_me==0) + init_ok=BornCWCST.init(ntypes, cutsq, host_rhoinv, host_born1, host_born2, + host_born3, host_a, host_c, host_d, sigma, + offset, special_lj, inum, nall, 300, + maxspecial, cell_size, gpu_split, screen, host_cut_ljsq, + host_cut_coulsq, host_special_coul, qqrd2e, + alf, e_shift, f_shift); + + BornCWCST.device->world_barrier(); + if (message) + fprintf(screen,"Done.\n"); + + for (int i=0; igpu_barrier(); + if (message) + fprintf(screen,"Done.\n"); + } + if (message) + fprintf(screen,"\n"); + + if (init_ok==0) + BornCWCST.estimate_gpu_overhead(); + return init_ok; +} + +void borncwcs_gpu_clear() { + BornCWCST.clear(); +} + +int** borncwcs_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) { + return BornCWCST.compute(ago, inum_full, nall, host_x, host_type, sublo, + subhi, tag, nspecial, special, eflag, vflag, eatom, + vatom, host_start, ilist, jnum, cpu_time, success, + host_q, boxlo, prd); +} + +void borncwcs_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) { + BornCWCST.compute(ago,inum_full,nall,host_x,host_type,ilist,numj, + firstneigh,eflag,vflag,eatom,vatom,host_start,cpu_time,success, + host_q,nlocal,boxlo,prd); +} + +double borncwcs_gpu_bytes() { + return BornCWCST.host_memory_usage(); +} + +