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/gpu/gpu_event.h"
18 #include "runtime/device/gpu/gpu_common.h"
19
20 namespace mindspore::device::gpu {
GpuEvent()21 GpuEvent::GpuEvent() {
22 auto ret = cudaEventCreate(&event_);
23 if (ret != cudaSuccess) {
24 MS_LOG(ERROR) << "cudaEventCreate failed, ret:" << ret;
25 event_ = nullptr;
26 }
27 }
28
~GpuEvent()29 GpuEvent::~GpuEvent() { CHECK_CUDA_RET_WITH_ERROR_NOTRACE(cudaEventDestroy(event_), "cudaEventDestory failed"); }
30
WaitEvent()31 void GpuEvent::WaitEvent() {
32 MS_EXCEPTION_IF_NULL(wait_stream_);
33 MS_EXCEPTION_IF_NULL(event_);
34 CHECK_CUDA_RET_WITH_EXCEPT_NOTRACE(cudaStreamWaitEvent(wait_stream_, event_, 0), "cudaStreamWaitEvent failed");
35 need_wait_ = false;
36 }
37
RecordEvent()38 void GpuEvent::RecordEvent() {
39 MS_EXCEPTION_IF_NULL(event_);
40 MS_EXCEPTION_IF_NULL(record_stream_);
41 CHECK_CUDA_RET_WITH_EXCEPT_NOTRACE(cudaEventRecord(event_, record_stream_), "cudaEventRecord failed");
42 need_wait_ = true;
43 }
44
SyncEvent()45 void GpuEvent::SyncEvent() {
46 MS_EXCEPTION_IF_NULL(event_);
47 CHECK_CUDA_RET_WITH_EXCEPT_NOTRACE(cudaEventSynchronize(event_), "cudaEventSynchronize failed");
48 }
49
ElapsedTime(float * cost_time,const DeviceEvent * other)50 void GpuEvent::ElapsedTime(float *cost_time, const DeviceEvent *other) {
51 MS_EXCEPTION_IF_NULL(event_);
52 auto gpu_event = static_cast<const GpuEvent *>(other);
53 MS_EXCEPTION_IF_NULL(gpu_event);
54 MS_EXCEPTION_IF_NULL(gpu_event->event_);
55 CHECK_CUDA_RET_WITH_EXCEPT_NOTRACE(cudaEventElapsedTime(cost_time, event_, gpu_event->event_),
56 "cudaEventElapsedTime failed");
57 }
58
NeedWait()59 bool GpuEvent::NeedWait() { return need_wait_; }
60 } // namespace mindspore::device::gpu
61