|
28 | 28 | #include "./memset.hpp"
|
29 | 29 | #include "../macros.hpp"
|
30 | 30 |
|
| 31 | +#include <mpi/mpi.hpp> |
| 32 | + |
31 | 33 | #include <algorithm>
|
32 | 34 | #include <cstddef>
|
33 | 35 | #include <cstdint>
|
@@ -61,6 +63,9 @@ namespace nda::mem {
|
61 | 63 |
|
62 | 64 | /// Size of the memory block in bytes.
|
63 | 65 | size_t s = 0;
|
| 66 | + |
| 67 | + /// Pointer to special data required by the allocator. |
| 68 | + void *userdata = nullptr; |
64 | 69 | };
|
65 | 70 |
|
66 | 71 | /**
|
@@ -653,6 +658,91 @@ namespace nda::mem {
|
653 | 658 | }
|
654 | 659 | };
|
655 | 660 |
|
| 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 | + |
656 | 746 | /** @} */
|
657 | 747 |
|
658 | 748 | } // namespace nda::mem
|
0 commit comments