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

HiCR: /home/runner/work/HiCR/HiCR/include/hicr/backends/opencl/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 <CL/opencl.hpp>
27#include <memory>
28#include <unordered_map>
33
34namespace HiCR::backend::opencl
35{
36
43{
44 public:
45
51 ComputeManager(const std::shared_ptr<cl::Context> &context)
52 : HiCR::ComputeManager(),
53 _context(context){};
54
55 ~ComputeManager() override = default;
56
61
69 __INLINE__ std::shared_ptr<HiCR::ExecutionUnit> createExecutionUnit(const std::vector<std::shared_ptr<opencl::Kernel>> &kernelOperations)
70 {
71 return std::make_shared<ExecutionUnit>(kernelOperations);
72 }
73
82 __INLINE__ std::unique_ptr<HiCR::ExecutionState> createExecutionState(std::shared_ptr<HiCR::ExecutionUnit> executionUnit, void *const argument = nullptr) const override
83 {
84 return std::make_unique<opencl::ExecutionState>(executionUnit);
85 }
86
94 __INLINE__ std::unique_ptr<HiCR::ProcessingUnit> createProcessingUnit(std::shared_ptr<HiCR::ComputeResource> resource) const override
95 {
96 return std::make_unique<ProcessingUnit>(resource, _context);
97 }
98
99 protected:
100
106 __INLINE__ void initializeImpl(std::unique_ptr<HiCR::ProcessingUnit> &processingUnit) override
107 {
108 auto p = getOpenCLPointer(processingUnit);
109 p->initialize();
110 }
111
118 __INLINE__ void startImpl(std::unique_ptr<HiCR::ProcessingUnit> &processingUnit, std::unique_ptr<HiCR::ExecutionState> &executionState) override
119 {
120 auto p = getOpenCLPointer(processingUnit);
121 p->start(executionState);
122 }
123
129 __INLINE__ void suspendImpl(std::unique_ptr<HiCR::ProcessingUnit> &processingUnit) override { HICR_THROW_RUNTIME("Suspend functionality not supported by OpenCL backend"); }
130
136 __INLINE__ void resumeImpl(std::unique_ptr<HiCR::ProcessingUnit> &processingUnit) override { HICR_THROW_RUNTIME("Resume functionality not supported by OpenCL backend"); }
137
143 __INLINE__ void terminateImpl(std::unique_ptr<HiCR::ProcessingUnit> &processingUnit) override {}
144
150 __INLINE__ void awaitImpl(std::unique_ptr<HiCR::ProcessingUnit> &processingUnit) override
151 {
152 auto p = getOpenCLPointer(processingUnit);
153 p->await();
154 }
155
156 private:
157
158 const std::shared_ptr<cl::Context> &_context;
159
160 [[nodiscard]] __INLINE__ opencl::ProcessingUnit *getOpenCLPointer(std::unique_ptr<HiCR::ProcessingUnit> &processingUnit)
161 {
162 // We can only handle processing units of OpenCL type. Make sure we got the correct one
163 // To make it fast and avoid string comparisons, we use the dynamic cast method
164 auto p = dynamic_cast<opencl::ProcessingUnit *>(processingUnit.get());
165
166 // If the processing unit is not recognized, throw error. We can use the processing unit's type (string) now.
167 if (p == nullptr) HICR_THROW_LOGIC("This compute manager cannot handle processing units of type '%s'", processingUnit->getType().c_str());
168
169 // Returning converted pointer
170 return p;
171 }
172};
173
174} // namespace HiCR::backend::opencl
This file implements the execution unit class for the OpenCL backend.
Implements the processing unit class for the OpenCL backend.
Definition computeManager.hpp:48
virtual __INLINE__ std::shared_ptr< HiCR::ExecutionUnit > createExecutionUnit(const replicableFc_t &function)
Definition computeManager.hpp:63
Definition computeManager.hpp:43
__INLINE__ std::shared_ptr< HiCR::ExecutionUnit > createExecutionUnit(const std::vector< std::shared_ptr< opencl::Kernel > > &kernelOperations)
Definition computeManager.hpp:69
__INLINE__ void suspendImpl(std::unique_ptr< HiCR::ProcessingUnit > &processingUnit) override
Definition computeManager.hpp:129
__INLINE__ void resumeImpl(std::unique_ptr< HiCR::ProcessingUnit > &processingUnit) override
Definition computeManager.hpp:136
__INLINE__ void initializeImpl(std::unique_ptr< HiCR::ProcessingUnit > &processingUnit) override
Definition computeManager.hpp:106
ComputeManager(const std::shared_ptr< cl::Context > &context)
Definition computeManager.hpp:51
__INLINE__ void terminateImpl(std::unique_ptr< HiCR::ProcessingUnit > &processingUnit) override
Definition computeManager.hpp:143
__INLINE__ std::unique_ptr< HiCR::ExecutionState > createExecutionState(std::shared_ptr< HiCR::ExecutionUnit > executionUnit, void *const argument=nullptr) const override
Definition computeManager.hpp:82
__INLINE__ void startImpl(std::unique_ptr< HiCR::ProcessingUnit > &processingUnit, std::unique_ptr< HiCR::ExecutionState > &executionState) override
Definition computeManager.hpp:118
__INLINE__ void awaitImpl(std::unique_ptr< HiCR::ProcessingUnit > &processingUnit) override
Definition computeManager.hpp:150
__INLINE__ std::unique_ptr< HiCR::ProcessingUnit > createProcessingUnit(std::shared_ptr< HiCR::ComputeResource > resource) const override
Definition computeManager.hpp:94
Definition processingUnit.hpp:50
Provides a definition for the abstract compute manager class.
#define HICR_THROW_RUNTIME(...)
Definition exceptions.hpp:74
#define HICR_THROW_LOGIC(...)
Definition exceptions.hpp:67
This file implements the Kernel class for the OpenCL backend.