profiler_test.cc 5.0 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
D
dangqingqing 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14

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. */

Y
Yi Wang 已提交
15
#include "paddle/fluid/platform/profiler.h"
16
#include <string>
17
#ifdef PADDLE_WITH_CUDA
18
#include <cuda_runtime.h>
19
#endif
D
dangqingqing 已提交
20 21 22 23
#include "gtest/gtest.h"

TEST(Event, CpuElapsedTime) {
  using paddle::platform::Event;
24
  using paddle::platform::EventType;
D
dangqingqing 已提交
25

26
  Event start_event(EventType::kPushRange, "test", 0, nullptr);
D
dangqingqing 已提交
27 28 29 30 31
  EXPECT_TRUE(start_event.has_cuda() == false);
  int counter = 0;
  while (counter != 1000) {
    counter++;
  }
32
  Event stop_event(EventType::kPopRange, "test", 0, nullptr);
33
  EXPECT_GT(start_event.CpuElapsedMs(stop_event), 0);
D
dangqingqing 已提交
34 35 36 37 38 39
}

#ifdef PADDLE_WITH_CUDA
TEST(Event, CudaElapsedTime) {
  using paddle::platform::DeviceContext;
  using paddle::platform::CUDADeviceContext;
D
dangqingqing 已提交
40
  using paddle::platform::CUDAPlace;
D
dangqingqing 已提交
41
  using paddle::platform::Event;
42
  using paddle::platform::EventType;
D
dangqingqing 已提交
43

D
dangqingqing 已提交
44
  DeviceContext* dev_ctx = new CUDADeviceContext(CUDAPlace(0));
45
  Event start_event(EventType::kPushRange, "test", 0, dev_ctx);
D
dangqingqing 已提交
46 47 48 49 50
  EXPECT_TRUE(start_event.has_cuda() == true);
  int counter = 0;
  while (counter != 1000) {
    counter++;
  }
51
  Event stop_event(EventType::kPopRange, "test", 0, dev_ctx);
52
  EXPECT_GT(start_event.CudaElapsedMs(stop_event), 0);
D
dangqingqing 已提交
53 54 55 56 57 58
}
#endif

TEST(RecordEvent, RecordEvent) {
  using paddle::platform::DeviceContext;
  using paddle::platform::Event;
59
  using paddle::platform::EventType;
D
dangqingqing 已提交
60 61
  using paddle::platform::RecordEvent;
  using paddle::platform::ProfilerState;
62
  using paddle::platform::EventSortingKey;
D
dangqingqing 已提交
63 64 65 66

  ProfilerState state = ProfilerState::kCPU;
  DeviceContext* dev_ctx = nullptr;
#ifdef PADDLE_WITH_CUDA
67
  using paddle::platform::CUDADeviceContext;
D
dangqingqing 已提交
68
  using paddle::platform::CUDAPlace;
D
dangqingqing 已提交
69 70
  state = ProfilerState::kCUDA;
  dev_ctx =
D
dangqingqing 已提交
71
      new paddle::platform::CUDADeviceContext(paddle::platform::CUDAPlace(0));
D
dangqingqing 已提交
72 73 74
#endif
  EnableProfiler(state);

75 76 77
  /* Usage 1:
  *  PushEvent(evt_name, dev_ctx);
  *  ...
Y
Yibing Liu 已提交
78
  *  code to be analyzed
79 80 81
  *  ...
  * PopEvent(evt_name, dev_ctx);
  */
82
  LOG(INFO) << "Usage 1: PushEvent & PopEvent";
83 84 85 86 87 88 89 90
  for (int loop = 0; loop < 3; ++loop) {
    for (int i = 1; i < 5; ++i) {
      std::string name = "op_" + std::to_string(i);
      PushEvent(name, dev_ctx);
      int counter = 1;
      while (counter != i * 1000) counter++;
      PopEvent(name, dev_ctx);
    }
91 92 93 94 95 96
  }

  /* Usage 2:
   * {
   *   RecordEvent record_event(name, dev_ctx);
   *   ...
Y
Yibing Liu 已提交
97
   *   code to be analyzed
98
   *   ...
99 100
   * }
   */
101
  LOG(INFO) << "Usage 2: RecordEvent";
102 103
  for (int i = 1; i < 5; ++i) {
    std::string name = "evs_op_" + std::to_string(i);
D
dangqingqing 已提交
104
    RecordEvent record_event(name, dev_ctx);
105 106 107
    int counter = 1;
    while (counter != i * 1000) counter++;
  }
108

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
  /* Usage 3
   * {
   *   RecordEvent record_event(name1, dev_ctx);
   *   ...
   *   code to be analyzed
   *   ...
   *   {
   *     RecordEvent nested_record_event(name2, dev_ctx);
   *     ...
   *     code to be analyzed
   *     ...
   *   }
   * }
   */
  LOG(INFO) << "Usage 3: nested RecordEvent";
  for (int i = 1; i < 5; ++i) {
    std::string name = "ano_evs_op_" + std::to_string(i);
    RecordEvent record_event(name, dev_ctx);
    int counter = 1;
    while (counter != i * 100) counter++;
    {
      std::string nested_name = "nested_ano_evs_op_" + std::to_string(i);
      RecordEvent nested_record_event(nested_name, dev_ctx);
      int nested_counter = 1;
      while (nested_counter != i * 100) nested_counter++;
    }
  }

137 138 139
  // Bad Usage:
  PushEvent("event_without_pop", dev_ctx);
  PopEvent("event_without_push", dev_ctx);
140
  std::vector<std::vector<Event>> events = paddle::platform::GetAllEvents();
141

D
dangqingqing 已提交
142 143 144 145 146 147 148 149 150
  int cuda_startup_count = 0;
  int start_profiler_count = 0;
  for (size_t i = 0; i < events.size(); ++i) {
    for (size_t j = 0; j < events[i].size(); ++j) {
      if (events[i][j].name() == "_cuda_startup_") ++cuda_startup_count;
      if (events[i][j].name() == "_start_profiler_") ++start_profiler_count;
      if (events[i][j].name() == "push") {
        EXPECT_EQ(events[i][j + 1].name(), "pop");
#ifdef PADDLE_WITH_CUDA
151
        EXPECT_GT(events[i][j].CudaElapsedMs(events[i][j + 1]), 0);
D
dangqingqing 已提交
152
#else
153
        EXPECT_GT(events[i][j].CpuElapsedMs(events[i][j + 1]), 0);
D
dangqingqing 已提交
154 155 156 157 158 159
#endif
      }
    }
  }
  EXPECT_EQ(cuda_startup_count % 5, 0);
  EXPECT_EQ(start_profiler_count, 1);
160 161

  // Will remove parsing-related code from test later
X
Xin Pan 已提交
162
  DisableProfiler(EventSortingKey::kTotal, "/tmp/profiler");
D
dangqingqing 已提交
163
}
Y
Yu Yang 已提交
164

165
#ifdef PADDLE_WITH_CUDA
Y
Yu Yang 已提交
166 167 168 169 170 171 172
TEST(TMP, stream_wait) {
  cudaStream_t stream;
  cudaStreamCreate(&stream);
  cudaStreamSynchronize(stream);
  cudaStreamSynchronize(stream);
  cudaStreamSynchronize(stream);
}
173
#endif