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

HiCR: /home/runner/work/HiCR/HiCR/include/hicr/backends/pthreads/instanceManager.hpp Source File
HiCR
instanceManager.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 <memory>
27#include <pthread.h>
28
30
31#include "core.hpp"
32#include "instance.hpp"
33
34namespace HiCR::backend::pthreads
35{
36
41{
42 public:
43
50 : HiCR::InstanceManager(),
51 _core(core)
52 {
53 // Create and set current instance in the base class
54 setCurrentInstance(_core.addInstance(pthread_self()));
55
56 // Get root instance id
57 _rootInstanceId = _core.getRootInstanceId();
58
59 // Wait for all the threads to add their own instance
60 _core.fence();
61
62 // Add all the instances to the base class
63 for (auto &i : _core.getInstances()) { addInstance(i); }
64 }
65
66 ~InstanceManager() override = default;
67
77 std::shared_ptr<HiCR::Instance> createInstanceImpl(const HiCR::InstanceTemplate instanceTemplate) override { HICR_THROW_RUNTIME("This backend does not support this operation"); }
78
86 std::shared_ptr<HiCR::Instance> addInstanceImpl(Instance::instanceId_t instanceId) override
87 {
88 // Get the instance from the core. Someone else already created it
89 auto instance = _core.getInstance(instanceId);
90 return instance;
91 }
92
98 void terminateInstanceImpl(const std::shared_ptr<HiCR::Instance> instance) override
99 {
100 // Nothing to do here
101 }
102
106 void finalize() override {}
107
113 void abort(int errorCode) override { exit(errorCode); }
114
120 HiCR::Instance::instanceId_t getRootInstanceId() const override { return _rootInstanceId; }
121
122 private:
123
127 Core &_core;
128
132 HiCR::Instance::instanceId_t _rootInstanceId;
133};
134} // namespace HiCR::backend::pthreads
Definition instanceManager.hpp:57
__INLINE__ void addInstance(HiCR::Instance::instanceId_t instanceId)
Definition instanceManager.hpp:132
__INLINE__ void setCurrentInstance(const std::shared_ptr< HiCR::Instance > &instance)
Definition instanceManager.hpp:196
Definition instanceTemplate.hpp:44
uint64_t instanceId_t
Definition instance.hpp:44
Definition core.hpp:42
__INLINE__ InstanceManager::instanceList_t getInstances() const
Definition core.hpp:193
__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__ void fence()
Definition core.hpp:184
__INLINE__ Instance::instanceId_t getRootInstanceId() const
Definition core.hpp:258
Definition instanceManager.hpp:41
std::shared_ptr< HiCR::Instance > createInstanceImpl(const HiCR::InstanceTemplate instanceTemplate) override
Definition instanceManager.hpp:77
void finalize() override
Definition instanceManager.hpp:106
void terminateInstanceImpl(const std::shared_ptr< HiCR::Instance > instance) override
Definition instanceManager.hpp:98
std::shared_ptr< HiCR::Instance > addInstanceImpl(Instance::instanceId_t instanceId) override
Definition instanceManager.hpp:86
InstanceManager(Core &core)
Definition instanceManager.hpp:49
HiCR::Instance::instanceId_t getRootInstanceId() const override
Definition instanceManager.hpp:120
void abort(int errorCode) override
Definition instanceManager.hpp:113
Provides a definition for the abstract instance manager class.
Provides a definition for the HiCR Instance class.
This file implements the core mechanism to exchange slots and detect instances for the pthreads backe...
#define HICR_THROW_RUNTIME(...)
Definition exceptions.hpp:74