/home/runner/work/HiCR/HiCR/include/hicr/backends/nosv/computeManager.hpp Source File

HiCR: /home/runner/work/HiCR/HiCR/include/hicr/backends/nosv/computeManager.hpp Source File
HiCR
computeManager.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 <hicr/core/definitions.hpp>
34
35namespace HiCR::backend::nosv
36{
37
44{
45 public:
46
54 __INLINE__ static std::shared_ptr<HiCR::ExecutionUnit> createExecutionUnit(const std::function<void(void *)> &executionUnit)
55 {
56 return std::make_shared<HiCR::backend::nosv::ExecutionUnit>(executionUnit);
57 }
58
66 [[nodiscard]] __INLINE__ std::unique_ptr<HiCR::ProcessingUnit> createProcessingUnit(std::shared_ptr<HiCR::ComputeResource> computeResource) const override
67 {
68 return std::make_unique<HiCR::backend::nosv::ProcessingUnit>(computeResource);
69 }
70
80 __INLINE__ std::unique_ptr<HiCR::ExecutionState> createExecutionState(std::shared_ptr<HiCR::ExecutionUnit> executionUnit, void *const argument = nullptr) const override
81 {
82 return std::make_unique<HiCR::backend::nosv::ExecutionState>(executionUnit, argument);
83 }
84
85 protected:
86
92 __INLINE__ void initializeImpl(std::unique_ptr<HiCR::ProcessingUnit> &processingUnit) override
93 {
94 auto p = getPUPointer(processingUnit);
95
96 // The logic for starting the posix thread is in the class itself
97 p->initialize();
98 }
99
106 __INLINE__ void startImpl(std::unique_ptr<HiCR::ProcessingUnit> &processingUnit, std::unique_ptr<HiCR::ExecutionState> &executionState) override
107 {
108 auto p = getPUPointer(processingUnit);
109
110 // The logic for starting the posix thread is in the class itself
111 p->start(executionState);
112 }
113
119 __INLINE__ void suspendImpl(std::unique_ptr<HiCR::ProcessingUnit> &processingUnit) override
120 {
121 auto p = getPUPointer(processingUnit);
122
123 // The logic for suspending the posix thread is in the class itself
124 p->suspend();
125 }
126
132 __INLINE__ void resumeImpl(std::unique_ptr<HiCR::ProcessingUnit> &processingUnit) override
133 {
134 auto p = getPUPointer(processingUnit);
135
136 // The logic for resuming the posix thread is in the class itself
137 p->resume();
138 }
139
145 __INLINE__ void terminateImpl(std::unique_ptr<HiCR::ProcessingUnit> &processingUnit) override
146 {
147 auto p = getPUPointer(processingUnit);
148
149 // The logic for resuming the posix thread is in the class itself
150 p->terminate();
151 }
152
158 __INLINE__ void awaitImpl(std::unique_ptr<HiCR::ProcessingUnit> &processingUnit) override
159 {
160 auto p = getPUPointer(processingUnit);
161
162 // The logic for awaiting the posix thread is in the class itself
163 p->await();
164 }
165
172 [[nodiscard]] __INLINE__ nosv::ProcessingUnit *getPUPointer(std::unique_ptr<HiCR::ProcessingUnit> &processingUnit)
173 {
174 // We can only handle processing units of Posix Thread type. Make sure we got the correct one
175 // To make it fast and avoid string comparisons, we use the dynamic cast method
176 auto p = dynamic_cast<nosv::ProcessingUnit *>(processingUnit.get());
177
178 // If the processing unit is not recognized, throw error. We can use the processing unit's type (string) now.
179 if (p == nullptr) HICR_THROW_LOGIC("This compute manager cannot handle processing units of type '%s'", processingUnit->getType());
180
181 // Returning converted pointer
182 return p;
183 }
184};
185} // namespace HiCR::backend::nosv
This file consists of the common nOS-V function used for all the backend implementations.
nOS-V execution state class. Main job is to store the nosv task and its metadata
nOS-V execution unit class. Main job is to store the function call
nOS-V processing unit class. Main job is to to submit the execution state task.
Definition computeManager.hpp:48
Definition computeManager.hpp:44
__INLINE__ void startImpl(std::unique_ptr< HiCR::ProcessingUnit > &processingUnit, std::unique_ptr< HiCR::ExecutionState > &executionState) override
Definition computeManager.hpp:106
__INLINE__ nosv::ProcessingUnit * getPUPointer(std::unique_ptr< HiCR::ProcessingUnit > &processingUnit)
Definition computeManager.hpp:172
static __INLINE__ std::shared_ptr< HiCR::ExecutionUnit > createExecutionUnit(const std::function< void(void *)> &executionUnit)
Definition computeManager.hpp:54
__INLINE__ void resumeImpl(std::unique_ptr< HiCR::ProcessingUnit > &processingUnit) override
Definition computeManager.hpp:132
__INLINE__ void initializeImpl(std::unique_ptr< HiCR::ProcessingUnit > &processingUnit) override
Definition computeManager.hpp:92
__INLINE__ std::unique_ptr< HiCR::ExecutionState > createExecutionState(std::shared_ptr< HiCR::ExecutionUnit > executionUnit, void *const argument=nullptr) const override
Definition computeManager.hpp:80
__INLINE__ void awaitImpl(std::unique_ptr< HiCR::ProcessingUnit > &processingUnit) override
Definition computeManager.hpp:158
__INLINE__ std::unique_ptr< HiCR::ProcessingUnit > createProcessingUnit(std::shared_ptr< HiCR::ComputeResource > computeResource) const override
Definition computeManager.hpp:66
__INLINE__ void suspendImpl(std::unique_ptr< HiCR::ProcessingUnit > &processingUnit) override
Definition computeManager.hpp:119
__INLINE__ void terminateImpl(std::unique_ptr< HiCR::ProcessingUnit > &processingUnit) override
Definition computeManager.hpp:145
Definition processingUnit.hpp:53
Provides a definition for the abstract compute manager class.
Provides a failure model and corresponding exception classes.
#define HICR_THROW_LOGIC(...)
Definition exceptions.hpp:67