Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: Combinable histograms for angle module parallel execution #1937

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/math/histogram2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,22 @@ void Histogram2D::operator=(const Histogram2D &source)
averages_ = source.averages_;
}

Histogram2D Histogram2D::operator+(const Histogram2D &other) const
{
assert(nXBins_ == other.nXBins_ && nYBins_ == other.nYBins_);

Histogram2D ret = *this;

std::transform(other.bins_.begin(), other.bins_.end(), ret.bins_.begin(), ret.bins_.begin(), std::plus<>());

ret.nBinned_ = this->nBinned_ + other.nBinned_;
ret.nMissed_ = this->nMissed_ + other.nMissed_;
ret.nXBins_ = this->nXBins_;
ret.nYBins_ = this->nYBins_;

return ret;
}

/*
* Serialisation
*/
Expand Down
1 change: 1 addition & 0 deletions src/math/histogram2D.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ class Histogram2D
*/
public:
void operator=(const Histogram2D &source);
Histogram2D operator+(const Histogram2D &other) const;

/*
* Serialisation
Expand Down
17 changes: 17 additions & 0 deletions src/math/histogram3D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,23 @@ void Histogram3D::operator=(const Histogram3D &source)
averages_ = source.averages_;
}

Histogram3D Histogram3D::operator+(const Histogram3D &other) const
{
assert(nXBins_ == other.nXBins_ && nYBins_ == other.nYBins_ && nZBins_ == other.nZBins_);

Histogram3D ret = *this;

std::transform(other.bins_.begin(), other.bins_.end(), ret.bins_.begin(), ret.bins_.begin(), std::plus<>());

ret.nBinned_ = this->nBinned_ + other.nBinned_;
ret.nMissed_ = this->nMissed_ + other.nMissed_;
ret.nXBins_ = this->nXBins_;
ret.nYBins_ = this->nYBins_;
ret.nZBins_ = this->nZBins_;

return ret;
}

/*
* Serialisation
*/
Expand Down
1 change: 1 addition & 0 deletions src/math/histogram3D.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ class Histogram3D
*/
public:
void operator=(const Histogram3D &source);
Histogram3D operator+(const Histogram3D &other) const;

/*
* Serialisation
Expand Down
34 changes: 32 additions & 2 deletions src/modules/angle/process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include "math/range.h"
#include "module/context.h"
#include "modules/angle/angle.h"
#include "templates/algorithms.h"
#include "templates/combinable.h"

// Run main processing
Module::ExecutionResult AngleModule::process(ModuleContext &moduleContext)
Expand Down Expand Up @@ -74,13 +76,31 @@ Module::ExecutionResult AngleModule::process(ModuleContext &moduleContext)
dAngleBC.zeroBins();
dAngleABC.zeroBins();

auto combinableRAB = dissolve::CombinableValue<Histogram1D>(rAB);
auto combinableRBC = dissolve::CombinableValue<Histogram1D>(rBC);
auto combinableAABC = dissolve::CombinableValue<Histogram1D>(aABC);
auto combinableDAngleAB = dissolve::CombinableValue<Histogram2D>(dAngleAB);
auto combinableDAngleBC = dissolve::CombinableValue<Histogram2D>(dAngleBC);
auto combinableDAngleABC = dissolve::CombinableValue<Histogram3D>(dAngleABC);

auto nAAvailable = a.sites().size(), nACumulative = a.sites().size();
auto nASelections = 1;
auto nBAvailable = 0, nBCumulative = 0, nBSelections = 0;
auto nCAvailable = 0, nCCumulative = 0, nCSelections = 0;

for (const auto &[siteA, indexA] : a.sites())
auto unaryOp = [this, &b, &c, &nAAvailable, &nACumulative, &nASelections, &nBAvailable, &nBCumulative, &nBSelections,
&nCAvailable, &nCCumulative, &nCSelections, &combinableRAB, &combinableRBC, &combinableAABC,
&combinableDAngleAB, &combinableDAngleBC, &combinableDAngleABC](const auto &pair)
{
const auto &[siteA, indexA] = pair;

auto &rAB = combinableRAB.local();
auto &rBC = combinableRBC.local();
auto &aABC = combinableAABC.local();
auto &dAngleAB = combinableDAngleAB.local();
auto &dAngleBC = combinableDAngleBC.local();
auto &dAngleABC = combinableDAngleABC.local();

++nBSelections;
for (const auto &[siteB, indexB] : b.sites())
{
Expand Down Expand Up @@ -129,7 +149,17 @@ Module::ExecutionResult AngleModule::process(ModuleContext &moduleContext)
dAngleABC.bin(distAB, distBC, angle);
}
}
}
};

dissolve::for_each(std::execution::par, a.sites().begin(), a.sites().end(), unaryOp);

// Finalize combinable histograms
rAB = combinableRAB.finalize();
rBC = combinableRBC.finalize();
aABC = combinableAABC.finalize();
dAngleAB = combinableDAngleAB.finalize();
dAngleBC = combinableDAngleBC.finalize();
dAngleABC = combinableDAngleABC.finalize();

// Accumulate histograms
rAB.accumulate();
Expand Down
Loading