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

HiCR: /home/runner/work/HiCR/HiCR/include/hicr/backends/ascend/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 <acl/acl.h>
27#include <hicr/core/definitions.hpp>
28#include <hicr/core/device.hpp>
31
32namespace HiCR::backend::ascend
33{
34
38class Device final : public HiCR::Device
39{
40 public:
41
45 typedef uint64_t deviceIdentifier_t;
46
54 Device(const deviceIdentifier_t id, const computeResourceList_t &computeResources, const memorySpaceList_t &memorySpaces)
55 : HiCR::Device(computeResources, memorySpaces),
56 _id(id),
57 _context(std::make_unique<aclrtContext>())
58 {
59 // create ACL context for executing operations on the given device
60 aclError err = aclrtCreateContext(_context.get(), id);
61 if (err != ACL_SUCCESS) HICR_THROW_RUNTIME("Can not create context for device %ld. Error %d", _id, err);
62 };
63
68 : HiCR::Device()
69 {}
70
79 Device(const nlohmann::json &input)
80 : HiCR::Device()
81 {
82 deserialize(input);
83 }
84
88 __INLINE__ void select() const { selectDevice(_context.get(), _id); }
89
94 {
95 // destroy the ACL context
96 aclError err = aclrtDestroyContext(*_context.get());
97 if (err != ACL_SUCCESS) HICR_THROW_RUNTIME("Can not destroy context for device %ld. Error %d", _id, err);
98 };
99
105 __INLINE__ std::string getType() const override { return "Ascend Device"; }
106
112 __INLINE__ deviceIdentifier_t getId() const { return _id; }
113
119 __INLINE__ aclrtContext *getContext() const { return _context.get(); }
120
121 private:
122
123 __INLINE__ void serializeImpl(nlohmann::json &output) const override
124 {
125 // Storing device identifier
126 output["Device Identifier"] = _id;
127 }
128
129 __INLINE__ void deserializeImpl(const nlohmann::json &input) override
130 {
131 // Getting device id
132 std::string key = "Device Identifier";
133 if (input.contains(key) == false) HICR_THROW_LOGIC("The serialized object contains no '%s' key", key.c_str());
134 if (input[key].is_number() == false) HICR_THROW_LOGIC("The '%s' entry is not a number", key.c_str());
135 _id = input[key].get<deviceIdentifier_t>();
136
137 // Iterating over the compute resource list
138 for (const auto &computeResource : input[_HICR_DEVICE_COMPUTE_RESOURCES_KEY_])
139 {
140 // Getting device type
141 const auto type = computeResource["Type"].get<std::string>();
142
143 // Checking whether the type is correct
144 if (type != "Ascend Processor") HICR_THROW_LOGIC("The passed device type '%s' is not compatible with this topology manager", type.c_str());
145
146 // Deserializing new device
147 auto computeResourceObj = std::make_shared<ascend::ComputeResource>(computeResource);
148
149 // Inserting device into the list
150 addComputeResource(computeResourceObj);
151 }
152
153 // Iterating over the memory space list
154 for (const auto &memorySpace : input[_HICR_DEVICE_MEMORY_SPACES_KEY_])
155 {
156 // Getting device type
157 const auto type = memorySpace["Type"].get<std::string>();
158
159 // Checking whether the type is correct
160 if (type != "Ascend Device RAM") HICR_THROW_LOGIC("The passed device type '%s' is not compatible with this topology manager", type.c_str());
161
162 // Deserializing new device
163 auto memorySpaceObj = std::make_shared<ascend::MemorySpace>(memorySpace);
164
165 // Inserting device into the list
166 addMemorySpace(memorySpaceObj);
167 }
168 }
169
174
178 const std::unique_ptr<aclrtContext> _context;
179
180 private:
181
188 __INLINE__ static void selectDevice(const aclrtContext *deviceContext, const deviceIdentifier_t deviceId)
189 {
190 // select the device context on which operations shoud be executed
191 aclError err = aclrtSetCurrentContext(*deviceContext);
192 if (err != ACL_SUCCESS) HICR_THROW_RUNTIME("can not set the device %ld context. Error %d", deviceId, err);
193 }
194};
195
196} // namespace HiCR::backend::ascend
This file implements the compute resource class for the Ascend backend.
This file implements the memory space class for the Ascend 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:39
__INLINE__ std::string getType() const override
Definition device.hpp:105
__INLINE__ void select() const
Definition device.hpp:88
~Device()
Definition device.hpp:93
Device(const deviceIdentifier_t id, const computeResourceList_t &computeResources, const memorySpaceList_t &memorySpaces)
Definition device.hpp:54
Device(const nlohmann::json &input)
Definition device.hpp:79
__INLINE__ aclrtContext * getContext() const
Definition device.hpp:119
uint64_t deviceIdentifier_t
Definition device.hpp:45
__INLINE__ deviceIdentifier_t getId() const
Definition device.hpp:112
Device()
Definition device.hpp:67
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_RUNTIME(...)
Definition exceptions.hpp:74
#define HICR_THROW_LOGIC(...)
Definition exceptions.hpp:67