Skip to content

Commit

Permalink
Merge pull request #504 from GOMC-WSU/omp-sections
Browse files Browse the repository at this point in the history
Use omp sections to remove redundant memset/memcpy calls
  • Loading branch information
jpotoff committed Jun 27, 2023
2 parents bf04333 + 0aacff4 commit 1fd5284
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 19 deletions.
46 changes: 38 additions & 8 deletions src/Ewald.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,12 +225,17 @@ void Ewald::BoxReciprocalSetup(uint box, XYZArray const &molCoords) {
hsqr[box], currentEnergyRecip[box], box);
#else
#ifdef _OPENMP
#pragma omp parallel default(none) shared(box)
#endif
#pragma omp parallel sections default(none) shared(box)
{
#pragma omp section
std::memset(sumRnew[box], 0.0, sizeof(double) * imageSize[box]);
#pragma omp section
std::memset(sumInew[box], 0.0, sizeof(double) * imageSize[box]);
}
#else
std::memset(sumRnew[box], 0.0, sizeof(double) * imageSize[box]);
std::memset(sumInew[box], 0.0, sizeof(double) * imageSize[box]);
#endif

while (thisMol != end) {
MoleculeKind const &thisKind = mols.GetKind(*thisMol);
Expand Down Expand Up @@ -307,12 +312,17 @@ void Ewald::BoxReciprocalSums(uint box, XYZArray const &molCoords) {
sumInew[box], currentEnergyRecip[box], box);
#else
#ifdef _OPENMP
#pragma omp parallel default(none) shared(box)
#endif
#pragma omp parallel sections default(none) shared(box)
{
#pragma omp section
std::memset(sumRnew[box], 0.0, sizeof(double) * imageSizeRef[box]);
#pragma omp section
std::memset(sumInew[box], 0.0, sizeof(double) * imageSizeRef[box]);
}
#else
std::memset(sumRnew[box], 0.0, sizeof(double) * imageSizeRef[box]);
std::memset(sumInew[box], 0.0, sizeof(double) * imageSizeRef[box]);
#endif

while (thisMol != end) {
MoleculeKind const &thisKind = mols.GetKind(*thisMol);
Expand Down Expand Up @@ -1011,17 +1021,32 @@ void Ewald::RecipCountInit(uint box, BoxDimensions const &boxAxes) {
// back up reciprocal value to Ref (will be called during initialization)
void Ewald::SetRecipRef(uint box) {
#ifdef _OPENMP
#pragma omp parallel default(none) shared(box)
#endif
#pragma omp parallel sections default(none) shared(box)
{
#pragma omp section
std::memcpy(sumRref[box], sumRnew[box], sizeof(double) * imageSize[box]);
#pragma omp section
std::memcpy(sumIref[box], sumInew[box], sizeof(double) * imageSize[box]);
#pragma omp section
std::memcpy(kxRef[box], kx[box], sizeof(double) * imageSize[box]);
#pragma omp section
std::memcpy(kyRef[box], ky[box], sizeof(double) * imageSize[box]);
#pragma omp section
std::memcpy(kzRef[box], kz[box], sizeof(double) * imageSize[box]);
#pragma omp section
std::memcpy(hsqrRef[box], hsqr[box], sizeof(double) * imageSize[box]);
#pragma omp section
std::memcpy(prefactRef[box], prefact[box], sizeof(double) * imageSize[box]);
}
#else
std::memcpy(sumRref[box], sumRnew[box], sizeof(double) * imageSize[box]);
std::memcpy(sumIref[box], sumInew[box], sizeof(double) * imageSize[box]);
std::memcpy(kxRef[box], kx[box], sizeof(double) * imageSize[box]);
std::memcpy(kyRef[box], ky[box], sizeof(double) * imageSize[box]);
std::memcpy(kzRef[box], kz[box], sizeof(double) * imageSize[box]);
std::memcpy(hsqrRef[box], hsqr[box], sizeof(double) * imageSize[box]);
std::memcpy(prefactRef[box], prefact[box], sizeof(double) * imageSize[box]);
#endif
#ifdef GOMC_CUDA
CopyCurrentToRefCUDA(ff.particles->getCUDAVars(), box, imageSize[box]);
#endif
Expand Down Expand Up @@ -1417,12 +1442,17 @@ void Ewald::CopyRecip(uint box) {
return;

#ifdef _OPENMP
#pragma omp parallel default(none) shared(box)
#endif
#pragma omp parallel sections default(none) shared(box)
{
#pragma omp section
std::memcpy(sumRnew[box], sumRref[box], sizeof(double) * imageSizeRef[box]);
#pragma omp section
std::memcpy(sumInew[box], sumIref[box], sizeof(double) * imageSizeRef[box]);
}
#else
std::memcpy(sumRnew[box], sumRref[box], sizeof(double) * imageSizeRef[box]);
std::memcpy(sumInew[box], sumIref[box], sizeof(double) * imageSizeRef[box]);
#endif
#ifdef GOMC_CUDA
CopyRefToNewCUDA(ff.particles->getCUDAVars(), box, imageSizeRef[box]);
#endif
Expand Down
27 changes: 21 additions & 6 deletions src/EwaldCached.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,17 @@ void EwaldCached::BoxReciprocalSetup(uint box, XYZArray const &molCoords) {
MoleculeLookup::box_iterator end = molLookup.BoxEnd(box);

#ifdef _OPENMP
#pragma omp parallel default(none) shared(box)
#endif
#pragma omp parallel sections default(none) shared(box)
{
#pragma omp section
std::memset(sumRnew[box], 0.0, sizeof(double) * imageSize[box]);
#pragma omp section
std::memset(sumInew[box], 0.0, sizeof(double) * imageSize[box]);
}
#else
std::memset(sumRnew[box], 0.0, sizeof(double) * imageSize[box]);
std::memset(sumInew[box], 0.0, sizeof(double) * imageSize[box]);
#endif

while (thisMol != end) {
MoleculeKind const &thisKind = mols.GetKind(*thisMol);
Expand Down Expand Up @@ -151,12 +156,17 @@ void EwaldCached::BoxReciprocalSums(uint box, XYZArray const &molCoords) {
MoleculeLookup::box_iterator end = molLookup.BoxEnd(box);

#ifdef _OPENMP
#pragma omp parallel default(none) shared(box)
#endif
#pragma omp parallel sections default(none) shared(box)
{
#pragma omp section
std::memset(sumRnew[box], 0.0, sizeof(double) * imageSizeRef[box]);
#pragma omp section
std::memset(sumInew[box], 0.0, sizeof(double) * imageSizeRef[box]);
}
#else
std::memset(sumRnew[box], 0.0, sizeof(double) * imageSizeRef[box]);
std::memset(sumInew[box], 0.0, sizeof(double) * imageSizeRef[box]);
#endif

while (thisMol != end) {
MoleculeKind const &thisKind = mols.GetKind(*thisMol);
Expand Down Expand Up @@ -295,14 +305,19 @@ double EwaldCached::SwapDestRecip(const cbmc::TrialMol &newMol, const uint box,
double energyRecipOld = 0.0;

#ifdef _OPENMP
#pragma omp parallel default(none) firstprivate(molIndex)
#endif
#pragma omp parallel sections default(none) firstprivate(molIndex)
{
#pragma omp section
std::memcpy(cosMolRestore, cosMolRef[molIndex],
sizeof(double) * imageTotal);
#pragma omp section
std::memcpy(sinMolRestore, sinMolRef[molIndex],
sizeof(double) * imageTotal);
}
#else
std::memcpy(cosMolRestore, cosMolRef[molIndex], sizeof(double) * imageTotal);
std::memcpy(sinMolRestore, sinMolRef[molIndex], sizeof(double) * imageTotal);
#endif

if (box < BOXES_WITH_U_NB) {
MoleculeKind const &thisKind = newMol.GetKind();
Expand Down
24 changes: 19 additions & 5 deletions src/XYZArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -370,13 +370,20 @@ inline void XYZArray::SetRange(const uint start, const uint stop,

inline void XYZArray::ResetRange(const uint val, const uint stop) {
#ifdef _OPENMP
#pragma omp parallel default(none) firstprivate(val, stop)
#endif
#pragma omp parallel sections default(none) firstprivate(val, stop)
{
#pragma omp section
memset(this->x, val, stop * sizeof(double));
#pragma omp section
memset(this->y, val, stop * sizeof(double));
#pragma omp section
memset(this->z, val, stop * sizeof(double));
}
#else
memset(this->x, val, stop * sizeof(double));
memset(this->y, val, stop * sizeof(double));
memset(this->z, val, stop * sizeof(double));
#endif
}

inline void XYZArray::Reset() { ResetRange(0, count); }
Expand Down Expand Up @@ -483,14 +490,21 @@ inline void XYZArray::ScaleAll(const double val) { ScaleRange(0, count, val); }
inline void XYZArray::CopyRange(XYZArray &dest, const uint srcIndex,
const uint destIndex, const uint len) const {
#ifdef _OPENMP
#pragma omp parallel default(none) firstprivate(srcIndex, destIndex, len) \
shared(dest)
#endif
#pragma omp parallel sections default(none) \
firstprivate(srcIndex, destIndex, len) shared(dest)
{
#pragma omp section
memcpy(dest.x + destIndex, x + srcIndex, len * sizeof(double));
#pragma omp section
memcpy(dest.y + destIndex, y + srcIndex, len * sizeof(double));
#pragma omp section
memcpy(dest.z + destIndex, z + srcIndex, len * sizeof(double));
}
#else
memcpy(dest.x + destIndex, x + srcIndex, len * sizeof(double));
memcpy(dest.y + destIndex, y + srcIndex, len * sizeof(double));
memcpy(dest.z + destIndex, z + srcIndex, len * sizeof(double));
#endif
}

inline double XYZArray::AdjointMatrix(XYZArray &Inv) {
Expand Down

0 comments on commit 1fd5284

Please sign in to comment.