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

HiCR: /home/runner/work/HiCR/HiCR/include/hicr/backends/opencl/executionState.hpp Source File
HiCR
executionState.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.h>
31
32namespace HiCR::backend::opencl
33{
39{
40 public:
41
47 ExecutionState(const std::shared_ptr<HiCR::ExecutionUnit> executionUnit)
48 : HiCR::ExecutionState(executionUnit)
49 {
50 // Getting up-casted pointer for the execution unit
51 auto e = dynamic_pointer_cast<opencl::ExecutionUnit>(executionUnit);
52
53 // Checking whether the execution unit passed is compatible with this backend
54 if (e == NULL) HICR_THROW_LOGIC("The execution unit of type '%s' is not supported by this backend\n", executionUnit->getType());
55
56 _executionUnit = e;
57 }
58
59 ~ExecutionState() = default;
60
66 __INLINE__ void setQueue(cl::CommandQueue *queue) { _queue = queue; }
67
71 __INLINE__ void finalizeStream()
72 {
73 if (_isStreamActive == true)
74 {
75 // synchronize on the queue
76 auto err = _syncEvent.wait();
77 if (err != CL_SUCCESS) [[unlikely]] { HICR_THROW_RUNTIME("Failed to wait after kernel execution. Error %d", err); }
78
79 // avoid deleting the queue more than once
80 _isStreamActive = false;
81 }
82 }
83
84 protected:
85
89 __INLINE__ void resumeImpl() override
90 {
91 // Create event to wait for completion and check kernel status
92 _syncEvent = cl::Event();
93
94 _isStreamActive = true;
95
96 // start the sequence of kernels execution
97 _executionUnit->start(_queue);
98
99 // add an event at the end of the operations to query its status and check for completion
100 auto err = _queue->enqueueMarkerWithWaitList(nullptr, &_syncEvent);
101 if (err != CL_SUCCESS) [[unlikely]] { HICR_THROW_RUNTIME("Failed to write event in the queue", err); }
102 }
103
104 __INLINE__ void suspendImpl() { HICR_THROW_RUNTIME("Suspend functionality not supported by OpenCL backend"); }
105
112 __INLINE__ bool checkFinalizationImpl() override
113 {
114 // Check if the event has been processed
115 auto status = _syncEvent.getInfo<CL_EVENT_COMMAND_EXECUTION_STATUS>();
116
117 if (status != CL_COMPLETE) { return false; }
118
119 // synchronize the queue and destroy it
121
122 return true;
123 }
124
125 private:
126
130 std::shared_ptr<ExecutionUnit> _executionUnit;
131
135 cl::Event _syncEvent;
136
140 cl::CommandQueue *_queue;
141
145 bool _isStreamActive = false;
146};
147
148} // namespace HiCR::backend::opencl
This file implements the Device class for the OpenCL backend.
This file implements the execution unit class for the OpenCL backend.
Definition executionState.hpp:38
Definition executionState.hpp:39
ExecutionState(const std::shared_ptr< HiCR::ExecutionUnit > executionUnit)
Definition executionState.hpp:47
__INLINE__ void setQueue(cl::CommandQueue *queue)
Definition executionState.hpp:66
__INLINE__ void resumeImpl() override
Definition executionState.hpp:89
__INLINE__ void finalizeStream()
Definition executionState.hpp:71
__INLINE__ void suspendImpl()
Definition executionState.hpp:104
__INLINE__ bool checkFinalizationImpl() override
Definition executionState.hpp:112
Provides a base definition for a HiCR Execution State class.
Provides a failure model and corresponding exception classes.
#define HICR_THROW_RUNTIME(...)
Definition exceptions.hpp:74
#define HICR_THROW_LOGIC(...)
Definition exceptions.hpp:67