Skip to content

Commit d2559e0

Browse files
committed
Clean up mpi::shared_window
- update docs - remove noexcept when not necessary
1 parent 7a956e0 commit d2559e0

File tree

1 file changed

+52
-48
lines changed

1 file changed

+52
-48
lines changed

c++/mpi/window.hpp

Lines changed: 52 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -356,89 +356,93 @@ namespace mpi {
356356
};
357357

358358
/**
359-
* @brief A shared memory window abstraction using MPI.
360-
*
361-
* @details This class provides an interface for creating and managing an MPI shared memory window.
362-
*
363-
* @tparam BaseType The data type stored in the shared memory window.
364-
*/
359+
* @brief A C++ wrapper around `MPI_Win` representing a shared memory window.
360+
*
361+
* @details This class provides an interface for creating and managing an MPI shared memory window.
362+
*
363+
* @tparam BaseType The type of elements stored in the shared memory window.
364+
*/
365365
template <class BaseType> class shared_window : public window<BaseType> {
366366
public:
367-
/// Default constructor
367+
///Construct a shared memory window with `MPI_WIN_NULL`.
368368
shared_window() = default;
369369

370370
/**
371-
* @brief Constructs a shared memory window.
371+
* @brief Construct a shared memory window by dynamically allocating memory.
372372
*
373-
* @details This constructor allocates shared memory within the given communicator.
373+
* @details This constructor allocates a shared memory window within the given communicator by calling
374+
* `MPI_Win_allocate_shared`. The allocated memory is automatically freed when the window is destroyed.
374375
*
375-
* @param c The shared communicator.
376-
* @param size The number of elements of type @p BaseType to allocate.
377-
* @param info MPI_Info object for optimization hints.
376+
* @param c mpi::shared_communicator object.
377+
* @param sz Number of elements to allocate for the calling process.
378+
* @param info Additional MPI information.
378379
*/
379-
explicit shared_window(shared_communicator const &c, MPI_Aint size, MPI_Info info = MPI_INFO_NULL) noexcept {
380-
ASSERT(size >= 0)
380+
explicit shared_window(shared_communicator const &c, MPI_Aint sz, MPI_Info info = MPI_INFO_NULL) {
381+
ASSERT(sz >= 0)
382+
comm_ = c.get();
383+
size_ = sz;
381384
if (has_env) {
382-
void *baseptr = nullptr;
383-
MPI_Win_allocate_shared(size * sizeof(BaseType), sizeof(BaseType), info, c.get(), &baseptr, &(this->win_));
384-
this->comm_ = c.get();
385-
this->data_ = static_cast<BaseType *>(baseptr);
386-
this->size_ = size;
385+
check_mpi_call(MPI_Win_allocate_shared(size_ * sizeof(BaseType), sizeof(BaseType), info, c.get(), &data_, &win_), "MPI_Win_allocate_shared");
387386
} else {
388-
this->owned_ = true;
389-
this->comm_ = c.get();
390-
this->data_ = new BaseType[size];
391-
this->size_ = size;
387+
owned_ = true;
388+
data_ = new BaseType[size_]; // NOLINT (new is fine here)
392389
}
393390
}
394391

395392
/**
396-
* @brief Queries attributes of a shared memory window.
393+
* @brief Query attributes of a shared memory window.
397394
*
398-
* @details Retrieves the size, displacement unit, and base address of the shared memory region for a given rank.
395+
* @details Retrieves the size, displacement unit, and a pointer to the beginning of the shared memory region for a
396+
* specific rank.
399397
*
400-
* @param rank The rank within the communicator (defaults to @p MPI_PROC_NULL for querying all ranks).
401-
* @return A tuple containing (size in bytes, displacement unit, base pointer).
398+
* @param rank Rank within the shared communicator.
399+
* @return A tuple containing the size in bytes, the displacement unit in bytes and the base pointer.
402400
*/
403-
std::tuple<MPI_Aint, int, void *> query(int rank = MPI_PROC_NULL) const noexcept {
401+
[[nodiscard]] std::tuple<MPI_Aint, int, void *> query(int rank = MPI_PROC_NULL) const {
404402
if (has_env) {
405-
MPI_Aint size = 0;
406-
int disp_unit = 0;
403+
MPI_Aint sz = 0;
404+
int du = 0;
407405
void *baseptr = nullptr;
408-
MPI_Win_shared_query(this->win_, rank, &size, &disp_unit, &baseptr);
409-
return {size, disp_unit, baseptr};
406+
check_mpi_call(MPI_Win_shared_query(win_, rank, &sz, &du, &baseptr), "MPI_Win_shared_query");
407+
return {sz, du, baseptr};
410408
} else {
411-
return {this->size_ * sizeof(BaseType), sizeof(BaseType), this->data_};
409+
return {size_, sizeof(BaseType), data_};
412410
}
413411
}
414412

415-
// Override the commonly used attributes of the window base class
416-
417413
/**
418-
* @brief Returns the base address of the shared memory for a specific rank.
414+
* @brief Get a pointer to the beginning of the shared memory region of a specific rank.
419415
*
420-
* @param rank The rank whose base address should be retrieved.
421-
* @return A pointer to the base address.
416+
* @param rank Rank within the shared communicator.
417+
* @return Pointer to the shared window of the given rank.
422418
*/
423-
BaseType *base(int rank = MPI_PROC_NULL) const noexcept { return static_cast<BaseType *>(std::get<2>(query(rank))); }
419+
[[nodiscard]] BaseType *base(int rank = MPI_PROC_NULL) const { return static_cast<BaseType *>(std::get<2>(query(rank))); }
424420

425421
/**
426-
* @brief Returns the number of elements stored in the shared memory window.
422+
* @brief Get the size of the shared memory region of a specific rank.
427423
*
428-
* @param rank The rank whose memory size should be retrieved.
429-
* @return The number of elements in the shared window.
424+
* @param rank Rank within the shared communicator.
425+
* @return Number of elements in the shared window of the given rank.
430426
*/
431-
MPI_Aint size(int rank = MPI_PROC_NULL) const noexcept { return std::get<0>(query(rank)) / sizeof(BaseType); }
427+
[[nodiscard]] MPI_Aint size(int rank = MPI_PROC_NULL) const { return std::get<0>(query(rank)) / sizeof(BaseType); }
432428

433429
/**
434-
* @brief Returns the displacement unit of the shared memory.
430+
* @brief Get the displacement unit of the shared memory region of a specific rank.
435431
*
436-
* @param rank The rank whose displacement unit should be retrieved.
437-
* @return The displacement unit.
432+
* @param rank Rank within the shared communicator.
433+
* @return Displacement unit in bytes.
438434
*/
439-
int disp_unit(int rank = MPI_PROC_NULL) const noexcept { return std::get<1>(query(rank)); }
435+
[[nodiscard]] int disp_unit(int rank = MPI_PROC_NULL) const { return std::get<1>(query(rank)); }
436+
437+
/// Get the mpi::shared_communicator associated with the window.
438+
[[nodiscard]] shared_communicator get_communicator() const { return comm_.get(); }
440439

441-
shared_communicator get_communicator() { return this->comm_.get(); }
440+
private:
441+
using window<BaseType>::win_;
442+
using window<BaseType>::comm_;
443+
using window<BaseType>::owned_;
444+
using window<BaseType>::data_;
445+
using window<BaseType>::size_;
442446
};
443447

444448
/** @} */

0 commit comments

Comments
 (0)