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

HiCR: /home/runner/work/HiCR/HiCR/include/hicr/backends/opencl/topologyManager.hpp Source File
HiCR
topologyManager.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>
30#include <hicr/core/topology.hpp>
32
33namespace HiCR::backend::opencl
34{
39{
40 public:
41
46 : HiCR::TopologyManager()
47 {}
48
52 ~TopologyManager() = default;
53
54 __INLINE__ HiCR::Topology queryTopology() override
55 {
56 // Storage for the topology to return
58
59 // Get available platforms
60 std::vector<cl::Platform> platforms;
61 cl::Platform::get(&platforms);
62
63 if (platforms.empty()) { HICR_THROW_RUNTIME("No devices found"); }
64
65 for (const auto &platform : platforms)
66 {
67 // Retrieve all kind of devices detected by OpenCL
68 std::vector<cl::Device> devices;
69 platform.getDevices(CL_DEVICE_TYPE_ALL, &devices);
70
72
73 for (const auto &device : devices)
74 {
75 // Get device type and its memory size
76 auto deviceTypeEnum = device.getInfo<CL_DEVICE_TYPE>();
77 auto deviceMemorySize = device.getInfo<CL_DEVICE_GLOBAL_MEM_SIZE>();
78
79 std::string deviceType;
80 switch (deviceTypeEnum)
81 {
82 case CL_DEVICE_TYPE_CPU: deviceType = "OpenCL Host"; break;
83 case CL_DEVICE_TYPE_GPU: deviceType = "OpenCL GPU"; break;
84 case CL_DEVICE_TYPE_ACCELERATOR: deviceType = "OpenCL Accelerator"; break;
85 case CL_DEVICE_TYPE_CUSTOM: deviceType = "OpenCL Custom Hardware"; break;
86 default: HICR_THROW_RUNTIME("Unsupported device type: %lu", deviceTypeEnum); break;
87 }
88
89 // Create HiCR device, memory space, and compute resource
90 auto openclDevice = std::make_shared<opencl::Device>(
91 deviceId, deviceType, std::make_shared<cl::Device>(device), HiCR::Device::computeResourceList_t({}), HiCR::Device::memorySpaceList_t({}));
92 auto openclDeviceMemorySpace = std::make_shared<opencl::MemorySpace>(openclDevice, deviceType + " RAM", deviceMemorySize);
93 auto openclDeviceComputeResource = std::make_shared<opencl::ComputeResource>(openclDevice, deviceType + " Processing Unit");
94 openclDevice->addMemorySpace(openclDeviceMemorySpace);
95 openclDevice->addComputeResource(openclDeviceComputeResource);
96
97 t.addDevice(openclDevice);
98 }
99 }
100
101 // Returning topology
102 return t;
103 }
104
110 __INLINE__ static HiCR::Topology deserializeTopology(const nlohmann::json &topology)
111 {
112 // Verifying input's syntax
113 HiCR::Topology::verify(topology);
114
115 // New topology to create
117
118 // Iterating over the device list entries in the serialized input
119 for (const auto &device : topology["Devices"])
120 {
121 // Getting device type
122 const auto type = device["Type"].get<std::string>();
123
124 // If the device type is recognized, add it to the new topology
125 if (type.find("OpenCL") != std::string::npos) { t.addDevice(std::make_shared<opencl::Device>(device)); }
126 }
127
128 // Returning new topology
129 return t;
130 }
131
132 __INLINE__ HiCR::Topology _deserializeTopology(const nlohmann::json &topology) const override { return deserializeTopology(topology); }
133
139 __INLINE__ static std::unique_ptr<HiCR::TopologyManager> createDefault()
140 {
141 // Initializing opencl topology manager
142 return std::make_unique<HiCR::backend::opencl::TopologyManager>();
143 }
144
145 private:
146
150 __INLINE__ HiCR::Device::computeResourceList_t queryComputeResources()
151 {
152 // New compute resource list to return
153 HiCR::Device::computeResourceList_t computeResourceList;
154
155 // Returning new compute resource list
156 return computeResourceList;
157 }
158
162 __INLINE__ HiCR::Device::memorySpaceList_t queryMemorySpaces()
163 {
164 // New memory space list to return
165 HiCR::Device::memorySpaceList_t memorySpaceList;
166
167 // Returning new memory space list
168 return memorySpaceList;
169 }
170};
171
172} // namespace HiCR::backend::opencl
This file implements the compute resource class for the OpenCL backend.
This file implements the Device class for the OpenCL backend.
This file implements the memory space class for the OpenCL backend.
std::vector< std::shared_ptr< ComputeResource > > computeResourceList_t
Definition device.hpp:59
std::vector< std::shared_ptr< MemorySpace > > memorySpaceList_t
Definition device.hpp:64
Definition topologyManager.hpp:50
Definition topology.hpp:40
__INLINE__ void addDevice(const std::shared_ptr< HiCR::Device > &device)
Definition topology.hpp:62
static __INLINE__ void verify(const nlohmann::json &input)
Definition topology.hpp:102
uint64_t deviceIdentifier_t
Definition device.hpp:43
Definition topologyManager.hpp:39
static __INLINE__ HiCR::Topology deserializeTopology(const nlohmann::json &topology)
Definition topologyManager.hpp:110
static __INLINE__ std::unique_ptr< HiCR::TopologyManager > createDefault()
Definition topologyManager.hpp:139
__INLINE__ HiCR::Topology _deserializeTopology(const nlohmann::json &topology) const override
Definition topologyManager.hpp:132
__INLINE__ HiCR::Topology queryTopology() override
Definition topologyManager.hpp:54
TopologyManager()
Definition topologyManager.hpp:45
Provides a definition for the abstract device manager class.
#define HICR_THROW_RUNTIME(...)
Definition exceptions.hpp:74