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

HiCR: /home/runner/work/HiCR/HiCR/include/hicr/backends/opencl/device.hpp Source File
HiCR
device.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 <hicr/core/definitions.hpp>
27#include <hicr/core/device.hpp>
30
31namespace HiCR::backend::opencl
32{
36class Device final : public HiCR::Device
37{
38 public:
39
43 typedef uint64_t deviceIdentifier_t;
44
55 const std::string &type,
56 const std::shared_ptr<cl::Device> &device,
57 const computeResourceList_t &computeResources,
58 const memorySpaceList_t &memorySpaces)
59 : HiCR::Device(computeResources, memorySpaces),
60 _id(id),
61 _type(type),
62 _device(device){};
63
68 : HiCR::Device()
69 {}
70
79 Device(const nlohmann::json &input)
80 : HiCR::Device()
81 {
82 deserialize(input);
83 }
84
89
90 };
91
97 __INLINE__ deviceIdentifier_t getId() const { return _id; }
98
104 __INLINE__ std::string getType() const override { return _type; }
105
111 __INLINE__ const cl::Device &getOpenCLDevice() const { return *_device; }
112
113 private:
114
115 __INLINE__ void serializeImpl(nlohmann::json &output) const override
116 {
117 output["Device Identifier"] = _id;
118 output["Device Type"] = _type;
119 }
120
121 __INLINE__ void deserializeImpl(const nlohmann::json &input) override
122 {
123 std::string idKey = "Device Identifier";
124 std::string typeKey = "Device Type";
125
126 if (input.contains(idKey) == false) HICR_THROW_LOGIC("The serialized object contains no '%s' key", idKey.c_str());
127 if (input[idKey].is_number() == false) HICR_THROW_LOGIC("The '%s' entry is not a number", idKey.c_str());
128
129 if (input.contains(typeKey) == false) HICR_THROW_LOGIC("The serialized object contains no '%s' key", typeKey.c_str());
130 if (input[typeKey].is_number() == false) HICR_THROW_LOGIC("The '%s' entry is not a number", typeKey.c_str());
131
132 _id = input[idKey].get<deviceIdentifier_t>();
133 _type = input[typeKey].get<std::string>();
134
135 for (const auto &computeResource : input[_HICR_DEVICE_COMPUTE_RESOURCES_KEY_])
136 {
137 // Getting device type
138 const auto type = computeResource["Type"].get<std::string>();
139
140 // Checking whether the type is correct
141 if (type != getType() + " Processing Unit") HICR_THROW_LOGIC("The passed device type '%s' is not compatible with this topology manager", type.c_str());
142
143 // Deserializing new device
144 auto computeResourceObj = std::make_shared<opencl::ComputeResource>(computeResource);
145
146 // Inserting device into the list
147 this->addComputeResource(computeResourceObj);
148 }
149
150 // Iterating over the memory space list
151 for (const auto &memorySpace : input[_HICR_DEVICE_MEMORY_SPACES_KEY_])
152 {
153 // Getting device type
154 const auto type = memorySpace["Type"].get<std::string>();
155
156 // Checking whether the type is correct
157 if (type != getType() + " RAM") HICR_THROW_LOGIC("The passed device type '%s' is not compatible with this topology manager", type.c_str());
158
159 // Deserializing new device
160 auto memorySpaceObj = std::make_shared<opencl::MemorySpace>(memorySpace);
161
162 // Inserting device into the list
163 this->addMemorySpace(memorySpaceObj);
164 }
165 }
166
171
175 std::string _type;
176
180 const std::shared_ptr<cl::Device> _device;
181};
182
183} // namespace HiCR::backend::opencl
This file implements the compute resource class for the OpenCL backend.
This file implements the memory space class for the OpenCL backend.
Definition device.hpp:53
__INLINE__ void deserialize(const nlohmann::json &input)
Definition device.hpp:152
std::vector< std::shared_ptr< ComputeResource > > computeResourceList_t
Definition device.hpp:59
std::vector< std::shared_ptr< MemorySpace > > memorySpaceList_t
Definition device.hpp:64
__INLINE__ void addMemorySpace(const std::shared_ptr< HiCR::MemorySpace > &memorySpace)
Definition device.hpp:99
__INLINE__ void addComputeResource(const std::shared_ptr< HiCR::ComputeResource > &computeResource)
Definition device.hpp:92
Definition device.hpp:37
__INLINE__ std::string getType() const override
Definition device.hpp:104
__INLINE__ const cl::Device & getOpenCLDevice() const
Definition device.hpp:111
Device(const nlohmann::json &input)
Definition device.hpp:79
__INLINE__ deviceIdentifier_t getId() const
Definition device.hpp:97
uint64_t deviceIdentifier_t
Definition device.hpp:43
Device(const deviceIdentifier_t id, const std::string &type, const std::shared_ptr< cl::Device > &device, const computeResourceList_t &computeResources, const memorySpaceList_t &memorySpaces)
Definition device.hpp:54
Device()
Definition device.hpp:67
~Device()
Definition device.hpp:88
Provides a base definition for a HiCR Device class.
constexpr std::string_view _HICR_DEVICE_MEMORY_SPACES_KEY_
Definition device.hpp:40
constexpr std::string_view _HICR_DEVICE_COMPUTE_RESOURCES_KEY_
Definition device.hpp:35
#define HICR_THROW_LOGIC(...)
Definition exceptions.hpp:67