Repository navigation
Expand file tree
/
Copy pathmark_path_validation.cpp
More file actions
314 lines (286 loc) · 11.7 KB
/
Copy pathmark_path_validation.cpp
File metadata and controls
314 lines (286 loc) · 11.7 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
/*
* 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.
*/
// ============================================================================
// Marking-path harness. Variant-agnostic: zero / zero_or_value / zero_any follow the
// compile-time macro (CHUNK_VARIANT_ZERO*), like the other harnesses here.
// ============================================================================
//
// Guards ONE property: a chunk whose elements are ALL written back to a
// constant is compressed by the library's own incremental path -- the write
// marks it a candidate, the pass between two regions compresses it -- with
// no compress() call from the application. compress() (compress_int) is the
// audit pass of the check builds: it scans every chunk and compresses what
// the incremental path left behind, so a program that relies on it sees no
// leak in a check build and a run-long leak in a perf build. This harness
// never calls it: what it counts after a region is what a perf build keeps.
//
// WHAT IT EXERCISES
//
// Each scenario writes an array of 100 chunks, region after region, and
// counts the chunks still materialised after each one (written(i) on the
// first element of every chunk):
//
// A plain values everywhere (2.5 / 3.5 alternating), then the first constant (0)
// everywhere. The chunk was materialised by a value that is NEITHER
// constant (nor uniform: zero_any would take a uniform run as its
// second value), and no write ever moved it from one constant to the other.
// A pre-check accepting only the constant-to-constant transition
// leaves this chunk materialised for the run.
//
// B the second constant everywhere (a fresh chunk goes const1 -> const2:
// a constant-to-constant transition), then a plain value,
// then the second constant again -- the case A for the second
// constant -- then the first.
//
// C an array that has declared NO second value, after another array
// declared 7.0: a plain value, then 0; then 7.0 -- the OTHER array's
// second value, which must not compress here on zero_or_value -- then 0, and a
// read-back of every element. The first 0 written over a materialised
// chunk of such an array must find its candidate list.
//
// What is expected depends on the variant: zero has no second
// constant, so its runs of 1.0 or 7.0 stay materialised; zero_any picks a
// second value per chunk, so its runs of 7.0 compress even with nothing
// declared. The table is in expect_after().
//
// THE PHASE PROTOCOL
//
// Same brackets as compress_roundtrip_validation: begin_parallel_phase() by every
// thread inside the region, finalize_parallel_phase() outside it -- that
// last one is the pass this harness relies on in a GNU build (under OMPT
// the callbacks run it, serially the counters compress inline). At 1
// thread and at 8, as the other harnesses: the answer must not depend on
// the team.
//
// EXIT STATUS: 0 when every count matches, 1 otherwise.
#include <cstdint> // std::uint8_t (the Kind enum's base)
#include <type_traits> // std::conditional_t
#include <utility> // std::as_const
#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/internal/zero_chunk.h" // release_chunk_pools, Runtime::shutdown
#include "chunk/config.h" // chunk_config::use_zero*
#include "chunk/internal/pmp_chunk.h" // CHUNK_PMP_MAX_THREADS
#include "chunk/phases.h" // begin_parallel_phase / finalize_parallel_phase
#include "chunk/print.h" // shrink_array::println
// Defined by every executable of the library (the fast allocator reads it).
#include "validation_harness.h" // self_command; run() is called by validation_driver.cpp
namespace validation::mark_path_validation
{
using namespace shrink_array;
// The phase brackets, no-ops under OMPT (the callbacks do it) and serially.
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";
}
}
// set_second_value only exists on zero_or_value / zero_any; no-op elsewhere.
template <typename Arr, typename Val>
void maybe_set_second_value(Arr& arr, const Val& value)
{
if constexpr (requires(Arr probe) { probe.set_second_value(value); })
{
arr.set_second_value(value);
}
}
constexpr int n_chunks = 100;
/// @brief The "plain" content: two values alternating, so that no chunk is
/// uniform -- zero_any takes any uniform run as a second constant of
/// its own, and a single plain value everywhere would compress there.
static double plain(int index)
{
return (index % 2) ? 2.5 : 3.5;
}
/// @brief The chunks of @p arr still holding a buffer, out of the n_chunks
/// written (the array has one more, empty, that no phase touches).
static int materialised(const Array<double>& arr)
{
const auto chunk_sz = static_cast<int>(arr.chunk_size());
int count = 0;
for (int j = 0; j < n_chunks; ++j)
{
count += arr.written(j * chunk_sz);
}
return count;
}
/// @brief One region writing every element, then the between-region pass.
///
/// A serial zero_any has no between-region pass at all (there is no region):
/// its candidates queue until make_block_compression() -- the pass a writer
/// runs once its list is long enough, and resize() runs -- consumes them,
/// and a hundred candidates never reach that length. The harness runs that
/// pass itself there: it consumes the candidate lists, so a chunk the write
/// path failed to queue still shows as materialised, which is the property
/// here. Every OpenMP build compresses at finalize_parallel_phase(), zero_any
/// included (zero_any::Chunk::compress_chunk_to_compress runs the writers' pass rank
/// by rank when no OMPT callback does it).
template <typename Fn>
static void phase(Array<double>& arr, int count, Fn&& body)
{
#pragma omp parallel
{
begin_phase();
#pragma omp for schedule(static)
for (int i = 0; i < count; ++i)
{
body(i);
}
}
end_phase();
if constexpr (chunk_config::use_zero_any && !build_config::use_openmp)
{
arr.make_block_compression();
}
}
/// @brief What a constant written everywhere must leave materialised, per
/// variant: `first` compresses everywhere; `declared` (the array's
/// declared second value) compresses on zero_or_value / zero_any; `other`
/// (a value the array never declared) compresses on zero_any alone,
/// which discovers its second value per chunk.
enum class Kind : std::uint8_t
{
first,
declared,
other,
plain
};
static int expect_after(Kind kind)
{
switch (kind)
{
using enum Kind;
case first:
return 0;
case declared:
return chunk_config::use_second_value ? 0 : n_chunks;
case other:
return chunk_config::use_zero_any ? 0 : n_chunks;
case plain:
return n_chunks;
}
return -1;
}
static int check(const char* scenario, const char* step, const Array<double>& arr, Kind kind)
{
const int got = materialised(arr);
if (const int exp = expect_after(kind); got != exp)
{
println("[{}/{}] FAIL after '{}': {} chunk(s) materialised, expected {}", variant_name(), scenario, step, got,
exp);
return 1;
}
println("[{}/{}] ok after '{:<10}': {:3} materialised", variant_name(), scenario, step, got);
return 0;
}
int run(int /*argc*/, char** /*argv*/)
{
Runtime::init(true);
// The geometry this build runs at, read off a throwaway array.
const auto chunk_sz = static_cast<int>(Array<double>(1).chunk_size());
const int count = n_chunks * chunk_sz;
println("=== mark_path_validation [variant = {}] chunk_size={} chunks={} threads={} ===", variant_name(),
chunk_sz, n_chunks, CHUNK_PMP_MAX_THREADS);
int fails = 0;
// A: materialised by a plain value, then the first constant everywhere.
{
Array<double> arr_a(count);
maybe_set_second_value(arr_a, 1.0);
phase(arr_a, count, [&](int i) { arr_a[i] = plain(i); });
fails += check("A", "plain", arr_a, Kind::plain);
phase(arr_a, count, [&](int i) { arr_a[i] = 0.0; });
fails += check("A", "first", arr_a, Kind::first);
arr_a.clear();
}
// B: the second constant, a plain value, the second constant, the first.
{
Array<double> arr_b(count);
maybe_set_second_value(arr_b, 1.0);
phase(arr_b, count, [&](int i) { arr_b[i] = 1.0; });
fails += check("B", "second", arr_b, Kind::declared);
phase(arr_b, count, [&](int i) { arr_b[i] = plain(i); });
fails += check("B", "plain", arr_b, Kind::plain);
phase(arr_b, count, [&](int i) { arr_b[i] = 1.0; });
fails += check("B", "second", arr_b, Kind::declared);
phase(arr_b, count, [&](int i) { arr_b[i] = 0.0; });
fails += check("B", "first", arr_b, Kind::first);
arr_b.clear();
}
// C: no second value declared, next to an array that declared 7.0.
{
Array<double> other(count);
maybe_set_second_value(other, 7.0);
Array<double> arr_c(count);
phase(arr_c, count, [&](int i) { arr_c[i] = plain(i); });
fails += check("C", "plain", arr_c, Kind::plain);
phase(arr_c, count, [&](int i) { arr_c[i] = 0.0; });
fails += check("C", "first", arr_c, Kind::first);
phase(arr_c, count, [&](int i) { arr_c[i] = 7.0; });
fails += check("C", "other", arr_c, Kind::other);
phase(arr_c, count, [&](int i) { arr_c[i] = 0.0; });
fails += check("C", "first", arr_c, Kind::first);
long bad = 0;
for (int i = 0; i < count; ++i)
{
bad += (std::as_const(arr_c)[i] != 0.0);
}
if (bad)
{
println("[{}/C] FAIL: {} element(s) do not read back 0", variant_name(), bad);
++fails;
}
arr_c.clear();
other.clear();
}
// The library's own teardown, as in the other harnesses.
release_chunk_pools();
Runtime::shutdown();
println("\n[{}] {}", variant_name(),
fails == 0 ? "=== MARK PATH OK, TEARDOWN OK ===" : "=== MARK PATH FAILURES ===");
return fails ? 1 : 0;
}
} // namespace validation::mark_path_validation