/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
64 __INLINE__ std::shared_ptr<HiCR::ExecutionUnit> createExecutionUnit(const std::vector<std::shared_ptr<opencl::Kernel>> &kernelOperations)
65 {
66 return std::make_shared<ExecutionUnit>(kernelOperations);
67 }
68
77 __INLINE__ std::unique_ptr<HiCR::ExecutionState> createExecutionState(std::shared_ptr<HiCR::ExecutionUnit> executionUnit, void *const argument = nullptr) const override
78 {
79 return std::make_unique<opencl::ExecutionState>(executionUnit);
80 }
81
89 __INLINE__ std::unique_ptr<HiCR::ProcessingUnit> createProcessingUnit(std::shared_ptr<HiCR::ComputeResource> resource) const override
90 {
91 return std::make_unique<ProcessingUnit>(resource, _context);
92 }
93
94 protected:
95
101 __INLINE__ void initializeImpl(std::unique_ptr<HiCR::ProcessingUnit> &processingUnit) override
102 {
103 auto p = getOpenCLPointer(processingUnit);
104 p->initialize();
105 }
106
113 __INLINE__ void startImpl(std::unique_ptr<HiCR::ProcessingUnit> &processingUnit, std::unique_ptr<HiCR::ExecutionState> &executionState) override
114 {
115 auto p = getOpenCLPointer(processingUnit);
116 p->start(executionState);
117 }
118
124 __INLINE__ void suspendImpl(std::unique_ptr<HiCR::ProcessingUnit> &processingUnit) override { HICR_THROW_RUNTIME("Suspend functionality not supported by OpenCL backend"); }
125
131 __INLINE__ void resumeImpl(std::unique_ptr<HiCR::ProcessingUnit> &processingUnit) override { HICR_THROW_RUNTIME("Resume functionality not supported by OpenCL backend"); }
132
138 __INLINE__ void terminateImpl(std::unique_ptr<HiCR::ProcessingUnit> &processingUnit) override {}
139
145 __INLINE__ void awaitImpl(std::unique_ptr<HiCR::ProcessingUnit> &processingUnit) override
146 {
147 auto p = getOpenCLPointer(processingUnit);
148 p->await();
149 }
150
151 private:
152
153 const std::shared_ptr<cl::Context> &_context;
154
155 [[nodiscard]] __INLINE__ opencl::ProcessingUnit *getOpenCLPointer(std::unique_ptr<HiCR::ProcessingUnit> &processingUnit)
156 {
157 // We can only handle processing units of OpenCL type. Make sure we got the correct one
158 // To make it fast and avoid string comparisons, we use the dynamic cast method
159 auto p = dynamic_cast<opencl::ProcessingUnit *>(processingUnit.get());
160
161 // If the processing unit is not recognized, throw error. We can use the processing unit's type (string) now.
162 if (p == nullptr) HICR_THROW_LOGIC("This compute manager cannot handle processing units of type '%s'", processingUnit->getType());
163
164 // Returning converted pointer
165 return p;
166 }
167};
168
169} // 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
Definition computeManager.hpp:43
__INLINE__ std::shared_ptr< HiCR::ExecutionUnit > createExecutionUnit(const std::vector< std::shared_ptr< opencl::Kernel > > &kernelOperations)
Definition computeManager.hpp:64
__INLINE__ void suspendImpl(std::unique_ptr< HiCR::ProcessingUnit > &processingUnit) override
Definition computeManager.hpp:124
__INLINE__ void resumeImpl(std::unique_ptr< HiCR::ProcessingUnit > &processingUnit) override
Definition computeManager.hpp:131
__INLINE__ void initializeImpl(std::unique_ptr< HiCR::ProcessingUnit > &processingUnit) override
Definition computeManager.hpp:101
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:138
__INLINE__ std::unique_ptr< HiCR::ExecutionState > createExecutionState(std::shared_ptr< HiCR::ExecutionUnit > executionUnit, void *const argument=nullptr) const override
Definition computeManager.hpp:77
__INLINE__ void startImpl(std::unique_ptr< HiCR::ProcessingUnit > &processingUnit, std::unique_ptr< HiCR::ExecutionState > &executionState) override
Definition computeManager.hpp:113
__INLINE__ void awaitImpl(std::unique_ptr< HiCR::ProcessingUnit > &processingUnit) override
Definition computeManager.hpp:145
__INLINE__ std::unique_ptr< HiCR::ProcessingUnit > createProcessingUnit(std::shared_ptr< HiCR::ComputeResource > resource) const override
Definition computeManager.hpp:89
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.