Repository navigation
Expand file tree
/
Copy pathnested_region_validation.cpp
More file actions
341 lines (313 loc) · 12.5 KB
/
Copy pathnested_region_validation.cpp
File metadata and controls
341 lines (313 loc) · 12.5 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
/*
* 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.
*/
// ============================================================================
// Nested-region harness. Variant-agnostic: zero / zero_or_value / zero_any by the
// compile-time macro (CHUNK_VARIANT_ZERO*), like the other harnesses.
// ============================================================================
//
// Guards ONE property: a thread keeps its own per-rank structures across a
// nested parallel region.
//
// The library indexes its per-thread structures (candidate lists, deferred-op
// queues) with a rank cached in thread-local storage (__fast_omp_rank): the
// thread's rank in the active team (omp_get_ancestor_thread_num when nested),
// cleared at the end of each implicit task. Caching omp_get_thread_num() --
// the rank in the INNERMOST team -- would bring a thread of the outer team
// back from a nested region, even a serialised one, with rank 0, sharing rank
// 0's structures unprotected. Two ACTIVE levels are an invariant failure.
//
// And a second one: the end of a nested region is not the end of the phase.
// With OMPT the runtime reports every region's end, the nested ones too; the
// library's phase end (the serial flush of every rank's deferred writes, the
// compression of the candidates) must run at the OUTERMOST one only, or it
// drains the queues of the outer team's threads while they are still writing.
// Without OMPT the same holds for a parallel_phase() opened inside a phase --
// a component's, a solver's: its finalize_parallel_phase() is the enclosing
// phase's business.
//
// What it does:
// A. max-active-levels 1: an outer team of 4 writes the arrays; every thread
// opens a nested region (serialised by that setting), writes in it, and
// goes on writing in the outer region after it. Checked against a
// std::vector; check builds also check each cached rank against the
// thread's rank in the active team.
// C. A with deferred writes queued when the nested regions end: one write in
// 7 forced down the deferred path (test_hooks.h, check builds). Before the
// fix: writes lost ("deferred ops still queued"), a crash, or wrong values.
// D. C with the nested regions opened by parallel_phase(2, ...), as a
// component that knows nothing of the enclosing phase would.
// B. re-runs itself with `--nested-active`: max-active-levels 2 and a
// 2-thread inner team writing the arrays, which must stop with the
// "nested ACTIVE" invariant failure.
// In a serial build there is no nesting: A, C and D run single-threaded, B is
// skipped.
//
// The outer team is num_threads(4) whatever OMP_NUM_THREADS says: where the job
// sees fewer than 4 cores, run it with CHUNK_THREAD_CAPACITY=4 (the ctest
// entries do), or the per-thread capacity refuses rank 3 -- rightly.
// ============================================================================
#include <cstdio> // std::fopen, std::fclose, std::fread, std::fflush, FILE, stdout, std::remove
#include <cstdlib> // std::system
#include <string> // std::string, std::to_string
#include <type_traits> // std::conditional_t
#include <vector> // std::vector
#include <sys/wait.h> // WIFEXITED, WEXITSTATUS
#include "chunk/internal/chunk_traits.h" // forward decls + default IntType = int
#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/runtime.h" // Runtime::init
#include "chunk/config.h" // chunk_config::use_zero_or_value*, build_config::use_openmp / needs_explicit_phases
#include "chunk/internal/pmp_chunk.h" // chunk_pmp_set_max_active_levels
#include "chunk/phases.h" // begin_parallel_phase / finalize_parallel_phase
#include "chunk/print.h" // shrink_array::println
#include "chunk/internal/test_hooks.h" // force_defer_every, forced_deferrals (C, D)
#include "chunk/internal/zero_chunk.h" // release_chunk_pools, Runtime::shutdown
#include "validation_harness.h" // self_command; run() is called by validation_driver.cpp
namespace validation::nested_region_validation
{
using namespace shrink_array;
static void begin_phase()
{
if constexpr (build_config::needs_explicit_phases)
{
shrink_array::begin_parallel_phase();
}
}
static void end_phase()
{
if constexpr (build_config::needs_explicit_phases)
{
shrink_array::finalize_parallel_phase();
}
}
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";
}
}
/// The value element i holds after round `round` (zero runs of 2048 so the
/// chunks alternate constant and materialised).
static int expected_before_inner(long index, int round)
{
return ((index / 2048) + round) % 3 == 0 ? 0 : static_cast<int>(((index * 5) + round) % 89) + 1;
}
/// The nested region's writes: one thread of the inner team adds 3 to every
/// 7th element of block @p block. A function of its own: an orphaned `omp single`,
/// so the compiler does not see it lexically inside the outer `omp for`.
static void inner_writes(Array<int>& arr, long block)
{
#pragma omp single
for (long i = block * 1024; i < (block + 1) * 1024; ++i)
{
if (i % 7 == 0)
{
arr[i] += 3;
}
}
}
/// How the nested regions are opened.
enum class Inner
{
pragma, ///< `#pragma omp parallel num_threads(2)`, begin_phase() in it (A, B, C)
parallel_phase ///< shrink_array::parallel_phase(2, ...) (D)
};
/// Scenario A (and B, C, D's body): each thread of the outer team writes its
/// share, opens a nested region that writes again, then writes after it.
static long run_nested(Array<int>& arr, std::vector<int>& ref, int rounds, Inner inner)
{
const auto count = static_cast<long>(ref.size());
for (int round = 0; round < rounds; ++round)
{
#pragma omp parallel num_threads(4)
{
begin_phase();
#pragma omp for schedule(static)
for (long i = 0; i < count; ++i)
{
arr[i] = expected_before_inner(i, round);
}
// A nested region per thread, over this thread's own slice (the
// static schedule gives the same slice as above).
#pragma omp for schedule(static)
for (long b = 0; b < count / 1024; ++b)
{
const auto inner_body = [&] { inner_writes(arr, b); };
if (inner == Inner::parallel_phase)
{
shrink_array::parallel_phase(2, inner_body);
}
else
{
#pragma omp parallel num_threads(2)
{
begin_phase();
inner_body();
}
}
// Back in the outer region: writes go to THIS thread's rank.
for (long i = b * 1024; i < (b + 1) * 1024; ++i)
{
arr[i] *= 2;
if (i % 11 == 0)
{
arr[i] = 0;
}
}
}
}
end_phase();
for (long i = 0; i < count; ++i)
{
ref[i] = expected_before_inner(i, round);
if (i % 7 == 0)
{
ref[i] += 3;
}
ref[i] *= 2;
if (i % 11 == 0)
{
ref[i] = 0;
}
}
}
long fails = 0;
for (long i = 0; i < count; ++i)
{
const int value = arr[i];
if (value != ref[i])
{
if (fails < 5)
{
println(" MISMATCH i={} {} (ref {})", i, value, ref[i]);
}
++fails;
}
}
return fails;
}
/// Re-runs this executable with --nested-active and checks that it stopped on
/// the nested-active invariant.
[[maybe_unused]] static int check_nested_active(const char* self) // OpenMP builds only
{
const std::string log = validation::temp_path("nested_region_active.log");
const std::string cmd = validation::self_command(self) + " --nested-active > \"" + 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 stopped = status != -1 && (!WIFEXITED(status) || WEXITSTATUS(status) != 0);
const bool message = out.find("nested ACTIVE") != std::string::npos;
println("[nested-active] two active levels: {}, {}", stopped ? "stopped" : "RAN ON",
message ? "invariant message printed" : "NO invariant message");
if (!stopped || !message)
{
println("---- child output ----\n{}----------------------", out.c_str());
return 1;
}
return 0;
}
static int run_body(int argc, char** argv)
{
const bool nested_active = argc > 1 && std::string(argv[1]) == "--nested-active";
chunk_pmp_set_max_active_levels(nested_active ? 2 : 1);
Runtime::init(true);
const long count = 1L << 16;
Array<int> arr(count);
std::vector ref(count, 0);
println("=== nested_region_validation [variant = {}] outer team 4, nested regions {} ===", variant_name(),
nested_active ? "ACTIVE (--nested-active)" : "serialised");
std::fflush(stdout);
long fails = run_nested(arr, ref, 3, Inner::pragma);
if (nested_active)
{
println("[nested-active] two active levels ran without an invariant failure");
return 1;
}
// C and D: deferred writes queued when the nested regions end.
if constexpr (test_hooks::force_defer_compiled)
{
const unsigned saved = test_hooks::force_defer_every;
test_hooks::force_defer_every = 7;
for (const Inner inner : {Inner::pragma, Inner::parallel_phase})
{
const unsigned long before = test_hooks::forced_deferrals.load();
Array<int> arr_c(count);
std::vector ref_c(count, 0);
const long nfail = run_nested(arr_c, ref_c, 3, inner);
const unsigned long forced = test_hooks::forced_deferrals.load() - before;
println("[{}] deferred writes queued at the nested ends: {} forced, {} wrong",
inner == Inner::pragma ? "C omp parallel" : "D parallel_phase", forced, nfail);
fails += nfail;
if (forced == 0)
{
println(" the forced-deferral hook did not fire: the scenario tested nothing");
++fails;
}
}
test_hooks::force_defer_every = saved;
}
else
{
println("[C, D] skipped: no forced-deferral hook in this build (check builds have it)");
}
if constexpr (build_config::use_openmp)
{
fails += check_nested_active(argv[0]);
}
if (fails)
{
println("=== NESTED REGION FAILED ({}) ===", fails);
return 1;
}
println("=== NESTED REGION OK ===");
return 0;
}
int run(int argc, char** argv)
{
// The arrays live in run_body() and are destroyed when it returns, which
// parks their buffers in the reuse lists; release_chunk_pools() frees them.
// Without it they stay parked until exit, and LeakSanitizer reports every
// one in the std-alloc builds (each buffer is a real malloc there).
const int rc = run_body(argc, argv);
release_chunk_pools();
Runtime::shutdown();
return rc;
}
} // namespace validation::nested_region_validation