|
| 1 | +// Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "paddle/phi/api/profiler/event.h" |
| 16 | + |
| 17 | +#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) |
| 18 | +#include "glog/logging.h" |
| 19 | +#endif |
| 20 | + |
| 21 | +namespace phi { |
| 22 | + |
| 23 | +#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) |
| 24 | + |
| 25 | +CudaEvent::CudaEvent() { |
| 26 | +#ifdef PADDLE_WITH_HIP |
| 27 | + hipEventCreateWithFlags(&event_, flags_); |
| 28 | +#else |
| 29 | + cudaEventCreateWithFlags(&event_, flags_); |
| 30 | +#endif |
| 31 | + VLOG(4) << "CudaEvent " << event_; |
| 32 | +} |
| 33 | + |
| 34 | +CudaEvent::CudaEvent(unsigned int flags) : flags_(flags) { |
| 35 | +#ifdef PADDLE_WITH_HIP |
| 36 | + hipEventCreateWithFlags(&event_, flags_); |
| 37 | +#else |
| 38 | + cudaEventCreateWithFlags(&event_, flags_); |
| 39 | +#endif |
| 40 | + VLOG(4) << "CudaEvent " << event_; |
| 41 | +} |
| 42 | + |
| 43 | +bool CudaEvent::Query() { |
| 44 | +#ifdef PADDLE_WITH_HIP |
| 45 | + gpuError_t err = hipEventQuery(event_); |
| 46 | + if (err == hipSuccess) { |
| 47 | + return true; |
| 48 | + } |
| 49 | + if (err == hipErrorNotReady) { |
| 50 | + return false; |
| 51 | + } |
| 52 | +#else |
| 53 | + gpuError_t err = cudaEventQuery(event_); |
| 54 | + if (err == cudaSuccess) { |
| 55 | + return true; |
| 56 | + } |
| 57 | + if (err == cudaErrorNotReady) { |
| 58 | + return false; |
| 59 | + } |
| 60 | +#endif |
| 61 | + PADDLE_ENFORCE_GPU_SUCCESS(err); |
| 62 | + return false; |
| 63 | +} |
| 64 | + |
| 65 | +float CudaEvent::ElapsedTime(CudaEvent *end_event) { |
| 66 | + float milliseconds = 0; |
| 67 | +#ifdef PADDLE_WITH_HIP |
| 68 | + hipEventSynchronize(end_event->GetRawCudaEvent()); |
| 69 | + PADDLE_ENFORCE_GPU_SUCCESS( |
| 70 | + hipEventElapsedTime(&milliseconds, event_, end_event->GetRawCudaEvent())); |
| 71 | +#else |
| 72 | + cudaEventSynchronize(end_event->GetRawCudaEvent()); |
| 73 | + PADDLE_ENFORCE_GPU_SUCCESS(cudaEventElapsedTime( |
| 74 | + &milliseconds, event_, end_event->GetRawCudaEvent())); |
| 75 | +#endif |
| 76 | + return milliseconds; |
| 77 | +} |
| 78 | + |
| 79 | +#endif |
| 80 | + |
| 81 | +} // namespace phi |
0 commit comments