Skip to content

Commit 322fa64

Browse files
committed
WIP: MPI shared memory
1 parent a35a31f commit 322fa64

File tree

3 files changed

+75
-2
lines changed

3 files changed

+75
-2
lines changed

c++/nda/mem/allocators.hpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#include <numeric>
2626
#include <concepts>
2727

28+
#include <mpi/mpi.hpp>
29+
2830
#include "address_space.hpp"
2931
#include "../macros.hpp"
3032

@@ -47,6 +49,7 @@ namespace nda::mem {
4749
struct blk_t {
4850
char *ptr = nullptr;
4951
size_t s = 0;
52+
void *userdata = nullptr;
5053
};
5154

5255
// ------------------------- Malloc allocator ----------------------------
@@ -335,4 +338,47 @@ namespace nda::mem {
335338
auto const &histogram() const noexcept { return hist; }
336339
};
337340

341+
// ------------------------- MPI shared memory allocator ----------------------------
342+
//
343+
// Allocates the same amount of memory on each shared memory island
344+
//
345+
class shared_allocator {
346+
public:
347+
shared_allocator() = default;
348+
shared_allocator(shared_allocator const &) = delete;
349+
shared_allocator(shared_allocator &&) = default;
350+
shared_allocator &operator=(shared_allocator const &) = delete;
351+
shared_allocator &operator=(shared_allocator &&) = default;
352+
353+
static constexpr auto address_space = Host;
354+
355+
static blk_t allocate(size_t s) noexcept {
356+
return allocate(s, mpi::communicator{}.split_shared());
357+
}
358+
359+
static blk_t allocate(MPI_Aint s, mpi::shared_communicator shm) noexcept {
360+
auto *win = new mpi::shared_window<char>{shm, shm.rank() == 0 ? s : 0};
361+
return {(char *)win->base(0), (std::size_t)s, (void *)win}; // NOLINT
362+
}
363+
364+
static blk_t allocate_zero(size_t s) noexcept {
365+
return allocate_zero(s, mpi::communicator{}.split_shared());
366+
}
367+
368+
static blk_t allocate_zero(MPI_Aint s, mpi::shared_communicator shm) noexcept {
369+
auto *win = new mpi::shared_window<char>{shm, shm.rank() == 0 ? s : 0};
370+
char *baseptr = win->base(0);
371+
win->fence();
372+
if (shm.rank() == 0) {
373+
std::memset(baseptr, 0, s);
374+
}
375+
win->fence();
376+
return {baseptr, (std::size_t)s, (void *)win}; // NOLINT
377+
}
378+
379+
static void deallocate(blk_t b) noexcept {
380+
delete static_cast<mpi::shared_window<char>*>(b.userdata);
381+
}
382+
};
383+
338384
} // namespace nda::mem

deps/CMakeLists.txt

Lines changed: 2 additions & 2 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
95+
GIT_REPO https://github.com/hmenke/mpi
9696
VERSION 1.2
97-
GIT_TAG unstable
97+
GIT_TAG shm
9898
)
9999

100100
## Pybind 11

test/c++/nda_mpi_shared.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
#include "./test_common.hpp"
18+
19+
// ==============================================================
20+
21+
TEST(SHM, Allocator) { //NOLINT
22+
if (!mpi::has_env()) GTEST_SKIP() << "MPI is unavailable";
23+
nda::basic_array<long, 2, C_layout, 'A', nda::heap_basic<nda::mem::shared_allocator>> A(3, 3);
24+
EXPECT_EQ(A.shape(), (nda::shape_t<2>{3, 3}));
25+
}
26+
27+
MAKE_MAIN_MPI

0 commit comments

Comments
 (0)