Skip to content

Commit 0bc73fa

Browse files
committed
Add move constructors to windows
1 parent 879dc69 commit 0bc73fa

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

c++/mpi/mpi.hpp

+18-7
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <complex>
2424
#include <type_traits>
2525
#include <algorithm>
26+
#include <utility>
2627
#include <unistd.h>
2728

2829
/// Library namespace
@@ -364,9 +365,15 @@ namespace mpi {
364365
public:
365366
window() = default;
366367
window(window const&) = delete;
367-
window(window &&) = delete;
368+
window(window &&other) noexcept : win{std::exchange(other.win, MPI_WIN_NULL)} {}
368369
window& operator=(window const&) = delete;
369-
window& operator=(window &&) = delete;
370+
window& operator=(window &&rhs) noexcept {
371+
if (this != std::addressof(rhs)) {
372+
this->free();
373+
this->win = std::exchange(rhs.win, MPI_WIN_NULL);
374+
}
375+
return *this;
376+
}
370377

371378
/// Create a window over an existing local memory buffer
372379
explicit window(communicator &c, BaseType *base, MPI_Aint size = 0) noexcept {
@@ -379,15 +386,17 @@ namespace mpi {
379386
MPI_Win_allocate(size * sizeof(BaseType), alignof(BaseType), MPI_INFO_NULL, c.get(), &baseptr, &win);
380387
}
381388

382-
~window() {
389+
~window() { free(); }
390+
391+
explicit operator MPI_Win() const noexcept { return win; };
392+
explicit operator MPI_Win*() noexcept { return &win; };
393+
394+
void free() noexcept {
383395
if (win != MPI_WIN_NULL) {
384396
MPI_Win_free(&win);
385397
}
386398
}
387399

388-
explicit operator MPI_Win() const noexcept { return win; };
389-
explicit operator MPI_Win*() noexcept { return &win; };
390-
391400
/// Synchronization routine in active target RMA. It opens and closes an access epoch.
392401
void fence(int assert = 0) const noexcept {
393402
MPI_Win_fence(assert, win);
@@ -474,8 +483,10 @@ namespace mpi {
474483
template <class BaseType>
475484
class shared_window : public window<BaseType> {
476485
public:
486+
shared_window() = default;
487+
477488
/// Create a window and allocate memory for a shared memory buffer
478-
shared_window(shared_communicator& c, MPI_Aint size) noexcept {
489+
explicit shared_window(shared_communicator& c, MPI_Aint size) noexcept {
479490
void* baseptr = nullptr;
480491
MPI_Win_allocate_shared(size * sizeof(BaseType), alignof(BaseType), MPI_INFO_NULL, c.get(), &baseptr, &(this->win));
481492
}

0 commit comments

Comments
 (0)