// clang-format off /* ---------------------------------------------------------------------- LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator https://www.lammps.org/, Sandia National Laboratories LAMMPS development team: developers@lammps.org Copyright (2003) Sandia Corporation. Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains certain rights in this software. This software is distributed under the GNU General Public License. See the README file in the top-level LAMMPS directory. ------------------------------------------------------------------------- */ #include "atom_vec_charge_kokkos.h" #include "atom_kokkos.h" #include "atom_masks.h" #include "domain.h" #include "error.h" #include "fix.h" #include "memory_kokkos.h" #include "modify.h" using namespace LAMMPS_NS; /* ---------------------------------------------------------------------- */ AtomVecChargeKokkos::AtomVecChargeKokkos(LAMMPS *lmp) : AtomVec(lmp), AtomVecKokkos(lmp), AtomVecCharge(lmp), q(nullptr) { unpack_exchange_indices_flag = 1; } /* ---------------------------------------------------------------------- grow atom arrays n = 0 grows arrays by DELTA n > 0 allocates arrays to size n ------------------------------------------------------------------------- */ void AtomVecChargeKokkos::grow(int n) { auto DELTA = LMP_KOKKOS_AV_DELTA; int step = MAX(DELTA,nmax*0.01); if (n == 0) nmax += step; else nmax = n; atomKK->nmax = nmax; if (nmax < 0 || nmax > MAXSMALLINT) error->one(FLERR,"Per-processor system is too big"); atomKK->sync(Device,ALL_MASK); atomKK->modified(Device,ALL_MASK); memoryKK->grow_kokkos(atomKK->k_tag,atomKK->tag,nmax,"atom:tag"); memoryKK->grow_kokkos(atomKK->k_type,atomKK->type,nmax,"atom:type"); memoryKK->grow_kokkos(atomKK->k_mask,atomKK->mask,nmax,"atom:mask"); memoryKK->grow_kokkos(atomKK->k_image,atomKK->image,nmax,"atom:image"); memoryKK->grow_kokkos(atomKK->k_x,atomKK->x,nmax,"atom:x"); memoryKK->grow_kokkos(atomKK->k_v,atomKK->v,nmax,"atom:v"); memoryKK->grow_kokkos(atomKK->k_f,atomKK->f,nmax,"atom:f"); memoryKK->grow_kokkos(atomKK->k_q,atomKK->q,nmax,"atom:q"); grow_pointers(); atomKK->sync(Host,ALL_MASK); if (atom->nextra_grow) for (int iextra = 0; iextra < atom->nextra_grow; iextra++) modify->fix[atom->extra_grow[iextra]]->grow_arrays(nmax); } /* ---------------------------------------------------------------------- reset local array ptrs ------------------------------------------------------------------------- */ void AtomVecChargeKokkos::grow_pointers() { tag = atomKK->tag; d_tag = atomKK->k_tag.d_view; h_tag = atomKK->k_tag.h_view; type = atomKK->type; d_type = atomKK->k_type.d_view; h_type = atomKK->k_type.h_view; mask = atomKK->mask; d_mask = atomKK->k_mask.d_view; h_mask = atomKK->k_mask.h_view; image = atomKK->image; d_image = atomKK->k_image.d_view; h_image = atomKK->k_image.h_view; x = atomKK->x; d_x = atomKK->k_x.d_view; h_x = atomKK->k_x.h_view; v = atomKK->v; d_v = atomKK->k_v.d_view; h_v = atomKK->k_v.h_view; f = atomKK->f; d_f = atomKK->k_f.d_view; h_f = atomKK->k_f.h_view; q = atomKK->q; d_q = atomKK->k_q.d_view; h_q = atomKK->k_q.h_view; } /* ---------------------------------------------------------------------- sort atom arrays on device ------------------------------------------------------------------------- */ void AtomVecChargeKokkos::sort_kokkos(Kokkos::BinSort &Sorter) { atomKK->sync(Device, ALL_MASK & ~F_MASK); Sorter.sort(LMPDeviceType(), d_tag); Sorter.sort(LMPDeviceType(), d_type); Sorter.sort(LMPDeviceType(), d_mask); Sorter.sort(LMPDeviceType(), d_image); Sorter.sort(LMPDeviceType(), d_x); Sorter.sort(LMPDeviceType(), d_v); Sorter.sort(LMPDeviceType(), d_q); atomKK->modified(Device, ALL_MASK & ~F_MASK); } /* ---------------------------------------------------------------------- */ template struct AtomVecChargeKokkos_PackComm { typedef DeviceType device_type; typedef ArrayTypes AT; typename AT::t_x_array_randomread _x; typename AT::t_xfloat_2d_um _buf; typename AT::t_int_1d_const _list; X_FLOAT _xprd,_yprd,_zprd,_xy,_xz,_yz; X_FLOAT _pbc[6]; AtomVecChargeKokkos_PackComm( const typename DAT::tdual_x_array &x, const typename DAT::tdual_xfloat_2d &buf, const typename DAT::tdual_int_1d &list, const X_FLOAT &xprd, const X_FLOAT &yprd, const X_FLOAT &zprd, const X_FLOAT &xy, const X_FLOAT &xz, const X_FLOAT &yz, const int* const pbc): _x(x.view()),_list(list.view()), _xprd(xprd),_yprd(yprd),_zprd(zprd), _xy(xy),_xz(xz),_yz(yz) { const size_t maxsend = (buf.view().extent(0)*buf.view().extent(1))/3; const size_t elements = 3; buffer_view(_buf,buf,maxsend,elements); _pbc[0] = pbc[0]; _pbc[1] = pbc[1]; _pbc[2] = pbc[2]; _pbc[3] = pbc[3]; _pbc[4] = pbc[4]; _pbc[5] = pbc[5]; }; KOKKOS_INLINE_FUNCTION void operator() (const int& i) const { const int j = _list(i); if (PBC_FLAG == 0) { _buf(i,0) = _x(j,0); _buf(i,1) = _x(j,1); _buf(i,2) = _x(j,2); } else { if (TRICLINIC == 0) { _buf(i,0) = _x(j,0) + _pbc[0]*_xprd; _buf(i,1) = _x(j,1) + _pbc[1]*_yprd; _buf(i,2) = _x(j,2) + _pbc[2]*_zprd; } else { _buf(i,0) = _x(j,0) + _pbc[0]*_xprd + _pbc[5]*_xy + _pbc[4]*_xz; _buf(i,1) = _x(j,1) + _pbc[1]*_yprd + _pbc[3]*_yz; _buf(i,2) = _x(j,2) + _pbc[2]*_zprd; } } } }; /* ---------------------------------------------------------------------- */ template struct AtomVecChargeKokkos_PackBorder { typedef DeviceType device_type; typedef ArrayTypes AT; typename AT::t_xfloat_2d _buf; const typename AT::t_int_1d_const _list; const typename AT::t_x_array_randomread _x; const typename AT::t_tagint_1d _tag; const typename AT::t_int_1d _type; const typename AT::t_int_1d _mask; const typename AT::t_float_1d _q; X_FLOAT _dx,_dy,_dz; AtomVecChargeKokkos_PackBorder( const typename AT::t_xfloat_2d &buf, const typename AT::t_int_1d_const &list, const typename AT::t_x_array &x, const typename AT::t_tagint_1d &tag, const typename AT::t_int_1d &type, const typename AT::t_int_1d &mask, const typename AT::t_float_1d &q, const X_FLOAT &dx, const X_FLOAT &dy, const X_FLOAT &dz): _buf(buf),_list(list), _x(x),_tag(tag),_type(type),_mask(mask),_q(q), _dx(dx),_dy(dy),_dz(dz) {} KOKKOS_INLINE_FUNCTION void operator() (const int& i) const { const int j = _list(i); if (PBC_FLAG == 0) { _buf(i,0) = _x(j,0); _buf(i,1) = _x(j,1); _buf(i,2) = _x(j,2); _buf(i,3) = d_ubuf(_tag(j)).d; _buf(i,4) = d_ubuf(_type(j)).d; _buf(i,5) = d_ubuf(_mask(j)).d; _buf(i,6) = _q(j); } else { _buf(i,0) = _x(j,0) + _dx; _buf(i,1) = _x(j,1) + _dy; _buf(i,2) = _x(j,2) + _dz; _buf(i,3) = d_ubuf(_tag(j)).d; _buf(i,4) = d_ubuf(_type(j)).d; _buf(i,5) = d_ubuf(_mask(j)).d; _buf(i,6) = _q(j); } } }; /* ---------------------------------------------------------------------- */ int AtomVecChargeKokkos::pack_border_kokkos(int n, DAT::tdual_int_1d k_sendlist, DAT::tdual_xfloat_2d buf, int pbc_flag, int *pbc, ExecutionSpace space) { X_FLOAT dx,dy,dz; if (pbc_flag != 0) { if (domain->triclinic == 0) { dx = pbc[0]*domain->xprd; dy = pbc[1]*domain->yprd; dz = pbc[2]*domain->zprd; } else { dx = pbc[0]; dy = pbc[1]; dz = pbc[2]; } if (space==Host) { AtomVecChargeKokkos_PackBorder f( buf.view(), k_sendlist.view(), h_x,h_tag,h_type,h_mask,h_q,dx,dy,dz); Kokkos::parallel_for(n,f); } else { AtomVecChargeKokkos_PackBorder f( buf.view(), k_sendlist.view(), d_x,d_tag,d_type,d_mask,d_q,dx,dy,dz); Kokkos::parallel_for(n,f); } } else { dx = dy = dz = 0; if (space==Host) { AtomVecChargeKokkos_PackBorder f( buf.view(), k_sendlist.view(), h_x,h_tag,h_type,h_mask,h_q,dx,dy,dz); Kokkos::parallel_for(n,f); } else { AtomVecChargeKokkos_PackBorder f( buf.view(), k_sendlist.view(), d_x,d_tag,d_type,d_mask,d_q,dx,dy,dz); Kokkos::parallel_for(n,f); } } return n*size_border; } /* ---------------------------------------------------------------------- */ template struct AtomVecChargeKokkos_UnpackBorder { typedef DeviceType device_type; typedef ArrayTypes AT; const typename AT::t_xfloat_2d_const _buf; typename AT::t_x_array _x; typename AT::t_tagint_1d _tag; typename AT::t_int_1d _type; typename AT::t_int_1d _mask; typename AT::t_float_1d _q; int _first; AtomVecChargeKokkos_UnpackBorder( const typename AT::t_xfloat_2d_const &buf, typename AT::t_x_array &x, typename AT::t_tagint_1d &tag, typename AT::t_int_1d &type, typename AT::t_int_1d &mask, typename AT::t_float_1d &q, const int& first): _buf(buf),_x(x),_tag(tag),_type(type),_mask(mask),_q(q),_first(first) { }; KOKKOS_INLINE_FUNCTION void operator() (const int& i) const { _x(i+_first,0) = _buf(i,0); _x(i+_first,1) = _buf(i,1); _x(i+_first,2) = _buf(i,2); _tag(i+_first) = (tagint) d_ubuf(_buf(i,3)).i; _type(i+_first) = (int) d_ubuf(_buf(i,4)).i; _mask(i+_first) = (int) d_ubuf(_buf(i,5)).i; _q(i+_first) = _buf(i,6); } }; /* ---------------------------------------------------------------------- */ void AtomVecChargeKokkos::unpack_border_kokkos(const int &n, const int &first, const DAT::tdual_xfloat_2d &buf,ExecutionSpace space) { if (first+n >= nmax) { grow(first+n+100); } if (space==Host) { struct AtomVecChargeKokkos_UnpackBorder f(buf.view(),h_x,h_tag,h_type,h_mask,h_q,first); Kokkos::parallel_for(n,f); } else { struct AtomVecChargeKokkos_UnpackBorder f(buf.view(),d_x,d_tag,d_type,d_mask,d_q,first); Kokkos::parallel_for(n,f); } atomKK->modified(space,X_MASK|TAG_MASK|TYPE_MASK|MASK_MASK|Q_MASK); } /* ---------------------------------------------------------------------- */ template struct AtomVecChargeKokkos_PackExchangeFunctor { typedef DeviceType device_type; typedef ArrayTypes AT; typename AT::t_x_array_randomread _x; typename AT::t_v_array_randomread _v; typename AT::t_tagint_1d_randomread _tag; typename AT::t_int_1d_randomread _type; typename AT::t_int_1d_randomread _mask; typename AT::t_imageint_1d_randomread _image; typename AT::t_float_1d_randomread _q; typename AT::t_x_array _xw; typename AT::t_v_array _vw; typename AT::t_tagint_1d _tagw; typename AT::t_int_1d _typew; typename AT::t_int_1d _maskw; typename AT::t_imageint_1d _imagew; typename AT::t_float_1d _qw; typename AT::t_xfloat_2d_um _buf; typename AT::t_int_1d_const _sendlist; typename AT::t_int_1d_const _copylist; int _size_exchange; AtomVecChargeKokkos_PackExchangeFunctor( const AtomKokkos* atom, const typename AT::tdual_xfloat_2d buf, typename AT::tdual_int_1d sendlist, typename AT::tdual_int_1d copylist): _x(atom->k_x.view()), _v(atom->k_v.view()), _tag(atom->k_tag.view()), _type(atom->k_type.view()), _mask(atom->k_mask.view()), _image(atom->k_image.view()), _q(atom->k_q.view()), _xw(atom->k_x.view()), _vw(atom->k_v.view()), _tagw(atom->k_tag.view()), _typew(atom->k_type.view()), _maskw(atom->k_mask.view()), _imagew(atom->k_image.view()), _qw(atom->k_q.view()), _sendlist(sendlist.template view()), _copylist(copylist.template view()), _size_exchange(atom->avecKK->size_exchange) { const int maxsendlist = (buf.template view().extent(0)* buf.template view().extent(1))/_size_exchange; buffer_view(_buf,buf,maxsendlist,_size_exchange); } KOKKOS_INLINE_FUNCTION void operator() (const int &mysend) const { const int i = _sendlist(mysend); _buf(mysend,0) = _size_exchange; _buf(mysend,1) = _x(i,0); _buf(mysend,2) = _x(i,1); _buf(mysend,3) = _x(i,2); _buf(mysend,4) = _v(i,0); _buf(mysend,5) = _v(i,1); _buf(mysend,6) = _v(i,2); _buf(mysend,7) = d_ubuf(_tag[i]).d; _buf(mysend,8) = d_ubuf(_type[i]).d; _buf(mysend,9) = d_ubuf(_mask[i]).d; _buf(mysend,10) = d_ubuf(_image[i]).d; _buf(mysend,11) = _q[i]; const int j = _copylist(mysend); if (j>-1) { _xw(i,0) = _x(j,0); _xw(i,1) = _x(j,1); _xw(i,2) = _x(j,2); _vw(i,0) = _v(j,0); _vw(i,1) = _v(j,1); _vw(i,2) = _v(j,2); _tagw(i) = _tag(j); _typew(i) = _type(j); _maskw(i) = _mask(j); _imagew(i) = _image(j); _qw(i) = _q(j); } } }; /* ---------------------------------------------------------------------- */ int AtomVecChargeKokkos::pack_exchange_kokkos(const int &nsend,DAT::tdual_xfloat_2d &k_buf, DAT::tdual_int_1d k_sendlist, DAT::tdual_int_1d k_copylist, ExecutionSpace space) { size_exchange = 12; if (nsend > (int) (k_buf.view().extent(0)*k_buf.view().extent(1))/size_exchange) { int newsize = nsend*size_exchange/k_buf.view().extent(1)+1; k_buf.resize(newsize,k_buf.view().extent(1)); } if (space == Host) { AtomVecChargeKokkos_PackExchangeFunctor f(atomKK,k_buf,k_sendlist,k_copylist); Kokkos::parallel_for(nsend,f); return nsend*size_exchange; } else { AtomVecChargeKokkos_PackExchangeFunctor f(atomKK,k_buf,k_sendlist,k_copylist); Kokkos::parallel_for(nsend,f); return nsend*size_exchange; } } /* ---------------------------------------------------------------------- */ template struct AtomVecChargeKokkos_UnpackExchangeFunctor { typedef DeviceType device_type; typedef ArrayTypes AT; typename AT::t_x_array _x; typename AT::t_v_array _v; typename AT::t_tagint_1d _tag; typename AT::t_int_1d _type; typename AT::t_int_1d _mask; typename AT::t_imageint_1d _image; typename AT::t_float_1d _q; typename AT::t_xfloat_2d_um _buf; typename AT::t_int_1d _nlocal; typename AT::t_int_1d _indices; int _dim; X_FLOAT _lo,_hi; int _size_exchange; AtomVecChargeKokkos_UnpackExchangeFunctor( const AtomKokkos* atom, const typename AT::tdual_xfloat_2d buf, typename AT::tdual_int_1d nlocal, typename AT::tdual_int_1d indices, int dim, X_FLOAT lo, X_FLOAT hi): _x(atom->k_x.view()), _v(atom->k_v.view()), _tag(atom->k_tag.view()), _type(atom->k_type.view()), _mask(atom->k_mask.view()), _image(atom->k_image.view()), _q(atom->k_q.view()), _nlocal(nlocal.template view()), _indices(indices.template view()),_dim(dim), _lo(lo),_hi(hi),_size_exchange(atom->avecKK->size_exchange) { const int maxsendlist = (buf.template view().extent(0)*buf.template view().extent(1))/_size_exchange; buffer_view(_buf,buf,maxsendlist,_size_exchange); } KOKKOS_INLINE_FUNCTION void operator() (const int &myrecv) const { X_FLOAT x = _buf(myrecv,_dim+1); int i = -1; if (x >= _lo && x < _hi) { i = Kokkos::atomic_fetch_add(&_nlocal(0),1); _x(i,0) = _buf(myrecv,1); _x(i,1) = _buf(myrecv,2); _x(i,2) = _buf(myrecv,3); _v(i,0) = _buf(myrecv,4); _v(i,1) = _buf(myrecv,5); _v(i,2) = _buf(myrecv,6); _tag[i] = (tagint) d_ubuf(_buf(myrecv,7)).i; _type[i] = (int) d_ubuf(_buf(myrecv,8)).i; _mask[i] = (int) d_ubuf(_buf(myrecv,9)).i; _image[i] = (imageint) d_ubuf(_buf(myrecv,10)).i; _q[i] = _buf(myrecv,11); } if (OUTPUT_INDICES) _indices(myrecv) = i; } }; /* ---------------------------------------------------------------------- */ int AtomVecChargeKokkos::unpack_exchange_kokkos(DAT::tdual_xfloat_2d &k_buf, int nrecv, int nlocal, int dim, X_FLOAT lo, X_FLOAT hi, ExecutionSpace space, DAT::tdual_int_1d &k_indices) { while (nlocal + nrecv/size_exchange >= nmax) grow(0); if (space == Host) { if (k_indices.h_view.data()) { k_count.h_view(0) = nlocal; AtomVecChargeKokkos_UnpackExchangeFunctor f(atomKK,k_buf,k_count,k_indices,dim,lo,hi); Kokkos::parallel_for(nrecv/size_exchange,f); } else { k_count.h_view(0) = nlocal; AtomVecChargeKokkos_UnpackExchangeFunctor f(atomKK,k_buf,k_count,k_indices,dim,lo,hi); Kokkos::parallel_for(nrecv/size_exchange,f); } } else { if (k_indices.h_view.data()) { k_count.h_view(0) = nlocal; k_count.modify(); k_count.sync(); AtomVecChargeKokkos_UnpackExchangeFunctor f(atomKK,k_buf,k_count,k_indices,dim,lo,hi); Kokkos::parallel_for(nrecv/size_exchange,f); k_count.modify(); k_count.sync(); } else { k_count.h_view(0) = nlocal; k_count.modify(); k_count.sync(); AtomVecChargeKokkos_UnpackExchangeFunctor f(atomKK,k_buf,k_count,k_indices,dim,lo,hi); Kokkos::parallel_for(nrecv/size_exchange,f); k_count.modify(); k_count.sync(); } } return k_count.h_view(0); } /* ---------------------------------------------------------------------- */ void AtomVecChargeKokkos::sync(ExecutionSpace space, unsigned int mask) { if (space == Device) { if (mask & X_MASK) atomKK->k_x.sync(); if (mask & V_MASK) atomKK->k_v.sync(); if (mask & F_MASK) atomKK->k_f.sync(); if (mask & TAG_MASK) atomKK->k_tag.sync(); if (mask & TYPE_MASK) atomKK->k_type.sync(); if (mask & MASK_MASK) atomKK->k_mask.sync(); if (mask & IMAGE_MASK) atomKK->k_image.sync(); if (mask & Q_MASK) atomKK->k_q.sync(); } else { if (mask & X_MASK) atomKK->k_x.sync(); if (mask & V_MASK) atomKK->k_v.sync(); if (mask & F_MASK) atomKK->k_f.sync(); if (mask & TAG_MASK) atomKK->k_tag.sync(); if (mask & TYPE_MASK) atomKK->k_type.sync(); if (mask & MASK_MASK) atomKK->k_mask.sync(); if (mask & IMAGE_MASK) atomKK->k_image.sync(); if (mask & Q_MASK) atomKK->k_q.sync(); } } /* ---------------------------------------------------------------------- */ void AtomVecChargeKokkos::modified(ExecutionSpace space, unsigned int mask) { if (space == Device) { if (mask & X_MASK) atomKK->k_x.modify(); if (mask & V_MASK) atomKK->k_v.modify(); if (mask & F_MASK) atomKK->k_f.modify(); if (mask & TAG_MASK) atomKK->k_tag.modify(); if (mask & TYPE_MASK) atomKK->k_type.modify(); if (mask & MASK_MASK) atomKK->k_mask.modify(); if (mask & IMAGE_MASK) atomKK->k_image.modify(); if (mask & Q_MASK) atomKK->k_q.modify(); } else { if (mask & X_MASK) atomKK->k_x.modify(); if (mask & V_MASK) atomKK->k_v.modify(); if (mask & F_MASK) atomKK->k_f.modify(); if (mask & TAG_MASK) atomKK->k_tag.modify(); if (mask & TYPE_MASK) atomKK->k_type.modify(); if (mask & MASK_MASK) atomKK->k_mask.modify(); if (mask & IMAGE_MASK) atomKK->k_image.modify(); if (mask & Q_MASK) atomKK->k_q.modify(); } } /* ---------------------------------------------------------------------- */ void AtomVecChargeKokkos::sync_overlapping_device(ExecutionSpace space, unsigned int mask) { if (space == Device) { if ((mask & X_MASK) && atomKK->k_x.need_sync()) perform_async_copy(atomKK->k_x,space); if ((mask & V_MASK) && atomKK->k_v.need_sync()) perform_async_copy(atomKK->k_v,space); if ((mask & F_MASK) && atomKK->k_f.need_sync()) perform_async_copy(atomKK->k_f,space); if ((mask & TAG_MASK) && atomKK->k_tag.need_sync()) perform_async_copy(atomKK->k_tag,space); if ((mask & TYPE_MASK) && atomKK->k_type.need_sync()) perform_async_copy(atomKK->k_type,space); if ((mask & MASK_MASK) && atomKK->k_mask.need_sync()) perform_async_copy(atomKK->k_mask,space); if ((mask & IMAGE_MASK) && atomKK->k_image.need_sync()) perform_async_copy(atomKK->k_image,space); if ((mask & Q_MASK) && atomKK->k_q.need_sync()) perform_async_copy(atomKK->k_q,space); } else { if ((mask & X_MASK) && atomKK->k_x.need_sync()) perform_async_copy(atomKK->k_x,space); if ((mask & V_MASK) && atomKK->k_v.need_sync()) perform_async_copy(atomKK->k_v,space); if ((mask & F_MASK) && atomKK->k_f.need_sync()) perform_async_copy(atomKK->k_f,space); if ((mask & TAG_MASK) && atomKK->k_tag.need_sync()) perform_async_copy(atomKK->k_tag,space); if ((mask & TYPE_MASK) && atomKK->k_type.need_sync()) perform_async_copy(atomKK->k_type,space); if ((mask & MASK_MASK) && atomKK->k_mask.need_sync()) perform_async_copy(atomKK->k_mask,space); if ((mask & IMAGE_MASK) && atomKK->k_image.need_sync()) perform_async_copy(atomKK->k_image,space); if ((mask & Q_MASK) && atomKK->k_q.need_sync()) perform_async_copy(atomKK->k_q,space); } }