forked from celeritas-project/celeritas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestMainImpl.cc
186 lines (159 loc) · 5.54 KB
/
TestMainImpl.cc
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
//----------------------------------*-C++-*----------------------------------//
// Copyright 2020-2023 UT-Battelle, LLC, and other Celeritas developers.
// See the top-level COPYRIGHT file for details.
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file testdetail/TestMainImpl.cc
//---------------------------------------------------------------------------//
#include "testdetail/TestMainImpl.hh"
#include <stdexcept>
#include <string_view>
#include "celeritas_config.h"
#include "celeritas_version.h"
#include "corecel/Macros.hh"
#include "corecel/cont/Range.hh"
#include "corecel/io/ColorUtils.hh"
#include "corecel/io/Logger.hh"
#include "corecel/sys/Device.hh"
#include "corecel/sys/Environment.hh"
#include "corecel/sys/MpiCommunicator.hh"
#include "corecel/sys/MpiOperations.hh"
#include "corecel/sys/ScopedMpiInit.hh"
#include "NonMasterResultPrinter.hh"
using std::cout;
using std::endl;
namespace celeritas
{
namespace testdetail
{
namespace
{
//---------------------------------------------------------------------------//
/*!
* Add barriers at test ends.
*/
class ParallelListener final : public ::testing::EmptyTestEventListener
{
public:
//! Construct with communicator
explicit ParallelListener(MpiCommunicator const& comm) : comm_(comm) {}
//! Write parallel testing info at startup
void OnTestProgramStart(::testing::UnitTest const&) override
{
if (comm_.rank() == 0)
{
std::cout << color_code('x') << "Testing "
<< "on " << comm_.size() << " process"
<< (comm_.size() > 1 ? "es" : "") << color_code(' ')
<< std::endl;
}
}
//! Barrier at beginning
void OnTestStart(::testing::TestInfo const&) override { barrier(comm_); }
//! Flush and barrier at end
void OnTestEnd(::testing::TestInfo const&) override
{
std::cout << std::flush;
barrier(comm_);
}
private:
MpiCommunicator const& comm_;
};
//---------------------------------------------------------------------------//
/*!
* Add barriers at test ends.
*/
class DeviceSkipper final : public ::testing::EmptyTestEventListener
{
public:
void OnTestStart(::testing::TestInfo const& test_info) override
{
constexpr auto npos = std::string_view::npos;
for (char const* s : {test_info.test_case_name(), test_info.name()})
{
std::string_view sview{s};
if (sview.find("Device") != npos || sview.find("device") != npos)
{
GTEST_SKIP() << "Skipping device test";
}
}
}
};
//---------------------------------------------------------------------------//
} // namespace
//---------------------------------------------------------------------------//
int test_main(int argc, char** argv)
{
ScopedMpiInit scoped_mpi(&argc, &argv);
MpiCommunicator comm = MpiCommunicator::comm_default();
try
{
// Initialize device
celeritas::activate_device(comm);
}
catch (std::exception const& e)
{
if (comm.rank() == 0)
{
cout << color_code('r') << "[ FAILED ]" << color_code(' ')
<< " Device failed to initialize: " << e.what() << endl;
}
return 1;
}
if (comm.rank() == 0)
{
cout << color_code('x') << "Celeritas version " << celeritas_version
<< color_code(' ') << endl;
}
// Initialize google test
::testing::InitGoogleTest(&argc, argv);
// Gets hold of the event listener list.
::testing::TestEventListeners& listeners
= ::testing::UnitTest::GetInstance()->listeners();
if (comm.rank() != 0)
{
// Don't print test completion messages (default pretty printer)
delete listeners.Release(listeners.default_result_printer());
// Instead, print just failure message (in case test fails on just one
// node)
listeners.Append(new NonMasterResultPrinter(comm.rank()));
}
if (CELER_USE_DEVICE && !celeritas::device())
{
cout << color_code('y') << "Disabling tests with 'device' in the name"
<< color_code(' ') << endl;
// Skip test parts if "device" in name and device isn't available
listeners.Append(new DeviceSkipper());
}
if (CELERITAS_USE_MPI && comm)
{
// Add MPI barriers at test end: Google Test takes ownership of pointer
listeners.Append(new ParallelListener(comm));
}
// Run everything
int failed = RUN_ALL_TESTS();
bool no_tests = testing::UnitTest::GetInstance()->test_to_run_count() == 0;
failed += (no_tests ? 1 : 0);
// Find out if any process failed
failed = allreduce(comm, Operation::max, failed);
// If no tests were run, there's a problem.
if (comm.rank() == 0)
{
if (no_tests)
{
cout << color_code('r') << "[ FAILED ]" << color_code(' ')
<< " No tests are written/enabled!" << endl;
}
// Write diagnostics and overall test result
CELER_LOG(debug) << "Celeritas environment variables: "
<< environment();
cout << color_code('x') << (argc > 0 ? argv[0] : "UNKNOWN")
<< ": tests " << (failed ? "FAILED" : "PASSED") << color_code(' ')
<< endl;
}
// Return 1 if any failure, 0 if all success
return failed;
}
//---------------------------------------------------------------------------//
} // namespace testdetail
} // namespace celeritas