1 /**
2 * Copyright 2021 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
17 #include "runtime/device/ascend/ascend_event.h"
18
19 #include "runtime/event.h"
20 #include "runtime/stream.h"
21 #include "utils/log_adapter.h"
22
23 namespace mindspore::device::ascend {
AscendEvent()24 AscendEvent::AscendEvent() {
25 auto ret = rtEventCreate(&event_);
26 if (ret != RT_ERROR_NONE) {
27 MS_LOG(ERROR) << "rtEventCreate failed, ret:" << ret;
28 event_ = nullptr;
29 }
30 }
31
AscendTimeEvent()32 AscendTimeEvent::AscendTimeEvent() {
33 auto ret = rtEventCreateWithFlag(&event_, RT_EVENT_TIME_LINE);
34 if (ret != RT_ERROR_NONE) {
35 MS_LOG(ERROR) << "rtEventCreate failed, ret:" << ret;
36 event_ = nullptr;
37 }
38 }
39
~AscendEvent()40 AscendEvent::~AscendEvent() {
41 auto ret = rtEventDestroy(event_);
42 if (ret != RT_ERROR_NONE) {
43 MS_LOG(ERROR) << "rtEventDestory failed, ret:" << ret;
44 }
45 }
46
RecordEvent()47 void AscendEvent::RecordEvent() {
48 MS_EXCEPTION_IF_NULL(event_);
49 MS_EXCEPTION_IF_NULL(record_stream_);
50 auto ret = rtEventRecord(event_, record_stream_);
51 if (ret != RT_ERROR_NONE) {
52 MS_LOG(EXCEPTION) << "rtEventRecord failed, ret:" << ret;
53 }
54 need_wait_ = true;
55 }
56
WaitEvent()57 void AscendEvent::WaitEvent() {
58 MS_EXCEPTION_IF_NULL(event_);
59 MS_EXCEPTION_IF_NULL(wait_stream_);
60 auto ret = rtStreamWaitEvent(wait_stream_, event_);
61 if (ret != RT_ERROR_NONE) {
62 MS_LOG(EXCEPTION) << "rtStreamWaitEvent failed, ret:" << ret;
63 }
64 ret = rtEventReset(event_, wait_stream_);
65 if (ret != RT_ERROR_NONE) {
66 MS_LOG(EXCEPTION) << "rtEventReset failed, ret:" << ret;
67 }
68 need_wait_ = false;
69 }
70
SyncEvent()71 void AscendEvent::SyncEvent() {
72 MS_EXCEPTION_IF_NULL(event_);
73 auto ret = rtEventSynchronize(event_);
74 if (ret != RT_ERROR_NONE) {
75 MS_LOG(EXCEPTION) << "rtEventSynchronize failed, ret:" << ret;
76 }
77 }
78
ElapsedTime(float * cost_time,const DeviceEvent * other)79 void AscendEvent::ElapsedTime(float *cost_time, const DeviceEvent *other) {
80 MS_EXCEPTION_IF_NULL(event_);
81 auto ascend_other = static_cast<const AscendEvent *>(other);
82 MS_EXCEPTION_IF_NULL(ascend_other);
83 MS_EXCEPTION_IF_NULL(ascend_other->event_);
84 auto ret = rtEventElapsedTime(cost_time, event_, ascend_other->event_);
85 if (ret != RT_ERROR_NONE) {
86 MS_LOG(EXCEPTION) << "rtEventElapsedTime failed, ret:" << ret;
87 }
88 }
89
NeedWait()90 bool AscendEvent::NeedWait() { return need_wait_; }
91 } // namespace mindspore::device::ascend
92