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

HiCR: /home/runner/work/HiCR/HiCR/include/hicr/backends/opencl/computationKernel.hpp Source File
HiCR
computationKernel.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 <vector>
28#include <unordered_set>
29#include <CL/opencl.hpp>
34
35namespace HiCR::backend::opencl
36{
37
43class ComputationKernel final : public Kernel
44{
45 public:
46
57 ComputationKernel(const std::shared_ptr<cl::Kernel> &kernel,
58 const std::vector<std::shared_ptr<HiCR::LocalMemorySlot>> &args,
59 const cl::NDRange offset,
60 const cl::NDRange global,
61 const cl::NDRange local)
62 : Kernel(),
63 _kernel(kernel),
64 _offset(offset),
65 _global(global),
66 _local(local),
67 _args(args)
68 {
69 for (size_t i = 0; i < _args.size(); i++)
70 {
71 auto a = getArgument(i);
72 // Set the kernel argument
73 auto err = _kernel->setArg<cl::Buffer>(i, *(a->getBuffer()));
74 if (err != CL_SUCCESS) [[unlikely]] { HICR_THROW_RUNTIME("Can not set kernel arg. Error: %d", err); }
75 }
76 };
77
78 ComputationKernel() = delete;
79
83 ~ComputationKernel() = default;
84
90 __INLINE__ void start(const cl::CommandQueue *queue) override
91 {
92 std::unordered_set<std::shared_ptr<HiCR::backend::opencl::LocalMemorySlot>> unmappedSlots({});
93
94 for (size_t i = 0; i < _args.size(); i++)
95 {
96 // Get the casted argument
97 auto a = getArgument(i);
98
99 // If already unmapped go to the next one
100 if (unmappedSlots.contains(a) == true) { continue; }
101
102 // Unmap all the arguments
103 auto err = queue->enqueueUnmapMemObject(*(a->getBuffer()), a->getPointer());
104 if (err != CL_SUCCESS) [[unlikely]] { HICR_THROW_RUNTIME("Can not unmap kernel arg. Error: %d", err); }
105
106 // Add the slot to the unmapped set
107 unmappedSlots.insert(a);
108 }
109
110 // start the kernel
111 auto err = queue->enqueueNDRangeKernel(*_kernel, _offset, _global, _local);
112 if (err != CL_SUCCESS) [[unlikely]] { HICR_THROW_RUNTIME("Failed to run the kernel. Error %d", err); }
113
114 for (size_t i = 0; i < _args.size(); i++)
115 {
116 // Get the casted argument
117 auto a = getArgument(i);
118
119 if (unmappedSlots.empty() == true) { break; }
120 if (unmappedSlots.contains(a) == false) { continue; }
121
122 // Map arguments after kernel execution
123 a->getPointer() = queue->enqueueMapBuffer(*(a->getBuffer()), CL_TRUE, CL_MAP_READ | CL_MAP_WRITE, 0, a->getSize(), nullptr, nullptr, &err);
124 if (err != CL_SUCCESS) [[unlikely]] { HICR_THROW_RUNTIME("Can not map kernel arg. Error: %d", err); }
125
126 // Remove the slot from the unmapped set
127 unmappedSlots.erase(a);
128 }
129 }
130
131 private:
132
136 std::shared_ptr<cl::Kernel> _kernel;
137
141 const cl::NDRange _offset;
142
146 const cl::NDRange _global;
147
151 const cl::NDRange _local;
152
156 const std::vector<std::shared_ptr<HiCR::LocalMemorySlot>> _args;
157
165 __INLINE__ std::shared_ptr<opencl::LocalMemorySlot> getArgument(uint64_t i)
166 {
167 const auto &arg = _args[i];
168 auto a = dynamic_pointer_cast<opencl::LocalMemorySlot>(arg);
169 if (a == nullptr) [[unlikely]] { HICR_THROW_RUNTIME("Provided memory slot containing the argument is not supported."); }
170 return a;
171 }
172};
173
174} // namespace HiCR::backend::opencl
Provides a definition for the local memory slot class for the OpenCL backend.
Definition computationKernel.hpp:44
ComputationKernel(const std::shared_ptr< cl::Kernel > &kernel, const std::vector< std::shared_ptr< HiCR::LocalMemorySlot > > &args, const cl::NDRange offset, const cl::NDRange global, const cl::NDRange local)
Definition computationKernel.hpp:57
__INLINE__ void start(const cl::CommandQueue *queue) override
Definition computationKernel.hpp:90
Definition kernel.hpp:37
Provides a definition for a HiCR Local Memory Slot class.
Provides a failure model and corresponding exception classes.
#define HICR_THROW_RUNTIME(...)
Definition exceptions.hpp:74
This file implements the Kernel class for the OpenCL backend.