File: gtest_mpi.cpp

package info (click to toggle)
spfft 1.1.1-5
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,688 kB
  • sloc: cpp: 11,562; f90: 665; ansic: 437; python: 41; makefile: 24
file content (216 lines) | stat: -rw-r--r-- 6,209 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include "gtest_mpi.hpp"
#include <gtest/gtest.h>
#include <iostream>
#include <memory>
#include <mpi.h>
#include <stdexcept>
#include <vector>

namespace gtest_mpi {

namespace {

class MPIListener : public testing::EmptyTestEventListener {
public:
  using UnitTest = testing::UnitTest;
  using TestCase = testing::TestCase;
  using TestInfo = testing::TestInfo;
  using TestPartResult = testing::TestPartResult;
  using TestSuite = testing::TestSuite;

  MPIListener(testing::TestEventListener *listener)
      : listener_(listener), comm_(MPI_COMM_WORLD), gather_called_(false) {
    MPI_Comm_dup(MPI_COMM_WORLD, &comm_);
    int rank;
    MPI_Comm_rank(comm_, &rank);
    if (rank != 0)
      listener_.reset();
  }

  void OnTestProgramStart(const UnitTest &u) override {
    if (listener_)
      listener_->OnTestProgramStart(u);
  }

  void OnTestProgramEnd(const UnitTest &u) override {
    if (listener_)
      listener_->OnTestProgramEnd(u);
  }

  void OnTestStart(const TestInfo &test_info) override {
    gather_called_ = false;
    if (listener_)
      listener_->OnTestStart(test_info);
  }

  void OnTestPartResult(const TestPartResult &test_part_result) override {
    if (listener_) {
      listener_->OnTestPartResult(test_part_result);
    } else if (test_part_result.type() == TestPartResult::Type::kFatalFailure ||
               test_part_result.type() ==
                   TestPartResult::Type::kNonFatalFailure) {
      std::size_t fileIndex = strings_.size();
      strings_ += test_part_result.file_name();
      strings_ += '\0';

      std::size_t messageIndex = strings_.size();
      strings_ += test_part_result.message();
      strings_ += '\0';

      infos_.emplace_back(ResultInfo{test_part_result.type(), fileIndex,
                                     test_part_result.line_number(),
                                     messageIndex});
    }
  }

  void OnTestEnd(const TestInfo &test_info) override {
    if(!gather_called_){
      std::cerr << "Missing GTEST_MPI_GUARD in test case!" << std::endl;
      throw std::runtime_error("Missing GTEST_MPI_GUARD in test case!");
    }

    if (listener_)
      listener_->OnTestEnd(test_info);
  }

  void OnTestIterationStart(const UnitTest &u, int it) override {
    if (listener_)
      listener_->OnTestIterationStart(u, it);
  }

  void OnEnvironmentsSetUpStart(const UnitTest &u) override {
    if (listener_)
      listener_->OnEnvironmentsSetUpStart(u);
  }

  void OnEnvironmentsSetUpEnd(const UnitTest &u) override {
    if (listener_)
      listener_->OnEnvironmentsSetUpEnd(u);
  }

  void OnTestSuiteStart(const TestSuite &t) override {
    if (listener_)
      listener_->OnTestSuiteStart(t);
  }

  void OnTestDisabled(const TestInfo &t) override {
    if (listener_)
      listener_->OnTestDisabled(t);
  }
  void OnTestSuiteEnd(const TestSuite &t) override {
    if (listener_)
      listener_->OnTestSuiteEnd(t);
  }

  void OnEnvironmentsTearDownStart(const UnitTest &u) override {
    if (listener_)
      listener_->OnEnvironmentsTearDownStart(u);
  }

  void OnEnvironmentsTearDownEnd(const UnitTest &u) override {
    if (listener_)
      listener_->OnEnvironmentsTearDownEnd(u);
  }

  void OnTestIterationEnd(const UnitTest &u, int it) override {
    if (listener_)
      listener_->OnTestIterationEnd(u, it);
  }

  void GatherPartResults() {
    gather_called_ = true;
    int rank, n_proc;
    MPI_Comm_rank(comm_, &rank);
    MPI_Comm_size(comm_, &n_proc);

    if (rank == 0) {
      decltype(infos_) remoteInfos;
      decltype(strings_) remoteStrings;
      for (int r = 1; r < n_proc; ++r) {
        MPI_Status status;
        int count;

        // Result infos
        MPI_Probe(r, 0, comm_, &status);
        MPI_Get_count(&status, MPI_CHAR, &count);
        auto numResults = static_cast<std::size_t>(count) /
                          sizeof(decltype(remoteInfos)::value_type);
        remoteInfos.resize(numResults);
        MPI_Recv(remoteInfos.data(), count, MPI_BYTE, r, 0, comm_,
                 MPI_STATUS_IGNORE);

        // Only continue if any results
        if (numResults) {
          // Get strings
          MPI_Probe(r, 0, comm_, &status);
          MPI_Get_count(&status, MPI_CHAR, &count);
          auto stringSize = static_cast<std::size_t>(count) /
                            sizeof(decltype(remoteStrings)::value_type);
          remoteStrings.resize(stringSize);
          MPI_Recv(&remoteStrings[0], count, MPI_BYTE, r, 0, comm_,
                   MPI_STATUS_IGNORE);

          // Create error for every remote fail
          for (const auto &info : remoteInfos) {
            if (info.type == TestPartResult::Type::kFatalFailure ||
                info.type == TestPartResult::Type::kNonFatalFailure) {
              ADD_FAILURE_AT(&remoteStrings[info.fileIndex], info.lineNumber)
                  << "Rank " << r << ": " << &remoteStrings[info.messageIndex];
            }
          }
        }
      }
    } else {
      MPI_Send(infos_.data(),
               infos_.size() * sizeof(decltype(infos_)::value_type), MPI_BYTE,
               0, 0, comm_);

      // Only send string if results exist
      if (infos_.size()) {
        MPI_Send(strings_.data(),
                 strings_.size() * sizeof(decltype(strings_)::value_type),
                 MPI_BYTE, 0, 0, comm_);
      }
    }

    infos_.clear();
    strings_.clear();
  }

private:
  struct ResultInfo {
    TestPartResult::Type type;
    std::size_t fileIndex;
    int lineNumber;
    std::size_t messageIndex;
  };

  std::unique_ptr<testing::TestEventListener> listener_;
  MPI_Comm comm_;
  bool gather_called_;

  std::vector<ResultInfo> infos_;
  std::string strings_;
};

MPIListener *globalMPIListener = nullptr;

} // namespace

void InitGoogleTestMPI(int *argc, char **argv) {

  ::testing::InitGoogleTest(argc, argv);

  auto &test_listeners = ::testing::UnitTest::GetInstance()->listeners();

  globalMPIListener = new MPIListener(
      test_listeners.Release(test_listeners.default_result_printer()));

  test_listeners.Append(globalMPIListener);
}

TestGuard CreateTestGuard() {
  return TestGuard{[]() { globalMPIListener->GatherPartResults(); }};
}

} // namespace gtest_mpi