17#ifdef NF_WITH_MPI_SUPPORT
41template<
typename ValueType>
62 : exec_(rhs.exec_), value_(rhs.value_), refValue_(rhs.refValue_),
63 valueFraction_(rhs.valueFraction_), refGrad_(rhs.refGrad_),
64 boundaryTypes_(rhs.boundaryTypes_), offset_(rhs.offset_), nBoundaries_(rhs.nBoundaries_),
65 nBoundaryFaces_(rhs.nBoundaryFaces_)
77 : exec_((rhs.
waitAll(), rhs.exec_)), value_(std::move(rhs.value_)),
78 refValue_(std::move(rhs.refValue_)), valueFraction_(std::move(rhs.valueFraction_)),
79 refGrad_(std::move(rhs.refGrad_)), boundaryTypes_(std::move(rhs.boundaryTypes_)),
80 offset_(std::move(rhs.offset_)), nBoundaries_(rhs.nBoundaries_),
81 nBoundaryFaces_(rhs.nBoundaryFaces_)
90 : exec_(rhs.exec_), value_(
exec, rhs.value_), refValue_(
exec, rhs.refValue_),
91 valueFraction_(
exec, rhs.valueFraction_), refGrad_(
exec, rhs.refGrad_),
93 nBoundaries_(rhs.nBoundaries_), nBoundaryFaces_(rhs.nBoundaryFaces_)
108 offset_(
SerialExecutor {}, nBoundaryTypes + 1), nBoundaries_(nBoundaryTypes),
201 return offset_.data()[patchId + 1] - offset_.data()[patchId];
211 nBoundaries_ = rhs.nBoundaries_;
212 nBoundaryFaces_ = rhs.nBoundaryFaces_;
215 refValue_ = rhs.refValue_;
216 valueFraction_ = rhs.valueFraction_;
217 refGrad_ = rhs.refGrad_;
218 boundaryTypes_ = rhs.boundaryTypes_;
219 offset_ = rhs.offset_;
234 nBoundaries_ = rhs.nBoundaries_;
235 nBoundaryFaces_ = rhs.nBoundaryFaces_;
237 value_ = std::move(rhs.value_);
238 refValue_ = std::move(rhs.refValue_);
239 valueFraction_ = std::move(rhs.valueFraction_);
240 refGrad_ = std::move(rhs.refGrad_);
241 boundaryTypes_ = std::move(rhs.boundaryTypes_);
242 offset_ = std::move(rhs.offset_);
246#ifdef NF_WITH_MPI_SUPPORT
247 void communicate(std::pair<localIdx, localIdx>
range,
int neighborRank)
249 const auto [rangeStart, rangeEnd] =
range;
251 rangeEnd > rangeStart,
"range ending index must be larger than range starting index"
253 const localIdx patchSize = rangeEnd - rangeStart;
255 mpi::Environment mpiEnv;
257 buf.rangeStart = rangeStart;
258 buf.patchSize = patchSize;
260 const auto byteCount =
262 const auto neighborRankLabel =
static_cast<mpi_label_t>(neighborRank);
268 const auto nProcs =
static_cast<mpi_label_t>(mpiEnv.sizeRank());
269 const auto myRankLabel =
static_cast<mpi_label_t>(mpiEnv.rank());
270 const mpi_label_t pairTag = std::min(myRankLabel, neighborRankLabel) * nProcs
271 + std::max(myRankLabel, neighborRankLabel);
273 const bool useGpuPath = mpiEnv.gpuAwareMpi() && std::holds_alternative<GPUExecutor>(exec_);
275 MPI_Request sendReq, recvReq;
283 reinterpret_cast<const char*
>(value_.data() + rangeStart),
291 reinterpret_cast<char*
>(buf.deviceRecvBuf->data()),
301 auto valH = value_.copyToHost();
302 buf.sendBuf.resize(
static_cast<sizeType>(patchSize));
303 buf.recvBuf.resize(
static_cast<sizeType>(patchSize));
304 for (
localIdx k = 0; k < patchSize; k++)
305 buf.sendBuf[
static_cast<sizeType>(k)] = valH.view()[rangeStart + k];
307 reinterpret_cast<const char*
>(buf.sendBuf.data()),
315 reinterpret_cast<char*
>(buf.recvBuf.data()),
323 communicating_ =
true;
324 requests_.push_back(sendReq);
325 requests_.push_back(recvReq);
326 commBuffers_.push_back(std::move(buf));
329 bool isComplete()
const
331 if (requests_.empty() || !communicating_)
return true;
332 for (
auto& req : requests_)
334 if (!mpi::test(&req))
return false;
336 communicating_ =
false;
345#ifdef NF_WITH_MPI_SUPPORT
346 if (requests_.empty() || !communicating_)
return;
347 while (!isComplete())
350 mpi::Environment mpiEnv;
351 const bool useGpuPath = mpiEnv.gpuAwareMpi() && std::holds_alternative<GPUExecutor>(exec_);
354 for (
const auto& buf : commBuffers_)
356 auto srcView = buf.deviceRecvBuf->view();
357 auto dstView = value_.view();
358 const localIdx start = buf.rangeStart;
362 KOKKOS_LAMBDA(
const localIdx k) { dstView[start + k] = srcView[k]; }
369 else if (std::holds_alternative<GPUExecutor>(exec_))
375 auto valH = value_.copyToHost();
376 for (
const auto& buf : commBuffers_)
378 for (
localIdx k = 0; k < buf.patchSize; k++)
379 valH.view()[buf.rangeStart + k] = buf.recvBuf[
static_cast<sizeType>(k)];
382 value_ = backToDevice;
391 for (
const auto& buf : commBuffers_)
393 ValueType* dst = value_.data() + buf.rangeStart;
394 for (
localIdx k = 0; k < buf.patchSize; k++)
395 dst[k] = buf.recvBuf[
static_cast<sizeType>(k)];
399 communicating_ =
false;
400 commBuffers_.clear();
410 return {offset_.data()[patchId], offset_.data()[patchId + 1]};
445#ifdef NF_WITH_MPI_SUPPORT
448 std::vector<ValueType> sendBuf;
449 std::vector<ValueType> recvBuf;
450 std::optional<Vector<ValueType>> deviceRecvBuf;
454 mutable std::vector<MPI_Request>
456 mutable std::vector<CommBuffer>
458 mutable bool communicating_ =
false;
472 template<
typename ValueType>
475 return in.valueNoWait();
Represents the boundary fields for a computational domain.
std::pair< localIdx, localIdx > range(localIdx patchId) const
Get the range for a given patchId.
Vector< ValueType > & refValue()
Get the view storing the Dirichlet boundary values.
ValueType BoundaryDataType
sizeType nBoundaryFaces() const
Get the number of boundary faces.
const Vector< ValueType > & refValue() const
Get the view storing the Dirichlet boundary values.
const Vector< ValueType > & value() const
Get the view storing the computed values from the boundary condition.
BoundaryData(BoundaryData< ValueType > &&rhs)
Move constructor.
const Vector< int > & boundaryTypes() const
Get the view storing the boundary types.
Vector< scalar > & valueFraction()
Get the view storing the fraction of the boundary value.
BoundaryData(const Executor &exec, const BoundaryData< ValueType > &rhs)
Copy constructor.
friend struct NoWaitAccess
Grants NoWaitAccess (and only it) access to valueNoWait().
const Vector< localIdx > & offset() const
Get the view storing the offsets of each boundary.
sizeType nBoundaries() const
Get the number of boundaries.
const Vector< scalar > & valueFraction() const
Get the view storing the fraction of the boundary value.
BoundaryData(const Executor &exec, sizeType nBoundaryFaces, localIdx nBoundaryTypes)
constructor with default initialized Vectors from sizes.
BoundaryData(const Executor &exec, const std::vector< localIdx > &offsets)
constructor from a given offsets vector @warn all members except offsets are default constructed
Vector< ValueType > & value()
Get the view storing the computed values from the boundary condition.
const Vector< ValueType > & refGrad() const
Get the view storing the Neumann boundary values.
Vector< ValueType > & refGrad()
Get the view storing the Neumann boundary values.
BoundaryData(const BoundaryData< ValueType > &rhs)
Copy constructor.
sizeType nBoundaryFaces(localIdx patchId) const
Get the number of boundary faces for this patch.
BoundaryData< ValueType > & operator=(BoundaryData< ValueType > &&rhs)
BoundaryData< ValueType > & operator=(const BoundaryData< ValueType > &rhs)
Reference executor for serial CPU execution.
An executor-aware contiguous container for numerical data with vector-style arithmetic semantics.
Vector< ValueType > copyToExecutor(Executor dstExec) const
Creates a copy of the Vector on the specified executor.
Integer types used throughout NeoN.
std::variant< SerialExecutor, CPUExecutor, GPUExecutor > Executor
void fence(const Executor &exec)
void parallelFor(const ExecutorType &, std::pair< localIdx, localIdx > range, const Kernel &kernel, std::string name)
void validateInput(bool condition, std::string_view message, const std::source_location location=std::source_location::current())
Passkey granting non-draining access to BoundaryData's value storage.
static Vector< ValueType > & value(BoundaryData< ValueType > &in)