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

HiCR: /home/runner/work/HiCR/HiCR/include/hicr/core/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
17/*
18 * Copyright Huawei Technologies Switzerland AG
19 * All rights reserved.
20 */
21
29#pragma once
30
31#include <map>
32#include <memory>
36#include <hicr/core/definitions.hpp>
38
39namespace HiCR
40{
41
51{
52 public:
53
57 virtual ~MemoryManager() = default;
58
66 __INLINE__ std::shared_ptr<LocalMemorySlot> allocateLocalMemorySlot(const std::shared_ptr<MemorySpace> &memorySpace, const size_t size)
67 {
68 // Creating new memory slot structure
69 auto newMemSlot = allocateLocalMemorySlotImpl(memorySpace, size);
70
71 // Returning the id of the new memory slot
72 return newMemSlot;
73 }
74
83 virtual std::shared_ptr<LocalMemorySlot> registerLocalMemorySlot(const std::shared_ptr<HiCR::MemorySpace> &memorySpace, void *const ptr, const size_t size)
84 {
85 // Creating new memory slot structure
86 auto newMemSlot = registerLocalMemorySlotImpl(memorySpace, ptr, size);
87
88 // Returning the id of the new memory slot
89 return newMemSlot;
90 }
91
97 __INLINE__ void deregisterLocalMemorySlot(const std::shared_ptr<HiCR::LocalMemorySlot> &memorySlot)
98 {
99 // Calling internal implementation
101 }
102
116 __INLINE__ void memset(const std::shared_ptr<HiCR::LocalMemorySlot> &memorySlot, int value, size_t size)
117 {
118 // Checking whether the pointer is valid
119 if (memorySlot->getPointer() == nullptr) HICR_THROW_RUNTIME("Invalid memory slot provided. It either does not exist or represents a NULL pointer.");
120
121 memsetImpl(memorySlot, value, size);
122 }
123
129 __INLINE__ void freeLocalMemorySlot(const std::shared_ptr<HiCR::LocalMemorySlot> &memorySlot)
130 {
131 // Actually freeing up slot
132 freeLocalMemorySlotImpl(memorySlot);
133 }
134
135 protected:
136
144 virtual std::shared_ptr<HiCR::LocalMemorySlot> allocateLocalMemorySlotImpl(std::shared_ptr<HiCR::MemorySpace> memorySpace, const size_t size) = 0;
145
154 virtual std::shared_ptr<LocalMemorySlot> registerLocalMemorySlotImpl(std::shared_ptr<HiCR::MemorySpace> memorySpace, void *const ptr, const size_t size) = 0;
155
163 virtual void memsetImpl(const std::shared_ptr<HiCR::LocalMemorySlot> memorySlot, int value, size_t size)
164 {
165 // Default implementation: using standard memset. Almost all backends can use this so no reason to reimplement it everywhere.
166 std::memset(memorySlot->getPointer(), value, size);
167 }
168
174 virtual void freeLocalMemorySlotImpl(std::shared_ptr<HiCR::LocalMemorySlot> memorySlot) = 0;
175
181 virtual void deregisterLocalMemorySlotImpl(std::shared_ptr<HiCR::LocalMemorySlot> memorySlot) = 0;
182};
183
184} // namespace HiCR
Definition memoryManager.hpp:51
__INLINE__ void freeLocalMemorySlot(const std::shared_ptr< HiCR::LocalMemorySlot > &memorySlot)
Definition memoryManager.hpp:129
virtual std::shared_ptr< LocalMemorySlot > registerLocalMemorySlot(const std::shared_ptr< HiCR::MemorySpace > &memorySpace, void *const ptr, const size_t size)
Definition memoryManager.hpp:83
virtual void freeLocalMemorySlotImpl(std::shared_ptr< HiCR::LocalMemorySlot > memorySlot)=0
__INLINE__ std::shared_ptr< LocalMemorySlot > allocateLocalMemorySlot(const std::shared_ptr< MemorySpace > &memorySpace, const size_t size)
Definition memoryManager.hpp:66
virtual std::shared_ptr< HiCR::LocalMemorySlot > allocateLocalMemorySlotImpl(std::shared_ptr< HiCR::MemorySpace > memorySpace, const size_t size)=0
virtual ~MemoryManager()=default
__INLINE__ void memset(const std::shared_ptr< HiCR::LocalMemorySlot > &memorySlot, int value, size_t size)
Definition memoryManager.hpp:116
virtual void memsetImpl(const std::shared_ptr< HiCR::LocalMemorySlot > memorySlot, int value, size_t size)
Definition memoryManager.hpp:163
virtual void deregisterLocalMemorySlotImpl(std::shared_ptr< HiCR::LocalMemorySlot > memorySlot)=0
virtual std::shared_ptr< LocalMemorySlot > registerLocalMemorySlotImpl(std::shared_ptr< HiCR::MemorySpace > memorySpace, void *const ptr, const size_t size)=0
__INLINE__ void deregisterLocalMemorySlot(const std::shared_ptr< HiCR::LocalMemorySlot > &memorySlot)
Definition memoryManager.hpp:97
Provides a definition for a HiCR Global Memory Slot class.
Provides a definition for a HiCR Local Memory Slot class.
Provides a base definition for a HiCR MemorySpace class.
Provides a failure model and corresponding exception classes.
#define HICR_THROW_RUNTIME(...)
Definition exceptions.hpp:74