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

HiCR: /home/runner/work/HiCR/HiCR/include/hicr/backends/pthreads/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>
32
33namespace HiCR::backend::pthreads
34{
35
39using pthreadFc_t = std::function<void(void *)>;
40
45{
46 public:
47
52 : HiCR::ComputeManager()
53 {}
54
58 ~ComputeManager() override = default;
59
67 __INLINE__ static std::shared_ptr<HiCR::ExecutionUnit> createExecutionUnit(const pthreadFc_t &threadFunction)
68 {
69 return std::make_shared<pthreads::ExecutionUnit>(threadFunction);
70 }
71
79 __INLINE__ std::unique_ptr<HiCR::ExecutionState> createExecutionState(std::shared_ptr<HiCR::ExecutionUnit> executionUnit, void *const argument = nullptr) const override
80 {
81 // Creating and returning new execution state
82 return std::make_unique<pthreads::ExecutionState>(executionUnit, argument);
83 }
84
85 [[nodiscard]] __INLINE__ std::unique_ptr<HiCR::ProcessingUnit> createProcessingUnit(std::shared_ptr<HiCR::ComputeResource> computeResource) const override
86 {
87 return std::make_unique<pthreads::ProcessingUnit>(computeResource);
88 }
89
90 private:
91
92 __INLINE__ void initializeImpl(std::unique_ptr<HiCR::ProcessingUnit> &processingUnit) override
93 {
94 auto p = getPosixThreadPointer(processingUnit);
95
96 // The logic for starting the posix thread is in the class itself
97 p->initialize();
98 }
99
100 __INLINE__ void startImpl(std::unique_ptr<HiCR::ProcessingUnit> &processingUnit, std::unique_ptr<HiCR::ExecutionState> &executionState) override
101 {
102 auto p = getPosixThreadPointer(processingUnit);
103
104 // The logic for starting the posix thread is in the class itself
105 p->start(executionState);
106 }
107
108 __INLINE__ void suspendImpl(std::unique_ptr<HiCR::ProcessingUnit> &processingUnit) override
109 {
110 auto p = getPosixThreadPointer(processingUnit);
111
112 // The logic for suspending the posix thread is in the class itself
113 p->suspend();
114 }
115
116 __INLINE__ void resumeImpl(std::unique_ptr<HiCR::ProcessingUnit> &processingUnit) override
117 {
118 auto p = getPosixThreadPointer(processingUnit);
119
120 // The logic for resuming the posix thread is in the class itself
121 p->resume();
122 }
123
124 __INLINE__ void terminateImpl(std::unique_ptr<HiCR::ProcessingUnit> &processingUnit) override
125 {
126 auto p = getPosixThreadPointer(processingUnit);
127
128 // The logic for resuming the posix thread is in the class itself
129 p->terminate();
130 }
131
132 __INLINE__ void awaitImpl(std::unique_ptr<HiCR::ProcessingUnit> &processingUnit) override
133 {
134 auto p = getPosixThreadPointer(processingUnit);
135
136 // The logic for awaiting the posix thread is in the class itself
137 p->await();
138 }
139
140 [[nodiscard]] __INLINE__ pthreads::ProcessingUnit *getPosixThreadPointer(std::unique_ptr<HiCR::ProcessingUnit> &processingUnit)
141 {
142 // We can only handle processing units of Posix Thread type. Make sure we got the correct one
143 // To make it fast and avoid string comparisons, we use the dynamic cast method
144 auto p = dynamic_cast<pthreads::ProcessingUnit *>(processingUnit.get());
145
146 // If the processing unit is not recognized, throw error. We can use the processing unit's type (string) now.
147 if (p == nullptr) HICR_THROW_LOGIC("This compute manager cannot handle processing units of type '%s'", processingUnit->getType());
148
149 // Returning converted pointer
150 return p;
151 }
152};
153
154} // namespace HiCR::backend::pthreads
This file implements the compute resource class for the HWLoc-based backend.
std::function< void(void *)> pthreadFc_t
Definition computeManager.hpp:39
This file implements the abstract execution state class for the pthreads backend.
Implements the processing unit class for the Pthreads backend.
Definition computeManager.hpp:48
Definition computeManager.hpp:45
__INLINE__ std::unique_ptr< HiCR::ProcessingUnit > createProcessingUnit(std::shared_ptr< HiCR::ComputeResource > computeResource) const override
Definition computeManager.hpp:85
__INLINE__ std::unique_ptr< HiCR::ExecutionState > createExecutionState(std::shared_ptr< HiCR::ExecutionUnit > executionUnit, void *const argument=nullptr) const override
Definition computeManager.hpp:79
ComputeManager()
Definition computeManager.hpp:51
static __INLINE__ std::shared_ptr< HiCR::ExecutionUnit > createExecutionUnit(const pthreadFc_t &threadFunction)
Definition computeManager.hpp:67
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