Repository navigation
Expand file tree
/
Copy pathrange_counters_validation.cpp
More file actions
530 lines (486 loc) · 17.9 KB
/
Copy pathrange_counters_validation.cpp
File metadata and controls
530 lines (486 loc) · 17.9 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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/*
* Copyright 2024-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.
*/
// ============================================================================
// Range ops and the per-chunk counters. Variant-agnostic: tests zero / zero_or_value
// / zero_any depending on the compile-time macro (CHUNK_VARIANT_ZERO*),
// like the other harnesses here.
// ============================================================================
//
// In counter mode (use_compression_with_counter: no fast block compression --
// the serial builds) zero and zero_or_value decide a chunk's compression on counters the
// write proxy maintains incrementally: how many elements equal the first value
// (zero, zero_or_value) and the array's second value (zero_or_value). fill_range / copy_range
// store most of a chunk straight into its buffer, past those counters, so they
// must recount afterwards (internal::resync_counters). zero_or_value was once left out:
// its stale counts reached chunk_size on a MIXED chunk and the pass folded it
// to a constant, losing everything else in it (rechunk_validation_zero_or_value_dyn,
// 34819 elements of 1e6).
//
// Each case: a whole chunk filled with a plain value through the raw path,
// then ONE element set to a constant through the proxy, then a compression
// pass. Every element but that one must still read the plain value. With a
// stale count the chunk comes back all constant.
// first -- fill_range, then one first value;
// second -- fill_range, then one second value (zero_or_value / zero_any);
// copy -- copy_range from a plain source, then one first value.
// Then fill_range over chunks already constant (run_fill_constant_case).
// Then the compound operators on a compressed chunk (run_compound_case).
// In an OpenMP build the counters do not exist and the cases simply pass.
//
// Exit 0 = every check holds. Any thread count.
#include <algorithm> // std::fill (run_fill_constant_case's reference)
#include <cstddef> // std::size_t
#include <type_traits> // std::conditional_t
#include <utility> // std::as_const
#include <vector> // std::vector
#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/range_ops.h" // fill_range, copy_range
#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_or_value*, use_compression_with_counter
#include "chunk/phases.h" // begin_parallel_phase / finalize_parallel_phase
#include "chunk/print.h" // shrink_array::println
#include "validation_harness.h" // self_command; run() is called by validation_driver.cpp
namespace validation::range_counters_validation
{
using namespace shrink_array;
namespace
{
static_assert(chunk_config::use_zero || chunk_config::use_zero_or_value || chunk_config::use_zero_any,
"range_counters_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";
}
}
void begin_phase()
{
if constexpr (build_config::needs_explicit_phases)
{
shrink_array::begin_parallel_phase();
}
}
void end_phase()
{
if constexpr (build_config::needs_explicit_phases)
{
shrink_array::finalize_parallel_phase();
}
}
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);
}
}
template <typename Arr>
void compress_in_region(Arr& arr)
{
#pragma omp parallel
{
begin_phase();
#pragma omp single
arr.compress();
}
end_phase();
}
/// @brief Serial zero_any has no between-region pass: the writers' pass folds
/// the uniform chunks (as pattern_validation's end_phase). A template, so
/// that the other builds do not see the call.
template <typename Arr>
void writers_pass()
{
if constexpr (chunk_config::use_zero_any && !build_config::use_openmp)
{
Arr::make_block_compression();
}
}
constexpr double plain_value = 1.0; ///< neither constant
constexpr double second_value = 2.0; ///< the arrays' second value
enum class Case
{
first,
second,
copy
};
const char* case_name(Case test_case)
{
switch (test_case)
{
case Case::first:
return "first";
case Case::second:
return "second";
default:
return "copy";
}
}
/// One chunk (chunk #1, so that chunk #0 stays at the first constant around
/// it) through the raw path, one proxied constant, a compression pass.
int run_case(Case test_case)
{
const bool second_applies = chunk_config::use_zero_or_value || chunk_config::use_zero_any;
if (test_case == Case::second && !second_applies)
{
return 0;
}
Array<double> arr;
const auto chunk_sz = static_cast<long>(arr.chunk_size());
const long count = 3 * chunk_sz;
arr.resize(static_cast<int>(count));
maybe_set_second_value(arr, second_value);
const auto lo = static_cast<std::size_t>(chunk_sz);
const auto hi = static_cast<std::size_t>(2 * chunk_sz);
if (test_case == Case::copy)
{
const std::vector src(static_cast<std::size_t>(chunk_sz), plain_value);
shrink_array::copy_range(arr, lo, hi, src.begin());
}
else
{
shrink_array::fill_range(arr, lo, hi, plain_value);
}
// The single proxied write of a constant: with a stale count, the one that
// makes it reach chunk_size.
const double constant = (test_case == Case::second) ? second_value : 0.0;
arr[static_cast<int>(chunk_sz)] = constant;
compress_in_region(arr);
int bad = 0;
for (long i = 0; i < count; ++i)
{
const double want = (i < chunk_sz || i >= 2 * chunk_sz) ? 0.0 : (i == chunk_sz ? constant : plain_value);
const double got = std::as_const(arr)[static_cast<int>(i)];
if (got != want)
{
if (bad < 5)
{
println("[{}/{}] FAIL [{}] = {:g}, expected {:g}", variant_name(), case_name(test_case), i, got, want);
}
++bad;
}
}
if (!bad)
{
println("[{}/{}] OK: raw span of {} plain values + one {} value, compressed, read back", variant_name(),
case_name(test_case), chunk_sz, test_case == Case::second ? "second" : "first");
}
return bad ? 1 : 0;
}
/// fill_range over chunks already constant: the value a chunk already holds
/// (whole chunk, part of one, across two), and the values it does not -- the
/// first value onto a second-constant chunk, the second onto a first-constant
/// one, and on zero_any a second value onto a chunk at ANOTHER second value.
/// Every element against a reference after each fill, then after a pass; a
/// fill of the chunk's own constant must leave it constant.
int run_fill_constant_case()
{
const bool second_applies = chunk_config::use_zero_or_value || chunk_config::use_zero_any;
Array<double> arr;
const auto csz = static_cast<long>(arr.chunk_size());
const long count = 6 * csz;
arr.resize(static_cast<int>(count));
maybe_set_second_value(arr, second_value);
std::vector ref(static_cast<std::size_t>(count), 0.0);
const auto fill = [&](long lo, long hi, double value)
{
shrink_array::fill_range(arr, static_cast<std::size_t>(lo), static_cast<std::size_t>(hi), value);
std::fill(ref.begin() + lo, ref.begin() + hi, value);
};
const auto constant = [&](long c)
{
return !shrink_array::internal::materialised(arr, arr.chunk_ref(static_cast<std::size_t>(c)),
shrink_array::internal::second_value_index(arr));
};
int bad = 0;
const auto check = [&](const char* step)
{
long wrong = 0;
for (long i = 0; i < count; ++i)
{
const double got = std::as_const(arr)[static_cast<int>(i)];
if (got != ref[static_cast<std::size_t>(i)])
{
if (wrong < 5)
{
println("[{}/fill-constant] FAIL {}: [{}] = {:g}, expected {:g}", variant_name(), step, i, got,
ref[static_cast<std::size_t>(i)]);
}
++wrong;
}
}
bad += wrong != 0 ? 1 : 0;
};
const auto expect_constant = [&](long c, const char* step)
{
if (!constant(c))
{
println("[{}/fill-constant] FAIL {}: chunk {} materialised", variant_name(), step, c);
++bad;
}
};
// Chunks 0, 4, 5 at the first constant; 1 at the second value (2.0);
// 2 at 3.0 (zero_any: a second constant of its own; plain elsewhere);
// 3 mixed.
fill(1 * csz, 2 * csz, second_applies ? second_value : plain_value);
fill(2 * csz, 3 * csz, 3.0);
fill(3 * csz, 4 * csz, plain_value);
arr[static_cast<int>(3 * csz)] = 0.0;
ref[static_cast<std::size_t>(3 * csz)] = 0.0;
compress_in_region(arr);
writers_pass<Array<double>>();
check("setup");
expect_constant(0, "setup");
if (second_applies)
{
expect_constant(1, "setup");
}
if (chunk_config::use_zero_any)
{
expect_constant(2, "setup");
}
// The chunk's own constant: nothing to write, the chunk stays constant.
fill(0, csz, 0.0);
check("first value, whole const1 chunk");
expect_constant(0, "first value, whole const1 chunk");
fill(4 * csz + 3, 5 * csz - 5, 0.0);
check("first value, part of a const1 chunk");
expect_constant(4, "first value, part of a const1 chunk");
fill(5 * csz - 7, 6 * csz, 0.0);
check("first value, across two const1 chunks");
expect_constant(4, "first value, across two const1 chunks");
expect_constant(5, "first value, across two const1 chunks");
if (second_applies)
{
fill(csz + 5, 2 * csz - 3, second_value);
check("second value, part of its const2 chunk");
expect_constant(1, "second value, part of its const2 chunk");
fill(csz, 2 * csz, second_value);
check("second value, its whole const2 chunk");
expect_constant(1, "second value, its whole const2 chunk");
}
// A value the chunk does not hold: written.
if (chunk_config::use_zero_any)
{
fill(2 * csz + 2, 3 * csz - 9, second_value); // onto the chunk at 3.0
check("second value onto another const2 chunk");
}
fill(csz + 1, csz + 11, 0.0);
check("first value onto a const2 chunk");
fill(5 * csz + 4, 5 * csz + 20, second_value);
check("second value onto a const1 chunk");
fill(3 * csz + 1, 3 * csz + csz / 2, 0.0);
check("first value onto a mixed chunk");
compress_in_region(arr);
check("after the pass");
if (bad == 0)
{
println("[{}/fill-constant] OK: fill_range over constant chunks, {} elements read back", variant_name(), count);
}
return bad != 0 ? 1 : 0;
}
/// `second * 0` through the proxy (zero_or_value): the element leaves the second value
/// AND enters the first. Both counters must move; the second-value count once
/// did not (zero_or_value::Proxy::apply_compound_operation), so it stayed one too high and the
/// LAST of the following second-value writes reached chunk_size with the zero
/// still in the chunk: folded to const2, the zero read back as the second value
/// (shrink_bench_zero_or_value_dyn, one element of a chunk in a check run).
int run_mul_zero_case()
{
if constexpr (!chunk_config::use_zero_or_value)
{
return 0;
}
Array<double> arr;
const auto chunk_sz = static_cast<long>(arr.chunk_size());
const long count = 3 * chunk_sz;
arr.resize(static_cast<int>(count));
maybe_set_second_value(arr, second_value);
shrink_array::fill_range(arr, static_cast<std::size_t>(chunk_sz), static_cast<std::size_t>(2 * chunk_sz),
plain_value);
arr[static_cast<int>(chunk_sz)] = second_value;
arr[static_cast<int>(chunk_sz)] *= 0.0;
for (long i = chunk_sz + 1; i < 2 * chunk_sz; ++i)
{
arr[static_cast<int>(i)] = second_value;
}
compress_in_region(arr);
int bad = 0;
for (long i = 0; i < count; ++i)
{
const double want = (i < chunk_sz || i >= 2 * chunk_sz || i == chunk_sz) ? 0.0 : second_value;
const double got = std::as_const(arr)[static_cast<int>(i)];
if (got != want)
{
if (bad < 5)
{
println("[{}/mul_zero] FAIL [{}] = {:g}, expected {:g}", variant_name(), i, got, want);
}
++bad;
}
}
if (!bad)
{
println("[{}/mul_zero] OK: second * 0, then the rest of the chunk to the second value", variant_name());
}
return bad ? 1 : 0;
}
// ── Compound operators on a compressed chunk (zero, counter mode) ───────────
// zero arms a compressed chunk at n_val_eq_zero_ = chunk_size - 1, betting that
// the element written turns non-zero. An operator that leaves it at 0 (0 &= x,
// 0 <<= k, 0 >>= k, 0 += 0, 0 -= 0, 0 |= 0, 0 ^= 0) lost the bet with nothing
// to correct it: one zero short for good, the chunk could never be compressed
// again. *=, /= and %= were never exposed: they skip a zero element.
// Each case: an all-zero array, the operator on one element of chunk #1, which
// must end compressed; then that element set to 9 and back to 0, which must
// compress it again (the count is exact); every element reads 0.
enum class Op
{
and_,
shl,
shr,
add0,
sub0,
or0,
xor0
};
const char* op_name(Op op)
{
switch (op)
{
using enum Op;
case and_:
return "&= 5";
case shl:
return "<<= 3";
case shr:
return ">>= 1";
case add0:
return "+= 0";
case sub0:
return "-= 0";
case or0:
return "|= 0";
default:
return "^= 0";
}
}
int run_compound_case(Op op)
{
using I = long long;
Array<I> arr;
const auto chunk_sz = static_cast<long>(arr.chunk_size());
arr.resize(static_cast<int>(3 * chunk_sz));
maybe_set_second_value(arr, I{7});
const int span = static_cast<int>(chunk_sz) + 3;
switch (op)
{
using enum Op;
case and_:
arr[span] &= I{5};
break;
case shl:
arr[span] <<= 3;
break;
case shr:
arr[span] >>= 1;
break;
case add0:
arr[span] += I{0};
break;
case sub0:
arr[span] -= I{0};
break;
case or0:
arr[span] |= I{0};
break;
case xor0:
arr[span] ^= I{0};
break;
}
int bad = 0;
const bool check = chunk_config::use_zero && chunk_config::use_compression_with_counter;
const bool after1 = std::as_const(arr).written(span);
arr[span] = I{9};
arr[span] = I{0};
const bool after2 = std::as_const(arr).written(span);
if (check && (after1 || after2))
{
println("[{}/0 {}] FAIL: chunk #1 still materialised {}", variant_name(), op_name(op),
after1 ? "after the operator (a zero result)" : "after 9 then 0 (the zero count drifted)");
++bad;
}
for (long i = 0; i < 3 * chunk_sz; ++i)
{
if (std::as_const(arr)[static_cast<int>(i)] != I{0})
{
if (bad < 5)
{
println("[{}/0 {}] FAIL [{}] = {}, expected 0", variant_name(), op_name(op), i,
static_cast<long long>(std::as_const(arr)[static_cast<int>(i)]));
}
++bad;
}
}
if (!bad)
{
println("[{}/0 {}] OK{}", variant_name(), op_name(op),
check ? ": chunk compressed after the operator and after 9 then 0" : " (values only)");
}
return bad ? 1 : 0;
}
} // namespace
int run(int /*argc*/, char** /*argv*/)
{
using enum Op;
Runtime::init(true);
println("=== range_counters_validation [variant = {}] counters={} ===", variant_name(),
chunk_config::use_compression_with_counter ? "on" : "off (nothing to check)");
int fails = 0;
fails += run_case(Case::first);
fails += run_case(Case::second);
fails += run_case(Case::copy);
fails += run_fill_constant_case();
fails += run_mul_zero_case();
for (const Op op : {and_, shl, shr, add0, sub0, or0, xor0})
{
fails += run_compound_case(op);
}
release_chunk_pools();
Runtime::shutdown();
println("\n[{}] {}", variant_name(), fails ? "=== RANGE COUNTERS FAILED ===" : "=== RANGE COUNTERS OK ===");
return fails ? 1 : 0;
}
} // namespace validation::range_counters_validation