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

HiCR: /home/runner/work/HiCR/HiCR/include/hicr/backends/ascend/executionState.hpp Source File
HiCR
executionState.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>
31
32namespace HiCR::backend::ascend
33{
34
40{
41 public:
42
48 ExecutionState(const std::shared_ptr<HiCR::ExecutionUnit> executionUnit)
49 : HiCR::ExecutionState(executionUnit)
50 {
51 // Getting up-casted pointer for the execution unit
52 auto e = dynamic_pointer_cast<ascend::ExecutionUnit>(executionUnit);
53
54 // Checking whether the execution unit passed is compatible with this backend
55 if (e == NULL) HICR_THROW_LOGIC("The execution unit of type '%s' is not supported by this backend\n", executionUnit->getType());
56
57 _executionUnit = e;
58 }
59
63 ~ExecutionState() = default;
64
70 __INLINE__ void setStream(const aclrtStream stream) { _stream = stream; }
71
75 __INLINE__ void finalizeStream()
76 {
77 if (_isStreamActive)
78 {
79 // synchronize on the stream
80 aclError err = aclrtSynchronizeStream(_stream);
81 if (err != ACL_SUCCESS) HICR_THROW_RUNTIME("Failed to synchronize stream after kernel execution. Error %d", err);
82
83 // Destroy the related event
84 err = aclrtDestroyEvent(_syncEvent);
85 if (err != ACL_SUCCESS) HICR_THROW_RUNTIME("Failed to destroy event. Error %d", err);
86
87 // avoid deleting the stream more than once
88 _isStreamActive = false;
89 }
90 }
91
92 protected:
93
97 __INLINE__ void resumeImpl() override
98 {
99 // Create an ACL event
100 aclError err = aclrtCreateEvent(&_syncEvent);
101 if (err != ACL_SUCCESS) HICR_THROW_RUNTIME("Can not create synchronize event. Error %d", err);
102
103 // Signal that the execution unit is running
104 _isStreamActive = true;
105
106 // start the sequence of kernels execution
107 _executionUnit->start(_stream);
108
109 // add an event at the end of the operations to query its status and check for completion
110 err = aclrtRecordEvent(_syncEvent, _stream);
111 if (err != ACL_SUCCESS) HICR_THROW_RUNTIME("can not set sync bit to 1. Error %d", err);
112 }
113
114 __INLINE__ void suspendImpl() { HICR_THROW_RUNTIME("Suspend functionality not supported by Ascend backend"); }
115
122 __INLINE__ bool checkFinalizationImpl() override
123 {
124 // Check if the event has been processed
125 aclrtEventRecordedStatus status;
126 aclError err = aclrtQueryEventStatus(_syncEvent, &status);
127 if (err != ACL_SUCCESS) HICR_THROW_RUNTIME("failed to query event status. err %d", err);
128
129 // check the synchronization event status for stream completion
130 if (status == ACL_EVENT_RECORDED_STATUS_NOT_READY) return false;
131
132 // synchronize the stream and destroy it
134
135 return true;
136 }
137
138 private:
139
143 std::shared_ptr<ExecutionUnit> _executionUnit;
144
148 aclrtEvent _syncEvent;
149
153 aclrtStream _stream;
154
158 bool _isStreamActive = false;
159};
160
161} // namespace HiCR::backend::ascend
This file implements the Device class for the Ascend backend.
This file implements the execution unit class for the Ascend backend.
Definition executionState.hpp:38
Definition executionState.hpp:40
__INLINE__ void resumeImpl() override
Definition executionState.hpp:97
__INLINE__ bool checkFinalizationImpl() override
Definition executionState.hpp:122
ExecutionState(const std::shared_ptr< HiCR::ExecutionUnit > executionUnit)
Definition executionState.hpp:48
__INLINE__ void setStream(const aclrtStream stream)
Definition executionState.hpp:70
__INLINE__ void finalizeStream()
Definition executionState.hpp:75
__INLINE__ void suspendImpl()
Definition executionState.hpp:114
Provides a base definition for a HiCR Execution State 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