From 59688503063612e001bf83b1f3c22c087cf4f315 Mon Sep 17 00:00:00 2001 From: Stan Moore Date: Tue, 12 Dec 2017 09:20:09 -0700 Subject: [PATCH 1/5] Fix broken charge history in fix_qeq_reax_kokkos --- src/KOKKOS/fix_qeq_reax_kokkos.cpp | 80 +++++++++++++++++++++++++++++- src/KOKKOS/fix_qeq_reax_kokkos.h | 10 ++-- src/USER-REAXC/fix_qeq_reax.h | 18 +++---- 3 files changed, 94 insertions(+), 14 deletions(-) diff --git a/src/KOKKOS/fix_qeq_reax_kokkos.cpp b/src/KOKKOS/fix_qeq_reax_kokkos.cpp index 91a22361fc..c7a1312519 100644 --- a/src/KOKKOS/fix_qeq_reax_kokkos.cpp +++ b/src/KOKKOS/fix_qeq_reax_kokkos.cpp @@ -72,6 +72,9 @@ template FixQEqReaxKokkos::~FixQEqReaxKokkos() { if (copymode) return; + + memoryKK->destroy_kokkos(k_s_hist,s_hist); + memoryKK->destroy_kokkos(k_t_hist,t_hist); } /* ---------------------------------------------------------------------- */ @@ -235,6 +238,8 @@ void FixQEqReaxKokkos::pre_force(int vflag) Kokkos::parallel_scan(inum,computeH_functor); // init_matvec + k_s_hist.template sync(); + k_t_hist.template sync(); FixQEqReaxKokkosMatVecFunctor matvec_functor(this); Kokkos::parallel_for(inum,matvec_functor); @@ -262,6 +267,8 @@ void FixQEqReaxKokkos::pre_force(int vflag) // calculate_Q(); calculate_q(); + k_s_hist.template modify(); + k_t_hist.template modify(); copymode = 0; @@ -462,7 +469,7 @@ double FixQEqReaxKokkos::calculate_H_k(const F_FLOAT &r, const F_FLO template KOKKOS_INLINE_FUNCTION -void FixQEqReaxKokkos::mat_vec_item(int ii) const +void FixQEqReaxKokkos::matvec_item(int ii) const { const int i = d_ilist[ii]; const int itype = type(i); @@ -1183,7 +1190,76 @@ double FixQEqReaxKokkos::memory_usage() return bytes; } -/* ---------------------------------------------------------------------- */\ +/* ---------------------------------------------------------------------- + allocate fictitious charge arrays +------------------------------------------------------------------------- */ + +template +void FixQEqReaxKokkos::grow_arrays(int nmax) +{ + k_s_hist.template sync(); // force reallocation on host + k_t_hist.template sync(); + + memoryKK->grow_kokkos(k_s_hist,s_hist,nmax,nprev,"qeq:s_hist"); + memoryKK->grow_kokkos(k_t_hist,t_hist,nmax,nprev,"qeq:t_hist"); + + k_s_hist.template modify(); + k_t_hist.template modify(); + +} + +/* ---------------------------------------------------------------------- + copy values within fictitious charge arrays +------------------------------------------------------------------------- */ + +template +void FixQEqReaxKokkos::copy_arrays(int i, int j, int delflag) +{ + k_s_hist.template sync(); + k_t_hist.template sync(); + + for (int m = 0; m < nprev; m++) { + s_hist[j][m] = s_hist[i][m]; + t_hist[j][m] = t_hist[i][m]; + } + + k_s_hist.template modify(); + k_t_hist.template modify(); + +} + +/* ---------------------------------------------------------------------- + pack values in local atom-based array for exchange with another proc +------------------------------------------------------------------------- */ + +template +int FixQEqReaxKokkos::pack_exchange(int i, double *buf) +{ + k_s_hist.template sync(); + k_t_hist.template sync(); + + for (int m = 0; m < nprev; m++) buf[m] = s_hist[i][m]; + for (int m = 0; m < nprev; m++) buf[nprev+m] = t_hist[i][m]; + return nprev*2; +} + +/* ---------------------------------------------------------------------- + unpack values in local atom-based array from exchange with another proc +------------------------------------------------------------------------- */ + +template +int FixQEqReaxKokkos::unpack_exchange(int nlocal, double *buf) +{ + for (int m = 0; m < nprev; m++) s_hist[nlocal][m] = buf[m]; + for (int m = 0; m < nprev; m++) t_hist[nlocal][m] = buf[nprev+m]; + + k_s_hist.template modify(); + k_t_hist.template modify(); + + return nprev*2; +} + +/* ---------------------------------------------------------------------- */ namespace LAMMPS_NS { template class FixQEqReaxKokkos; diff --git a/src/KOKKOS/fix_qeq_reax_kokkos.h b/src/KOKKOS/fix_qeq_reax_kokkos.h index 9014e20b8e..517b541f6f 100644 --- a/src/KOKKOS/fix_qeq_reax_kokkos.h +++ b/src/KOKKOS/fix_qeq_reax_kokkos.h @@ -57,7 +57,7 @@ class FixQEqReaxKokkos : public FixQEqReax { void compute_h_item(int, int &, const bool &) const; KOKKOS_INLINE_FUNCTION - void mat_vec_item(int) const; + void matvec_item(int) const; KOKKOS_INLINE_FUNCTION void sparse12_item(int) const; @@ -145,7 +145,7 @@ class FixQEqReaxKokkos : public FixQEqReax { void unpack_reverse_comm(int, int *, double *); double memory_usage(); - protected: + private: int inum; int allocated_flag; @@ -210,6 +210,10 @@ class FixQEqReaxKokkos : public FixQEqReax { typename AT::t_int_2d d_sendlist; typename AT::t_xfloat_1d_um v_buf; + void grow_arrays(int); + void copy_arrays(int, int, int); + int pack_exchange(int, double *); + int unpack_exchange(int, double *); }; template @@ -235,7 +239,7 @@ struct FixQEqReaxKokkosMatVecFunctor { }; KOKKOS_INLINE_FUNCTION void operator()(const int ii) const { - c.mat_vec_item(ii); + c.matvec_item(ii); } }; diff --git a/src/USER-REAXC/fix_qeq_reax.h b/src/USER-REAXC/fix_qeq_reax.h index 19efcd2b03..96a174b908 100644 --- a/src/USER-REAXC/fix_qeq_reax.h +++ b/src/USER-REAXC/fix_qeq_reax.h @@ -122,15 +122,15 @@ class FixQEqReax : public Fix { //int GMRES(double*,double*); virtual void sparse_matvec(sparse_matrix*,double*,double*); - int pack_forward_comm(int, int *, double *, int, int *); - void unpack_forward_comm(int, int, double *); - int pack_reverse_comm(int, int, double *); - void unpack_reverse_comm(int, int *, double *); - double memory_usage(); - void grow_arrays(int); - void copy_arrays(int, int, int); - int pack_exchange(int, double *); - int unpack_exchange(int, double *); + virtual int pack_forward_comm(int, int *, double *, int, int *); + virtual void unpack_forward_comm(int, int, double *); + virtual int pack_reverse_comm(int, int, double *); + virtual void unpack_reverse_comm(int, int *, double *); + virtual double memory_usage(); + virtual void grow_arrays(int); + virtual void copy_arrays(int, int, int); + virtual int pack_exchange(int, double *); + virtual int unpack_exchange(int, double *); virtual double parallel_norm( double*, int ); virtual double parallel_dot( double*, double*, int ); From 193252275f0e53d68212a9a82aab1824c7c3127e Mon Sep 17 00:00:00 2001 From: Stan Moore Date: Tue, 12 Dec 2017 09:54:28 -0700 Subject: [PATCH 2/5] A few more tweaks to charge history in fix_qeq_reax_kokkos --- src/KOKKOS/fix_qeq_reax_kokkos.cpp | 37 ++++++------------------------ 1 file changed, 7 insertions(+), 30 deletions(-) diff --git a/src/KOKKOS/fix_qeq_reax_kokkos.cpp b/src/KOKKOS/fix_qeq_reax_kokkos.cpp index c7a1312519..b251c634b6 100644 --- a/src/KOKKOS/fix_qeq_reax_kokkos.cpp +++ b/src/KOKKOS/fix_qeq_reax_kokkos.cpp @@ -160,25 +160,11 @@ void FixQEqReaxKokkos::init_shielding_k() template void FixQEqReaxKokkos::init_hist() { - int i,j; - - k_s_hist = DAT::tdual_ffloat_2d("qeq/kk:s_hist",atom->nmax,nprev); - d_s_hist = k_s_hist.template view(); - h_s_hist = k_s_hist.h_view; - k_t_hist = DAT::tdual_ffloat_2d("qeq/kk:t_hist",atom->nmax,nprev); - d_t_hist = k_t_hist.template view(); - h_t_hist = k_t_hist.h_view; - - for( i = 0; i < atom->nmax; i++ ) - for( j = 0; j < nprev; j++ ) - k_s_hist.h_view(i,j) = k_t_hist.h_view(i,j) = 0.0; - - k_s_hist.template modify(); - k_s_hist.template sync(); - - k_t_hist.template modify(); - k_t_hist.template sync(); + Kokkos::deep_copy(d_s_hist,0.0); + Kokkos::deep_copy(d_t_hist,0.0); + k_s_hist.template modify(); + k_t_hist.template modify(); } /* ---------------------------------------------------------------------- */ @@ -341,14 +327,6 @@ void FixQEqReaxKokkos::allocate_array() k_d = DAT::tdual_ffloat_1d("qeq/kk:h_d",nmax); d_d = k_d.template view(); h_d = k_d.h_view; - - k_s_hist = DAT::tdual_ffloat_2d("qeq/kk:s_hist",nmax,nprev); - d_s_hist = k_s_hist.template view(); - h_s_hist = k_s_hist.h_view; - - k_t_hist = DAT::tdual_ffloat_2d("qeq/kk:t_hist",nmax,nprev); - d_t_hist = k_t_hist.template view(); - h_t_hist = k_t_hist.h_view; } // init_storage @@ -376,8 +354,6 @@ void FixQEqReaxKokkos::zero_item(int ii) const d_o[i] = 0.0; d_r[i] = 0.0; d_d[i] = 0.0; - //for( int j = 0; j < nprev; j++ ) - //d_s_hist(i,j) = d_t_hist(i,j) = 0.0; } } @@ -1203,9 +1179,11 @@ void FixQEqReaxKokkos::grow_arrays(int nmax) memoryKK->grow_kokkos(k_s_hist,s_hist,nmax,nprev,"qeq:s_hist"); memoryKK->grow_kokkos(k_t_hist,t_hist,nmax,nprev,"qeq:t_hist"); + d_s_hist = k_s_hist.template view(); + d_t_hist = k_t_hist.template view(); + k_s_hist.template modify(); k_t_hist.template modify(); - } /* ---------------------------------------------------------------------- @@ -1225,7 +1203,6 @@ void FixQEqReaxKokkos::copy_arrays(int i, int j, int delflag) k_s_hist.template modify(); k_t_hist.template modify(); - } /* ---------------------------------------------------------------------- From 00a9672524e881c2ba7b507f3e54c1320818b253 Mon Sep 17 00:00:00 2001 From: Stan Moore Date: Mon, 18 Dec 2017 13:57:56 -0700 Subject: [PATCH 3/5] Fix issue in fix_qeq_reax_kokkos, can't call child function from base constructor --- src/KOKKOS/fix_qeq_reax_kokkos.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/KOKKOS/fix_qeq_reax_kokkos.cpp b/src/KOKKOS/fix_qeq_reax_kokkos.cpp index b251c634b6..ca14159b55 100644 --- a/src/KOKKOS/fix_qeq_reax_kokkos.cpp +++ b/src/KOKKOS/fix_qeq_reax_kokkos.cpp @@ -64,6 +64,10 @@ FixQEqReaxKokkos(LAMMPS *lmp, int narg, char **arg) : nmax = nmax = m_cap = 0; allocated_flag = 0; nprev = 4; + + memory->destroy(s_hist); + memory->destroy(t_hist); + grow_arrays(atom->nmax); } /* ---------------------------------------------------------------------- */ From 46fe0a968bed3e54e4cc8562474c7f2986d24bd0 Mon Sep 17 00:00:00 2001 From: Stan Moore Date: Mon, 18 Dec 2017 14:09:39 -0700 Subject: [PATCH 4/5] Fix compiler warnings in atom_vec_hybrid_kokkos --- src/KOKKOS/atom_vec_hybrid_kokkos.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/KOKKOS/atom_vec_hybrid_kokkos.cpp b/src/KOKKOS/atom_vec_hybrid_kokkos.cpp index b5aadb18d6..67dce07b80 100644 --- a/src/KOKKOS/atom_vec_hybrid_kokkos.cpp +++ b/src/KOKKOS/atom_vec_hybrid_kokkos.cpp @@ -255,6 +255,7 @@ int AtomVecHybridKokkos::pack_comm_kokkos(const int &n, const DAT::tdual_int_2d const int &pbc_flag, const int pbc[]) { error->all(FLERR,"AtomVecHybridKokkos doesn't yet support threaded comm"); + return 0; } void AtomVecHybridKokkos::unpack_comm_kokkos(const int &n, const int &nfirst, const DAT::tdual_xfloat_2d &buf) @@ -266,12 +267,14 @@ int AtomVecHybridKokkos::pack_comm_self(const int &n, const DAT::tdual_int_2d &l const int &pbc_flag, const int pbc[]) { error->all(FLERR,"AtomVecHybridKokkos doesn't yet support threaded comm"); + return 0; } int AtomVecHybridKokkos::pack_border_kokkos(int n, DAT::tdual_int_2d k_sendlist, DAT::tdual_xfloat_2d buf,int iswap, int pbc_flag, int *pbc, ExecutionSpace space) { error->all(FLERR,"AtomVecHybridKokkos doesn't yet support threaded comm"); + return 0; } void AtomVecHybridKokkos::unpack_border_kokkos(const int &n, const int &nfirst, const DAT::tdual_xfloat_2d &buf, @@ -286,12 +289,14 @@ int AtomVecHybridKokkos::pack_exchange_kokkos(const int &nsend,DAT::tdual_xfloat X_FLOAT lo, X_FLOAT hi) { error->all(FLERR,"AtomVecHybridKokkos doesn't yet support threaded comm"); + return 0; } int AtomVecHybridKokkos::unpack_exchange_kokkos(DAT::tdual_xfloat_2d &k_buf, int nrecv, int nlocal, int dim, X_FLOAT lo, X_FLOAT hi, ExecutionSpace space) { error->all(FLERR,"AtomVecHybridKokkos doesn't yet support threaded comm"); + return 0; } /* ---------------------------------------------------------------------- */ From 7d07baa8ad0ad9f98f64dcf416c395a58d088db2 Mon Sep 17 00:00:00 2001 From: Stan Moore Date: Thu, 21 Dec 2017 11:07:06 -0700 Subject: [PATCH 5/5] Better load balance fix_qeq_reax_kokkos for half neigh list --- src/KOKKOS/fix_qeq_reax_kokkos.cpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/KOKKOS/fix_qeq_reax_kokkos.cpp b/src/KOKKOS/fix_qeq_reax_kokkos.cpp index ca14159b55..ebf270bd78 100644 --- a/src/KOKKOS/fix_qeq_reax_kokkos.cpp +++ b/src/KOKKOS/fix_qeq_reax_kokkos.cpp @@ -392,19 +392,19 @@ void FixQEqReaxKokkos::compute_h_item(int ii, int &m_fill, const boo const X_FLOAT delz = x(j,2) - ztmp; if (neighflag != FULL) { + // skip half of the interactions const tagint jtag = tag(j); - flag = 0; - if (j < nlocal) flag = 1; - else if (itag < jtag) flag = 1; - else if (itag == jtag) { - if (delz > SMALL) flag = 1; - else if (fabs(delz) < SMALL) { - if (dely > SMALL) flag = 1; - else if (fabs(dely) < SMALL && delx > SMALL) - flag = 1; + if (j >= nlocal) { + if (itag > jtag) { + if ((itag+jtag) % 2 == 0) continue; + } else if (itag < jtag) { + if ((itag+jtag) % 2 == 1) continue; + } else { + if (x(j,2) < ztmp) continue; + if (x(j,2) == ztmp && x(j,1) < ytmp) continue; + if (x(j,2) == ztmp && x(j,1) == ytmp && x(j,0) < xtmp) continue; } } - if (!flag) continue; } const F_FLOAT rsq = delx*delx + dely*dely + delz*delz;