test_CustomStackTrace.cpp 3.1 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Y
Yu Yang 已提交
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. */

F
fengjiayi 已提交
15 16
#include <gflags/gflags.h>  // NOLINT
#include <gtest/gtest.h>    // NOLINT
L
liaogang 已提交
17

X
Xin Pan 已提交
18 19 20 21
#include "paddle/legacy/utils/CustomStackTrace.h"
#include "paddle/legacy/utils/Locks.h"
#include "paddle/legacy/utils/StringUtil.h"
#include "paddle/legacy/utils/Util.h"
Y
Yu Yang 已提交
22

23
DEFINE_int32(test_thread_num, 10, "testing thread number");
Y
Yu Yang 已提交
24

25 26 27 28 29 30
void testNormalImpl(
    const std::function<void(paddle::CustomStackTrace<std::string>&,
                             size_t,
                             size_t,
                             paddle::ThreadBarrier&,
                             paddle::ThreadBarrier&)>& callback) {
Y
Yu Yang 已提交
31 32 33 34 35 36 37 38
  paddle::CustomStackTrace<std::string> tracer;
  paddle::ThreadBarrier doneBarrier(FLAGS_test_thread_num + 1);
  paddle::ThreadBarrier startBarrier(FLAGS_test_thread_num + 1);
  constexpr size_t countDown = 10;
  constexpr size_t layerSize = 1000;
  std::vector<std::unique_ptr<std::thread>> threads;
  threads.reserve(FLAGS_test_thread_num);

39
  for (int32_t i = 0; i < FLAGS_test_thread_num; ++i) {
F
fengjiayi 已提交
40 41 42 43
    threads.emplace_back(
        new std::thread([&tracer, &startBarrier, &doneBarrier, &callback] {
          callback(tracer, countDown, layerSize, startBarrier, doneBarrier);
        }));
Y
Yu Yang 已提交
44 45 46 47
  }
  size_t cntDown = countDown;
  while (cntDown-- > 0) {
    startBarrier.wait();
48
    sleep(1);
Y
Yu Yang 已提交
49 50 51 52 53 54 55 56 57 58 59
    doneBarrier.wait();
    ASSERT_TRUE(tracer.empty());
  }

  for (auto& thread : threads) {
    thread->join();
  }
}

TEST(CustomStackTrace, normalTrain) {
  testNormalImpl([](paddle::CustomStackTrace<std::string>& tracer,
60 61 62 63
                    size_t countDown,
                    size_t layerSize,
                    paddle::ThreadBarrier& start,
                    paddle::ThreadBarrier& finish) {
Y
Yu Yang 已提交
64 65
    while (countDown-- > 0) {
      start.wait();
66
      for (size_t i = 0; i < layerSize; ++i) {
67
        tracer.push("layer_" + paddle::str::to_string(i));
Y
Yu Yang 已提交
68
      }
69
      for (size_t i = 0; i < layerSize; ++i) {
70
        tracer.pop("layer_" + paddle::str::to_string(layerSize - 1 - i));
Y
Yu Yang 已提交
71 72 73 74 75 76 77
      }
      finish.wait();
    }
  });
}

TEST(CustomStackTrace, normalTest) {
78 79 80 81 82
  testNormalImpl([](paddle::CustomStackTrace<std::string>& tracer,
                    size_t countDown,
                    size_t layerSize,
                    paddle::ThreadBarrier& start,
                    paddle::ThreadBarrier& finish) {
Y
Yu Yang 已提交
83 84
    while (countDown-- > 0) {
      start.wait();
85
      for (size_t i = 0; i < layerSize; ++i) {
86
        tracer.push("layer_" + paddle::str::to_string(i));
Y
Yu Yang 已提交
87 88 89 90 91 92
      }
      tracer.clear();  // in forward test, tracer will clear after forward.
      finish.wait();
    }
  });
}