Skip to content

Commit 5a66c89

Browse files
committed
WIP: MPI shared memory
1 parent 49e0be2 commit 5a66c89

File tree

3 files changed

+124
-3
lines changed

3 files changed

+124
-3
lines changed

c++/nda/mem/allocators.hpp

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
#include "./memset.hpp"
2929
#include "../macros.hpp"
3030

31+
#include <mpi/mpi.hpp>
32+
3133
#include <algorithm>
3234
#include <cstddef>
3335
#include <cstdint>
@@ -61,6 +63,9 @@ namespace nda::mem {
6163

6264
/// Size of the memory block in bytes.
6365
size_t s = 0;
66+
67+
/// Pointer to special data required by the allocator.
68+
void *userdata = nullptr;
6469
};
6570

6671
/**
@@ -653,6 +658,91 @@ namespace nda::mem {
653658
}
654659
};
655660

661+
/**
662+
* @brief Custom allocator that uses mpi::shared_window to allocate memory.
663+
* @tparam AdrSp nda::mem::AddressSpace in which the memory is allocated.
664+
*
665+
* Allocates the same amount of memory on each shared memory island.
666+
*/
667+
class shared_allocator {
668+
public:
669+
/// Default constructor.
670+
shared_allocator() = default;
671+
672+
/// Deleted copy constructor.
673+
shared_allocator(shared_allocator const &) = delete;
674+
675+
/// Default move constructor.
676+
shared_allocator(shared_allocator &&) = default;
677+
678+
/// Deleted copy assignment operator.
679+
shared_allocator &operator=(shared_allocator const &) = delete;
680+
681+
/// Default move assignment operator.
682+
shared_allocator &operator=(shared_allocator &&) = default;
683+
684+
/// MPI shared memory always lives in the Host address space.
685+
static constexpr auto address_space = Host;
686+
687+
/**
688+
* @brief Allocate memory using mpi::shared_window.
689+
*
690+
* @param s Size in bytes of the memory to allocate.
691+
* @return nda::mem::blk_t memory block.
692+
*/
693+
static blk_t allocate(size_t s) noexcept {
694+
return allocate(s, mpi::communicator{}.split_shared());
695+
}
696+
697+
/**
698+
* @brief Allocate memory using mpi::shared_window.
699+
*
700+
* @param s Size in bytes of the memory to allocate.
701+
* @param shm MPI shared memory communicator.
702+
* @return nda::mem::blk_t memory block.
703+
*/
704+
static blk_t allocate(MPI_Aint s, mpi::shared_communicator shm) noexcept {
705+
auto *win = new mpi::shared_window<char>{shm, shm.rank() == 0 ? s : 0};
706+
return {(char *)win->base(0), (std::size_t)s, (void *)win}; // NOLINT
707+
}
708+
709+
/**
710+
* @brief Allocate memory and set it to zero.
711+
*
712+
* @param s Size in bytes of the memory to allocate.
713+
* @return nda::mem::blk_t memory block.
714+
*/
715+
static blk_t allocate_zero(size_t s) noexcept {
716+
return allocate_zero(s, mpi::communicator{}.split_shared());
717+
}
718+
719+
/**
720+
* @brief Allocate memory and set it to zero.
721+
*
722+
* @param s Size in bytes of the memory to allocate.
723+
* @param shm MPI shared memory communicator.
724+
* @return nda::mem::blk_t memory block.
725+
*/
726+
static blk_t allocate_zero(MPI_Aint s, mpi::shared_communicator shm) noexcept {
727+
auto *win = new mpi::shared_window<char>{shm, shm.rank() == 0 ? s : 0};
728+
char *baseptr = win->base(0);
729+
win->fence();
730+
if (shm.rank() == 0) {
731+
std::memset(baseptr, 0, s);
732+
}
733+
win->fence();
734+
return {baseptr, (std::size_t)s, (void *)win}; // NOLINT
735+
}
736+
737+
/**
738+
* @brief Deallocate memory using mpi::shared_window.
739+
* @param b nda::mem::blk_t memory block to deallocate.
740+
*/
741+
static void deallocate(blk_t b) noexcept {
742+
delete static_cast<mpi::shared_window<char>*>(b.userdata);
743+
}
744+
};
745+
656746
/** @} */
657747

658748
} // namespace nda::mem

deps/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ external_dependency(h5
9292

9393
# -- MPI --
9494
external_dependency(mpi
95-
GIT_REPO https://github.com/TRIQS/mpi
96-
VERSION 1.3
97-
GIT_TAG unstable
95+
GIT_REPO https://github.com/hmenke/mpi
96+
VERSION 1.2
97+
GIT_TAG shm
9898
)
9999

100100
## Pybind 11

test/c++/nda_mpi_shared.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) 2020-2023 Simons Foundation
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0.txt
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
// Authors: Olivier Parcollet, Nils Wentzell
16+
17+
#define NDA_DEBUG_LEAK_CHECK
18+
19+
#include "./test_common.hpp"
20+
21+
#include <nda/basic_array.hpp>
22+
#include <nda/mem.hpp>
23+
24+
// ==============================================================
25+
26+
TEST(SHM, Allocator) { //NOLINT
27+
nda::basic_array<long, 2, nda::C_layout, 'A', nda::heap_basic<nda::mem::shared_allocator>> A(3, 3);
28+
EXPECT_EQ(A.shape(), (shape_t<2>{3, 3}));
29+
}
30+
31+
MPI_TEST_MAIN;

0 commit comments

Comments
 (0)