Repository navigation
Expand file tree
/
Copy pathchunk_array_new_validation.cpp
More file actions
191 lines (170 loc) · 7.28 KB
/
Copy pathchunk_array_new_validation.cpp
File metadata and controls
191 lines (170 loc) · 7.28 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
/*
* 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.
*/
// ============================================================================
// operator new[] / init_chunks_const_val harness. Variant-agnostic: chunk /
// zero_or_value / zero_any follow the compile-time macro, like the other harnesses
// here -- the three carry the same pair of operators, built on the one
// chunk_array_new::init_chunks (chunk_array_new.h).
// ============================================================================
//
// Guards ONE property: an array of chunks allocated through the variant's
// operator new[] comes back with EVERY chunk pointing at the first constant
// -- at every size, from a serial context and from inside a parallel region,
// at whatever thread count OMP_NUM_THREADS gives.
//
// WHY A HARNESS FOR THIS
//
// The initialisation splits on a threshold (500 chunks) and on whether a
// parallel region is open, and nothing in the library calls it: the arrays
// hold a std::vector<ChunkType> and initialise it with their own loops, so
// a fault here would surface in no other test. Two hazards:
//
// - the sizes tested below bracket the threshold, where an off-by-one
// (`if (n < 500) ... else if (n > 500)`) initialises NOTHING at 500;
// - the "region" cases call the operator from an `omp single`, which is
// what an allocation inside a parallel region looks like. Sharing the
// initialisation with an orphaned `omp for` HANGS there (the caller
// waits on the loop's barrier, the other threads on the single's); the
// operator therefore parallelises only when no region is open. A
// nested `omp parallel` is not an option either -- serialised by every
// default runtime, and N x N/2 threads from an allocation when nesting
// is enabled.
//
// `new C[n]` is NOT one of the cases: the default constructors run after the
// operator and overwrite what it wrote. The operator pays off for an array
// built by placement new, which is the last case below.
//
// EXIT STATUS: 0 when every chunk of every case is at the first constant.
#include <type_traits> // std::conditional_t
#include "chunk/internal/chunk_traits.h" // forward decls + default IntType = int
#include "chunk/zero_array.h" // zero::Chunk<T> complete (zero_chunk.h): its operator new[]
#include "chunk/zero_or_value_array.h" // zero_or_value::Chunk<T> complete: its operator new[]
#include "chunk/zero_any_array.h" // zero_any::Chunk<T> complete (zero_any_chunk.h): its operator new[]
#include "chunk/runtime.h" // Runtime::init
#include "chunk/config.h" // chunk_config::use_zero*
#include "chunk/internal/pmp_chunk.h" // CHUNK_PMP_MAX_THREADS
#include "chunk/print.h" // shrink_array::println
// The array-new is the threaded *ThreadPolicy's (the serial specialisations declare
// none), and the "region" cases call it from an `omp single`: CMake builds this
// target under OMP alone, so the std backend's array-new is not covered here.
static_assert(shrink_array::build_config::use_openmp, "OpenMP builds only: the region cases use omp single");
// 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::chunk_array_new_validation
{
using namespace shrink_array;
namespace
{
/// The chunk type of this build, as the arrays pick theirs.
using Ch = std::conditional_t<
chunk_config::use_zero_any,
zero_any::Chunk<double>,
std::conditional_t<chunk_config::use_zero_or_value, zero_or_value::Chunk<double>, zero::Chunk<double>>>;
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";
}
}
/// @brief How many of the @p count chunks hold a buffer (none should).
/// @note A TEMPLATE, so that the branch `if constexpr` discards is not
/// type-checked: zero_or_value's written() takes the second-value index (0 --
/// nothing else is registered here), zero's and zero_any's take none, and
/// in a plain function both spellings would have to compile.
template <typename C>
int materialised(const C* chunks, std::size_t count)
{
int bad = 0;
for (std::size_t k = 0; k < count; ++k)
{
if constexpr (requires { chunks[k].written(0); })
{
bad += chunks[k].written(0);
}
else
{
bad += chunks[k].written();
}
}
return bad;
}
/// @brief One case: allocate @p count chunks, serially or from an `omp single`
/// inside a region, and check every one of them.
int one_case(std::size_t count, bool in_region)
{
void* raw = nullptr;
if (in_region)
{
#pragma omp parallel
{
#pragma omp single
raw = Ch::operator new[](count * sizeof(Ch));
}
}
else
{
raw = Ch::operator new[](count * sizeof(Ch));
}
const auto* chunks = static_cast<Ch*>(raw);
const int bad = materialised(chunks, count);
println("[{}/array-new] {:<6} n={:5} : {}", variant_name(), in_region ? "region" : "serial", count,
bad ? "FAIL" : "ok, every chunk at the first constant");
if (bad)
{
println(" {} of {} chunk(s) not initialised", bad, count);
}
Ch::operator delete[](raw);
return bad ? 1 : 0;
}
/// @brief The operator's real use: an array built by placement new, where no
/// constructor runs to overwrite the initialisation.
int placement_case(std::size_t count)
{
void* raw = Ch::operator new[](count * sizeof(Ch));
const auto* chunks = static_cast<Ch*>(raw);
const int bad = materialised(chunks, count);
println("[{}/array-new] placement n={:5} : {}", variant_name(), count,
bad ? "FAIL" : "ok, every chunk at the first constant");
Ch::operator delete[](raw);
return bad ? 1 : 0;
}
} // namespace
int run(int /*argc*/, char** /*argv*/)
{
Runtime::init(true);
println("=== chunk_array_new_validation [variant = {}] threads={} ===", variant_name(), CHUNK_PMP_MAX_THREADS);
int fails = 0;
// 1 and 5000 are the plain cases; 499 / 500 / 501 bracket the threshold.
for (const std::size_t count :
{std::size_t(1), std::size_t(499), std::size_t(500), std::size_t(501), std::size_t(5000)})
{
fails += one_case(count, /*in_region=*/false);
fails += one_case(count, /*in_region=*/true);
}
fails += placement_case(1000);
println("\n[{}] {}", variant_name(), fails == 0 ? "=== ARRAY-NEW OK ===" : "=== ARRAY-NEW FAILURES ===");
return fails ? 1 : 0;
}
} // namespace validation::chunk_array_new_validation