Nektar++
Loading...
Searching...
No Matches
TestGlobalConfiguration.hpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: TestGlobalConfiguration.hpp
4//
5// For more information, please see: http://www.nektar.info
6//
7// The MIT License
8//
9// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10// Department of Aeronautics, Imperial College London (UK), and Scientific
11// Computing and Imaging Institute, University of Utah (USA).
12//
13// Permission is hereby granted, free of charge, to any person obtaining a
14// copy of this software and associated documentation files (the "Software"),
15// to deal in the Software without restriction, including without limitation
16// the rights to use, copy, modify, merge, publish, distribute, sublicense,
17// and/or sell copies of the Software, and to permit persons to whom the
18// Software is furnished to do so, subject to the following conditions:
19//
20// The above copyright notice and this permission notice shall be included
21// in all copies or substantial portions of the Software.
22//
23// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29// DEALINGS IN THE SOFTWARE.
30//
31// Description: The Boost.Test global fixture shared by every Nektar unit
32// test. It starts and stops MPI and MAGMA, and captures the execution space
33// and implementation names the test binary was invoked with, so that the
34// fixtures can read them back as GlobalConfiguration::ExecStr() and
35// GlobalConfiguration::ImplStr().
36//
37// Include this after TestBoostSetup.hpp and before TestBoostTeardown.hpp:
38// it needs the Boost.Test headers the former pulls in, and it tests
39// BOOST_TEST_NO_MAIN, which the latter undefines. Then invoke
40// NEKTAR_TEST_GLOBAL_CONFIGURATION once, naming the arguments the fixture's
41// tests require.
42//
43///////////////////////////////////////////////////////////////////////////////
44
45#pragma once
46
48
49#include <cstdlib>
50#include <iostream>
51#include <string>
52
53#if defined(NEKTAR_USE_MPI)
54#include <mpi.h>
55#endif
56
57#if defined(NEKTAR_USE_MAGMA)
58#include <magma_v2.h>
59#endif
60
61namespace Nektar::UnitTests
62{
63
64/// The arguments a fixture's tests require, passed to the binary after "--".
65enum class TestArgs
66{
67 None, ///< exe
68 Exec, ///< exe -- ExecName
69 ExecAndImpl ///< exe -- ExecName ImplName
70};
71
72/**
73 * @struct GlobalConfiguration
74 *
75 * The Boost.Test global fixture. The constructor runs once before the first
76 * test case and the destructor once after the last one, so this is where the
77 * runtimes the tests share - MPI and MAGMA - are started and stopped.
78 *
79 * @tparam Args The arguments the including fixture's tests require. A binary
80 * invoked with fewer prints the usage message and exits.
81 */
82template <TestArgs Args> struct GlobalConfiguration
83{
84 /// The number of arguments, the executable name included, that are needed.
85 static constexpr int RequiredArgc = (Args == TestArgs::ExecAndImpl) ? 3
86 : (Args == TestArgs::Exec) ? 2
87 : 1;
88
89 /// The execution space name the binary was invoked with.
90 static std::string &ExecStr()
91 {
92 static std::string s;
93 return s;
94 }
95
96 /// The implementation name the binary was invoked with.
97 static std::string &ImplStr()
98 {
99 static std::string s;
100 return s;
101 }
102
104 {
105 int argc = boost::unit_test::framework::master_test_suite().argc;
106 char **argv = boost::unit_test::framework::master_test_suite().argv;
107
108 // Copy the names out of argv up front. MPI_Init below is handed the
109 // live argv and may rewrite it, and reading argv[1]/argv[2] per test
110 // without a bounds check is undefined when the arguments are missing
111 // - which happens more easily than it sounds, e.g. zsh passes an
112 // unquoted $var holding "Serial SumFac" as a single argument. The
113 // bounds check below turns that into a usage message instead.
114 std::string execname(argv[0]);
115
116 if (argc > 1)
117 {
118 ExecStr() = argv[1];
119 }
120
121 if (argc > 2)
122 {
123 ImplStr() = argv[2];
124 }
125
126#if defined(NEKTAR_USE_MAGMA)
127 magma_init();
128#endif
129#if defined(NEKTAR_USE_MPI)
130 MPI_Init(&argc, &argv);
131#endif
132
133 if (argc < RequiredArgc)
134 {
135 PrintUsage(execname);
136
137#if defined(NEKTAR_USE_MPI)
138 MPI_Finalize();
139#endif
140 exit(1);
141 }
142 }
143
145 {
146#if defined(NEKTAR_USE_MPI)
147 MPI_Finalize();
148#endif
149#if defined(NEKTAR_USE_MAGMA)
150 magma_finalize();
151#endif
152 }
153
154 /// Writes the accepted arguments to stderr, on the root rank only.
155 static void PrintUsage(const std::string &execname)
156 {
157 int rank = 0;
158
159#if defined(NEKTAR_USE_MPI)
160 MPI_Comm comm = MPI_COMM_WORLD;
161 MPI_Comm_rank(comm, &rank);
162#endif
163 if (rank != 0)
164 {
165 return;
166 }
167
168 std::cerr << "Usage: " << execname << " -- ExecName";
169
170 if constexpr (Args == TestArgs::ExecAndImpl)
171 {
172 std::cerr << " ImplName";
173 }
174
175 std::cerr << std::endl;
176
177#if defined(NEKTAR_ENABLE_DEVICE) && defined(NEKTAR_ENABLE_SIMD)
178 std::cerr << "\t ExecName = Serial, AVX, Device" << std::endl;
179#elif defined(NEKTAR_ENABLE_DEVICE)
180 std::cerr << "\t ExecName = Serial, Device" << std::endl;
181#elif defined(NEKTAR_ENABLE_SIMD)
182 std::cerr << "\t ExecName = Serial, AVX" << std::endl;
183#else
184 std::cerr << "\t ExecName = Serial" << std::endl;
185#endif
186
187 if constexpr (Args == TestArgs::ExecAndImpl)
188 {
189#if defined(NEKTAR_ENABLE_DEVICE)
190 std::cerr << "\t ImplName = StdMat, SumFac, SumFacTOP" << std::endl;
191#else
192 std::cerr << "\t ImplName = StdMat, SumFac" << std::endl;
193#endif
194 }
195 }
196};
197
198} // namespace Nektar::UnitTests
199
200/**
201 * Registers the global fixture with Boost.Test, and names it
202 * GlobalConfiguration for the enclosing fixture header. Invoke it once, at
203 * file scope, passing the Nektar::UnitTests::TestArgs the tests require.
204 *
205 * Under BOOST_TEST_NO_MAIN the fixture cannot be registered, so the entry
206 * point is defined here instead and constructs it directly.
207 */
208#if defined(BOOST_TEST_NO_MAIN)
209
210#define NEKTAR_TEST_GLOBAL_CONFIGURATION(args) \
211 using GlobalConfiguration = Nektar::UnitTests::GlobalConfiguration<args>; \
212 \
213 inline bool init_function() \
214 { \
215 return true; \
216 } \
217 \
218 int main(int argc, char *argv[]) \
219 { \
220 GlobalConfiguration gc; \
221 \
222 return boost::unit_test::unit_test_main(&init_function, argc, argv); \
223 }
224
225#else
226
227#define NEKTAR_TEST_GLOBAL_CONFIGURATION(args) \
228 using GlobalConfiguration = Nektar::UnitTests::GlobalConfiguration<args>; \
229 BOOST_TEST_GLOBAL_CONFIGURATION(GlobalConfiguration)
230
231#endif
TestArgs
The arguments a fixture's tests require, passed to the binary after "--".
@ ExecAndImpl
exe – ExecName ImplName
@ Exec
exe – ExecName
static std::string & ImplStr()
The implementation name the binary was invoked with.
static constexpr int RequiredArgc
The number of arguments, the executable name included, that are needed.
static void PrintUsage(const std::string &execname)
Writes the accepted arguments to stderr, on the root rank only.
static std::string & ExecStr()
The execution space name the binary was invoked with.