/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 // Increasing memory space usage
69 memorySpace->increaseUsage(size);
70
71 // Creating new memory slot structure
72 auto newMemSlot = allocateLocalMemorySlotImpl(memorySpace, size);
73
74 // Returning the id of the new memory slot
75 return newMemSlot;
76 }
77
86 virtual std::shared_ptr<LocalMemorySlot> registerLocalMemorySlot(const std::shared_ptr<HiCR::MemorySpace> &memorySpace, void *const ptr, const size_t size)
87 {
88 // Increasing memory space usage
89 memorySpace->increaseUsage(size);
90
91 // Creating new memory slot structure
92 auto newMemSlot = registerLocalMemorySlotImpl(memorySpace, ptr, size);
93
94 // Returning the id of the new memory slot
95 return newMemSlot;
96 }
97
103 __INLINE__ void deregisterLocalMemorySlot(const std::shared_ptr<HiCR::LocalMemorySlot> &memorySlot)
104 {
105 // Decreasing memory space usage
106 memorySlot->getMemorySpace()->decreaseUsage(memorySlot->getSize());
107
108 // Calling internal implementation
110 }
111
125 __INLINE__ void memset(const std::shared_ptr<HiCR::LocalMemorySlot> &memorySlot, int value, size_t size)
126 {
127 // Checking whether the pointer is valid
128 if (memorySlot->getPointer() == nullptr) HICR_THROW_RUNTIME("Invalid memory slot provided. It either does not exist or represents a NULL pointer.");
129
130 memsetImpl(memorySlot, value, size);
131 }
132
138 __INLINE__ void freeLocalMemorySlot(const std::shared_ptr<HiCR::LocalMemorySlot> &memorySlot)
139 {
140 // Decreasing memory space usage
141 memorySlot->getMemorySpace()->decreaseUsage(memorySlot->getSize());
142
143 // Actually freeing up slot
144 freeLocalMemorySlotImpl(memorySlot);
145 }
146
147 protected:
148
156 virtual std::shared_ptr<HiCR::LocalMemorySlot> allocateLocalMemorySlotImpl(std::shared_ptr<HiCR::MemorySpace> memorySpace, const size_t size) = 0;
157
166 virtual std::shared_ptr<LocalMemorySlot> registerLocalMemorySlotImpl(std::shared_ptr<HiCR::MemorySpace> memorySpace, void *const ptr, const size_t size) = 0;
167
175 virtual void memsetImpl(const std::shared_ptr<HiCR::LocalMemorySlot> memorySlot, int value, size_t size)
176 {
177 // Default implementation: using standard memset. Almost all backends can use this so no reason to reimplement it everywhere.
178 std::memset(memorySlot->getPointer(), value, size);
179 }
180
186 virtual void freeLocalMemorySlotImpl(std::shared_ptr<HiCR::LocalMemorySlot> memorySlot) = 0;
187
193 virtual void deregisterLocalMemorySlotImpl(std::shared_ptr<HiCR::LocalMemorySlot> memorySlot) = 0;
194};
195
196} // namespace HiCR
Definition memoryManager.hpp:51
__INLINE__ void freeLocalMemorySlot(const std::shared_ptr< HiCR::LocalMemorySlot > &memorySlot)
Definition memoryManager.hpp:138
virtual std::shared_ptr< LocalMemorySlot > registerLocalMemorySlot(const std::shared_ptr< HiCR::MemorySpace > &memorySpace, void *const ptr, const size_t size)
Definition memoryManager.hpp:86
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:125
virtual void memsetImpl(const std::shared_ptr< HiCR::LocalMemorySlot > memorySlot, int value, size_t size)
Definition memoryManager.hpp:175
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:103
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