1/* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7    http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15
16#include "tensorflow/stream_executor/cuda/cuda_event.h"
17
18#include "tensorflow/stream_executor/cuda/cuda_gpu_executor.h"
19#include "tensorflow/stream_executor/cuda/cuda_stream.h"
20#include "tensorflow/stream_executor/lib/statusor.h"
21
22namespace perftools {
23namespace gputools {
24namespace cuda {
25
26CUDAEvent::CUDAEvent(CUDAExecutor* parent)
27    : parent_(parent), cuda_event_(nullptr) {}
28
29CUDAEvent::~CUDAEvent() {}
30
31port::Status CUDAEvent::Init() {
32  return CUDADriver::CreateEvent(parent_->cuda_context(), &cuda_event_,
33                                 CUDADriver::EventFlags::kDisableTiming);
34}
35
36port::Status CUDAEvent::Destroy() {
37  return CUDADriver::DestroyEvent(parent_->cuda_context(), &cuda_event_);
38}
39
40port::Status CUDAEvent::Record(CUDAStream* stream) {
41  return CUDADriver::RecordEvent(parent_->cuda_context(), cuda_event_,
42                                 stream->cuda_stream());
43}
44
45Event::Status CUDAEvent::PollForStatus() {
46  port::StatusOr<CUresult> status =
47      CUDADriver::QueryEvent(parent_->cuda_context(), cuda_event_);
48  if (!status.ok()) {
49    LOG(ERROR) << "Error polling for event status: "
50               << status.status().error_message();
51    return Event::Status::kError;
52  }
53
54  switch (status.ValueOrDie()) {
55    case CUDA_SUCCESS:
56      return Event::Status::kComplete;
57    case CUDA_ERROR_NOT_READY:
58      return Event::Status::kPending;
59    default:
60      LOG(INFO) << "Error condition returned for event status: "
61                << status.ValueOrDie();
62      return Event::Status::kError;
63  }
64}
65
66const CUevent& CUDAEvent::cuda_event() {
67  return cuda_event_;
68}
69
70}  // namespace cuda
71}  // namespace gputools
72}  // namespace perftools
73