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

HiCR: /home/runner/work/HiCR/HiCR/include/hicr/backends/opencl/processingUnit.hpp Source File
HiCR
processingUnit.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 <CL/opencl.hpp>
33
34namespace HiCR::backend::opencl
35{
36
40class ComputeManager;
41} // namespace HiCR::backend::opencl
42
43namespace HiCR::backend::opencl
44{
45
50{
52
53 public:
54
61 __INLINE__ ProcessingUnit(const std::shared_ptr<HiCR::ComputeResource> &computeResource, const std::shared_ptr<cl::Context> &context)
62 : HiCR::ProcessingUnit(computeResource)
63 {
64 // Getting up-casted pointer for the instance
65 auto c = dynamic_pointer_cast<opencl::ComputeResource>(computeResource);
66 // Checking whether the execution unit passed is compatible with this backend
67 if (c == NULL) HICR_THROW_LOGIC("The passed compute resource is not supported by this processing unit type\n");
68
69 // Create queue
70 _queue = std::make_unique<cl::CommandQueue>(*context, c->getDevice().lock()->getOpenCLDevice());
71 };
72
78 [[nodiscard]] __INLINE__ std::string getType() override { return "OpenCL Device"; }
79
80 private:
81
85 std::unique_ptr<cl::CommandQueue> _queue;
86
90 std::unique_ptr<ExecutionState> _executionState;
91
95 __INLINE__ void initialize() {}
96
102 __INLINE__ void start(std::unique_ptr<HiCR::ExecutionState> &executionState)
103 {
104 // Getting up-casted pointer for the execution unit
105 auto e = std::unique_ptr<ExecutionState>(dynamic_cast<ExecutionState *>(executionState.release()));
106
107 // Checking whether the execution unit passed is compatible with this backend
108 if (e == NULL) HICR_THROW_LOGIC("The execution state is not supported by this backend\n");
109
110 // Storing execution state object
111 _executionState = std::move(e);
112
113 // Set context
114 _executionState->setQueue(_queue.get());
115
116 // Staring execution state
117 _executionState.get()->resume();
118 }
119
123 __INLINE__ void await()
124 {
125 // force the execution state to finalize
126 _executionState.get()->finalizeStream();
127 }
128
129 [[nodiscard]] __INLINE__ opencl::ExecutionState *getOpenCLExecutionStatePointer(std::unique_ptr<HiCR::ExecutionState> &executionState)
130 {
131 // We can only handle execution state of OpenCL type. Make sure we got the correct one
132 // To make it fast and avoid string comparisons, we use the dynamic cast method
133 auto p = dynamic_cast<opencl::ExecutionState *>(executionState.get());
134
135 // If the execution state is not recognized, throw error
136 if (p == nullptr) HICR_THROW_LOGIC("Execution state is not of type OpenCL");
137
138 // Returning converted pointer
139 return p;
140 }
141};
142
143} // namespace HiCR::backend::opencl
This file implements the compute resource class for the OpenCL backend.
This file implements the execution state class for the OpenCL backend.
This file implements the execution unit class for the OpenCL backend.
Definition executionState.hpp:38
Definition processingUnit.hpp:49
Definition computeManager.hpp:43
Definition processingUnit.hpp:50
__INLINE__ std::string getType() override
Definition processingUnit.hpp:78
__INLINE__ ProcessingUnit(const std::shared_ptr< HiCR::ComputeResource > &computeResource, const std::shared_ptr< cl::Context > &context)
Definition processingUnit.hpp:61
Provides a definition for a HiCR ProcessingUnit class.
Provides a failure model and corresponding exception classes.
#define HICR_THROW_LOGIC(...)
Definition exceptions.hpp:67