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.

ascend_event.cc 2.8 kB

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