From f558ea56b930091179e4007607f812a139824811 Mon Sep 17 00:00:00 2001 From: RobBuchananCompPhys Date: Tue, 11 Jun 2024 13:55:44 +0100 Subject: [PATCH 1/3] combinable histograms --- src/math/histogram2D.cpp | 16 +++++++++++ src/math/histogram2D.h | 1 + src/math/histogram3D.cpp | 17 ++++++++++++ src/math/histogram3D.h | 1 + src/modules/angle/process.cpp | 51 +++++++++++++++++++++++++++++++++-- 5 files changed, 84 insertions(+), 2 deletions(-) diff --git a/src/math/histogram2D.cpp b/src/math/histogram2D.cpp index 37a7b8bc1c..cb1d135e99 100644 --- a/src/math/histogram2D.cpp +++ b/src/math/histogram2D.cpp @@ -195,6 +195,22 @@ void Histogram2D::operator=(const Histogram2D &source) averages_ = source.averages_; } +Histogram2D Histogram2D::operator+(const Histogram2D &other) const +{ + assert(nBins_ == other.nBins_ && nXBins_ == 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 */ diff --git a/src/math/histogram2D.h b/src/math/histogram2D.h index 814492050c..9376278ed6 100644 --- a/src/math/histogram2D.h +++ b/src/math/histogram2D.h @@ -95,6 +95,7 @@ class Histogram2D */ public: void operator=(const Histogram2D &source); + Histogram2D operator+(const Histogram2D &other) const; /* * Serialisation diff --git a/src/math/histogram3D.cpp b/src/math/histogram3D.cpp index f39f33fb44..70826a807a 100644 --- a/src/math/histogram3D.cpp +++ b/src/math/histogram3D.cpp @@ -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 */ diff --git a/src/math/histogram3D.h b/src/math/histogram3D.h index b3d0c6d874..687de449f3 100644 --- a/src/math/histogram3D.h +++ b/src/math/histogram3D.h @@ -122,6 +122,7 @@ class Histogram3D */ public: void operator=(const Histogram3D &source); + Histogram3D operator+(const Histogram3D &other) const; /* * Serialisation diff --git a/src/modules/angle/process.cpp b/src/modules/angle/process.cpp index bd213f2410..c8d1c0d611 100644 --- a/src/modules/angle/process.cpp +++ b/src/modules/angle/process.cpp @@ -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) @@ -74,13 +76,48 @@ Module::ExecutionResult AngleModule::process(ModuleContext &moduleContext) dAngleBC.zeroBins(); dAngleABC.zeroBins(); + auto combinableRAB = dissolve::CombinableValue(rAB); + auto combinableRBC = dissolve::CombinableValue(rBC); + auto combinableAABC = dissolve::CombinableValue(aABC); + auto combinableDAngleAB = dissolve::CombinableValue(dAngleAB); + auto combinableDAngleBC = dissolve::CombinableValue(dAngleBC); + auto combinableDAngleABC = dissolve::CombinableValue(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()) { @@ -129,7 +166,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(); From 2982085309709af6962cd9f8498251987e06931d Mon Sep 17 00:00:00 2001 From: RobBuchananCompPhys Date: Tue, 11 Jun 2024 14:57:04 +0100 Subject: [PATCH 2/3] clang format --- src/modules/angle/process.cpp | 27 +++++---------------------- 1 file changed, 5 insertions(+), 22 deletions(-) diff --git a/src/modules/angle/process.cpp b/src/modules/angle/process.cpp index c8d1c0d611..595c2dbba7 100644 --- a/src/modules/angle/process.cpp +++ b/src/modules/angle/process.cpp @@ -88,26 +88,9 @@ Module::ExecutionResult AngleModule::process(ModuleContext &moduleContext) auto nBAvailable = 0, nBCumulative = 0, nBSelections = 0; auto nCAvailable = 0, nCCumulative = 0, nCSelections = 0; - auto unaryOp = [ - this, - &b, - &c, - &nAAvailable, - &nACumulative, - &nASelections, - &nBAvailable, - &nBCumulative, - &nBSelections, - &nCAvailable, - &nCCumulative, - &nCSelections, - &combinableRAB, - &combinableRBC, - &combinableAABC, - &combinableDAngleAB, - &combinableDAngleBC, - &combinableDAngleABC - ](const auto &pair) + 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; @@ -116,7 +99,7 @@ Module::ExecutionResult AngleModule::process(ModuleContext &moduleContext) auto &aABC = combinableAABC.local(); auto &dAngleAB = combinableDAngleAB.local(); auto &dAngleBC = combinableDAngleBC.local(); - auto &dAngleABC = combinableDAngleABC.local(); + auto &dAngleABC = combinableDAngleABC.local(); ++nBSelections; for (const auto &[siteB, indexB] : b.sites()) @@ -176,7 +159,7 @@ Module::ExecutionResult AngleModule::process(ModuleContext &moduleContext) aABC = combinableAABC.finalize(); dAngleAB = combinableDAngleAB.finalize(); dAngleBC = combinableDAngleBC.finalize(); - dAngleABC = combinableDAngleABC.finalize(); + dAngleABC = combinableDAngleABC.finalize(); // Accumulate histograms rAB.accumulate(); From 364fae757fd7143ed136b528defdc9fb38b76ea9 Mon Sep 17 00:00:00 2001 From: RobBuchananCompPhys Date: Wed, 12 Jun 2024 10:21:20 +0100 Subject: [PATCH 3/3] nBins -> nXBins --- src/math/histogram2D.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/math/histogram2D.cpp b/src/math/histogram2D.cpp index cb1d135e99..5eecc9f373 100644 --- a/src/math/histogram2D.cpp +++ b/src/math/histogram2D.cpp @@ -197,7 +197,7 @@ void Histogram2D::operator=(const Histogram2D &source) Histogram2D Histogram2D::operator+(const Histogram2D &other) const { - assert(nBins_ == other.nBins_ && nXBins_ == other.nYBins_); + assert(nXBins_ == other.nXBins_ && nYBins_ == other.nYBins_); Histogram2D ret = *this;