Repository navigation
Expand file tree
/
Copy pathchunk_exchange_validation.cpp
More file actions
494 lines (461 loc) · 18.2 KB
/
Copy pathchunk_exchange_validation.cpp
File metadata and controls
494 lines (461 loc) · 18.2 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
/*
* 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.
*/
/**
* @file chunk_exchange_validation.cpp
* @brief export_chunk() / import_chunk() and export_array() / import_array()
* (chunk/chunk_exchange.h): an array rebuilt from its exported states is
* the same array.
*
* @details A source of eight chunks and a partial ninth, every state the
* variant has: one chunk never written and one written with the first
* value, three uniform (at a, a and b), two holding the same motif at
* offsets 100 and 200, the others varied. What they compress to
* depends on the variant and the type (expected_kind()): zero keeps
* only the first value, zero_or_value its declared second value (a;
* bool: true), zero_any any value, pattern any value and the motif
* (installed directly: the registry's admission rule would take two
* passes). After shrink_to_fit() every chunk is exported, and the kinds
* must be the expected ones.
*
* 1. chunk by chunk into a fresh array: the same elements, the same
* states (a buffer or a motif element for element), the same
* number of chunks holding a buffer; import_chunk() refuses a
* written chunk; the imported array is then written as any other
* (a write into an imported uniform chunk materialises it from its
* value, the value written back and shrink_to_fit() put it back).
* 2. import_array() into an array already written with other values,
* and into the source itself: the same again.
* 3. import_array() of an array grown by push_back to whole chunks
* (one chunk fewer than a fresh array of its size) and of one
* after a reserve() (chunks past its size): the same elements.
*
* Every array is destroyed at the end (a second value or motif shared
* by several counted right, or a later destruction fails).
*
* The variant's Array<double> and Array<bool>; CHUNK_EXCHANGE_PATTERN
* (its own executable, on the zero_any library): pattern::Array<int>.
*/
#include <cstddef> // std::size_t
#include <format> // std::format (check_copy's promoted count)
#include <string> // std::string (same)
#include <type_traits> // std::conditional_t, std::is_same_v
#include <utility> // std::as_const
#include <vector> // std::vector (the exported states)
#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
#if defined(CHUNK_EXCHANGE_PATTERN)
#include "chunk/pattern_array.h" // pattern::Array
#endif
#include "chunk/chunk_exchange.h" // export_chunk, import_chunk, export_array, import_array
#include "chunk/runtime.h" // Runtime::init
#include "chunk/phases.h" // parallel_phase
#include "chunk/config.h" // chunk_config::use_zero*
#include "chunk/internal/pmp_chunk.h" // pmp::current_team
#include "chunk/internal/type_name.h" // type_name<T>()
#include "chunk/print.h" // shrink_array::println
#include "validation_harness.h" // run() is called by validation_driver.cpp
namespace validation::chunk_exchange_validation
{
using namespace shrink_array;
namespace
{
#if defined(CHUNK_EXCHANGE_PATTERN)
constexpr bool pattern_build = true;
#else
constexpr bool pattern_build = false;
template <typename T>
using VariantArray =
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>>>;
#endif
constexpr const char* variant_name()
{
if constexpr (pattern_build)
{
return "pattern";
}
else 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_full_chunks = 8; ///< then a partial chunk of half a chunk
template <typename T>
constexpr bool is_bool = std::is_same_v<T, bool>;
/// @brief The uniform values: a (chunks 1 and 5, zero_or_value's second
/// value), b (chunk 2), and the one a write pokes in.
template <typename T>
T value_a()
{
return is_bool<T> ? static_cast<T>(true) : static_cast<T>(5);
}
template <typename T>
T value_b()
{
return is_bool<T> ? static_cast<T>(true) : static_cast<T>(7);
}
template <typename T>
T value_poke()
{
return is_bool<T> ? static_cast<T>(false) : static_cast<T>(1);
}
/// @brief The source's element @p i (@p chunk_size elements per chunk).
template <typename T>
T source_value(long i, long chunk_size)
{
const long k = i % chunk_size;
if constexpr (is_bool<T>)
{
switch (i / chunk_size)
{
case 0:
case 4:
return false;
case 1:
case 2:
case 5:
return true;
case 6:
return k < chunk_size / 2 || k % 2 == 0;
default:
return i % 3 == 0;
}
}
else
{
const auto motif = static_cast<T>((k * 3) + (k % 7));
switch (i / chunk_size)
{
case 0:
case 4:
return T{}; // never written (0), written with the first value (4)
case 1:
case 5:
return value_a<T>();
case 2:
return value_b<T>();
case 3:
return static_cast<T>(motif + 100);
case 7:
return static_cast<T>(motif + 200);
case 6:
return k < chunk_size / 2 ? static_cast<T>(3) : static_cast<T>(i);
default:
return static_cast<T>(i + 1); // the partial chunk: varied
}
}
}
/// @brief The kind chunk @p chunk must export as, in this variant, for T.
template <typename T>
ChunkKind expected_kind(int chunk)
{
const bool second_a = chunk_config::use_zero_any || chunk_config::use_zero_or_value;
const bool second_b = chunk_config::use_zero_any || (chunk_config::use_zero_or_value && is_bool<T>);
switch (chunk)
{
using enum ChunkKind;
case 0:
case 4:
return first_value;
case 1:
case 5:
return second_a ? second_value : materialised;
case 2:
return second_b ? second_value : materialised;
case 3:
case 7:
return pattern_build ? motif : materialised;
default:
return materialised;
}
}
const char* kind_name(ChunkKind kind)
{
switch (kind)
{
using enum ChunkKind;
case first_value:
return "first_value";
case second_value:
return "second_value";
case motif:
return "motif";
case materialised:
return "materialised";
}
return "?";
}
/// @brief zero_or_value (not bool): declare the second value a on @p arr.
template <typename A>
void prepare(A& arr)
{
using T = A::value_type;
if constexpr (chunk_config::use_zero_or_value && !pattern_build && !is_bool<T>)
{
arr.set_second_value(value_a<T>());
}
}
/// @brief The differences between two exports (kinds, values, elements).
/// @param promoted If not null: pattern only, a chunk @p lhs has materialised and @p rhs
/// as a motif is counted there, not as a difference. A copy holds the same chunk as
/// its source: a second sighting by another chunk, so a pass may register its motif
/// and compress it (an OMPT build's pass at every region's end does); the elements
/// are checked one by one apart.
template <typename T>
long compare_states(const std::vector<ChunkState<T>>& lhs,
const std::vector<ChunkState<T>>& rhs,
long chunk_size,
long size,
int* promoted = nullptr)
{
long bad = lhs.size() != rhs.size() ? 1 : 0;
for (std::size_t c = 0; c < lhs.size() && c < rhs.size(); ++c)
{
using enum ChunkKind;
const auto& a = lhs[c];
const auto& b = rhs[c];
if (promoted != nullptr && a.kind == materialised && b.kind == motif)
{
++*promoted;
continue;
}
if (a.kind != b.kind || (a.kind != materialised && a.value != b.value))
{
println(" FAIL chunk {}: {} {} vs {} {}", c, kind_name(a.kind), a.value, kind_name(b.kind), b.value);
++bad;
continue;
}
if ((a.kind == materialised || a.kind == motif) && a.data != nullptr)
{
const long begin = static_cast<long>(c) * chunk_size;
const long n = begin + chunk_size < size ? chunk_size : size - begin;
for (long k = 0; k < n; ++k)
{
if (a.data[k] != b.data[k])
{
println(" FAIL chunk {} element {}: {} vs {}", c, k, a.data[k], b.data[k]);
++bad;
break;
}
}
}
}
return bad;
}
/// @brief The elements of @p arr that differ from source_value().
template <typename A>
long wrong_elements(const A& arr, long chunk_size)
{
using T = A::value_type;
long bad = 0;
for (long i = 0; i < static_cast<long>(arr.size()); ++i)
{
bad += std::as_const(arr)[static_cast<int>(i)] != source_value<T>(i, chunk_size) ? 1 : 0;
}
return bad;
}
/// @brief Is chunk @p chunk written through a[i] (pattern's motif chunks are installed)?
bool written_by_elements(long chunk)
{
return chunk != 0 && !(pattern_build && (chunk == 3 || chunk == 7));
}
/// @brief @p arr against @p expected: the elements, the states, the buffer count.
template <typename A>
long check_copy(const A& arr,
const std::vector<ChunkState<typename A::value_type>>& expected,
int written,
long chunk_size,
const char* what)
{
const auto size = static_cast<long>(arr.size());
const long wrong = wrong_elements(arr, chunk_size);
int promoted = 0; // pattern: materialised chunks found compressed to a motif since
const long differ =
compare_states(expected, export_array(arr).chunks, chunk_size, size, pattern_build ? &promoted : nullptr);
const int got = arr.written_chunk_count();
const int want = written - promoted;
const bool ok = wrong == 0 && differ == 0 && got == want;
println("[{}/{}] {}: {} wrong element(s), {} state difference(s), {} vs {} chunks with a buffer{} {}",
variant_name(), type_name<typename A::value_type>(), what, wrong, differ, got, want,
promoted != 0 ? std::format(" ({} compressed to a motif since)", promoted) : std::string{},
ok ? "ok" : "FAIL");
return wrong + differ + (got != want ? 1 : 0);
}
template <typename A>
long round_trip()
{
using T = A::value_type;
long fails = 0;
A src(0);
prepare(src);
const auto chunk_size = static_cast<long>(src.chunk_size());
const long size = (n_full_chunks * chunk_size) + (chunk_size / 2);
src.resize(static_cast<int>(size));
if constexpr (pattern_build)
{
std::vector<T> elems(static_cast<std::size_t>(chunk_size));
for (const int c : {3, 7})
{
for (long k = 0; k < chunk_size; ++k)
{
elems[static_cast<std::size_t>(k)] = source_value<T>((c * chunk_size) + k, chunk_size);
}
(void)src.install_motif(c, elems.data());
}
}
parallel_phase(
[&]
{
pmp::current_team().for_static(0, static_cast<int>(size),
[&](int i)
{
if (written_by_elements(i / chunk_size))
{
src[i] = source_value<T>(i, chunk_size);
}
});
});
src.shrink_to_fit();
const auto states = export_array(src).chunks;
const int written = src.written_chunk_count();
for (int c = 0; c < static_cast<int>(states.size()); ++c)
{
if (states[static_cast<std::size_t>(c)].kind != expected_kind<T>(c))
{
println(" FAIL source chunk {} exports as {}, expected {}", c,
kind_name(states[static_cast<std::size_t>(c)].kind), kind_name(expected_kind<T>(c)));
++fails;
}
}
// 1. Chunk by chunk into a fresh array.
{
A dst(static_cast<int>(size));
prepare(dst);
for (std::size_t c = 0; c < states.size(); ++c)
{
if (!import_chunk(dst, c, states[c]))
{
println(" FAIL import_chunk refused fresh chunk {}", c);
++fails;
}
}
fails += check_copy(dst, states, written, chunk_size, "imported chunk by chunk");
// A written chunk is refused (chunk 6: materialised in every variant).
long bad = import_chunk(dst, 6, states[0]) ? 1 : 0;
// Written as any other array: one element of the imported chunk 1,
// then that value back, then a pass.
const int at = static_cast<int>(chunk_size) + 3;
dst[at] = value_poke<T>();
bad += std::as_const(dst)[at] != value_poke<T>() ? 1 : 0;
bad += std::as_const(dst)[at + 1] != value_a<T>() ? 1 : 0;
dst[at] = value_a<T>();
dst.shrink_to_fit();
bad += wrong_elements(dst, chunk_size);
bad += export_chunk(dst, 1).kind != expected_kind<T>(1) ? 1 : 0;
if constexpr (pattern_build)
{
// A write into an imported motif chunk: materialised from its motif.
const int in_motif = static_cast<int>(3 * chunk_size) + 2;
dst[in_motif] = static_cast<T>(-1);
bad += std::as_const(dst)[in_motif] != static_cast<T>(-1) ? 1 : 0;
bad += std::as_const(dst)[in_motif + 1] != source_value<T>(in_motif + 1, chunk_size) ? 1 : 0;
dst[in_motif] = source_value<T>(in_motif, chunk_size);
bad += wrong_elements(dst, chunk_size);
}
println("[{}/{}] refused on a written chunk, then written after the import: {} problem(s) {}", variant_name(),
type_name<T>(), bad, bad == 0 ? "ok" : "FAIL");
fails += bad;
}
// 2. import_array() over an array already written, and over the source itself.
{
A other(static_cast<int>(size) / 3);
for (long i = 0; i < static_cast<long>(other.size()); ++i)
{
other[static_cast<int>(i)] = value_poke<T>();
}
const bool done = import_array(other, export_array(src));
fails += done ? 0 : 1;
fails += check_copy(other, states, written, chunk_size, "import_array over a written array");
// states' buffers were src's: released by the import. Kinds and
// values only; the elements are checked one by one.
std::vector<ChunkState<T>> kinds = states;
for (auto& state : kinds)
{
state.data = nullptr;
}
const bool self = import_array(src, export_array(src));
fails += self ? 0 : 1;
fails += check_copy(src, kinds, written, chunk_size, "import_array over itself");
}
// 3. Arrays with another chunk count than a fresh one of their size: grown
// by push_back to whole chunks (no spare last chunk), and after a
// reserve() (chunks past the size). import_array() refused both.
{
const auto import_back = [&](const A& from, const char* how)
{
A into(1);
const bool done = import_array(into, export_array(from));
const long bad = (done ? 0 : 1) + (into.size() != from.size() ? 1 : 0) + wrong_elements(into, chunk_size);
println("[{}/{}] import_array of an array {} ({} chunks for {} elements): {} problem(s) {}",
variant_name(), type_name<T>(), how, from.chunk_count(), static_cast<long>(from.size()), bad,
bad == 0 ? "ok" : "FAIL");
return bad;
};
A grown(0);
prepare(grown);
for (long i = 0; i < n_full_chunks * chunk_size; ++i)
{
grown.push_back(source_value<T>(i, chunk_size));
}
fails += import_back(grown, "grown by push_back");
A reserved(0);
prepare(reserved);
reserved.resize(static_cast<int>(size));
for (long i = 0; i < size; ++i)
{
reserved[static_cast<int>(i)] = source_value<T>(i, chunk_size);
}
reserved.reserve(static_cast<int>(40 * chunk_size));
fails += import_back(reserved, "after reserve()");
}
return fails;
}
} // namespace
int run(int /*argc*/, char** /*argv*/)
{
Runtime::init(true);
long fails = 0;
#if defined(CHUNK_EXCHANGE_PATTERN)
fails += round_trip<pattern::Array<int>>();
#else
fails += round_trip<VariantArray<double>>();
fails += round_trip<VariantArray<bool>>();
#endif
println("{}", fails ? "chunk_exchange_validation: FAILED" : "chunk_exchange_validation: OK");
return fails ? 1 : 0;
}
} // namespace validation::chunk_exchange_validation