Repository navigation
Expand file tree
/
Copy pathbulk_write_team_validation.cpp
More file actions
285 lines (255 loc) · 10.7 KB
/
Copy pathbulk_write_team_validation.cpp
File metadata and controls
285 lines (255 loc) · 10.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
/*
* 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.
*/
// ============================================================================
// The whole-array writes that take a team of their own outside a region
// (ArrayChunkBase::write_range_by_chunks): the copy constructor and copy
// assignment (copy_from), assign(count, val), assign(first, last) from a
// random-access and from a forward source, resize(n, val); a shrinking resize
// (release_chunk_range on a team); and zero_any::Array's operator== (on a team,
// stopping at a difference).
//
// The arrays have far more chunks than parallel_init_min_chunks, so outside a
// region every one of these runs on a team -- through parallel_phase(): in a
// check build without OMPT, a write deferred by a thread that did not begin
// the phase fails the phase protocol. Each result is compared element by
// element with a std::vector, before and after a compression pass. The same
// operations on a small array inside a region run on the calling thread, one
// thread at a time (the copy and assign() are sequential points).
//
// Exit 0 = every check holds. Any thread count (the tests run it at 1 and 8).
// ============================================================================
#include <forward_list> // std::forward_list (assign from a forward iterator)
#include <mutex> // std::mutex, std::scoped_lock (the in-region arrays, one at a time)
#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/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" // parallel_phase
#include "chunk/internal/env_parse.h" // shrink_array::arg_number
#include "chunk/print.h" // shrink_array::println
#include "validation_harness.h" // run() is called by validation_driver.cpp
namespace validation::bulk_write_team_validation
{
using namespace shrink_array;
namespace
{
static_assert(chunk_config::use_zero || chunk_config::use_zero_or_value || chunk_config::use_zero_any,
"bulk_write_team_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";
}
}
/// @brief Runs of zero, runs of 7 and varied values, by blocks of 1000.
template <typename T>
T value_at(long index)
{
switch ((index / 1000) % 3)
{
case 0:
return T{};
case 1:
return static_cast<T>(7);
default:
return static_cast<T>((index % 97) + 1);
}
}
template <typename T>
int compare(const char* type, const char* step, const Array<T>& arr, const std::vector<T>& ref)
{
if (static_cast<std::size_t>(arr.size()) != ref.size())
{
println("[{}/{}/{}] FAIL: size {}, expected {}", variant_name(), type, step, static_cast<long>(arr.size()),
ref.size());
return 1;
}
long bad = 0;
for (std::size_t i = 0; i < ref.size(); ++i)
{
if (!(std::as_const(arr)[static_cast<int>(i)] == ref[i]))
{
if (bad < 3)
{
println("[{}/{}/{}] FAIL: element {} differs", variant_name(), type, step, i);
}
++bad;
}
}
if (bad)
{
println("[{}/{}/{}] FAIL: {} element(s) differ", variant_name(), type, step, bad);
}
return bad ? 1 : 0;
}
/// @brief compare(), then again after a compression pass.
template <typename T>
int check(const char* type, const char* step, Array<T>& arr, const std::vector<T>& ref)
{
int fails = compare(type, step, arr, ref);
arr.compress();
fails += compare(type, step, arr, ref);
return fails;
}
template <typename T>
int run_type(const char* type, long count)
{
int fails = 0;
std::vector<T> ref(static_cast<std::size_t>(count));
Array<T> arr(static_cast<int>(count));
for (long i = 0; i < count; ++i)
{
ref[static_cast<std::size_t>(i)] = value_at<T>(i);
arr[static_cast<int>(i)] = value_at<T>(i);
}
fails += check(type, "source", arr, ref);
// Copy constructor.
Array<T> copy(arr);
fails += check(type, "copy-construct", copy, ref);
// Copy assignment into an array of another size and content.
Array<T> small(static_cast<int>(count / 3));
for (long i = 0; i < count / 3; ++i)
{
small[static_cast<int>(i)] = static_cast<T>(3);
}
small = arr;
fails += check(type, "copy-assign", small, ref);
if constexpr (chunk_config::use_zero_any)
{
if (!(copy == arr) || !(small == arr))
{
println("[{}/{}] FAIL: operator== on equal arrays", variant_name(), type);
++fails;
}
copy[static_cast<int>(count - 2)] = static_cast<T>(55);
if (copy == arr)
{
println("[{}/{}] FAIL: operator== misses a difference near the end", variant_name(), type);
++fails;
}
copy[static_cast<int>(count - 2)] = ref[static_cast<std::size_t>(count - 2)];
small[1] = static_cast<T>(55);
if (small == arr)
{
println("[{}/{}] FAIL: operator== misses a difference at the start", variant_name(), type);
++fails;
}
}
// assign(count, val): a value, then the first constant.
copy.assign(static_cast<int>(count + count / 2), static_cast<T>(9));
fails +=
check(type, "assign-9", copy, std::vector<T>(static_cast<std::size_t>(count + count / 2), static_cast<T>(9)));
copy.assign(static_cast<int>(count), T{});
fails += check(type, "assign-0", copy, std::vector<T>(static_cast<std::size_t>(count), T{}));
// assign(first, last): random access (a team), forward (in order).
copy.assign(ref.begin(), ref.end());
fails += check(type, "assign-random-access", copy, ref);
std::forward_list<T> fl;
for (long i = 999; i >= 0; --i)
{
fl.push_front(value_at<T>(i + 1000));
}
const std::vector<T> ref_fl(fl.begin(), fl.end());
copy.assign(fl.begin(), fl.end());
fails += check(type, "assign-forward", copy, ref_fl);
// resize(count, val): the new elements only.
std::vector<T> ref_grow = ref;
ref_grow.resize(static_cast<std::size_t>(count + count / 2), static_cast<T>(5));
arr.resize(static_cast<int>(count + count / 2), static_cast<T>(5));
fails += check(type, "resize-grow-val", arr, ref_grow);
// A shrink large enough for its release to take a team, then a growth.
ref_grow.resize(static_cast<std::size_t>(count / 4));
arr.resize(static_cast<int>(count / 4));
fails += check(type, "resize-shrink", arr, ref_grow);
ref_grow.resize(static_cast<std::size_t>(count), T{});
arr.resize(static_cast<int>(count));
fails += check(type, "resize-grow-again", arr, ref_grow);
// Inside a region: each thread its own small array, on the calling thread.
// One thread at a time (a mutex, not `omp critical`: the region is a team
// of the build's backend, the std backend's too, where an OpenMP pragma is
// ignored). Building and destroying arrays from several threads at once
// would be allowed -- both take the cold-path lock (cold_path_lock.h), and
// region_storage_validation covers the whole team doing it -- but the copy
// constructor (copy_from() resizes) and assign() are sequential points:
// inside a region, one thread at a time while the others wait
// (docs/getting_started.md, section 5).
int region_fails = 0;
std::mutex one_at_a_time;
parallel_phase(
[&]
{
{
const std::scoped_lock lock(one_at_a_time);
const int n_region = 3000;
std::vector<T> ref(static_cast<std::size_t>(n_region));
Array<T> src(n_region);
for (int i = 0; i < n_region; ++i)
{
ref[static_cast<std::size_t>(i)] = value_at<T>(i);
src[i] = value_at<T>(i);
}
Array<T> dst(src);
int fails = compare(type, "in-region-copy", dst, ref);
dst.assign(n_region, static_cast<T>(4));
fails += compare(type, "in-region-assign", dst,
std::vector<T>(static_cast<std::size_t>(n_region), static_cast<T>(4)));
region_fails += fails; // under the lock
}
});
fails += region_fails;
if (fails == 0)
{
println("[{}/{}] OK: N={} copy, assign, resize(n, val), shrink{}", variant_name(), type, count,
chunk_config::use_zero_any ? ", operator==" : "");
}
return fails;
}
} // namespace
int run(int argc, char** argv)
{
Runtime::init(true);
const long count = argc > 1 ? shrink_array::arg_number<long>(argv[1], 1L << 20, "N") : (1L << 20);
println("=== bulk_write_team_validation [variant = {}] threads={} ===", variant_name(), CHUNK_PMP_MAX_THREADS);
int fails = 0;
fails += run_type<double>("double", count);
fails += run_type<int>("int", count);
release_chunk_pools();
Runtime::shutdown();
println("\n[{}] {}", variant_name(), fails ? "=== BULK WRITES FAILED ===" : "=== BULK WRITES OK ===");
return fails ? 1 : 0;
}
} // namespace validation::bulk_write_team_validation