Repository navigation
Expand file tree
/
Copy pathsigned_zero_validation.cpp
More file actions
541 lines (499 loc) · 18 KB
/
Copy pathsigned_zero_validation.cpp
File metadata and controls
541 lines (499 loc) · 18 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
531
532
533
534
535
536
537
538
539
540
541
/*
* 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.
*/
// ============================================================================
// Signed zero and NaN payload harness. Variant-agnostic: zero / zero_or_value /
// zero_any by the compile-time macro (CHUNK_VARIANT_ZERO*), like the others.
// ============================================================================
//
// Guards ONE property: an element reads back the exact bits written, for the
// values operator== cannot tell apart. -0.0 == +0.0, so a decision taken with
// == ("this value is already there", "this run is the constant") skipped the
// write of a -0.0 onto a +0.0 and compressed a run of -0.0 to the constant
// +0.0: 1/x went from -inf to +inf, std::signbit and std::atan2 changed. The
// library decides with same_value() (internal/same_value.h): the bits for
// float and double, part by part for std::complex, ValueIdentity<T> for an
// aggregate.
//
// THE CASES (each on double, float, std::complex<double>)
//
// fresh-element one -0.0 into a chunk still compressed to +0.0
// fresh-chunk a whole chunk of -0.0, written by the team, compressed
// rewrite-chunk a materialised chunk rewritten with -0.0, compressed
// onto-plus-zero one -0.0 onto a materialised +0.0
// mixed-run a chunk of alternating +0.0 / -0.0, compressed
// fill-range fill_range and fill_range_mt of -0.0, then compressed
// second-value -0.0 declared as the second value (zero_or_value), runs of
// it and of +0.0 side by side, compressed
//
// Plus: a run of one NaN payload compresses and reads back bit for bit, and
// an aggregate with a ValueIdentity specialisation keeps a -0.0 member.
//
// Every case compares each element's bytes with a std::vector reference
// (memcmp: the property is the bits, not ==).
//
// Compile OMP or non-OMP, static or dyn, check. Exit 0 = every bit round-trips.
#include <cmath> // std::signbit, std::isnan
#include <complex> // std::complex
#include <cstdint> // std::uint64_t
#include <cstdlib> // std::strtol
#include <cstring> // std::memcmp
#include <format> // std::format (run_compound_on_zero)
#include <limits> // std::numeric_limits
#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, fill_range_mt
#include "chunk/runtime.h" // Runtime::init
#include "chunk/internal/zero_chunk.h" // release_chunk_pools, Runtime::shutdown
#include "chunk/internal/same_value.h" // ValueIdentity, same_value
#include "chunk/config.h" // chunk_config::use_zero_or_value*, build_config::needs_explicit_phases
#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
#include "validation_harness.h" // self_command; run() is called by validation_driver.cpp
namespace validation::signed_zero_validation
{
/// Three doubles: the aggregate case. The defaulted == says {-0.0} == {+0.0};
/// the ValueIdentity specialisation below makes the library tell them apart.
struct Vec3
{
auto operator<=>(const Vec3&) const = default;
double x;
double y;
double z;
};
} // namespace validation::signed_zero_validation
template <>
struct shrink_array::ValueIdentity<validation::signed_zero_validation::Vec3>
{
static bool same(const validation::signed_zero_validation::Vec3& a,
const validation::signed_zero_validation::Vec3& b) noexcept
{
return same_value(a.x, b.x) && same_value(a.y, b.y) && same_value(a.z, b.z);
}
};
namespace validation::signed_zero_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 values of a case for one element type: the negative zero, the
/// positive one, and a non-zero value to materialise chunks with.
template <typename T>
struct Values;
template <>
struct Values<double>
{
static double neg()
{
return -0.0;
}
static double pos()
{
return 0.0;
}
static double other(long i)
{
return 1.0 + static_cast<double>(i % 97);
}
};
template <>
struct Values<float>
{
static float neg()
{
return -0.0f;
}
static float pos()
{
return 0.0f;
}
static float other(long i)
{
return 1.0f + static_cast<float>(i % 97);
}
};
template <>
struct Values<std::complex<double>>
{
static std::complex<double> neg()
{
return {-0.0, -0.0};
}
static std::complex<double> pos()
{
return {0.0, 0.0};
}
static std::complex<double> other(long i)
{
return {1.0 + static_cast<double>(i % 97), 0.5};
}
};
template <>
struct Values<Vec3>
{
static Vec3 neg()
{
return {-0.0, 0.0, -0.0};
}
static Vec3 pos()
{
return {0.0, 0.0, 0.0};
}
static Vec3 other(long i)
{
return {1.0 + static_cast<double>(i % 97), 2.0, 3.0};
}
};
template <typename T>
static bool same_bits(const T& a, const T& b)
{
return std::memcmp(&a, &b, sizeof(T)) == 0;
}
/// Compress from the whole team (the pass every harness uses).
template <typename Arr>
static void compress_all(Arr& arr)
{
#pragma omp parallel
{
begin_phase();
arr.compress();
}
end_phase();
}
/// Every element's bits against the reference; prints the first mismatch.
template <typename T>
static int verify(const char* type_name, const char* label, const Array<T>& arr, const std::vector<T>& ref)
{
long bad = 0;
long first = -1;
const auto n = static_cast<long>(ref.size());
for (long i = 0; i < n; ++i)
{
const T got = std::as_const(arr)[i];
if (!same_bits(got, ref[static_cast<std::size_t>(i)]))
{
if (first < 0)
{
first = i;
}
++bad;
}
}
if (bad != 0)
{
const auto csz = static_cast<long>(arr.chunk_size());
println("[{}/{}/{}] FAIL: {}/{} elements lost their bits, first at i={} (chunk #{}, offset {})", variant_name(),
type_name, label, bad, n, first, first / csz, first % csz);
return 1;
}
println("[{}/{}/{}] OK", variant_name(), type_name, label);
return 0;
}
template <typename T>
static int run_type(const char* type_name)
{
using V = Values<T>;
int fails = 0;
// Four chunks: enough for each case to own whole chunks of the geometry.
Array<T> probe(1);
const auto csz = static_cast<long>(probe.chunk_size());
probe.clear();
const long n = 4 * csz;
// fresh-element: one -0.0 into a chunk still compressed to +0.0.
{
Array<T> arr(n);
std::vector<T> ref(static_cast<std::size_t>(n), V::pos());
arr[csz + 3] = V::neg();
ref[static_cast<std::size_t>(csz + 3)] = V::neg();
fails += verify(type_name, "fresh-element", arr, ref);
compress_all(arr);
fails += verify(type_name, "fresh-element+compress", arr, ref);
arr.clear();
}
// fresh-chunk: a whole chunk of -0.0 written by the team, then compressed.
{
Array<T> arr(n);
std::vector<T> ref(static_cast<std::size_t>(n), V::pos());
#pragma omp parallel
{
begin_phase();
#pragma omp for
for (long i = csz; i < 2 * csz; ++i)
{
arr[i] = V::neg();
}
}
end_phase();
for (long i = csz; i < 2 * csz; ++i)
{
ref[static_cast<std::size_t>(i)] = V::neg();
}
compress_all(arr);
fails += verify(type_name, "fresh-chunk", arr, ref);
arr.clear();
}
// rewrite-chunk: materialised with non-zero values, rewritten with -0.0,
// compressed.
{
Array<T> arr(n);
std::vector<T> ref(static_cast<std::size_t>(n));
for (long i = 0; i < n; ++i)
{
arr[i] = V::other(i);
ref[static_cast<std::size_t>(i)] = V::other(i);
}
for (long i = 2 * csz; i < 3 * csz; ++i)
{
arr[i] = V::neg();
ref[static_cast<std::size_t>(i)] = V::neg();
}
compress_all(arr);
fails += verify(type_name, "rewrite-chunk", arr, ref);
arr.clear();
}
// onto-plus-zero: one -0.0 onto a +0.0 of a materialised chunk.
{
Array<T> arr(n);
std::vector<T> ref(static_cast<std::size_t>(n), V::pos());
for (long i = 0; i < csz; ++i)
{
const T v = (i % 2 == 0) ? V::pos() : V::other(i);
arr[i] = v;
ref[static_cast<std::size_t>(i)] = v;
}
arr[4] = V::neg();
ref[4] = V::neg();
fails += verify(type_name, "onto-plus-zero", arr, ref);
compress_all(arr);
fails += verify(type_name, "onto-plus-zero+compress", arr, ref);
arr.clear();
}
// mixed-run: alternating +0.0 / -0.0 over a whole chunk, compressed.
{
Array<T> arr(n);
std::vector<T> ref(static_cast<std::size_t>(n), V::pos());
for (long i = csz; i < 2 * csz; ++i)
{
const T v = (i % 2 == 0) ? V::pos() : V::neg();
arr[i] = v;
ref[static_cast<std::size_t>(i)] = v;
}
compress_all(arr);
fails += verify(type_name, "mixed-run", arr, ref);
arr.clear();
}
// fill-range: fill_range over a chunk and a half (a whole chunk and a
// partial one), fill_range_mt from the team over the next chunks.
{
Array<T> arr(n);
std::vector<T> ref(static_cast<std::size_t>(n), V::pos());
arr[(2 * csz) + 1] = V::other(1); // a materialised chunk under the partial fill
ref[static_cast<std::size_t>((2 * csz) + 1)] = V::other(1);
fill_range(arr, 0, static_cast<std::size_t>(csz + (csz / 2)), V::neg());
for (long i = 0; i < csz + (csz / 2); ++i)
{
ref[static_cast<std::size_t>(i)] = V::neg();
}
#pragma omp parallel
{
begin_phase();
#pragma omp for schedule(static, 1)
for (long c = 2; c < 4; ++c)
{
fill_range_mt(arr, static_cast<std::size_t>((c * csz) + 2), static_cast<std::size_t>((c + 1) * csz),
V::neg());
}
}
end_phase();
for (long i = (2 * csz) + 2; i < 3 * csz; ++i)
{
ref[static_cast<std::size_t>(i)] = V::neg();
}
for (long i = (3 * csz) + 2; i < 4 * csz; ++i)
{
ref[static_cast<std::size_t>(i)] = V::neg();
}
fails += verify(type_name, "fill-range", arr, ref);
compress_all(arr);
fails += verify(type_name, "fill-range+compress", arr, ref);
arr.clear();
}
// second-value: -0.0 as the second constant (zero_or_value), next to runs of +0.0.
if constexpr (requires(Array<T> a) { a.set_second_value(V::neg()); })
{
Array<T> arr(n);
std::vector<T> ref(static_cast<std::size_t>(n), V::pos());
arr.set_second_value(V::neg());
for (long i = 0; i < n; ++i)
{
const T v = ((i / csz) % 2 == 1) ? V::neg() : V::other(i);
arr[i] = v;
ref[static_cast<std::size_t>(i)] = v;
}
for (long i = 2 * csz; i < 3 * csz; ++i)
{
arr[i] = V::pos();
ref[static_cast<std::size_t>(i)] = V::pos();
}
compress_all(arr);
fails += verify(type_name, "second-value", arr, ref);
arr[csz + 5] = V::pos(); // a +0.0 into the -0.0 constant chunk
ref[static_cast<std::size_t>(csz + 5)] = V::pos();
arr[(2 * csz) + 5] = V::neg(); // a -0.0 into the +0.0 one
ref[static_cast<std::size_t>((2 * csz) + 5)] = V::neg();
fails += verify(type_name, "second-value+writes", arr, ref);
arr.clear();
}
return fails;
}
/// A run of one NaN payload compresses (same bits: the same value) and reads
/// back bit for bit; another payload next to it keeps its own.
static int run_nan()
{
Array<double> probe(1);
const auto csz = static_cast<long>(probe.chunk_size());
probe.clear();
const long n = 3 * csz;
const double qnan = std::numeric_limits<double>::quiet_NaN();
const double other = -qnan; // same payload, sign bit set: a different value
Array<double> arr(n);
std::vector ref(static_cast<std::size_t>(n), 0.0);
// NaNs on purpose: off with the CHUNK_DO_CHECK builds' NaN check, which
// every write and every checked read would trip. After the constructor,
// which switches it back on.
chunk_config::CheckNaN<chunk_config::do_check>::update(false);
for (long i = csz; i < 2 * csz; ++i)
{
arr[i] = qnan;
ref[static_cast<std::size_t>(i)] = qnan;
}
arr[(2 * csz) + 7] = other;
ref[static_cast<std::size_t>((2 * csz) + 7)] = other;
compress_all(arr);
const int fails = verify("double", "nan-payload", arr, ref);
arr.clear();
chunk_config::CheckNaN<chunk_config::do_check>::update(true);
return fails;
}
/// A compound operation on a zero element gives what it gives on a double:
/// 0 * -1 is -0.0, 0 / 0 and 0 * inf are NaN. The variants used to skip it
/// ("0 * x is 0") and leave +0.0. On a zero of a compressed chunk and on a
/// zero of a materialised one.
static int run_compound_on_zero()
{
Array<double> probe(1);
const auto csz = static_cast<long>(probe.chunk_size());
probe.clear();
const long n = 2 * csz;
Array<double> arr(n);
chunk_config::CheckNaN<chunk_config::do_check>::update(false); // NaNs on purpose (see run_nan)
arr[csz + 1] = 2.5; // the second chunk materialised
const double inf = std::numeric_limits<double>::infinity();
struct Case
{
long index;
const char* what;
};
int fails = 0;
for (const Case c : {Case{3, "compressed chunk"}, Case{csz + 3, "materialised chunk"}})
{
arr[c.index] = 0.0;
arr[c.index] *= -1.0;
const double neg = arr[c.index];
arr[c.index] /= 0.0; // -0.0 / 0.0
const double div = arr[c.index];
arr[c.index] = 0.0;
arr[c.index] *= inf;
const double mul_inf = arr[c.index];
arr[c.index] = 0.0;
arr[c.index] *= 3.0;
const double kept = arr[c.index];
const bool ok = neg == 0.0 && std::signbit(neg) && std::isnan(div) && std::isnan(mul_inf) && kept == 0.0 &&
!std::signbit(kept);
println("[{}/double/compound-on-zero, {}] {}", variant_name(), c.what,
ok ? "OK" : std::format("FAIL: 0*-1={} 0/0={} 0*inf={} 0*3={}", neg, div, mul_inf, kept));
fails += ok ? 0 : 1;
arr[c.index] = 0.0;
}
arr.clear();
chunk_config::CheckNaN<chunk_config::do_check>::update(true);
return fails;
}
int run(int argc, char** argv)
{
(void)argc;
(void)argv;
Runtime::init(true);
println("=== signed_zero_validation [variant = {}] threads={} ===", variant_name(), CHUNK_PMP_MAX_THREADS);
// The predicate itself, before any array.
int fails = 0;
if (same_value(-0.0, 0.0) || same_value(-0.0f, 0.0f) || !same_value(-0.0, -0.0) ||
same_value(std::complex<double>{0.0, -0.0}, std::complex<double>{0.0, 0.0}) ||
!same_value(std::numeric_limits<double>::quiet_NaN(), std::numeric_limits<double>::quiet_NaN()) ||
same_value(-0.0L, 0.0L) || !same_value(1.5L, 1.5L) || same_value(Vec3{0.0, -0.0, 0.0}, Vec3{})) [[unlikely]]
{
println("[{}/same_value] FAIL: the predicate does not tell the bits apart", variant_name());
++fails;
}
fails += run_type<double>("double");
fails += run_type<float>("float");
fails += run_type<std::complex<double>>("complex<double>");
fails += run_type<Vec3>("Vec3");
fails += run_nan();
fails += run_compound_on_zero();
release_chunk_pools();
Runtime::shutdown();
println("\n[{}] {}", variant_name(), fails == 0 ? "=== SIGNED ZERO OK ===" : "=== SIGNED ZERO FAILURES ===");
return fails;
}
} // namespace validation::signed_zero_validation