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

HiCR: /home/runner/work/HiCR/HiCR/include/hicr/backends/ascend/processingUnit.hpp Source File
HiCR
processingUnit.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>
32
33namespace HiCR::backend::ascend
34{
35
39class ComputeManager;
40} // namespace HiCR::backend::ascend
41
42namespace HiCR::backend::ascend
43{
44
49{
51
52 public:
53
59 __INLINE__ ProcessingUnit(const std::shared_ptr<HiCR::ComputeResource> &computeResource)
60 : HiCR::ProcessingUnit(computeResource)
61 {
62 // Getting up-casted pointer for the instance
63 auto c = dynamic_pointer_cast<ascend::ComputeResource>(computeResource);
64
65 // Checking whether the execution unit passed is compatible with this backend
66 if (c == NULL) HICR_THROW_LOGIC("The passed compute resource is not supported by this processing unit type\n");
67
68 // Select the device
69 c->getDevice().lock()->select();
70
71 // Use FAST_LAUNCH option since the stream is meant to execute a sequence of kernels
72 // that reuse the same stream
73 auto err = aclrtCreateStreamWithConfig(&_stream, 0, ACL_STREAM_FAST_LAUNCH);
74 if (err != ACL_SUCCESS) HICR_THROW_RUNTIME("Could not create stream. Error %d", err);
75 };
76
81 {
82 // destroy the stream
83 auto err = aclrtDestroyStream(_stream);
84 if (err != ACL_SUCCESS) HICR_THROW_RUNTIME("Failed to delete the stream after kernel execution. Error %d", err);
85 }
86
93 [[nodiscard]] __INLINE__ std::string getType() override { return "Ascend Device"; }
94
95 private:
96
100 aclrtStream _stream;
101
105 std::unique_ptr<ExecutionState> _executionState;
106
110 __INLINE__ void initialize() {}
111
117 __INLINE__ void start(std::unique_ptr<HiCR::ExecutionState> &executionState)
118 {
119 // Getting up-casted pointer for the execution unit
120 auto e = std::unique_ptr<ExecutionState>(dynamic_cast<ExecutionState *>(executionState.release()));
121
122 // Checking whether the execution unit passed is compatible with this backend
123 if (e == NULL) HICR_THROW_LOGIC("The execution state is not supported by this backend\n");
124
125 // Storing execution state object
126 _executionState = std::move(e);
127
128 // Getting up-casted pointer for the instance
129 auto c = static_cast<ascend::ComputeResource *>(getComputeResource().get());
130
131 // select the curent Ascend card before starting the execution state
132 c->getDevice().lock()->select();
133
134 // set the stream in the execution state
135 _executionState->setStream(_stream);
136
137 // Staring execution state
138 _executionState.get()->resume();
139 }
140
144 __INLINE__ void await()
145 {
146 // Getting up-casted pointer for the instance
147 auto c = static_cast<ascend::ComputeResource *>(getComputeResource().get());
148
149 // select the curent Ascend card before starting the execution state
150 c->getDevice().lock()->select();
151
152 // force the execution state to finalize
153 _executionState.get()->finalizeStream();
154 }
155
156 [[nodiscard]] __INLINE__ ascend::ExecutionState *getAscendExecutionStatePointer(std::unique_ptr<HiCR::ExecutionState> &executionState)
157 {
158 // We can only handle execution state of Ascend type. Make sure we got the correct one
159 // To make it fast and avoid string comparisons, we use the dynamic cast method
160 auto p = dynamic_cast<ascend::ExecutionState *>(executionState.get());
161
162 // If the execution state is not recognized, throw error
163 if (p == nullptr) HICR_THROW_LOGIC("Execution state is not of type Ascend");
164
165 // Returning converted pointer
166 return p;
167 }
168};
169
170} // namespace HiCR::backend::ascend
This file implements the compute resource class for the Ascend backend.
This file implements the execution state class for the Ascend backend.
This file implements the execution unit class for the Ascend backend.
Definition executionState.hpp:38
Definition processingUnit.hpp:49
__INLINE__ std::shared_ptr< ComputeResource > getComputeResource()
Definition processingUnit.hpp:120
Definition computeManager.hpp:48
Definition computeResource.hpp:42
__INLINE__ const std::weak_ptr< const ascend::Device > getDevice() const
Definition computeResource.hpp:80
Definition processingUnit.hpp:49
~ProcessingUnit()
Definition processingUnit.hpp:80
__INLINE__ std::string getType() override
Definition processingUnit.hpp:93
__INLINE__ ProcessingUnit(const std::shared_ptr< HiCR::ComputeResource > &computeResource)
Definition processingUnit.hpp:59
Provides a definition for a HiCR ProcessingUnit 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