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

HiCR: /home/runner/work/HiCR/HiCR/include/hicr/backends/pthreads/core.hpp Source File
HiCR
core.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 <map>
27#include <pthread.h>
28
32
33namespace HiCR::backend::pthreads
34{
35
41class Core
42{
43 public:
44
50 Core(const size_t instanceCount)
51 : _fenceCount(instanceCount),
52 _currentInstanceId(0)
53 {
54 // Init barrier
55 pthread_barrier_init(&_barrier, nullptr, _fenceCount);
56
57 // Init mutex
58 pthread_mutex_init(&_globalSlotMutex, nullptr);
59 pthread_mutex_init(&_instanceMutex, nullptr);
60 }
61
62 ~Core()
63 {
64 // Destroy barrier
65 pthread_barrier_destroy(&_barrier);
66
67 // Destroy mutex
68 pthread_mutex_destroy(&_globalSlotMutex);
69 pthread_mutex_destroy(&_instanceMutex);
70 }
71
81 __INLINE__ void insertGlobalSlot(const GlobalMemorySlot::tag_t tag, const GlobalMemorySlot::globalKey_t key, const std::shared_ptr<GlobalMemorySlot> &slot)
82 {
83 // Lock the resource
84 pthread_mutex_lock(&_globalSlotMutex);
85
86 // Add the memory slot
87 _globalMemorySlots[tag][key] = slot;
88
89 // Unlock the resource
90 pthread_mutex_unlock(&_globalSlotMutex);
91 }
92
103 __INLINE__ std::shared_ptr<GlobalMemorySlot> getGlobalSlot(const GlobalMemorySlot::tag_t tag, const GlobalMemorySlot::globalKey_t key) const
104 {
105 std::shared_ptr<GlobalMemorySlot> value = nullptr;
106
107 // Lock the resource
108 pthread_mutex_lock(&_globalSlotMutex);
109
110 // Look for the tag
111 if (_globalMemorySlots.find(tag) != _globalMemorySlots.end())
112 {
113 // Look for the key
114 if (_globalMemorySlots.at(tag).find(key) != _globalMemorySlots.at(tag).end()) { value = _globalMemorySlots.at(tag).at(key); }
115 }
116
117 // Unlock the resource
118 pthread_mutex_unlock(&_globalSlotMutex);
119
120 return value;
121 }
122
132 {
133 // Lock the resource
134 pthread_mutex_lock(&_globalSlotMutex);
135
136 // Look for the tag
137 if (_globalMemorySlots.find(tag) != _globalMemorySlots.end())
138 {
139 // Look for the key
140 if (_globalMemorySlots[tag].find(key) != _globalMemorySlots[tag].end()) { _globalMemorySlots[tag].erase(key); }
141 }
142
143 // Unlock the resource
144 pthread_mutex_unlock(&_globalSlotMutex);
145 }
146
157 {
158 // Lock the resource
159 pthread_mutex_lock(&_globalSlotMutex);
160
161 // Search for the tag
162 auto it = _globalMemorySlots.find(tag);
163
164 // Fail if not found
165 if (it == _globalMemorySlots.end())
166 {
167 // Unlock the resource
168 pthread_mutex_unlock(&_globalSlotMutex);
169
170 // Return empty map
171 return {};
172 }
173
174 // Unlock the resource
175 pthread_mutex_unlock(&_globalSlotMutex);
176
177 // Return key slot pairs
178 return it->second;
179 }
180
184 __INLINE__ void fence() { pthread_barrier_wait(&_barrier); }
185
194 {
195 // Storage for the instance list
197
198 // Lock the instances
199 pthread_mutex_lock(&_instanceMutex);
200
201 // Populate list
202 for (const auto &[_, i] : _pthreadsInstanceMap) { instances.push_back(i); }
203
204 // Unlock the instances
205 pthread_mutex_unlock(&_instanceMutex);
206
207 return instances;
208 }
209
219 __INLINE__ std::shared_ptr<Instance> addInstance(const pthread_t pthreadId)
220 {
221 // Lock the instances
222 pthread_mutex_lock(&_instanceMutex);
223
224 // Create the new instance
225 auto instance = std::make_shared<backend::pthreads::Instance>(_currentInstanceId++, pthreadId, 0);
226
227 // Add it to the map
228 _pthreadsInstanceMap[pthreadId] = instance;
229
230 // Unlock the instances
231 pthread_mutex_unlock(&_instanceMutex);
232
233 return instance;
234 }
235
245 __INLINE__ std::shared_ptr<Instance> getInstance(const pthread_t pthreadId) const
246 {
247 pthread_mutex_lock(&_instanceMutex);
248 auto instance = _pthreadsInstanceMap.at(pthreadId);
249 pthread_mutex_unlock(&_instanceMutex);
250 return instance;
251 }
252
258 __INLINE__ Instance::instanceId_t getRootInstanceId() const { return 0; }
259
260 private:
261
265 pthread_barrier_t _barrier{};
266
272 mutable pthread_mutex_t _globalSlotMutex{};
273
279 mutable pthread_mutex_t _instanceMutex{};
280
284 const size_t _fenceCount;
285
290
294 Instance::instanceId_t _currentInstanceId;
295
299 std::map<pthread_t, std::shared_ptr<Instance>> _pthreadsInstanceMap;
300};
301} // namespace HiCR::backend::pthreads
This file implements the instance class for the Pthreads backend.
std::map< GlobalMemorySlot::tag_t, globalKeyToMemorySlotMap_t > globalMemorySlotTagKeyMap_t
Definition communicationManager.hpp:70
std::map< GlobalMemorySlot::globalKey_t, std::shared_ptr< GlobalMemorySlot > > globalKeyToMemorySlotMap_t
Definition communicationManager.hpp:65
uint64_t tag_t
Definition globalMemorySlot.hpp:49
uint64_t globalKey_t
Definition globalMemorySlot.hpp:44
std::vector< std::shared_ptr< HiCR::Instance > > instanceList_t
Definition instanceManager.hpp:63
uint64_t instanceId_t
Definition instance.hpp:44
Definition core.hpp:42
__INLINE__ InstanceManager::instanceList_t getInstances() const
Definition core.hpp:193
__INLINE__ CommunicationManager::globalKeyToMemorySlotMap_t getKeyMemorySlots(const GlobalMemorySlot::tag_t tag) const
Definition core.hpp:156
Core(const size_t instanceCount)
Definition core.hpp:50
__INLINE__ std::shared_ptr< Instance > addInstance(const pthread_t pthreadId)
Definition core.hpp:219
__INLINE__ std::shared_ptr< Instance > getInstance(const pthread_t pthreadId) const
Definition core.hpp:245
__INLINE__ std::shared_ptr< GlobalMemorySlot > getGlobalSlot(const GlobalMemorySlot::tag_t tag, const GlobalMemorySlot::globalKey_t key) const
Definition core.hpp:103
__INLINE__ void fence()
Definition core.hpp:184
__INLINE__ void insertGlobalSlot(const GlobalMemorySlot::tag_t tag, const GlobalMemorySlot::globalKey_t key, const std::shared_ptr< GlobalMemorySlot > &slot)
Definition core.hpp:81
__INLINE__ void removeGlobalSlot(const GlobalMemorySlot::tag_t tag, const GlobalMemorySlot::globalKey_t key)
Definition core.hpp:131
__INLINE__ Instance::instanceId_t getRootInstanceId() const
Definition core.hpp:258
Provides a definition for the base backend's communication manager class.
Provides a definition for the abstract instance manager class.