/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
40{
41 public:
42
47 : HiCR::ComputeManager()
48 {}
49
53 ~ComputeManager() override = default;
54
62 __INLINE__ std::shared_ptr<HiCR::ExecutionUnit> createExecutionUnit(const replicableFc_t &function) override { return std::make_shared<pthreads::ExecutionUnit>(function); }
63
71 __INLINE__ std::unique_ptr<HiCR::ExecutionState> createExecutionState(std::shared_ptr<HiCR::ExecutionUnit> executionUnit, void *const argument = nullptr) const override
72 {
73 // Creating and returning new execution state
74 return std::make_unique<pthreads::ExecutionState>(executionUnit, argument);
75 }
76
77 [[nodiscard]] __INLINE__ std::unique_ptr<HiCR::ProcessingUnit> createProcessingUnit(std::shared_ptr<HiCR::ComputeResource> computeResource) const override
78 {
79 return std::make_unique<pthreads::ProcessingUnit>(computeResource);
80 }
81
82 private:
83
84 __INLINE__ void initializeImpl(std::unique_ptr<HiCR::ProcessingUnit> &processingUnit) override
85 {
86 auto p = getPosixThreadPointer(processingUnit);
87
88 // The logic for starting the posix thread is in the class itself
89 p->initialize();
90 }
91
92 __INLINE__ void startImpl(std::unique_ptr<HiCR::ProcessingUnit> &processingUnit, std::unique_ptr<HiCR::ExecutionState> &executionState) override
93 {
94 auto p = getPosixThreadPointer(processingUnit);
95
96 // The logic for starting the posix thread is in the class itself
97 p->start(executionState);
98 }
99
100 __INLINE__ void suspendImpl(std::unique_ptr<HiCR::ProcessingUnit> &processingUnit) override
101 {
102 auto p = getPosixThreadPointer(processingUnit);
103
104 // The logic for suspending the posix thread is in the class itself
105 p->suspend();
106 }
107
108 __INLINE__ void resumeImpl(std::unique_ptr<HiCR::ProcessingUnit> &processingUnit) override
109 {
110 auto p = getPosixThreadPointer(processingUnit);
111
112 // The logic for resuming the posix thread is in the class itself
113 p->resume();
114 }
115
116 __INLINE__ void terminateImpl(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->terminate();
122 }
123
124 __INLINE__ void awaitImpl(std::unique_ptr<HiCR::ProcessingUnit> &processingUnit) override
125 {
126 auto p = getPosixThreadPointer(processingUnit);
127
128 // The logic for awaiting the posix thread is in the class itself
129 p->await();
130 }
131
132 [[nodiscard]] __INLINE__ pthreads::ProcessingUnit *getPosixThreadPointer(std::unique_ptr<HiCR::ProcessingUnit> &processingUnit)
133 {
134 // We can only handle processing units of Posix Thread type. Make sure we got the correct one
135 // To make it fast and avoid string comparisons, we use the dynamic cast method
136 auto p = dynamic_cast<pthreads::ProcessingUnit *>(processingUnit.get());
137
138 // If the processing unit is not recognized, throw error. We can use the processing unit's type (string) now.
139 if (p == nullptr) HICR_THROW_LOGIC("This compute manager cannot handle processing units of type '%s'", processingUnit->getType().c_str());
140
141 // Returning converted pointer
142 return p;
143 }
144};
145
146} // namespace HiCR::backend::pthreads
This file implements the compute resource class for the HWLoc-based backend.
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:40
__INLINE__ std::unique_ptr< HiCR::ProcessingUnit > createProcessingUnit(std::shared_ptr< HiCR::ComputeResource > computeResource) const override
Definition computeManager.hpp:77
__INLINE__ std::unique_ptr< HiCR::ExecutionState > createExecutionState(std::shared_ptr< HiCR::ExecutionUnit > executionUnit, void *const argument=nullptr) const override
Definition computeManager.hpp:71
ComputeManager()
Definition computeManager.hpp:46
__INLINE__ std::shared_ptr< HiCR::ExecutionUnit > createExecutionUnit(const replicableFc_t &function) override
Definition computeManager.hpp:62
Provides a definition for the abstract compute manager class.
std::function< void(void *)> replicableFc_t
Definition executionUnit.hpp:34
Provides a failure model and corresponding exception classes.
#define HICR_THROW_LOGIC(...)
Definition exceptions.hpp:67