Repository navigation
Expand file tree
/
Copy pathphase_protocol_validation.cpp
More file actions
383 lines (353 loc) · 14.6 KB
/
Copy pathphase_protocol_validation.cpp
File metadata and controls
383 lines (353 loc) · 14.6 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
* Copyright 2026 CEA (www.cea.fr)
*
* 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.
*/
// ============================================================================
// Phase-protocol harness. Variant-agnostic: zero / zero_or_value / zero_any by the
// compile-time macro (CHUNK_VARIANT_ZERO*), like the other harnesses.
// ============================================================================
//
// Guards the guard of chunk/internal/phase_guard.h. Without OMPT the program
// brackets its regions -- begin_parallel_phase() by every thread,
// finalize_parallel_phase() after the region -- and a forgotten call springs
// only when the team size changes between regions: a freed deferred op
// replayed, or values silently wrong. Every case runs in a child (this
// executable again, --case k), with the team size changing from region to
// region and one write in 7 forced down the deferred path (test_hooks.h):
//
// - the correct uses run to the end and read back what a std::vector does:
// the brackets by hand, parallel_phase() (a reduction in its body),
// the only_flush / after_flush pair, a begin in a nested region, a begin
// outside any region;
// - a forgotten finalize stops at the next begin, in every build (release
// included: the check is per region, not per write);
// - check builds: a forgotten begin stops at the first deferred write, and
// release_chunk_pools() inside a region stops, as does an explicit
// make_block_compression() (a sequential point: without a proxy it takes
// no writer handshake and no reader grace period);
// - a forgotten begin with a stale rank cache (every thread handed another
// thread's rank, as a team that changed would leave it): release builds
// re-read the rank on every use (get_thr_rank_fast) and read back right;
// check builds stop on the stale cache.
//
// OpenMP without OMPT only (the protocol is manual there); 77 = skipped.
// ============================================================================
#include <cstdio> // std::fopen, std::fclose, std::fread, FILE, std::remove
#include <cstdlib> // std::system, std::atoi
#include <cstring> // std::strcmp
#include <iterator> // std::size
#include <string> // std::string
#include <type_traits> // std::conditional_t
#include <utility> // std::as_const
#include <vector> // std::vector, the reference
#include <sys/wait.h> // WIFEXITED, WEXITSTATUS
#include "chunk/zero_array.h" // zero::Array
#include "chunk/zero_or_value_array.h" // zero_or_value::Array
#include "chunk/zero_any_array.h" // zero_any::Array
#include "chunk/internal/zero_chunk.h" // release_chunk_pools
#include "chunk/config.h" // chunk_config::use_zero*, build_config::needs_explicit_phases, any_check
#include "chunk/internal/test_hooks.h" // test_hooks::force_defer_every
#include "chunk/internal/pmp_chunk.h" // chunk_rank_cache, chunk_pmp_team_rank (the stale-rank case)
#include "chunk/phases.h" // begin_parallel_phase, finalize_parallel_phase, parallel_phase
#include "chunk/runtime.h" // Runtime::init
#include "chunk/print.h" // shrink_array::println
#include "validation_harness.h" // self_command; run() is called by validation_driver.cpp
namespace validation::phase_protocol_validation
{
using namespace shrink_array;
namespace
{
static_assert(chunk_config::use_zero || chunk_config::use_zero_or_value || chunk_config::use_zero_any,
"phase_protocol_validation must be built with CHUNK_VARIANT_ZERO, _ZERO_OR_VALUE or _ZERO_ANY");
template <typename T>
using Array =
std::conditional_t<chunk_config::use_zero_any,
zero_any::Array<T>,
std::conditional_t<chunk_config::use_zero_or_value, zero_or_value::Array<T>, zero::Array<T>>>;
constexpr const char* variant_name()
{
if constexpr (chunk_config::use_zero_any)
{
return "zero_any";
}
else if constexpr (chunk_config::use_zero_or_value)
{
return "zero_or_value";
}
else
{
return "zero";
}
}
constexpr int n_items = 1 << 18;
constexpr int n_regions = 6;
/// Region r's value of element i: a third of the chunks constant (they
/// compress between regions), the rest varied.
int value(int round, int index)
{
return ((index >> 8) % 3 == 0) ? round : (index % 5) + round;
}
/// Region r's team: 2 to 8 threads, a different size from one region to the
/// next -- what makes a forgotten call spring.
int team(int round)
{
return 2 + (round * 5) % 7;
}
/// Region r's schedule chunk: the owner of an element changes too.
int sched(int round)
{
return 1 << (4 + round % 5);
}
/// How a case brackets its regions.
enum class Bracket
{
by_hand, ///< begin by every thread, finalize after
helper, ///< parallel_phase()
split, ///< begin, only_flush inside, after_flush after
nested_begin, ///< by_hand, plus a begin in a serialised nested region
begin_serial, ///< by_hand, plus a begin outside any region first
no_finalize, ///< begin, never finalize
no_begin, ///< finalize, never begin
stale_rank, ///< no_begin, each thread's rank cache set to another thread's rank
};
/// The helper's reduction. At namespace scope, not a local of regions():
/// the body lambda would capture it, and clang refuses a reduction on a
/// captured variable ("reduction variable must be shared"; GCC accepts it).
long helper_sum = 0;
/// The regions of a case; the elements that do not read back as a
/// std::vector does.
long regions(Bracket how)
{
Array<int> arr(n_items);
std::vector ref(n_items, 0);
helper_sum = 0;
for (int r = 0; r < n_regions; ++r)
{
auto body = [&]
{
// A local of the lambda, not a capture: clang crashes generating an
// orphaned omp for whose schedule chunk names a captured variable
// (CGOpenMPInlinedRegionInfo::getContextValue; GCC accepts it).
const int cs = sched(r);
#pragma omp for schedule(static, cs) reduction(+ : helper_sum)
for (int i = 0; i < n_items; ++i)
{
const int expected = value(r, i);
arr[i] = expected;
ref[i] = expected;
helper_sum += expected;
}
};
if (how == Bracket::helper)
{
parallel_phase(team(r), body);
continue;
}
if (how == Bracket::begin_serial)
{
begin_parallel_phase(); // outside any region: nothing to bracket, ignored
}
#pragma omp parallel num_threads(team(r))
{
if (how == Bracket::stale_rank)
{
// What a team that changed leaves in the cache when nothing
// clears it (no OMPT, no begin): another thread's rank.
chunk_rank_cache() = (chunk_pmp_team_rank() + 1) % team(r);
}
else if (how != Bracket::no_begin)
{
begin_parallel_phase();
}
if (how == Bracket::nested_begin)
{
#pragma omp parallel num_threads(2)
{
begin_parallel_phase(); // serialised: the same thread, the same phase
}
}
body();
if (how == Bracket::split)
{
finalize_parallel_phase_only_flush();
}
}
if (how == Bracket::split)
{
finalize_parallel_phase_after_flush();
}
else if (how != Bracket::no_finalize)
{
finalize_parallel_phase();
}
}
long bad = 0;
const auto& ca = std::as_const(arr);
for (int i = 0; i < n_items; ++i)
{
bad += (ca[i] != ref[i]) ? 1 : 0;
}
if (how == Bracket::helper)
{
long want = 0;
for (int r = 0; r < n_regions; ++r)
{
for (int i = 0; i < n_items; ++i)
{
want += value(r, i);
}
}
if (helper_sum != want)
{
println(" reduction: {}, expected {}", helper_sum, want);
++bad;
}
}
return bad;
}
/// A sequential point a case calls inside a region, instead of the regions.
enum class InRegion
{
nothing, ///< the regions of Case::how
release_pools, ///< release_chunk_pools()
block_pass, ///< an explicit make_block_compression() on an array
};
/// The builds a case runs in.
enum class Builds
{
all, ///< every build
check_only, ///< the check exists in check builds only
release_only, ///< what a check build stops on, a release build must survive
};
/// One case: how its child brackets the regions, and what must happen.
struct Case
{
const char* name;
Bracket how;
InRegion in_region; ///< a sequential point called in a region instead
Builds builds; ///< the builds it runs in
const char* must_print; ///< nullptr: must run to the end, values right
};
constexpr const char* msg_twice = "begin_parallel_phase() twice in the same phase";
constexpr const char* msg_deferred = "deferred write in a parallel region by thread";
constexpr const char* msg_in_region = "release_chunk_pools() called inside a parallel region";
constexpr const char* msg_block = "make_block_compression() called inside a parallel region";
constexpr const char* msg_stale = "stale thread-rank cache";
const Case case_table[] = {
{"brackets by hand", Bracket::by_hand, InRegion::nothing, Builds::all, nullptr},
{"parallel_phase()", Bracket::helper, InRegion::nothing, Builds::all, nullptr},
{"only_flush / after_flush pair", Bracket::split, InRegion::nothing, Builds::all, nullptr},
{"begin in a nested region", Bracket::nested_begin, InRegion::nothing, Builds::all, nullptr},
{"begin outside any region", Bracket::begin_serial, InRegion::nothing, Builds::all, nullptr},
{"finalize forgotten", Bracket::no_finalize, InRegion::nothing, Builds::all, msg_twice},
{"begin forgotten (deferred write)", Bracket::no_begin, InRegion::nothing, Builds::check_only, msg_deferred},
{"release_chunk_pools() in a region", Bracket::by_hand, InRegion::release_pools, Builds::check_only, msg_in_region},
{"make_block_compression() in a region", Bracket::by_hand, InRegion::block_pass, Builds::check_only, msg_block},
{"stale rank cache, no begin", Bracket::stale_rank, InRegion::nothing, Builds::check_only, msg_stale},
{"stale rank cache, no begin", Bracket::stale_rank, InRegion::nothing, Builds::release_only, nullptr},
};
constexpr int n_cases = static_cast<int>(std::size(case_table));
/// The child: run case @p case_idx; 0 when every element read back right.
int child(int case_idx)
{
const Case& test_case = case_table[case_idx];
if (test_case.in_region == InRegion::release_pools)
{
#pragma omp parallel
release_chunk_pools();
return 0;
}
if (test_case.in_region == InRegion::block_pass)
{
Array<int> arr(1024);
#pragma omp parallel
arr.make_block_compression();
return 0;
}
const long bad = regions(test_case.how);
if (bad != 0)
{
println(" {} wrong element(s)", bad);
}
return bad == 0 ? 0 : 1;
}
/// The parent: run case @p case_idx in a child, check what happened.
int check_case(const char* self, int case_idx)
{
const Case& test_case = case_table[case_idx];
const std::string log = validation::temp_path("phase_protocol_" + std::to_string(case_idx) + ".log");
const std::string cmd =
validation::self_command(self) + " --case " + std::to_string(case_idx) + " > \"" + log + "\" 2>&1";
const int status = std::system(cmd.c_str());
std::string out;
if (FILE* file = std::fopen(log.c_str(), "r"))
{
char buf[4096];
for (std::size_t n = 0; (n = std::fread(buf, 1, sizeof buf, file)) > 0;)
{
out.append(buf, n);
}
std::fclose(file);
}
std::remove(log.c_str());
const bool ran_ok = status != -1 && WIFEXITED(status) && WEXITSTATUS(status) == 0;
const bool must_run = test_case.must_print == nullptr;
const bool pass = must_run ? (ran_ok && out.find("[PHASE PROTOCOL]") == std::string::npos)
: (!ran_ok && out.find(test_case.must_print) != std::string::npos);
println("[{}/phase-protocol] {:<36} {}{}", variant_name(), test_case.name,
must_run ? (ran_ok ? "ran" : "STOPPED") : (ran_ok ? "RAN ON" : "stopped"), pass ? "" : " <-- FAIL");
if (!pass)
{
println("---- child output ----\n{}----------------------", out);
return 1;
}
return 0;
}
} // namespace
int run(int argc, char** argv)
{
if constexpr (!build_config::use_openmp || !build_config::needs_explicit_phases ||
!test_hooks::force_defer_compiled)
{
println("phase_protocol_validation: skipped (OpenMP without OMPT, with the test hooks, only)");
return 77;
}
Runtime::init(true);
if (argc > 2 && std::strcmp(argv[1], "--case") == 0)
{
test_hooks::force_defer_every = 7; // the deferred path, every 7th write of every thread
return child(std::atoi(argv[2]));
}
println("=== phase_protocol_validation [variant = {}] check build: {} ===", variant_name(),
build_config::any_check ? "yes" : "no");
int fails = 0;
for (int k = 0; k < n_cases; ++k)
{
const Builds builds = case_table[k].builds;
if (builds == Builds::check_only && !build_config::any_check)
{
println("[{}/phase-protocol] {:<36} skipped (check builds only)", variant_name(), case_table[k].name);
continue;
}
if (builds == Builds::release_only && build_config::any_check)
{
println("[{}/phase-protocol] {:<36} skipped (release builds only)", variant_name(), case_table[k].name);
continue;
}
fails += check_case(argv[0], k);
}
println("{}", fails ? "phase_protocol_validation: FAILED" : "phase_protocol_validation: OK");
return fails ? 1 : 0;
}
} // namespace validation::phase_protocol_validation