ParallelPAvgDynamicSourceData: template Scalar type

This commit is contained in:
Arne Morten Kvarving 2024-02-20 20:27:55 +01:00
parent b9ad890ea5
commit b68a854909
5 changed files with 42 additions and 32 deletions

View File

@ -33,31 +33,34 @@
#include <utility> #include <utility>
#include <vector> #include <vector>
Opm::ParallelPAvgDynamicSourceData:: template<class Scalar>
Opm::ParallelPAvgDynamicSourceData<Scalar>::
ParallelPAvgDynamicSourceData(const Parallel::Communication& comm, ParallelPAvgDynamicSourceData(const Parallel::Communication& comm,
const std::vector<std::size_t>& sourceLocations, const std::vector<std::size_t>& sourceLocations,
GlobalToLocal localCellIdx) GlobalToLocal localCellIdx)
: PAvgDynamicSourceData { sourceLocations } : PAvgDynamicSourceData<Scalar> { sourceLocations }
, comm_ { comm } , comm_ { comm }
{ {
this->finaliseConstruction(sourceLocations, std::move(localCellIdx)); this->finaliseConstruction(sourceLocations, std::move(localCellIdx));
} }
void Opm::ParallelPAvgDynamicSourceData::setToZero() template<class Scalar>
void Opm::ParallelPAvgDynamicSourceData<Scalar>::setToZero()
{ {
std::fill_n(this->localSrc_.begin(), this->localSrc_.size(), 0.0); std::fill_n(this->localSrc_.begin(), this->localSrc_.size(), 0.0);
} }
void template<class Scalar>
Opm::ParallelPAvgDynamicSourceData:: void Opm::ParallelPAvgDynamicSourceData<Scalar>::
reconstruct(const std::vector<std::size_t>& sourceLocations, reconstruct(const std::vector<std::size_t>& sourceLocations,
GlobalToLocal localCellIdx) GlobalToLocal localCellIdx)
{ {
PAvgDynamicSourceData::reconstruct(sourceLocations); // Reconstruct base PAvgDynamicSourceData<Scalar>::reconstruct(sourceLocations); // Reconstruct base
this->finaliseConstruction(sourceLocations, std::move(localCellIdx)); this->finaliseConstruction(sourceLocations, std::move(localCellIdx));
} }
void Opm::ParallelPAvgDynamicSourceData::collectLocalSources(Evaluator eval) template<class Scalar>
void Opm::ParallelPAvgDynamicSourceData<Scalar>::collectLocalSources(Evaluator eval)
{ {
auto localIx = std::size_t{0}; auto localIx = std::size_t{0};
@ -66,7 +69,8 @@ void Opm::ParallelPAvgDynamicSourceData::collectLocalSources(Evaluator eval)
} }
} }
void Opm::ParallelPAvgDynamicSourceData::synchroniseSources() template<class Scalar>
void Opm::ParallelPAvgDynamicSourceData<Scalar>::synchroniseSources()
{ {
this->comm_.get() this->comm_.get()
.allgatherv(this->localSrc_.data(), // Input (from) .allgatherv(this->localSrc_.data(), // Input (from)
@ -76,15 +80,16 @@ void Opm::ParallelPAvgDynamicSourceData::synchroniseSources()
this->startPointers_.data()); // Output offsets this->startPointers_.data()); // Output offsets
} }
std::vector<double>::size_type template<class Scalar>
Opm::ParallelPAvgDynamicSourceData:: typename std::vector<Scalar>::size_type
storageIndex(const std::vector<double>::size_type elemIndex) const Opm::ParallelPAvgDynamicSourceData<Scalar>::
storageIndex(const typename std::vector<Scalar>::size_type elemIndex) const
{ {
return this->storageIndex_[elemIndex]; return this->storageIndex_[elemIndex];
} }
void template<class Scalar>
Opm::ParallelPAvgDynamicSourceData:: void Opm::ParallelPAvgDynamicSourceData<Scalar>::
finaliseConstruction(const std::vector<std::size_t>& sourceLocations, finaliseConstruction(const std::vector<std::size_t>& sourceLocations,
GlobalToLocal localCellIdx) GlobalToLocal localCellIdx)
{ {
@ -100,18 +105,20 @@ finaliseConstruction(const std::vector<std::size_t>& sourceLocations,
ix += 1; ix += 1;
} }
this->localSrc_.assign(numSpanItems() * this->locations_.size(), 0.0); this->localSrc_.assign(this->numSpanItems() * this->locations_.size(), 0.0);
this->defineCommunication(); this->defineCommunication();
} }
Opm::PAvgDynamicSourceData<double>::SourceDataSpan<double> template<class Scalar>
Opm::ParallelPAvgDynamicSourceData::localSourceTerm(const std::size_t localIx) typename Opm::PAvgDynamicSourceData<Scalar>::template SourceDataSpan<Scalar>
Opm::ParallelPAvgDynamicSourceData<Scalar>::localSourceTerm(const std::size_t localIx)
{ {
return this->sourceTerm(localIx, this->localSrc_); return this->sourceTerm(localIx, this->localSrc_);
} }
void Opm::ParallelPAvgDynamicSourceData::defineCommunication() template<class Scalar>
void Opm::ParallelPAvgDynamicSourceData<Scalar>::defineCommunication()
{ {
// 1) Determine origins/owning ranks for all source terms. // 1) Determine origins/owning ranks for all source terms.
auto ixVec = std::vector<std::size_t>(this->locations_.size()); auto ixVec = std::vector<std::size_t>(this->locations_.size());
@ -119,7 +126,7 @@ void Opm::ParallelPAvgDynamicSourceData::defineCommunication()
ixVec.begin(), ixVec.begin(),
[](const auto& location) { return location.ix; }); [](const auto& location) { return location.ix; });
constexpr auto numItems = numSpanItems(); constexpr auto numItems = ParallelPAvgDynamicSourceData<Scalar>::numSpanItems();
const auto& [allIndices, allIxStart] = allGatherv(ixVec, this->comm_.get()); const auto& [allIndices, allIxStart] = allGatherv(ixVec, this->comm_.get());
@ -166,3 +173,5 @@ void Opm::ParallelPAvgDynamicSourceData::defineCommunication()
this->storageIndex_[elemIndex] = storageIx++; this->storageIndex_[elemIndex] = storageIx++;
} }
} }
template class Opm::ParallelPAvgDynamicSourceData<double>;

View File

@ -32,13 +32,15 @@ namespace Opm {
/// Dynamic source data for block-average pressure calculations. /// Dynamic source data for block-average pressure calculations.
/// Specialisation for parallel runs. /// Specialisation for parallel runs.
class ParallelPAvgDynamicSourceData : public PAvgDynamicSourceData<double> template<class Scalar>
class ParallelPAvgDynamicSourceData : public PAvgDynamicSourceData<Scalar>
{ {
public: public:
/// Translate globally unique, linearised Cartesian cell indices to /// Translate globally unique, linearised Cartesian cell indices to
/// local, on-rank, cell indices. Assumed to return a negative value /// local, on-rank, cell indices. Assumed to return a negative value
/// result if the input cell index is not owned by the current rank. /// result if the input cell index is not owned by the current rank.
using GlobalToLocal = std::function<int(const std::size_t)>; using GlobalToLocal = std::function<int(const std::size_t)>;
template<class T> using SourceDataSpan = typename PAvgDynamicSourceData<Scalar>::template SourceDataSpan<T>;
/// Collect source term contributions from local, on-rank, cell. /// Collect source term contributions from local, on-rank, cell.
/// ///
@ -49,7 +51,7 @@ public:
/// in which \c cellIndex is the local, on-rank, cell index in the range /// in which \c cellIndex is the local, on-rank, cell index in the range
/// 0 to #active cells on rank - 1. Function \c eval is expected to /// 0 to #active cells on rank - 1. Function \c eval is expected to
/// fill in/assign all \c sourceTerm items for this cell. /// fill in/assign all \c sourceTerm items for this cell.
using Evaluator = std::function<void(int, SourceDataSpan<double>)>; using Evaluator = std::function<void(int, SourceDataSpan<Scalar>)>;
/// Constructor /// Constructor
/// ///
@ -117,11 +119,11 @@ private:
std::vector<LocalLocation> locations_{}; std::vector<LocalLocation> locations_{};
/// Source data values owned by current rank. /// Source data values owned by current rank.
std::vector<double> localSrc_{}; std::vector<Scalar> localSrc_{};
/// Translation map from element index to storage index in /// Translation map from element index to storage index in
/// PAvgDynamicSourceData::src_. /// PAvgDynamicSourceData::src_.
std::vector<std::vector<double>::size_type> storageIndex_{}; std::vector<typename std::vector<Scalar>::size_type> storageIndex_{};
/// Receive size from all ranks (allgatherv()). /// Receive size from all ranks (allgatherv()).
std::vector<int> allSizes_{}; // Type int to meet API requirements. std::vector<int> allSizes_{}; // Type int to meet API requirements.
@ -136,8 +138,8 @@ private:
/// \param[in] elemIndex Source element index. /// \param[in] elemIndex Source element index.
/// ///
/// \return Storage (starting) index in PAvgDynamicSourceData::src_. /// \return Storage (starting) index in PAvgDynamicSourceData::src_.
[[nodiscard]] std::vector<double>::size_type [[nodiscard]] typename std::vector<Scalar>::size_type
storageIndex(std::vector<double>::size_type elemIndex) const override; storageIndex(typename std::vector<Scalar>::size_type elemIndex) const override;
/// Identify local source term elements on rank and build communication /// Identify local source term elements on rank and build communication
/// pattern for all source terms. /// pattern for all source terms.
@ -159,7 +161,7 @@ private:
/// \param[in] localIx Logical element index into \c localSrc_. /// \param[in] localIx Logical element index into \c localSrc_.
/// ///
/// \return Mutable view into \c localSrc_. /// \return Mutable view into \c localSrc_.
[[nodiscard]] SourceDataSpan<double> [[nodiscard]] SourceDataSpan<Scalar>
localSourceTerm(const std::size_t localIx); localSourceTerm(const std::size_t localIx);
/// Build communication pattern for all source terms. /// Build communication pattern for all source terms.

View File

@ -42,7 +42,6 @@
#include <cassert> #include <cassert>
#include <cstddef> #include <cstddef>
#include <memory> #include <memory>
#include <numeric>
#include <stdexcept> #include <stdexcept>
#include <tuple> #include <tuple>
#include <utility> #include <utility>
@ -97,7 +96,7 @@ Opm::ParallelWBPCalculation::SourceData::
buildStructure(const std::vector<std::size_t>& sourceLocations) buildStructure(const std::vector<std::size_t>& sourceLocations)
{ {
if (this->srcData_ == nullptr) { if (this->srcData_ == nullptr) {
this->srcData_ = std::make_unique<ParallelPAvgDynamicSourceData> this->srcData_ = std::make_unique<ParallelPAvgDynamicSourceData<double>>
(this->comm_, sourceLocations, this->localIdx_); (this->comm_, sourceLocations, this->localIdx_);
} }
else { else {

View File

@ -51,10 +51,10 @@ class ParallelWBPCalculation
public: public:
/// Callback for inferring the source locations which are active on the /// Callback for inferring the source locations which are active on the
/// current MPI rank. /// current MPI rank.
using GlobalToLocal = ParallelPAvgDynamicSourceData::GlobalToLocal; using GlobalToLocal = ParallelPAvgDynamicSourceData<double>::GlobalToLocal;
/// Callback for evaluating WBPn source terms on the current MPI rank. /// Callback for evaluating WBPn source terms on the current MPI rank.
using Evaluator = ParallelPAvgDynamicSourceData::Evaluator; using Evaluator = ParallelPAvgDynamicSourceData<double>::Evaluator;
/// Callback for constructing a source term evaluation function on the /// Callback for constructing a source term evaluation function on the
/// current MPI rank. Needed for deferred construction of per-well /// current MPI rank. Needed for deferred construction of per-well
@ -213,7 +213,7 @@ private:
/// ///
/// Enables transparent usage of this object as an argument to \code /// Enables transparent usage of this object as an argument to \code
/// PAvgCalculator::inferBlockAveragePressures() \endcode. /// PAvgCalculator::inferBlockAveragePressures() \endcode.
operator const ParallelPAvgDynamicSourceData&() const operator const ParallelPAvgDynamicSourceData<double>&() const
{ {
return *this->srcData_; return *this->srcData_;
} }
@ -294,7 +294,7 @@ private:
private: private:
/// Type of wrapped object. /// Type of wrapped object.
using DataPtr = std::unique_ptr<ParallelPAvgDynamicSourceData>; using DataPtr = std::unique_ptr<ParallelPAvgDynamicSourceData<double>>;
/// MPI communicator for this source data object. /// MPI communicator for this source data object.
std::reference_wrapper<const Parallel::Communication> comm_; std::reference_wrapper<const Parallel::Communication> comm_;

View File

@ -166,7 +166,7 @@ BOOST_AUTO_TEST_CASE(Eval_and_collect)
const auto num_src_loc = std::size_t{50}; const auto num_src_loc = std::size_t{50};
auto source_data = Opm::ParallelPAvgDynamicSourceData { auto source_data = Opm::ParallelPAvgDynamicSourceData<double> {
comm, sourceLocations(num_src_loc), comm, sourceLocations(num_src_loc),
LocalCellIndex { comm_rank, comm_size } LocalCellIndex { comm_rank, comm_size }
}; };