From 33e677f74314242d564cfd983663bdf326e2e407 Mon Sep 17 00:00:00 2001 From: Aurelius Date: Thu, 12 Aug 2021 11:03:29 +0000 Subject: [PATCH 1/7] add device_context --- paddle/fluid/platform/device_event.cc | 25 ++++ paddle/fluid/platform/device_event.h | 144 ++++++++++++++++++++++ paddle/fluid/platform/device_event_gpu.cc | 28 +++++ 3 files changed, 197 insertions(+) create mode 100644 paddle/fluid/platform/device_event.cc create mode 100644 paddle/fluid/platform/device_event.h create mode 100644 paddle/fluid/platform/device_event_gpu.cc diff --git a/paddle/fluid/platform/device_event.cc b/paddle/fluid/platform/device_event.cc new file mode 100644 index 0000000000000..6b2d4b49a984a --- /dev/null +++ b/paddle/fluid/platform/device_event.cc @@ -0,0 +1,25 @@ +// 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. + +#include "paddle/fluid/platform/device_event.h" + +namespace paddle { +namespace platform { + +EventCreateFunction DeviceEvent::event_creator_[MaxDeviceTypes]; +EventRecordFunction DeviceEvent::event_recorder_[MaxDeviceTypes]; +EventQueryFunction DeviceEvent::event_querier_[MaxDeviceTypes]; + +} // namespace platform +} // namespace paddle diff --git a/paddle/fluid/platform/device_event.h b/paddle/fluid/platform/device_event.h new file mode 100644 index 0000000000000..47f6bccc75723 --- /dev/null +++ b/paddle/fluid/platform/device_event.h @@ -0,0 +1,144 @@ +// 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. +#pragma once +#include +#include "paddle/fluid/platform/enforce.h" +#include "paddle/fluid/platform/place.h" + +namespace paddle { +namespace platform { + +class DeviceOption; +class DeviceEvent; + +enum class DeviceType { + kCPU = 0, + kCUDA = 1, // CUDA. + kHIP = 2, // AMD HIP + + MAX_DEVICE_TYPES = 3, +}; + +constexpr int MaxDeviceTypes = static_cast(DeviceType::MAX_DEVICE_TYPES); + +typedef void (*EventCreateFunction)(DeviceEvent*, const DeviceOption&); +typedef void (*EventRecordFunction)(DeviceEvent*, const platform::Place&, + const void*); +typedef bool (*EventQueryFunction)(const DeviceEvent*); + +class DeviceOption { + public: + explicit DeviceOption(int device_type) : device_type_(device_type) {} + + int device_type() const { return device_type_; } + + private: + int device_type_; +}; + +class DeviceEvent { + public: + explicit DeviceEvent(const DeviceOption& device_option) + : event_(), + type_(device_option.device_type()), + device_option_(device_option) { + PADDLE_ENFORCE_LT(type_, MaxDeviceTypes, + platform::errors::PreconditionNotMet( + "Required type < %d, but received type = %d", + MaxDeviceTypes, type_)); + PADDLE_ENFORCE_NOT_NULL( + event_creator_[type_], + platform::errors::Unavailable( + "event_creator_[%d] shall not be nullptr.", type_)); + event_creator_[type_](this, device_option_); + } + + ~DeviceEvent() {} + + void Record(const platform::Place& place, const void* dev_ctx) { + PADDLE_ENFORCE_NOT_NULL( + event_recorder_[type_], + platform::errors::Unavailable( + "event_recorder_[%d] shall not be nullptr.", type_)); + event_recorder_[type_](this, place, dev_ctx); + } + + bool Query() { + PADDLE_ENFORCE_NOT_NULL( + event_querier_[type_], + platform::errors::Unavailable( + "event_querier_[%d] shall not be nullptr.", type_)); + event_querier_[type_](this); + } + + private: + std::shared_ptr event_; + int type_; + DeviceOption device_option_; + + static EventCreateFunction event_creator_[MaxDeviceTypes]; + static EventRecordFunction event_recorder_[MaxDeviceTypes]; + static EventQueryFunction event_querier_[MaxDeviceTypes]; + + template + friend struct EventCreateFunctionRegisterer; + + template + friend struct EventRecordFunctionRegisterer; + + template + friend struct EventQueryFunctionRegisterer; +}; + +template +struct EventCreateFunctionRegisterer { + explicit EventCreateFunctionRegisterer(EventCreateFunction func) { + auto type_idx = DeviceTypeToId(device_type); + DeviceEvent::event_creator_[type_idx] = func; + } +}; +#define REGISTER_EVENT_CREATE_FUNCTION(device_type, func) \ + namespace { \ + static EventCreateFunctionRegisterer \ + g_device_event_create_##type_idx(func) \ + } + +template +struct EventRecordFunctionRegisterer { + explicit EventRecordFunctionRegisterer(EventRecordFunction func) { + auto type_idx = DeviceTypeToId(device_type); + DeviceEvent::event_recorder_[type_idx] = func; + } +}; +#define REGISTER_EVENT_RECORD_FUNCTION(device_type, func) \ + namespace { \ + static EventRecordFunctionRegisterer \ + g_device_event_record_##type_idx(func) \ + } + +template +struct EventQueryFunctionRegisterer { + explicit EventQueryFunctionRegisterer(EventQueryFunction func) { + auto type_idx = DeviceTypeToId(device_type); + DeviceEvent::event_querier_[type_idx] = func; + } +}; +#define REGISTER_EVENT_QUERY_FUNCTION(device_type, func) \ + namespace { \ + static EventQueryFunctionRegisterer \ + g_device_event_query_##type_idx(func) \ + } + +} // namespace platform +} // namespace paddle diff --git a/paddle/fluid/platform/device_event_gpu.cc b/paddle/fluid/platform/device_event_gpu.cc new file mode 100644 index 0000000000000..953c9054e3510 --- /dev/null +++ b/paddle/fluid/platform/device_event_gpu.cc @@ -0,0 +1,28 @@ +// 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. + +#include "paddle/fluid/platform/device_event.h" +#include "paddle/fluid/platform/event.h" + +namespace paddle { +namespace platform { + +struct CUDADeviceEvent { + explicit CUDADeviceEvent(const DeviceOption& dev_opt) {} + + CudaEvent inner_event_; +}; + +} // namespace platform +} // namespace paddle From df577acd57d7d57e2c6eba50eab635db54dd4bdd Mon Sep 17 00:00:00 2001 From: Aurelius Date: Mon, 16 Aug 2021 02:57:12 +0000 Subject: [PATCH 2/7] add gtest for device_event_gpu --- paddle/fluid/platform/CMakeLists.txt | 5 +++ paddle/fluid/platform/device_event.h | 16 ++++++-- paddle/fluid/platform/device_event_gpu.cc | 44 ++++++++++++++++++++-- paddle/fluid/platform/device_event_test.cc | 36 ++++++++++++++++++ 4 files changed, 95 insertions(+), 6 deletions(-) create mode 100644 paddle/fluid/platform/device_event_test.cc diff --git a/paddle/fluid/platform/CMakeLists.txt b/paddle/fluid/platform/CMakeLists.txt index efd25bc892940..e8178abc6ac9d 100644 --- a/paddle/fluid/platform/CMakeLists.txt +++ b/paddle/fluid/platform/CMakeLists.txt @@ -151,11 +151,16 @@ endif() cc_test(init_test SRCS init_test.cc DEPS device_context) +cc_library(device_event SRCS device_event.cc DEPS place enforce device_context) +cc_library(device_event_gpu SRCS device_event_gpu.cc DEPS device_event) + + if(WITH_GPU) nv_test(device_context_test SRCS device_context_test.cu DEPS device_context gpu_info) nv_test(cudnn_helper_test SRCS cudnn_helper_test.cc DEPS dynload_cuda) nv_test(cudnn_desc_test SRCS cudnn_desc_test.cc DEPS dynload_cuda) nv_test(transform_test SRCS transform_test.cu DEPS memory place device_context) + nv_test(device_event_test SRCS device_event_test.cc DEPS device_event_gpu) endif() if(WITH_ROCM) diff --git a/paddle/fluid/platform/device_event.h b/paddle/fluid/platform/device_event.h index 47f6bccc75723..d513efa77df84 100644 --- a/paddle/fluid/platform/device_event.h +++ b/paddle/fluid/platform/device_event.h @@ -41,10 +41,16 @@ class DeviceOption { public: explicit DeviceOption(int device_type) : device_type_(device_type) {} + DeviceOption(int device_type, int device_id) + : device_type_(device_type), device_id_(device_id) {} + int device_type() const { return device_type_; } + int device_id() const { return device_id_; } + private: int device_type_; + int device_id_; }; class DeviceEvent { @@ -82,6 +88,10 @@ class DeviceEvent { event_querier_[type_](this); } + void InitEvent(std::shared_ptr event) { event_ = event; } + + std::shared_ptr GetEvent() const { return event_; } + private: std::shared_ptr event_; int type_; @@ -111,7 +121,7 @@ struct EventCreateFunctionRegisterer { #define REGISTER_EVENT_CREATE_FUNCTION(device_type, func) \ namespace { \ static EventCreateFunctionRegisterer \ - g_device_event_create_##type_idx(func) \ + g_device_event_create_##type_idx(func); \ } template @@ -124,7 +134,7 @@ struct EventRecordFunctionRegisterer { #define REGISTER_EVENT_RECORD_FUNCTION(device_type, func) \ namespace { \ static EventRecordFunctionRegisterer \ - g_device_event_record_##type_idx(func) \ + g_device_event_record_##type_idx(func); \ } template @@ -137,7 +147,7 @@ struct EventQueryFunctionRegisterer { #define REGISTER_EVENT_QUERY_FUNCTION(device_type, func) \ namespace { \ static EventQueryFunctionRegisterer \ - g_device_event_query_##type_idx(func) \ + g_device_event_query_##type_idx(func); \ } } // namespace platform diff --git a/paddle/fluid/platform/device_event_gpu.cc b/paddle/fluid/platform/device_event_gpu.cc index 953c9054e3510..47b85a1f8a350 100644 --- a/paddle/fluid/platform/device_event_gpu.cc +++ b/paddle/fluid/platform/device_event_gpu.cc @@ -13,16 +13,54 @@ // limitations under the License. #include "paddle/fluid/platform/device_event.h" +#include "paddle/fluid/platform/device_context.h" #include "paddle/fluid/platform/event.h" namespace paddle { namespace platform { - -struct CUDADeviceEvent { - explicit CUDADeviceEvent(const DeviceOption& dev_opt) {} +#ifdef PADDLE_WITH_CUDA +struct CUDADeviceEventWrapper { + explicit CUDADeviceEventWrapper(const DeviceOption& dev_opt) { + PADDLE_ENFORCE_EQ( + dev_opt.device_type(), static_cast(DeviceType::kCUDA), + platform::errors::PreconditionNotMet( + "Required device type shall be CUDA, but received %d. ", + dev_opt.device_type())); + PADDLE_ENFORCE_GT( + dev_opt.device_id(), -1, + platform::errors::PreconditionNotMet( + "Required DeviceOption.device_id > -1, but received %d. ", + dev_opt.device_id())); + device_id_ = dev_opt.device_id(); + inner_event_ = platform::CudaEvent(); + } CudaEvent inner_event_; + int device_id_; }; +void DeviceEventCreateCUDA(DeviceEvent* event, const DeviceOption& dev_opt) { + event->InitEvent(std::make_shared(dev_opt)); +} + +void DeviceEventRecordCUDA(DeviceEvent* event, const platform::Place& place, + const void* context) { + auto* wrapper = static_cast(event->GetEvent().get()); + auto* cuda_dev_ctx = static_cast(context); + + // TODO(Aurelius84): verify device_id and stream is as expected. + wrapper->inner_event_.Record(cuda_dev_ctx->context()->Stream()); +} + +bool DeviceEventQueryCUDA(const DeviceEvent* event) { + auto* wrapper = static_cast(event->GetEvent().get()); + return wrapper->inner_event_.Query(); +} + +REGISTER_EVENT_CREATE_FUNCTION(DeviceType::kCUDA, DeviceEventCreateCUDA) +REGISTER_EVENT_RECORD_FUNCTION(DeviceType::kCUDA, DeviceEventRecordCUDA) +REGISTER_EVENT_QUERY_FUNCTION(DeviceType::kCUDA, DeviceEventQueryCUDA) + +#endif } // namespace platform } // namespace paddle diff --git a/paddle/fluid/platform/device_event_test.cc b/paddle/fluid/platform/device_event_test.cc new file mode 100644 index 0000000000000..d7c445bef72c3 --- /dev/null +++ b/paddle/fluid/platform/device_event_test.cc @@ -0,0 +1,36 @@ +// 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. + +#include "paddle/fluid/platform/device_event.h" + +#include "glog/logging.h" +#include "gtest/gtest.h" + +#ifdef PADDLE_WITH_CUDA +Test(DeviceEvent, GPU) { + using paddle::platform::CUDAPlace; + using paddle::platform::DeviceOption; + using paddle::platform::DeviceEvent; + + auto& pool = DeviceContextPool::Instance(); + auto place = CUDAPlace(0); + auto* context = pool.get(place); + DeviceOption dev_opt(place.device); + + DeviceEvent event(dev_opt); + event.Record(place, context); + bool status = event.Query(); + ASSERT_EQ(status, true); +} +#endif From c25c49494d6a7e10922cf068fdcd96026aa0915c Mon Sep 17 00:00:00 2001 From: Aurelius Date: Mon, 16 Aug 2021 07:25:00 +0000 Subject: [PATCH 3/7] Remvoe duplicate DeviceType --- paddle/fluid/platform/device_context.h | 2 ++ paddle/fluid/platform/device_event.h | 16 +++++++--------- paddle/fluid/platform/device_event_gpu.cc | 11 +++++------ paddle/fluid/platform/device_event_test.cc | 1 + 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/paddle/fluid/platform/device_context.h b/paddle/fluid/platform/device_context.h index abac12ff26648..87ce7c438b65b 100644 --- a/paddle/fluid/platform/device_context.h +++ b/paddle/fluid/platform/device_context.h @@ -97,6 +97,8 @@ enum DeviceType { CUDA = 1, XPU = 2, NPU = 3, + + MAX_DEVICE_TYPES = 4, }; DeviceType Place2DeviceType(const platform::Place& place); diff --git a/paddle/fluid/platform/device_event.h b/paddle/fluid/platform/device_event.h index d513efa77df84..c77725d92775f 100644 --- a/paddle/fluid/platform/device_event.h +++ b/paddle/fluid/platform/device_event.h @@ -13,6 +13,7 @@ // limitations under the License. #pragma once #include +#include "paddle/fluid/platform/device_context.h" #include "paddle/fluid/platform/enforce.h" #include "paddle/fluid/platform/place.h" @@ -22,15 +23,8 @@ namespace platform { class DeviceOption; class DeviceEvent; -enum class DeviceType { - kCPU = 0, - kCUDA = 1, // CUDA. - kHIP = 2, // AMD HIP - - MAX_DEVICE_TYPES = 3, -}; - -constexpr int MaxDeviceTypes = static_cast(DeviceType::MAX_DEVICE_TYPES); +constexpr int MaxDeviceTypes = + static_cast(platform::DeviceType::MAX_DEVICE_TYPES); typedef void (*EventCreateFunction)(DeviceEvent*, const DeviceOption&); typedef void (*EventRecordFunction)(DeviceEvent*, const platform::Place&, @@ -111,6 +105,10 @@ class DeviceEvent { friend struct EventQueryFunctionRegisterer; }; +inline int DeviceTypeToId(const DeviceType& device_type) { + return static_cast(device_type); +} + template struct EventCreateFunctionRegisterer { explicit EventCreateFunctionRegisterer(EventCreateFunction func) { diff --git a/paddle/fluid/platform/device_event_gpu.cc b/paddle/fluid/platform/device_event_gpu.cc index 47b85a1f8a350..d5ef0762aa53b 100644 --- a/paddle/fluid/platform/device_event_gpu.cc +++ b/paddle/fluid/platform/device_event_gpu.cc @@ -13,7 +13,6 @@ // limitations under the License. #include "paddle/fluid/platform/device_event.h" -#include "paddle/fluid/platform/device_context.h" #include "paddle/fluid/platform/event.h" namespace paddle { @@ -22,7 +21,7 @@ namespace platform { struct CUDADeviceEventWrapper { explicit CUDADeviceEventWrapper(const DeviceOption& dev_opt) { PADDLE_ENFORCE_EQ( - dev_opt.device_type(), static_cast(DeviceType::kCUDA), + dev_opt.device_type(), static_cast(DeviceType::CUDA), platform::errors::PreconditionNotMet( "Required device type shall be CUDA, but received %d. ", dev_opt.device_type())); @@ -49,7 +48,7 @@ void DeviceEventRecordCUDA(DeviceEvent* event, const platform::Place& place, auto* cuda_dev_ctx = static_cast(context); // TODO(Aurelius84): verify device_id and stream is as expected. - wrapper->inner_event_.Record(cuda_dev_ctx->context()->Stream()); + wrapper->inner_event_.Record(*cuda_dev_ctx->context()->Stream()); } bool DeviceEventQueryCUDA(const DeviceEvent* event) { @@ -57,9 +56,9 @@ bool DeviceEventQueryCUDA(const DeviceEvent* event) { return wrapper->inner_event_.Query(); } -REGISTER_EVENT_CREATE_FUNCTION(DeviceType::kCUDA, DeviceEventCreateCUDA) -REGISTER_EVENT_RECORD_FUNCTION(DeviceType::kCUDA, DeviceEventRecordCUDA) -REGISTER_EVENT_QUERY_FUNCTION(DeviceType::kCUDA, DeviceEventQueryCUDA) +REGISTER_EVENT_CREATE_FUNCTION(DeviceType::CUDA, DeviceEventCreateCUDA) +REGISTER_EVENT_RECORD_FUNCTION(DeviceType::CUDA, DeviceEventRecordCUDA) +REGISTER_EVENT_QUERY_FUNCTION(DeviceType::CUDA, DeviceEventQueryCUDA) #endif } // namespace platform diff --git a/paddle/fluid/platform/device_event_test.cc b/paddle/fluid/platform/device_event_test.cc index d7c445bef72c3..9c885224954eb 100644 --- a/paddle/fluid/platform/device_event_test.cc +++ b/paddle/fluid/platform/device_event_test.cc @@ -28,6 +28,7 @@ Test(DeviceEvent, GPU) { auto* context = pool.get(place); DeviceOption dev_opt(place.device); + ASSERT_NE(context, nullptr); DeviceEvent event(dev_opt); event.Record(place, context); bool status = event.Query(); From 73831b0156b62d95adf8b2c91fe7590bad3c6d7b Mon Sep 17 00:00:00 2001 From: Aurelius84 Date: Tue, 17 Aug 2021 02:51:17 +0000 Subject: [PATCH 4/7] push for test --- paddle/fluid/platform/CMakeLists.txt | 2 +- paddle/fluid/platform/device_event.cc | 7 ++++--- paddle/fluid/platform/device_event.h | 21 ++++++++++++++++----- paddle/fluid/platform/device_event_gpu.cc | 12 +++++++----- paddle/fluid/platform/device_event_test.cc | 19 +++++++++++++------ 5 files changed, 41 insertions(+), 20 deletions(-) diff --git a/paddle/fluid/platform/CMakeLists.txt b/paddle/fluid/platform/CMakeLists.txt index e8178abc6ac9d..b9663e70f4bfe 100644 --- a/paddle/fluid/platform/CMakeLists.txt +++ b/paddle/fluid/platform/CMakeLists.txt @@ -151,7 +151,7 @@ endif() cc_test(init_test SRCS init_test.cc DEPS device_context) -cc_library(device_event SRCS device_event.cc DEPS place enforce device_context) +cc_library(device_event SRCS device_event.cc DEPS place enforce device_context op_registry) cc_library(device_event_gpu SRCS device_event_gpu.cc DEPS device_event) diff --git a/paddle/fluid/platform/device_event.cc b/paddle/fluid/platform/device_event.cc index 6b2d4b49a984a..60a4ca417628e 100644 --- a/paddle/fluid/platform/device_event.cc +++ b/paddle/fluid/platform/device_event.cc @@ -16,10 +16,11 @@ namespace paddle { namespace platform { +#define PD_CX __attribute__((visibility("default"))) -EventCreateFunction DeviceEvent::event_creator_[MaxDeviceTypes]; -EventRecordFunction DeviceEvent::event_recorder_[MaxDeviceTypes]; -EventQueryFunction DeviceEvent::event_querier_[MaxDeviceTypes]; +PD_CX EventCreateFunction DeviceEvent::event_creator_[MaxDeviceTypes]; +PD_CX EventRecordFunction DeviceEvent::event_recorder_[MaxDeviceTypes]; +PD_CX EventQueryFunction DeviceEvent::event_querier_[MaxDeviceTypes]; } // namespace platform } // namespace paddle diff --git a/paddle/fluid/platform/device_event.h b/paddle/fluid/platform/device_event.h index c77725d92775f..6c6e7e06b220f 100644 --- a/paddle/fluid/platform/device_event.h +++ b/paddle/fluid/platform/device_event.h @@ -13,6 +13,7 @@ // limitations under the License. #pragma once #include +#include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/platform/device_context.h" #include "paddle/fluid/platform/enforce.h" #include "paddle/fluid/platform/place.h" @@ -79,7 +80,7 @@ class DeviceEvent { event_querier_[type_], platform::errors::Unavailable( "event_querier_[%d] shall not be nullptr.", type_)); - event_querier_[type_](this); + return event_querier_[type_](this); } void InitEvent(std::shared_ptr event) { event_ = event; } @@ -114,14 +115,24 @@ struct EventCreateFunctionRegisterer { explicit EventCreateFunctionRegisterer(EventCreateFunction func) { auto type_idx = DeviceTypeToId(device_type); DeviceEvent::event_creator_[type_idx] = func; + VLOG(2) << "register creator " << type_idx << " with " + << DeviceEvent::event_creator_[type_idx]; } + void Touch() {} }; -#define REGISTER_EVENT_CREATE_FUNCTION(device_type, func) \ - namespace { \ - static EventCreateFunctionRegisterer \ - g_device_event_create_##type_idx(func); \ + +#define REGISTER_EVENT_CREATE_FUNCTION(device_type, func) \ + static ::paddle::platform::EventCreateFunctionRegisterer \ + g_device_event_create_1(func); \ + int touch_g_device_event_create_1() { \ + g_device_event_create_1.Touch(); \ + return 0; \ } +#define USE_EVENT(device_type) \ + extern int touch_g_device_event_create_1(); \ + UNUSED static int use_event_itself_1 = touch_g_device_event_create_1(); + template struct EventRecordFunctionRegisterer { explicit EventRecordFunctionRegisterer(EventRecordFunction func) { diff --git a/paddle/fluid/platform/device_event_gpu.cc b/paddle/fluid/platform/device_event_gpu.cc index d5ef0762aa53b..ba5118ccb6c4b 100644 --- a/paddle/fluid/platform/device_event_gpu.cc +++ b/paddle/fluid/platform/device_event_gpu.cc @@ -15,9 +15,9 @@ #include "paddle/fluid/platform/device_event.h" #include "paddle/fluid/platform/event.h" +#ifdef PADDLE_WITH_CUDA namespace paddle { namespace platform { -#ifdef PADDLE_WITH_CUDA struct CUDADeviceEventWrapper { explicit CUDADeviceEventWrapper(const DeviceOption& dev_opt) { PADDLE_ENFORCE_EQ( @@ -56,10 +56,12 @@ bool DeviceEventQueryCUDA(const DeviceEvent* event) { return wrapper->inner_event_.Query(); } -REGISTER_EVENT_CREATE_FUNCTION(DeviceType::CUDA, DeviceEventCreateCUDA) -REGISTER_EVENT_RECORD_FUNCTION(DeviceType::CUDA, DeviceEventRecordCUDA) -REGISTER_EVENT_QUERY_FUNCTION(DeviceType::CUDA, DeviceEventQueryCUDA) +// REGISTER_EVENT_RECORD_FUNCTION(DeviceType::CUDA, DeviceEventRecordCUDA) +// REGISTER_EVENT_QUERY_FUNCTION(DeviceType::CUDA, DeviceEventQueryCUDA) -#endif } // namespace platform } // namespace paddle + +using ::paddle::platform::DeviceType::CUDA; +REGISTER_EVENT_CREATE_FUNCTION(CUDA, paddle::platform::DeviceEventCreateCUDA) +#endif diff --git a/paddle/fluid/platform/device_event_test.cc b/paddle/fluid/platform/device_event_test.cc index 9c885224954eb..cbee9a6c442f9 100644 --- a/paddle/fluid/platform/device_event_test.cc +++ b/paddle/fluid/platform/device_event_test.cc @@ -17,21 +17,28 @@ #include "glog/logging.h" #include "gtest/gtest.h" +USE_EVENT(1); + #ifdef PADDLE_WITH_CUDA -Test(DeviceEvent, GPU) { +TEST(DeviceEvent, GPU) { + VLOG(1) << "In Test"; using paddle::platform::CUDAPlace; using paddle::platform::DeviceOption; using paddle::platform::DeviceEvent; + using paddle::platform::DeviceContextPool; + using paddle::platform::DeviceType; auto& pool = DeviceContextPool::Instance(); auto place = CUDAPlace(0); - auto* context = pool.get(place); - DeviceOption dev_opt(place.device); + auto* context = pool.Get(place); + int device_type = static_cast(DeviceType::CUDA); + DeviceOption dev_opt(device_type, place.device); ASSERT_NE(context, nullptr); DeviceEvent event(dev_opt); - event.Record(place, context); - bool status = event.Query(); - ASSERT_EQ(status, true); + ASSERT_NE(event.GetEvent().get(), nullptr); + // event.Record(place, context); + // bool status = event.Query(); + // ASSERT_EQ(status, true); } #endif From 81eaa14223c4e98aa0d2eec89b6c402cf9d3d1ae Mon Sep 17 00:00:00 2001 From: Aurelius84 Date: Tue, 17 Aug 2021 08:58:27 +0000 Subject: [PATCH 5/7] add unittest --- paddle/fluid/platform/device_event.cc | 9 +- paddle/fluid/platform/device_event.h | 172 +++++++++++++++++---- paddle/fluid/platform/device_event_gpu.cc | 52 ++++++- paddle/fluid/platform/device_event_test.cc | 50 +++++- paddle/fluid/pybind/cuda_streams_py.cc | 2 +- 5 files changed, 236 insertions(+), 49 deletions(-) diff --git a/paddle/fluid/platform/device_event.cc b/paddle/fluid/platform/device_event.cc index 60a4ca417628e..2c96de163799f 100644 --- a/paddle/fluid/platform/device_event.cc +++ b/paddle/fluid/platform/device_event.cc @@ -16,11 +16,12 @@ namespace paddle { namespace platform { -#define PD_CX __attribute__((visibility("default"))) -PD_CX EventCreateFunction DeviceEvent::event_creator_[MaxDeviceTypes]; -PD_CX EventRecordFunction DeviceEvent::event_recorder_[MaxDeviceTypes]; -PD_CX EventQueryFunction DeviceEvent::event_querier_[MaxDeviceTypes]; +EventCreateFunction DeviceEvent::event_creator_[MaxDeviceTypes]; +EventRecordFunction DeviceEvent::event_recorder_[MaxDeviceTypes]; +EventQueryFunction DeviceEvent::event_querier_[MaxDeviceTypes]; +EventFinishFunction DeviceEvent::event_finisher_[MaxDeviceTypes]; +EventWaitFunction DeviceEvent::event_waiter_[MaxDeviceTypes][MaxDeviceTypes]; } // namespace platform } // namespace paddle diff --git a/paddle/fluid/platform/device_event.h b/paddle/fluid/platform/device_event.h index 6c6e7e06b220f..7e2ea2ae8b96a 100644 --- a/paddle/fluid/platform/device_event.h +++ b/paddle/fluid/platform/device_event.h @@ -29,8 +29,14 @@ constexpr int MaxDeviceTypes = typedef void (*EventCreateFunction)(DeviceEvent*, const DeviceOption&); typedef void (*EventRecordFunction)(DeviceEvent*, const platform::Place&, - const void*); + const DeviceContext*); typedef bool (*EventQueryFunction)(const DeviceEvent*); +typedef void (*EventFinishFunction)(const DeviceEvent*); +typedef void (*EventWaitFunction)(const DeviceEvent*, DeviceContext*); + +inline int DeviceTypeToId(const DeviceType& device_type) { + return static_cast(device_type); +} class DeviceOption { public: @@ -67,7 +73,7 @@ class DeviceEvent { ~DeviceEvent() {} - void Record(const platform::Place& place, const void* dev_ctx) { + void Record(const platform::Place& place, const DeviceContext* dev_ctx) { PADDLE_ENFORCE_NOT_NULL( event_recorder_[type_], platform::errors::Unavailable( @@ -83,6 +89,23 @@ class DeviceEvent { return event_querier_[type_](this); } + void Finish() const { + PADDLE_ENFORCE_NOT_NULL( + event_finisher_[type_], + platform::errors::Unavailable( + "event_finisher_[%d] shall not be nullptr.", type_)); + event_finisher_[type_](this); + } + + void Wait(const DeviceType& waiter_type, DeviceContext* context) const { + auto waiter_idx = DeviceTypeToId(waiter_type); + PADDLE_ENFORCE_NOT_NULL( + event_waiter_[waiter_idx][type_], + platform::errors::Unavailable( + "event_waiter_[%d][%d] shall not be nullptr.", waiter_idx, type_)); + event_waiter_[waiter_idx][type_](this, context); + } + void InitEvent(std::shared_ptr event) { event_ = event; } std::shared_ptr GetEvent() const { return event_; } @@ -95,6 +118,8 @@ class DeviceEvent { static EventCreateFunction event_creator_[MaxDeviceTypes]; static EventRecordFunction event_recorder_[MaxDeviceTypes]; static EventQueryFunction event_querier_[MaxDeviceTypes]; + static EventFinishFunction event_finisher_[MaxDeviceTypes]; + static EventWaitFunction event_waiter_[MaxDeviceTypes][MaxDeviceTypes]; template friend struct EventCreateFunctionRegisterer; @@ -104,60 +129,149 @@ class DeviceEvent { template friend struct EventQueryFunctionRegisterer; + + template + friend struct EventFinishFunctionRegisterer; + + template + friend struct EventWaitFunctionRegisterer; }; -inline int DeviceTypeToId(const DeviceType& device_type) { - return static_cast(device_type); -} +/** + * check if MACRO is used in GLOBAL NAMESPACE. + */ +#define STATIC_ASSERT_GLOBAL_NAMESPACE(uniq_name, msg) \ + struct __test_global_namespace_##uniq_name##__ {}; \ + static_assert(std::is_same<::__test_global_namespace_##uniq_name##__, \ + __test_global_namespace_##uniq_name##__>::value, \ + msg) +// =============== Register for Create =============== template -struct EventCreateFunctionRegisterer { +struct EventCreateFunctionRegisterer : public framework::Registrar { explicit EventCreateFunctionRegisterer(EventCreateFunction func) { auto type_idx = DeviceTypeToId(device_type); + VLOG(3) << "register event_creator with type_id :" << type_idx; DeviceEvent::event_creator_[type_idx] = func; - VLOG(2) << "register creator " << type_idx << " with " - << DeviceEvent::event_creator_[type_idx]; } - void Touch() {} }; -#define REGISTER_EVENT_CREATE_FUNCTION(device_type, func) \ - static ::paddle::platform::EventCreateFunctionRegisterer \ - g_device_event_create_1(func); \ - int touch_g_device_event_create_1() { \ - g_device_event_create_1.Touch(); \ - return 0; \ +#define REGISTER_EVENT_CREATE_FUNCTION(device_type, func) \ + STATIC_ASSERT_GLOBAL_NAMESPACE( \ + __reg_event_creator__##device_type, \ + "REGISTER_EVENT_CREATE_FUNCTION must be called in global namespace"); \ + static ::paddle::platform::EventCreateFunctionRegisterer \ + __reg_event_create_##device_type##__(func); \ + int TouchDeviceEventCreate##device_type() { \ + __reg_event_create_##device_type##__.Touch(); \ + return 0; \ } -#define USE_EVENT(device_type) \ - extern int touch_g_device_event_create_1(); \ - UNUSED static int use_event_itself_1 = touch_g_device_event_create_1(); - +// =============== Register for Record =============== template -struct EventRecordFunctionRegisterer { +struct EventRecordFunctionRegisterer : public framework::Registrar { explicit EventRecordFunctionRegisterer(EventRecordFunction func) { auto type_idx = DeviceTypeToId(device_type); + VLOG(3) << "register event_recorder with type_id :" << type_idx; DeviceEvent::event_recorder_[type_idx] = func; } }; -#define REGISTER_EVENT_RECORD_FUNCTION(device_type, func) \ - namespace { \ - static EventRecordFunctionRegisterer \ - g_device_event_record_##type_idx(func); \ + +#define REGISTER_EVENT_RECORD_FUNCTION(device_type, func) \ + STATIC_ASSERT_GLOBAL_NAMESPACE( \ + __reg_event_recorder__##device_type, \ + "REGISTER_EVENT_RECORD_FUNCTION must be called in global namespace"); \ + static ::paddle::platform::EventRecordFunctionRegisterer \ + __reg_event_record_##device_type##__(func); \ + int TouchDeviceEventRecord##device_type() { \ + __reg_event_record_##device_type##__.Touch(); \ + return 0; \ } +// =============== Register for Query =============== template -struct EventQueryFunctionRegisterer { +struct EventQueryFunctionRegisterer : public framework::Registrar { explicit EventQueryFunctionRegisterer(EventQueryFunction func) { auto type_idx = DeviceTypeToId(device_type); + VLOG(3) << "register event_querier with type_id :" << type_idx; DeviceEvent::event_querier_[type_idx] = func; } }; -#define REGISTER_EVENT_QUERY_FUNCTION(device_type, func) \ - namespace { \ - static EventQueryFunctionRegisterer \ - g_device_event_query_##type_idx(func); \ + +#define REGISTER_EVENT_QUERY_FUNCTION(device_type, func) \ + STATIC_ASSERT_GLOBAL_NAMESPACE( \ + __reg_event_querier__##device_type, \ + "REGISTER_EVENT_QUERY_FUNCTION must be called in global namespace"); \ + static ::paddle::platform::EventQueryFunctionRegisterer \ + __reg_event_query_##device_type##__(func); \ + int TouchDeviceEventQuery##device_type() { \ + __reg_event_query_##device_type##__.Touch(); \ + return 0; \ } +// =============== Register for Finish =============== +template +struct EventFinishFunctionRegisterer : public framework::Registrar { + explicit EventFinishFunctionRegisterer(EventFinishFunction func) { + auto type_idx = DeviceTypeToId(device_type); + VLOG(3) << "register event_finisher with type_id :" << type_idx; + DeviceEvent::event_finisher_[type_idx] = func; + } +}; + +#define REGISTER_EVENT_FINISH_FUNCTION(device_type, func) \ + STATIC_ASSERT_GLOBAL_NAMESPACE( \ + __reg_event_finishier__##device_type, \ + "REGISTER_EVENT_FINISH_FUNCTION must be called in global namespace"); \ + static ::paddle::platform::EventFinishFunctionRegisterer \ + __reg_event_finish_##device_type##__(func); \ + int TouchDeviceEventFinish##device_type() { \ + __reg_event_finish_##device_type##__.Touch(); \ + return 0; \ + } + +// =============== Register for Wait =============== +template +struct EventWaitFunctionRegisterer : public framework::Registrar { + explicit EventWaitFunctionRegisterer(EventWaitFunction func) { + auto waiter_idx = DeviceTypeToId(waiter_type); + auto event_idx = DeviceTypeToId(event_type); + VLOG(3) << "register event_finisher with waiter_idx : " << type_idx + << ", event_idx : " << event_idx; + DeviceEvent::event_waiter_[waiter_idx][event_idx] = func; + } +}; + +#define REGISTER_EVENT_WAIT_FUNCTION(waiter_type, event_type, func) \ + STATIC_ASSERT_GLOBAL_NAMESPACE( \ + __reg_event_waiter__##waiter_type##event_type, \ + "REGISTER_EVENT_WAIT_FUNCTION must be called in global namespace"); \ + static ::paddle::platform::EventWaitFunctionRegisterer \ + __reg_event_wait_##waiter_type##event_type##__(func); \ + int TouchDeviceEventWait##waiter_type##event_type() { \ + __reg_event_wait_##waiter_type##event_type##__.Touch(); \ + return 0; \ + } + +#define USE_EVENT(device_type) \ + extern int TouchDeviceEventCreate##device_type(); \ + extern int TouchDeviceEventRecord##device_type(); \ + extern int TouchDeviceEventQuery##device_type(); \ + extern int TouchDeviceEventFinish##device_type(); \ + UNUSED static int use_event_creator_##device_type = \ + TouchDeviceEventCreate##device_type(); \ + UNUSED static int use_event_recorder_##device_type = \ + TouchDeviceEventRecord##device_type(); \ + UNUSED static int use_event_querier_##device_type = \ + TouchDeviceEventQuery##device_type(); \ + UNUSED static int use_event_finisher_##device_type = \ + TouchDeviceEventFinish##device_type(); + +#define USE_EVENT_WAIT(waiter_type, event_type) \ + extern int TouchDeviceEventWait##waiter_type##event_type(); \ + UNUSED static int use_event_waiter_##waiter_type##event_type = \ + TouchDeviceEventWait##waiter_type##event_type(); + } // namespace platform } // namespace paddle diff --git a/paddle/fluid/platform/device_event_gpu.cc b/paddle/fluid/platform/device_event_gpu.cc index ba5118ccb6c4b..53d2a6507c509 100644 --- a/paddle/fluid/platform/device_event_gpu.cc +++ b/paddle/fluid/platform/device_event_gpu.cc @@ -19,7 +19,8 @@ namespace paddle { namespace platform { struct CUDADeviceEventWrapper { - explicit CUDADeviceEventWrapper(const DeviceOption& dev_opt) { + explicit CUDADeviceEventWrapper(const DeviceOption& dev_opt) + : inner_event_() { PADDLE_ENFORCE_EQ( dev_opt.device_type(), static_cast(DeviceType::CUDA), platform::errors::PreconditionNotMet( @@ -31,7 +32,6 @@ struct CUDADeviceEventWrapper { "Required DeviceOption.device_id > -1, but received %d. ", dev_opt.device_id())); device_id_ = dev_opt.device_id(); - inner_event_ = platform::CudaEvent(); } CudaEvent inner_event_; @@ -43,25 +43,63 @@ void DeviceEventCreateCUDA(DeviceEvent* event, const DeviceOption& dev_opt) { } void DeviceEventRecordCUDA(DeviceEvent* event, const platform::Place& place, - const void* context) { + const DeviceContext* context) { auto* wrapper = static_cast(event->GetEvent().get()); - auto* cuda_dev_ctx = static_cast(context); - // TODO(Aurelius84): verify device_id and stream is as expected. + auto* cuda_dev_ctx = + dynamic_cast(context); + PADDLE_ENFORCE_NOT_NULL( + cuda_dev_ctx, + platform::errors::PreconditionNotMet( + "Failed to dynamic_cast context into CUDADeviceContext.")); + wrapper->inner_event_.Record(*cuda_dev_ctx->context()->Stream()); } bool DeviceEventQueryCUDA(const DeviceEvent* event) { auto* wrapper = static_cast(event->GetEvent().get()); + PADDLE_ENFORCE_NOT_NULL( + wrapper, + platform::errors::PreconditionNotMet( + "Failed to dynamic_cast event into CUDADeviceEventWrapper.")); + return wrapper->inner_event_.Query(); } -// REGISTER_EVENT_RECORD_FUNCTION(DeviceType::CUDA, DeviceEventRecordCUDA) -// REGISTER_EVENT_QUERY_FUNCTION(DeviceType::CUDA, DeviceEventQueryCUDA) +void DeviceEventFinishCUDA(const DeviceEvent* event) { + auto* wrapper = static_cast(event->GetEvent().get()); + // calling cudaEventSynchronize + wrapper->inner_event_.Synchronize(); +} + +void DeviceEventCUDAWaitCUDA(const DeviceEvent* event, DeviceContext* context) { + auto* wrapper = static_cast(event->GetEvent().get()); + auto* cuda_dev_ctx = + dynamic_cast(context); + PADDLE_ENFORCE_NOT_NULL( + cuda_dev_ctx, + platform::errors::PreconditionNotMet( + "Failed to dynamic_cast context into CUDADeviceContext.")); + // calling cudaStreamWaitEvent(stream, event, 0) + cuda_dev_ctx->context()->Stream()->WaitEvent( + wrapper->inner_event_.GetRawCudaEvent()); +} + +void DeviceEventCPUWaitCUDA(const DeviceEvent* event, DeviceContext* context) { + DeviceEventFinishCUDA(event); +} } // namespace platform } // namespace paddle using ::paddle::platform::DeviceType::CUDA; +using ::paddle::platform::DeviceType::CPU; REGISTER_EVENT_CREATE_FUNCTION(CUDA, paddle::platform::DeviceEventCreateCUDA) +REGISTER_EVENT_RECORD_FUNCTION(CUDA, paddle::platform::DeviceEventRecordCUDA) +REGISTER_EVENT_QUERY_FUNCTION(CUDA, paddle::platform::DeviceEventQueryCUDA) +REGISTER_EVENT_FINISH_FUNCTION(CUDA, paddle::platform::DeviceEventFinishCUDA) +REGISTER_EVENT_WAIT_FUNCTION(CUDA, CUDA, + paddle::platform::DeviceEventCUDAWaitCUDA) +REGISTER_EVENT_WAIT_FUNCTION(CPU, CUDA, + paddle::platform::DeviceEventCPUWaitCUDA) #endif diff --git a/paddle/fluid/platform/device_event_test.cc b/paddle/fluid/platform/device_event_test.cc index cbee9a6c442f9..3ac05b6bffa51 100644 --- a/paddle/fluid/platform/device_event_test.cc +++ b/paddle/fluid/platform/device_event_test.cc @@ -13,14 +13,18 @@ // limitations under the License. #include "paddle/fluid/platform/device_event.h" - #include "glog/logging.h" #include "gtest/gtest.h" -USE_EVENT(1); - #ifdef PADDLE_WITH_CUDA -TEST(DeviceEvent, GPU) { +#include +using ::paddle::platform::DeviceType::CUDA; +using ::paddle::platform::DeviceType::CPU; +USE_EVENT(CUDA); +USE_EVENT_WAIT(CUDA, CUDA) +USE_EVENT_WAIT(CPU, CUDA) + +TEST(DeviceEvent, CUDA) { VLOG(1) << "In Test"; using paddle::platform::CUDAPlace; using paddle::platform::DeviceOption; @@ -30,15 +34,45 @@ TEST(DeviceEvent, GPU) { auto& pool = DeviceContextPool::Instance(); auto place = CUDAPlace(0); - auto* context = pool.Get(place); + auto* context = + static_cast(pool.Get(place)); int device_type = static_cast(DeviceType::CUDA); DeviceOption dev_opt(device_type, place.device); ASSERT_NE(context, nullptr); + // case 1. test for event_creator DeviceEvent event(dev_opt); ASSERT_NE(event.GetEvent().get(), nullptr); - // event.Record(place, context); - // bool status = event.Query(); - // ASSERT_EQ(status, true); + // case 2. test for event_recorder + event.Record(place, context); + bool status = event.Query(); + ASSERT_EQ(status, false); + // case 3. test for event_finisher + event.Finish(); + status = event.Query(); + ASSERT_EQ(status, true); + + // case 4. test for event_waiter + float *src_fp32, *dst_fp32; + int size = 1000000 * sizeof(float); + cudaMallocHost(reinterpret_cast(&src_fp32), size); + cudaMalloc(reinterpret_cast(&dst_fp32), size); + cudaMemcpyAsync(dst_fp32, src_fp32, size, cudaMemcpyHostToDevice, + context->stream()); + event.Record(place, context); // step 1. record it + status = event.Query(); + ASSERT_EQ(status, false); + + event.Wait(CUDA, context); // step 2. add streamWaitEvent + status = event.Query(); + ASSERT_EQ(status, false); // async + + event.Wait(CPU, context); // step 3. EventSynchornize + status = event.Query(); + ASSERT_EQ(status, true); // sync + + // release resource + cudaFree(dst_fp32); + cudaFreeHost(src_fp32); } #endif diff --git a/paddle/fluid/pybind/cuda_streams_py.cc b/paddle/fluid/pybind/cuda_streams_py.cc index 21c6e0a4f28ca..5ea0a2553f751 100644 --- a/paddle/fluid/pybind/cuda_streams_py.cc +++ b/paddle/fluid/pybind/cuda_streams_py.cc @@ -105,7 +105,7 @@ void BindCudaStream(py::module *m_ptr) { .def("wait_stream", [](paddle::platform::stream::CUDAStream &self, paddle::platform::stream::CUDAStream &stream) { - auto event = paddle::platform::CudaEvent(); + paddle::platform::CudaEvent event; event.Record(stream); self.WaitEvent(event.GetRawCudaEvent()); From cae86ad16bdde1c686b1cfac903f191ea1ad0971 Mon Sep 17 00:00:00 2001 From: Aurelius84 Date: Tue, 17 Aug 2021 10:59:14 +0000 Subject: [PATCH 6/7] fix macros --- paddle/fluid/platform/device_event.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/paddle/fluid/platform/device_event.h b/paddle/fluid/platform/device_event.h index 7e2ea2ae8b96a..c1f0acc00e6c9 100644 --- a/paddle/fluid/platform/device_event.h +++ b/paddle/fluid/platform/device_event.h @@ -236,7 +236,7 @@ struct EventWaitFunctionRegisterer : public framework::Registrar { explicit EventWaitFunctionRegisterer(EventWaitFunction func) { auto waiter_idx = DeviceTypeToId(waiter_type); auto event_idx = DeviceTypeToId(event_type); - VLOG(3) << "register event_finisher with waiter_idx : " << type_idx + VLOG(3) << "register event_finisher with waiter_idx : " << waiter_idx << ", event_idx : " << event_idx; DeviceEvent::event_waiter_[waiter_idx][event_idx] = func; } From 48083059c1f961846bda652c5892c500554ac335 Mon Sep 17 00:00:00 2001 From: Aurelius84 Date: Wed, 18 Aug 2021 11:24:36 +0000 Subject: [PATCH 7/7] fix MSVC using usage --- paddle/fluid/platform/device_event_gpu.cc | 16 ++++++++-------- paddle/fluid/platform/device_event_test.cc | 14 +++++++------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/paddle/fluid/platform/device_event_gpu.cc b/paddle/fluid/platform/device_event_gpu.cc index 53d2a6507c509..86bcfdad5ba95 100644 --- a/paddle/fluid/platform/device_event_gpu.cc +++ b/paddle/fluid/platform/device_event_gpu.cc @@ -92,14 +92,14 @@ void DeviceEventCPUWaitCUDA(const DeviceEvent* event, DeviceContext* context) { } // namespace platform } // namespace paddle -using ::paddle::platform::DeviceType::CUDA; -using ::paddle::platform::DeviceType::CPU; -REGISTER_EVENT_CREATE_FUNCTION(CUDA, paddle::platform::DeviceEventCreateCUDA) -REGISTER_EVENT_RECORD_FUNCTION(CUDA, paddle::platform::DeviceEventRecordCUDA) -REGISTER_EVENT_QUERY_FUNCTION(CUDA, paddle::platform::DeviceEventQueryCUDA) -REGISTER_EVENT_FINISH_FUNCTION(CUDA, paddle::platform::DeviceEventFinishCUDA) -REGISTER_EVENT_WAIT_FUNCTION(CUDA, CUDA, +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) +REGISTER_EVENT_WAIT_FUNCTION(kCUDA, kCUDA, paddle::platform::DeviceEventCUDAWaitCUDA) -REGISTER_EVENT_WAIT_FUNCTION(CPU, CUDA, +REGISTER_EVENT_WAIT_FUNCTION(kCPU, kCUDA, paddle::platform::DeviceEventCPUWaitCUDA) #endif diff --git a/paddle/fluid/platform/device_event_test.cc b/paddle/fluid/platform/device_event_test.cc index 3ac05b6bffa51..04288599c40a4 100644 --- a/paddle/fluid/platform/device_event_test.cc +++ b/paddle/fluid/platform/device_event_test.cc @@ -18,11 +18,11 @@ #ifdef PADDLE_WITH_CUDA #include -using ::paddle::platform::DeviceType::CUDA; -using ::paddle::platform::DeviceType::CPU; -USE_EVENT(CUDA); -USE_EVENT_WAIT(CUDA, CUDA) -USE_EVENT_WAIT(CPU, CUDA) +using ::paddle::platform::kCUDA; +using ::paddle::platform::kCPU; +USE_EVENT(kCUDA); +USE_EVENT_WAIT(kCUDA, kCUDA) +USE_EVENT_WAIT(kCPU, kCUDA) TEST(DeviceEvent, CUDA) { VLOG(1) << "In Test"; @@ -63,11 +63,11 @@ TEST(DeviceEvent, CUDA) { status = event.Query(); ASSERT_EQ(status, false); - event.Wait(CUDA, context); // step 2. add streamWaitEvent + event.Wait(kCUDA, context); // step 2. add streamWaitEvent status = event.Query(); ASSERT_EQ(status, false); // async - event.Wait(CPU, context); // step 3. EventSynchornize + event.Wait(kCPU, context); // step 3. EventSynchornize status = event.Query(); ASSERT_EQ(status, true); // sync