event.h 3.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* Copyright (c) 2019 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
C
chengduo 已提交
16

17
#include <string>
P
peizhilin 已提交
18
#ifdef PADDLE_WITH_CUDA
P
peizhilin 已提交
19
#include <cuda_runtime.h>
P
peizhilin 已提交
20
#endif
21 22 23
#ifdef PADDLE_WITH_HIP
#include <hip/hip_runtime.h>
#endif
C
chengduo 已提交
24
#include "paddle/fluid/platform/place.h"
25 26 27 28

namespace paddle {
namespace platform {

29
enum class EventType { kMark, kPushRange, kPopRange };
30

31 32 33 34
enum class EventRole {
  kOrdinary,  // only record op time with op type key
  kInnerOp,   // record op detail time with op type key
  kUniqueOp,  // record op detail time with op unique name key
W
wangchaochaohu 已提交
35
  kSpecial,   // record event such as PE which is outer of thread local
36 37
};

38 39 40 41
class Event {
 public:
  // The DeviceContext is used to get the cuda stream.
  // If CPU profiling mode, can pass nullptr.
42 43
  Event(EventType type, std::string name, uint32_t thread_id,
        EventRole role = EventRole::kOrdinary);
44 45

  const EventType& type() const;
46 47
  Event* parent() const { return parent_; }
  void set_parent(Event* parent) { parent_ = parent; }
48
  std::string name() const { return name_; }
49
  EventRole role() const { return role_; }
50
  uint32_t thread_id() const { return thread_id_; }
51
  void set_name(std::string name) { name_ = name; }
52
  void set_role(EventRole role) { role_ = role; }
53

54
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
55
#ifndef PADDLE_WITH_CUPTI
56
  gpuEvent_t event() const { return event_; }
57 58 59 60 61 62 63 64 65
  int device() const { return device_; }
#endif
#endif

  double CpuElapsedMs(const Event& e) const;
  double CudaElapsedMs(const Event& e) const;

 private:
  EventType type_;
66 67
  std::string name_{};
  Event* parent_{nullptr};
68
  uint32_t thread_id_;
69
  EventRole role_{};
70
  int64_t cpu_ns_;
71
  bool visited_status_{false};
72
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
73 74 75 76 77 78 79 80 81 82
#ifdef PADDLE_WITH_CUPTI
  int64_t gpu_ns_ = 0;

 public:
  void AddCudaElapsedTime(int64_t start_ns, int64_t end_ns) {
    gpu_ns_ += end_ns - start_ns;
  }

 private:
#else
83
  gpuEvent_t event_ = nullptr;
84 85 86 87
  int device_ = -1;
#endif
#endif
};
C
chengduo 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118

class MemEvent {
 public:
  MemEvent(EventType type, uint64_t start_ns, uint64_t end_ns, size_t bytes,
           Place place, int64_t thread_id, const std::string& annotation)
      : type_(type),
        start_ns_(start_ns),
        end_ns_(end_ns),
        bytes_(bytes),
        place_(place),
        thread_id_(thread_id),
        annotation_(annotation) {}

  const EventType& type() const { return type_; }
  uint64_t start_ns() const { return start_ns_; }
  uint64_t end_ns() const { return end_ns_; }
  size_t bytes() const { return bytes_; }
  Place place() const { return place_; }
  int64_t thread_id() const { return thread_id_; }
  const std::string& annotation() const { return annotation_; }

 private:
  EventType type_;
  uint64_t start_ns_ = 0;
  uint64_t end_ns_ = 0;
  size_t bytes_;
  Place place_;
  int64_t thread_id_;
  std::string annotation_;
};

119 120
}  // namespace platform
}  // namespace paddle