Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: MPI shared memory #51

Open
wants to merge 1 commit into
base: unstable
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions c++/nda/mem/allocators.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#include <numeric>
#include <concepts>

#include <mpi/mpi.hpp>

#include "address_space.hpp"
#include "../macros.hpp"

Expand All @@ -47,6 +49,7 @@ namespace nda::mem {
struct blk_t {
char *ptr = nullptr;
size_t s = 0;
void *userdata = nullptr;
};

// ------------------------- Malloc allocator ----------------------------
Expand Down Expand Up @@ -335,4 +338,47 @@ namespace nda::mem {
auto const &histogram() const noexcept { return hist; }
};

// ------------------------- MPI shared memory allocator ----------------------------
//
// Allocates the same amount of memory on each shared memory island
//
class shared_allocator {
public:
shared_allocator() = default;
shared_allocator(shared_allocator const &) = delete;
shared_allocator(shared_allocator &&) = default;
shared_allocator &operator=(shared_allocator const &) = delete;
shared_allocator &operator=(shared_allocator &&) = default;

static constexpr auto address_space = Host;

static blk_t allocate(size_t s) noexcept {
return allocate(s, mpi::communicator{}.split_shared());
}

static blk_t allocate(MPI_Aint s, mpi::shared_communicator shm) noexcept {
auto *win = new mpi::shared_window<char>{shm, shm.rank() == 0 ? s : 0};
return {(char *)win->base(0), (std::size_t)s, (void *)win}; // NOLINT
}

static blk_t allocate_zero(size_t s) noexcept {
return allocate_zero(s, mpi::communicator{}.split_shared());
}

static blk_t allocate_zero(MPI_Aint s, mpi::shared_communicator shm) noexcept {
auto *win = new mpi::shared_window<char>{shm, shm.rank() == 0 ? s : 0};
char *baseptr = win->base(0);
win->fence();
if (shm.rank() == 0) {
std::memset(baseptr, 0, s);
}
win->fence();
return {baseptr, (std::size_t)s, (void *)win}; // NOLINT
}

static void deallocate(blk_t b) noexcept {
delete static_cast<mpi::shared_window<char>*>(b.userdata);
}
};

} // namespace nda::mem
4 changes: 2 additions & 2 deletions deps/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ external_dependency(h5

# -- MPI --
external_dependency(mpi
GIT_REPO https://github.com/TRIQS/mpi
GIT_REPO https://github.com/hmenke/mpi
VERSION 1.2
GIT_TAG unstable
GIT_TAG shm
)

## Pybind 11
Expand Down
27 changes: 27 additions & 0 deletions test/c++/nda_mpi_shared.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) 2020-2023 Simons Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0.txt
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Authors: Olivier Parcollet, Nils Wentzell

#include "./test_common.hpp"

// ==============================================================

TEST(SHM, Allocator) { //NOLINT
if (!mpi::has_env) GTEST_SKIP() << "MPI is unavailable";
nda::basic_array<long, 2, C_layout, 'A', nda::heap_basic<nda::mem::shared_allocator>> A(3, 3);
EXPECT_EQ(A.shape(), (nda::shape_t<2>{3, 3}));
}

MAKE_MAIN_MPI
Loading