/home/runner/work/HiCR/HiCR/include/hicr/backends/mpi/memoryManager.hpp Source File

HiCR: /home/runner/work/HiCR/HiCR/include/hicr/backends/mpi/memoryManager.hpp Source File
HiCR
memoryManager.hpp
Go to the documentation of this file.
1/*
2 * Copyright 2025 Huawei Technologies Co., Ltd.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
24#pragma once
25
26#include <mpi.h>
27#include <hicr/core/definitions.hpp>
30#include "localMemorySlot.hpp"
31
32namespace HiCR::backend::mpi
33{
34
41{
42 public:
43
48 : HiCR::MemoryManager()
49 {}
50 ~MemoryManager() override = default;
51
60 __INLINE__ std::shared_ptr<HiCR::LocalMemorySlot> allocateLocalMemorySlotImpl(std::shared_ptr<HiCR::MemorySpace> memorySpace, const size_t size) override
61 {
62 // Getting up-casted pointer for the MPI instance
63 auto m = dynamic_pointer_cast<hwloc::MemorySpace>(memorySpace);
64
65 // Checking whether the execution unit passed is compatible with this backend
66 if (m == nullptr) HICR_THROW_LOGIC("The passed memory space is not supported by this memory manager\n");
67
68 // Storage for the new pointer
69 void *ptr = nullptr;
70
71 // Attempting to allocate the new memory slot
72 auto status = MPI_Alloc_mem((MPI_Aint)size, MPI_INFO_NULL, &ptr);
73
74 // Check whether it was successful
75 if (status != MPI_SUCCESS || ptr == nullptr) HICR_THROW_RUNTIME("Could not allocate memory of size %lu", size);
76
77 // Creating and returning new memory slot
78 return registerLocalMemorySlotImpl(memorySpace, ptr, size);
79 }
80
89 __INLINE__ void freeLocalMemorySlotImpl(std::shared_ptr<HiCR::LocalMemorySlot> memorySlot) override {}
90
91 __INLINE__ std::shared_ptr<HiCR::LocalMemorySlot> registerLocalMemorySlotImpl(std::shared_ptr<HiCR::MemorySpace> memorySpace, void *const ptr, const size_t size) override
92 {
93 // Creating new memory slot object
94 auto memorySlot = std::make_shared<HiCR::backend::mpi::LocalMemorySlot>(ptr, size, memorySpace);
95
96 // Returning new memory slot pointer
97 return memorySlot;
98 }
99
105 __INLINE__ void deregisterLocalMemorySlotImpl(std::shared_ptr<HiCR::LocalMemorySlot> memorySlot) override
106 {
107 // Nothing to do here for this backend
108 }
109};
110
111} // namespace HiCR::backend::mpi
This file implements the memory space class for the HWLoc-based backend.
Definition memoryManager.hpp:51
Definition memoryManager.hpp:41
__INLINE__ std::shared_ptr< HiCR::LocalMemorySlot > registerLocalMemorySlotImpl(std::shared_ptr< HiCR::MemorySpace > memorySpace, void *const ptr, const size_t size) override
Definition memoryManager.hpp:91
__INLINE__ void freeLocalMemorySlotImpl(std::shared_ptr< HiCR::LocalMemorySlot > memorySlot) override
Definition memoryManager.hpp:89
__INLINE__ void deregisterLocalMemorySlotImpl(std::shared_ptr< HiCR::LocalMemorySlot > memorySlot) override
Definition memoryManager.hpp:105
MemoryManager()
Definition memoryManager.hpp:47
~MemoryManager() override=default
__INLINE__ std::shared_ptr< HiCR::LocalMemorySlot > allocateLocalMemorySlotImpl(std::shared_ptr< HiCR::MemorySpace > memorySpace, const size_t size) override
Definition memoryManager.hpp:60
Provides a definition for a HiCR Local Memory Slot class.
Provides a definition for the base backend's memory manager class.
#define HICR_THROW_RUNTIME(...)
Definition exceptions.hpp:74
#define HICR_THROW_LOGIC(...)
Definition exceptions.hpp:67