You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

gpu_event.cc 2.2 kB

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