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

HiCR: /home/runner/work/HiCR/HiCR/include/hicr/backends/lpf/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
25#pragma once
26
27#include <cstring>
28#include <lpf/collectives.h>
29#include <lpf/core.h>
32#include "localMemorySlot.hpp"
33
34namespace HiCR::backend::lpf
35{
36
43{
44 private:
45
46 const lpf_t _lpf;
47
52 public:
53
63 MemoryManager(lpf_t lpf)
64 : HiCR::MemoryManager(),
65 _lpf(lpf)
66 {}
67
76 __INLINE__ std::shared_ptr<HiCR::LocalMemorySlot> registerLocalMemorySlotImpl(std::shared_ptr<HiCR::MemorySpace> memorySpace, void *const ptr, const size_t size) override
77 {
78 // Getting up-casted pointer for the MPI instance
79 auto m = dynamic_pointer_cast<hwloc::MemorySpace>(memorySpace);
80
81 // Checking whether the execution unit passed is compatible with this backend
82 if (m == nullptr) HICR_THROW_LOGIC("The passed memory space is not supported by this memory manager\n");
83
84 lpf_memslot_t lpfSlot = LPF_INVALID_MEMSLOT;
85 auto rc = lpf_register_local(_lpf, ptr, size, &lpfSlot);
86 if (rc != LPF_SUCCESS) HICR_THROW_RUNTIME("LPF Memory Manager: lpf_register_local failed");
87
88 // Creating new memory slot object
89 auto memorySlot = std::make_shared<lpf::LocalMemorySlot>(lpfSlot, ptr, size, memorySpace);
90
91 return memorySlot;
92 }
93
99 __INLINE__ void deregisterLocalMemorySlotImpl(std::shared_ptr<HiCR::LocalMemorySlot> memorySlot) override
100 {
101 // Getting up-casted pointer for the memory slot
102 auto slot = dynamic_pointer_cast<lpf::LocalMemorySlot>(memorySlot);
103
104 // Checking whether the memory slot is compatible with this backend
105 if (slot == nullptr) HICR_THROW_LOGIC("The passed memory slot is not supported by this backend\n");
106
107 // Deregistering memory slot from the LPF backend
108 lpf_deregister(_lpf, slot->getLPFSlot());
109 }
110
118 __INLINE__ std::shared_ptr<HiCR::LocalMemorySlot> allocateLocalMemorySlotImpl(std::shared_ptr<HiCR::MemorySpace> memorySpace, const size_t size) override
119 {
120 // Getting up-casted pointer for the LPF instance
121 auto m = dynamic_pointer_cast<hwloc::MemorySpace>(memorySpace);
122
123 // Checking whether the execution unit passed is compatible with this backend
124 if (m == nullptr) HICR_THROW_LOGIC("The passed memory space is not supported by this memory manager\n");
125
126 // Storage for the new pointer
127 const size_t MIN_BYTES = 32;
128 const auto newSize = std::max(size, MIN_BYTES);
129 void *ptr = malloc(newSize);
130 if (ptr == nullptr) HICR_THROW_RUNTIME("Could not allocate memory of size %lu", newSize);
131
132 // Update the memory usage for the memory space
133 memorySpace->increaseUsage(newSize - size);
134 // Creating and returning new memory slot
135 return registerLocalMemorySlotImpl(memorySpace, ptr, newSize);
136 }
137
143 __INLINE__ void freeLocalMemorySlotImpl(std::shared_ptr<HiCR::LocalMemorySlot> memorySlot) override
144 {
145 // Getting up-casted pointer for the memory slot
146 auto slot = dynamic_pointer_cast<lpf::LocalMemorySlot>(memorySlot);
147
148 // Checking whether the memory slot is compatible with this backend
149 if (slot == nullptr) HICR_THROW_LOGIC("The passed memory slot is not supported by this backend\n");
150
151 // Getting memory slot pointer
152 const auto pointer = slot->getPointer();
153
154 // Checking whether the pointer is valid
155 if (slot == nullptr) HICR_THROW_RUNTIME("Invalid memory slot(s) provided. It either does not exist or represents a NULL pointer.");
156
157 // First, deregistering LPF memory slot
159
160 // Deallocating memory
161 free(pointer);
162 }
163};
164
165} // namespace HiCR::backend::lpf
This file implements the memory space class for the HWLoc-based backend.
Definition memoryManager.hpp:51
Definition memoryManager.hpp:43
__INLINE__ void deregisterLocalMemorySlotImpl(std::shared_ptr< HiCR::LocalMemorySlot > memorySlot) override
Definition memoryManager.hpp:99
__INLINE__ std::shared_ptr< HiCR::LocalMemorySlot > allocateLocalMemorySlotImpl(std::shared_ptr< HiCR::MemorySpace > memorySpace, const size_t size) override
Definition memoryManager.hpp:118
MemoryManager(lpf_t lpf)
Definition memoryManager.hpp:63
__INLINE__ std::shared_ptr< HiCR::LocalMemorySlot > registerLocalMemorySlotImpl(std::shared_ptr< HiCR::MemorySpace > memorySpace, void *const ptr, const size_t size) override
Definition memoryManager.hpp:76
__INLINE__ void freeLocalMemorySlotImpl(std::shared_ptr< HiCR::LocalMemorySlot > memorySlot) override
Definition memoryManager.hpp:143
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