device_event_gpu.cc 4.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

15
#include "paddle/fluid/platform/device_event_base.h"
16 17
#include "paddle/fluid/platform/event.h"

18
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
19 20 21
namespace paddle {
namespace platform {
struct CUDADeviceEventWrapper {
22 23
  CUDADeviceEventWrapper(const platform::Place& place, unsigned int flag)
      : inner_event_(flag) {
24
    PADDLE_ENFORCE_EQ(
25
        platform::is_gpu_place(place), true,
26
        platform::errors::PreconditionNotMet(
27 28
            "Required device shall be CUDAPlace, but received %d. ", place));

29
    device_id_ = place.device;
30
    PADDLE_ENFORCE_GT(
31
        device_id_, -1,
32 33
        platform::errors::PreconditionNotMet(
            "Required DeviceOption.device_id > -1, but received %d. ",
34
            device_id_));
35 36 37 38 39 40
  }

  CudaEvent inner_event_;
  int device_id_;
};

41 42 43
void DeviceEventCreateCUDA(DeviceEvent* event, const platform::Place& place,
                           unsigned int flag) {
  event->InitEvent(std::make_shared<CUDADeviceEventWrapper>(place, flag));
44 45
}

46
void DeviceEventRecordCUDA(DeviceEvent* event, const DeviceContext* context) {
47 48 49 50 51 52 53 54 55
  auto* wrapper = static_cast<CUDADeviceEventWrapper*>(event->GetEvent().get());

  auto* cuda_dev_ctx =
      dynamic_cast<const platform::CUDADeviceContext*>(context);
  PADDLE_ENFORCE_NOT_NULL(
      cuda_dev_ctx,
      platform::errors::PreconditionNotMet(
          "Failed to dynamic_cast context into CUDADeviceContext."));

W
Wilber 已提交
56
  wrapper->inner_event_.Record(cuda_dev_ctx->stream());
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
}

bool DeviceEventQueryCUDA(const DeviceEvent* event) {
  auto* wrapper = static_cast<CUDADeviceEventWrapper*>(event->GetEvent().get());
  PADDLE_ENFORCE_NOT_NULL(
      wrapper,
      platform::errors::PreconditionNotMet(
          "Failed to dynamic_cast event into CUDADeviceEventWrapper."));

  return wrapper->inner_event_.Query();
}

void DeviceEventFinishCUDA(const DeviceEvent* event) {
  auto* wrapper = static_cast<CUDADeviceEventWrapper*>(event->GetEvent().get());
  // calling cudaEventSynchronize
  wrapper->inner_event_.Synchronize();
}

75 76
void DeviceEventCUDAWaitCUDA(const DeviceEvent* event,
                             const DeviceContext* context) {
77 78 79 80 81 82 83 84
  auto* wrapper = static_cast<CUDADeviceEventWrapper*>(event->GetEvent().get());
  auto* cuda_dev_ctx =
      dynamic_cast<const platform::CUDADeviceContext*>(context);
  PADDLE_ENFORCE_NOT_NULL(
      cuda_dev_ctx,
      platform::errors::PreconditionNotMet(
          "Failed to dynamic_cast context into CUDADeviceContext."));
  // calling cudaStreamWaitEvent(stream, event, 0)
W
Wilber 已提交
85
  cuda_dev_ctx->WaitEvent(wrapper->inner_event_.GetRawCudaEvent());
86 87
}

88 89
void DeviceEventCPUWaitCUDA(const DeviceEvent* event,
                            const DeviceContext* context) {
90 91 92
  DeviceEventFinishCUDA(event);
}

93 94 95 96
void DeviceEventSetFinishedCUDA(const DeviceEvent* event) {
  // do nothing
}

97 98 99 100
void EventResetCUDA(const DeviceEvent* event) {
  // do nothing
}

101 102 103 104 105 106 107 108 109
}  // namespace platform
}  // namespace paddle

using ::paddle::platform::kCUDA;
using ::paddle::platform::kCPU;
REGISTER_EVENT_CREATE_FUNCTION(kCUDA, paddle::platform::DeviceEventCreateCUDA)
REGISTER_EVENT_RECORD_FUNCTION(kCUDA, paddle::platform::DeviceEventRecordCUDA)
REGISTER_EVENT_QUERY_FUNCTION(kCUDA, paddle::platform::DeviceEventQueryCUDA)
REGISTER_EVENT_FINISH_FUNCTION(kCUDA, paddle::platform::DeviceEventFinishCUDA)
110 111
REGISTER_EVENT_SET_FINISHED_FUNCTION(
    kCUDA, paddle::platform::DeviceEventSetFinishedCUDA)
112 113 114 115
REGISTER_EVENT_WAIT_FUNCTION(kCUDA, kCUDA,
                             paddle::platform::DeviceEventCUDAWaitCUDA)
REGISTER_EVENT_WAIT_FUNCTION(kCPU, kCUDA,
                             paddle::platform::DeviceEventCPUWaitCUDA)
116
REGISTER_EVENT_RESET_FUNCTION(kCUDA, paddle::platform::EventResetCUDA)
117
#endif