| 1 |
/* Populated with mapped data, validate, mutate, validate again. |
| 2 |
The cases using sets do not mutate. |
| 3 |
Note: Some of the code in here really sucks due to being made to be |
| 4 |
compatible with c++98. */ |
| 5 |
|
| 6 |
#include <vector> |
| 7 |
#include <deque> |
| 8 |
#include <list> |
| 9 |
#include <set> |
| 10 |
#include <map> |
| 11 |
#if __cplusplus >= 201103L |
| 12 |
#include <array> |
| 13 |
#include <forward_list> |
| 14 |
#include <unordered_set> |
| 15 |
#include <unordered_map> |
| 16 |
#endif |
| 17 |
|
| 18 |
#include <limits> |
| 19 |
#include <iterator> |
| 20 |
|
| 21 |
#include "target-flex-common.h" |
| 22 |
|
| 23 |
template<bool B, class T = void> |
| 24 |
struct enable_if {}; |
| 25 |
|
| 26 |
template<class T> |
| 27 |
struct enable_if<true, T> { typedef T type; }; |
| 28 |
|
| 29 |
struct identity_func |
| 30 |
{ |
| 31 |
#if __cplusplus < 201103L |
| 32 |
template<typename T> |
| 33 |
T& operator()(T& arg) const BL_NOEXCEPT { return arg; } |
| 34 |
template<typename T> |
| 35 |
T const& operator()(T const& arg) const BL_NOEXCEPT { return arg; } |
| 36 |
#else |
| 37 |
template<typename T> |
| 38 |
constexpr T&& operator()(T&& arg) const BL_NOEXCEPT { return std::forward<T>(arg); } |
| 39 |
#endif |
| 40 |
}; |
| 41 |
|
| 42 |
/* Applies projection to the second iterator. */ |
| 43 |
template<typename It0, typename It1, typename Proj> |
| 44 |
bool validate_sequential_elements(const It0 begin0, const It0 end0, |
| 45 |
const It1 begin1, const It1 end1, |
| 46 |
Proj proj) BL_NOEXCEPT |
| 47 |
{ |
| 48 |
It0 it0 = begin0; |
| 49 |
It1 it1 = begin1; |
| 50 |
for (; it0 != end0; ++it0, ++it1) |
| 51 |
{ |
| 52 |
/* Sizes mismatch, don't bother aborting though just fail the test. */ |
| 53 |
if (it1 == end1) |
| 54 |
return false; |
| 55 |
if (*it0 != proj(*it1)) |
| 56 |
return false; |
| 57 |
} |
| 58 |
/* Sizes mismatch, do as above. */ |
| 59 |
if (it1 != end1) |
| 60 |
return false; |
| 61 |
return true; |
| 62 |
} |
| 63 |
|
| 64 |
template<typename It0, typename It1> |
| 65 |
bool validate_sequential_elements(const It0 begin0, const It0 end0, |
| 66 |
const It1 begin1, const It1 end1) BL_NOEXCEPT |
| 67 |
{ |
| 68 |
return validate_sequential_elements(begin0, end0, begin1, end1, identity_func()); |
| 69 |
} |
| 70 |
|
| 71 |
/* Inefficient, but simple. */ |
| 72 |
template<typename It, typename OutIt> |
| 73 |
void simple_copy(const It begin, const It end, OutIt out) BL_NOEXCEPT |
| 74 |
{ |
| 75 |
for (It it = begin; it != end; ++it, ++out) |
| 76 |
*out = *it; |
| 77 |
} |
| 78 |
|
| 79 |
template<typename It, typename MutateFn> |
| 80 |
void simple_mutate(const It begin, const It end, MutateFn mut_fn) BL_NOEXCEPT |
| 81 |
{ |
| 82 |
for (It it = begin; it != end; ++it) |
| 83 |
*it = mut_fn(*it); |
| 84 |
} |
| 85 |
|
| 86 |
template<typename MutationFunc, typename T, std::size_t Size> |
| 87 |
bool vector_test(const T (&arr)[Size]) |
| 88 |
{ |
| 89 |
bool ok; |
| 90 |
T out_arr[Size]; |
| 91 |
T out_mut_arr[Size]; |
| 92 |
#pragma omp target map(from: ok, out_arr[:Size], out_mut_arr[:Size]) \ |
| 93 |
map(to: arr[:Size]) |
| 94 |
{ |
| 95 |
bool inner_ok = true; |
| 96 |
{ |
| 97 |
std::vector<T> vector(arr, arr + Size); |
| 98 |
VERIFY (validate_sequential_elements(vector.begin(), vector.end(), |
| 99 |
arr, arr + Size)); |
| 100 |
simple_copy(vector.begin(), vector.end(), out_arr); |
| 101 |
simple_mutate(vector.begin(), vector.end(), MutationFunc()); |
| 102 |
VERIFY (validate_sequential_elements(vector.begin(), vector.end(), |
| 103 |
arr, arr + Size, MutationFunc())); |
| 104 |
simple_copy(vector.begin(), vector.end(), out_mut_arr); |
| 105 |
} |
| 106 |
end: |
| 107 |
ok = inner_ok; |
| 108 |
} |
| 109 |
if (!ok) |
| 110 |
return false; |
| 111 |
VERIFY_NON_TARGET (validate_sequential_elements(out_arr, out_arr + Size, |
| 112 |
arr, arr + Size)); |
| 113 |
VERIFY_NON_TARGET (validate_sequential_elements(out_mut_arr, out_mut_arr + Size, |
| 114 |
arr, arr + Size, MutationFunc())); |
| 115 |
return true; |
| 116 |
} |
| 117 |
|
| 118 |
template<typename MutationFunc, typename T, std::size_t Size> |
| 119 |
bool deque_test(const T (&arr)[Size]) |
| 120 |
{ |
| 121 |
bool ok; |
| 122 |
T out_arr[Size]; |
| 123 |
T out_mut_arr[Size]; |
| 124 |
#pragma omp target map(from: ok, out_arr[:Size], out_mut_arr[:Size]) \ |
| 125 |
map(to: arr[:Size]) |
| 126 |
{ |
| 127 |
bool inner_ok = true; |
| 128 |
{ |
| 129 |
std::deque<T> deque(arr, arr + Size); |
| 130 |
VERIFY (validate_sequential_elements(deque.begin(), deque.end(), |
| 131 |
arr, arr + Size)); |
| 132 |
simple_copy(deque.begin(), deque.end(), out_arr); |
| 133 |
simple_mutate(deque.begin(), deque.end(), MutationFunc()); |
| 134 |
VERIFY (validate_sequential_elements(deque.begin(), deque.end(), |
| 135 |
arr, arr + Size, MutationFunc())); |
| 136 |
simple_copy(deque.begin(), deque.end(), out_mut_arr); |
| 137 |
} |
| 138 |
end: |
| 139 |
ok = inner_ok; |
| 140 |
} |
| 141 |
if (!ok) |
| 142 |
return false; |
| 143 |
VERIFY_NON_TARGET (validate_sequential_elements(out_arr, out_arr + Size, |
| 144 |
arr, arr + Size)); |
| 145 |
VERIFY_NON_TARGET (validate_sequential_elements(out_mut_arr, out_mut_arr + Size, |
| 146 |
arr, arr + Size, MutationFunc())); |
| 147 |
return true; |
| 148 |
} |
| 149 |
|
| 150 |
template<typename MutationFunc, typename T, std::size_t Size> |
| 151 |
bool list_test(const T (&arr)[Size]) |
| 152 |
{ |
| 153 |
bool ok; |
| 154 |
T out_arr[Size]; |
| 155 |
T out_mut_arr[Size]; |
| 156 |
#pragma omp target map(from: ok, out_arr[:Size], out_mut_arr[:Size]) \ |
| 157 |
map(to: arr[:Size]) |
| 158 |
{ |
| 159 |
bool inner_ok = true; |
| 160 |
{ |
| 161 |
std::list<T> list(arr, arr + Size); |
| 162 |
VERIFY (validate_sequential_elements(list.begin(), list.end(), |
| 163 |
arr, arr + Size)); |
| 164 |
simple_copy(list.begin(), list.end(), out_arr); |
| 165 |
simple_mutate(list.begin(), list.end(), MutationFunc()); |
| 166 |
VERIFY (validate_sequential_elements(list.begin(), list.end(), |
| 167 |
arr, arr + Size, MutationFunc())); |
| 168 |
simple_copy(list.begin(), list.end(), out_mut_arr); |
| 169 |
} |
| 170 |
end: |
| 171 |
ok = inner_ok; |
| 172 |
} |
| 173 |
if (!ok) |
| 174 |
return false; |
| 175 |
VERIFY_NON_TARGET (validate_sequential_elements(out_arr, out_arr + Size, |
| 176 |
arr, arr + Size)); |
| 177 |
VERIFY_NON_TARGET (validate_sequential_elements(out_mut_arr, out_mut_arr + Size, |
| 178 |
arr, arr + Size, MutationFunc())); |
| 179 |
return true; |
| 180 |
} |
| 181 |
|
| 182 |
template<typename T> |
| 183 |
const T& get_key(const T& arg) BL_NOEXCEPT |
| 184 |
{ return arg; } |
| 185 |
template<typename K, typename V> |
| 186 |
const K& get_key(const std::pair<K, V>& pair) BL_NOEXCEPT |
| 187 |
{ return pair.first; } |
| 188 |
template<typename T> |
| 189 |
const T& get_value(const T& arg) BL_NOEXCEPT |
| 190 |
{ return arg; } |
| 191 |
template<typename K, typename V> |
| 192 |
const K& get_value(const std::pair<K, V>& pair) BL_NOEXCEPT |
| 193 |
{ return pair.second; } |
| 194 |
|
| 195 |
template<typename T> |
| 196 |
struct key_type { typedef T type; }; |
| 197 |
template<typename K, typename V> |
| 198 |
struct key_type<std::pair<K, V> > { typedef K type; }; |
| 199 |
|
| 200 |
template<typename Proj, typename Container, typename It> |
| 201 |
bool validate_associative(const Container& container, |
| 202 |
const It compare_begin, |
| 203 |
const It compare_end, |
| 204 |
Proj proj) BL_NOEXCEPT |
| 205 |
{ |
| 206 |
const typename Container::const_iterator elem_end = container.end(); |
| 207 |
for (It compare_it = compare_begin; compare_it != compare_end; ++compare_it) |
| 208 |
{ |
| 209 |
const typename Container::const_iterator elem_it = container.find(get_key(*compare_it)); |
| 210 |
VERIFY_NON_TARGET (elem_it != elem_end); |
| 211 |
VERIFY_NON_TARGET (proj(get_value(*compare_it)) == get_value(*elem_it)); |
| 212 |
} |
| 213 |
return true; |
| 214 |
} |
| 215 |
|
| 216 |
template<typename Container, typename It> |
| 217 |
bool validate_associative(const Container& container, |
| 218 |
const It compare_begin, |
| 219 |
const It compare_end) BL_NOEXCEPT |
| 220 |
{ |
| 221 |
return validate_associative(container, compare_begin, compare_end, identity_func()); |
| 222 |
} |
| 223 |
|
| 224 |
template<typename It, typename MutateFn> |
| 225 |
void simple_mutate_map(const It begin, const It end, MutateFn mut_fn) BL_NOEXCEPT |
| 226 |
{ |
| 227 |
for (It it = begin; it != end; ++it) |
| 228 |
it->second = mut_fn(it->second); |
| 229 |
} |
| 230 |
|
| 231 |
template<typename It, typename OutIter> |
| 232 |
void simple_copy_unique(const It begin, const It end, OutIter out) BL_NOEXCEPT |
| 233 |
{ |
| 234 |
/* In case anyone reads this, I want it to be known that I hate c++98. */ |
| 235 |
typedef typename key_type<typename std::iterator_traits<It>::value_type>::type key_t; |
| 236 |
std::set<key_t> already_seen; |
| 237 |
for (It it = begin; it != end; ++it, ++out) |
| 238 |
{ |
| 239 |
key_t key = get_key(*it); |
| 240 |
if (already_seen.find(key) != already_seen.end()) |
| 241 |
continue; |
| 242 |
already_seen.insert(key); |
| 243 |
*out = *it; |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
template<typename MutationFunc, typename K, typename V, std::size_t Size> |
| 248 |
bool map_test(const std::pair<K, V> (&arr)[Size]) |
| 249 |
{ |
| 250 |
std::map<K, V> reference_map(arr, arr + Size); |
| 251 |
bool ok; |
| 252 |
/* Both sizes should be the same. */ |
| 253 |
std::pair<K, V> out_pairs[Size]; |
| 254 |
std::size_t out_size; |
| 255 |
std::pair<K, V> out_pairs_mut[Size]; |
| 256 |
std::size_t out_size_mut; |
| 257 |
#pragma omp target map(from: ok, out_pairs[:Size], out_size, \ |
| 258 |
out_pairs_mut[:Size], out_size_mut) \ |
| 259 |
map(to: arr[:Size]) |
| 260 |
{ |
| 261 |
bool inner_ok = true; |
| 262 |
{ |
| 263 |
std::vector<std::pair<K, V> > unique_elems; |
| 264 |
simple_copy_unique(arr, arr + Size, |
| 265 |
std::back_insert_iterator<std::vector<std::pair<K, V> > >(unique_elems)); |
| 266 |
|
| 267 |
std::map<K, V> map(arr, arr + Size); |
| 268 |
VERIFY (validate_associative(map, unique_elems.begin(), unique_elems.end())); |
| 269 |
simple_copy(map.begin(), map.end(), out_pairs); |
| 270 |
out_size = map.size(); |
| 271 |
simple_mutate_map(map.begin(), map.end(), MutationFunc()); |
| 272 |
VERIFY (validate_associative(map, unique_elems.begin(), unique_elems.end(), |
| 273 |
MutationFunc())); |
| 274 |
simple_copy(map.begin(), map.end(), out_pairs_mut); |
| 275 |
out_size_mut = map.size(); |
| 276 |
} |
| 277 |
end: |
| 278 |
ok = inner_ok; |
| 279 |
} |
| 280 |
if (!ok) |
| 281 |
return false; |
| 282 |
VERIFY_NON_TARGET (out_size == out_size_mut); |
| 283 |
VERIFY_NON_TARGET (validate_associative(reference_map, |
| 284 |
out_pairs, out_pairs + out_size)); |
| 285 |
simple_mutate_map(reference_map.begin(), reference_map.end(), MutationFunc()); |
| 286 |
VERIFY_NON_TARGET (validate_associative(reference_map, |
| 287 |
out_pairs_mut, out_pairs_mut + out_size_mut)); |
| 288 |
return true; |
| 289 |
} |
| 290 |
|
| 291 |
template<typename T, std::size_t Size> |
| 292 |
bool set_test(const T (&arr)[Size]) |
| 293 |
{ |
| 294 |
std::set<T> reference_set(arr, arr + Size); |
| 295 |
bool ok; |
| 296 |
/* Both sizes should be the same. */ |
| 297 |
T out_arr[Size]; |
| 298 |
std::size_t out_size; |
| 299 |
#pragma omp target map(from: ok, out_arr[:Size], out_size) \ |
| 300 |
map(to: arr[:Size]) |
| 301 |
{ |
| 302 |
bool inner_ok = true; |
| 303 |
{ |
| 304 |
std::vector<T> unique_elems; |
| 305 |
simple_copy_unique(arr, arr + Size, |
| 306 |
std::back_insert_iterator<std::vector<T> >(unique_elems)); |
| 307 |
|
| 308 |
std::set<T> set(arr, arr + Size); |
| 309 |
VERIFY (validate_associative(set, unique_elems.begin(), unique_elems.end())); |
| 310 |
simple_copy(set.begin(), set.end(), out_arr); |
| 311 |
out_size = set.size(); |
| 312 |
/* Sets can't be mutated, we could create another set with mutated |
| 313 |
but it gets a little annoying and probably isn't an interesting test. */ |
| 314 |
} |
| 315 |
end: |
| 316 |
ok = inner_ok; |
| 317 |
} |
| 318 |
if (!ok) |
| 319 |
return false; |
| 320 |
VERIFY_NON_TARGET (validate_associative(reference_set, |
| 321 |
out_arr, out_arr + out_size)); |
| 322 |
return true; |
| 323 |
} |
| 324 |
|
| 325 |
template<typename Proj, typename Container, typename It> |
| 326 |
bool validate_multi_associative(const Container& container, |
| 327 |
const It compare_begin, |
| 328 |
const It compare_end, |
| 329 |
Proj proj) BL_NOEXCEPT |
| 330 |
{ |
| 331 |
/* Once again, for the poor soul reviewing these, I hate c++98. */ |
| 332 |
typedef typename key_type<typename std::iterator_traits<It>::value_type>::type key_t; |
| 333 |
typedef std::map<key_t, std::size_t> counter_map; |
| 334 |
counter_map key_count_map; |
| 335 |
for (It it = compare_begin; it != compare_end; ++it) |
| 336 |
{ |
| 337 |
const key_t& key = get_key(*it); |
| 338 |
typename counter_map::iterator counter_it |
| 339 |
= key_count_map.find(key); |
| 340 |
if (counter_it != key_count_map.end()) |
| 341 |
++counter_it->second; |
| 342 |
else |
| 343 |
key_count_map.insert(std::pair<const key_t, std::size_t>(key, std::size_t(1))); |
| 344 |
} |
| 345 |
const typename Container::const_iterator elem_end = container.end(); |
| 346 |
for (It compare_it = compare_begin; compare_it != compare_end; ++compare_it) |
| 347 |
{ |
| 348 |
const key_t& key = get_key(*compare_it); |
| 349 |
typename counter_map::iterator count_it = key_count_map.find(key); |
| 350 |
std::size_t key_count = count_it != key_count_map.end() ? count_it->second |
| 351 |
: std::size_t(0); |
| 352 |
VERIFY_NON_TARGET (key_count > std::size_t(0) && "this will never happen"); |
| 353 |
/* This gets tested multiple times but that should be fine. */ |
| 354 |
VERIFY_NON_TARGET (key_count == container.count(key)); |
| 355 |
typename Container::const_iterator elem_it = container.find(key); |
| 356 |
/* This will never happen if the previous case passed. */ |
| 357 |
VERIFY_NON_TARGET (elem_it != elem_end); |
| 358 |
bool found_element = false; |
| 359 |
for (; elem_it != elem_end; ++elem_it) |
| 360 |
if (proj(get_value(*compare_it)) == get_value(*elem_it)) |
| 361 |
{ |
| 362 |
found_element = true; |
| 363 |
break; |
| 364 |
} |
| 365 |
VERIFY_NON_TARGET (found_element); |
| 366 |
} |
| 367 |
return true; |
| 368 |
} |
| 369 |
|
| 370 |
template<typename Container, typename It> |
| 371 |
bool validate_multi_associative(const Container& container, |
| 372 |
const It compare_begin, |
| 373 |
const It compare_end) BL_NOEXCEPT |
| 374 |
{ |
| 375 |
return validate_multi_associative(container, compare_begin, compare_end, identity_func()); |
| 376 |
} |
| 377 |
|
| 378 |
template<typename MutationFunc, typename K, typename V, std::size_t Size> |
| 379 |
bool multimap_test(const std::pair<K, V> (&arr)[Size]) |
| 380 |
{ |
| 381 |
std::multimap<K, V> reference_multimap(arr, arr + Size); |
| 382 |
bool ok; |
| 383 |
std::pair<K, V> out_pairs[Size]; |
| 384 |
std::pair<K, V> out_pairs_mut[Size]; |
| 385 |
#pragma omp target map(from: ok, out_pairs[:Size], out_pairs_mut[:Size]) \ |
| 386 |
map(to: arr[:Size]) |
| 387 |
{ |
| 388 |
bool inner_ok = true; |
| 389 |
{ |
| 390 |
std::multimap<K, V> multimap(arr, arr + Size); |
| 391 |
VERIFY (validate_multi_associative(multimap, arr, arr + Size)); |
| 392 |
simple_copy(multimap.begin(), multimap.end(), out_pairs); |
| 393 |
simple_mutate_map(multimap.begin(), multimap.end(), MutationFunc()); |
| 394 |
VERIFY (validate_multi_associative(multimap, arr, arr + Size, MutationFunc())); |
| 395 |
simple_copy(multimap.begin(), multimap.end(), out_pairs_mut); |
| 396 |
} |
| 397 |
end: |
| 398 |
ok = inner_ok; |
| 399 |
} |
| 400 |
if (!ok) |
| 401 |
return false; |
| 402 |
VERIFY_NON_TARGET (validate_multi_associative(reference_multimap, |
| 403 |
out_pairs, out_pairs + Size)); |
| 404 |
simple_mutate_map(reference_multimap.begin(), reference_multimap.end(), MutationFunc()); |
| 405 |
VERIFY_NON_TARGET (validate_multi_associative(reference_multimap, |
| 406 |
out_pairs_mut, out_pairs_mut + Size)); |
| 407 |
return true; |
| 408 |
} |
| 409 |
|
| 410 |
template<typename T, std::size_t Size> |
| 411 |
bool multiset_test(const T (&arr)[Size]) |
| 412 |
{ |
| 413 |
std::multiset<T> reference_multiset(arr, arr + Size); |
| 414 |
bool ok; |
| 415 |
T out_arr[Size]; |
| 416 |
#pragma omp target map(from: ok, out_arr[:Size]) \ |
| 417 |
map(to: arr[:Size]) |
| 418 |
{ |
| 419 |
bool inner_ok = true; |
| 420 |
{ |
| 421 |
std::multiset<T> set(arr, arr + Size); |
| 422 |
VERIFY (validate_multi_associative(set, arr, arr + Size)); |
| 423 |
simple_copy(set.begin(), set.end(), out_arr); |
| 424 |
/* Sets can't be mutated, we could create another set with mutated |
| 425 |
but it gets a little annoying and probably isn't an interesting test. */ |
| 426 |
} |
| 427 |
end: |
| 428 |
ok = inner_ok; |
| 429 |
} |
| 430 |
if (!ok) |
| 431 |
return false; |
| 432 |
VERIFY_NON_TARGET (validate_multi_associative(reference_multiset, |
| 433 |
out_arr, out_arr + Size)); |
| 434 |
return true; |
| 435 |
} |
| 436 |
|
| 437 |
#if __cplusplus >= 201103L |
| 438 |
|
| 439 |
template<typename MutationFunc, typename T, std::size_t Size> |
| 440 |
bool array_test(const T (&arr)[Size]) |
| 441 |
{ |
| 442 |
bool ok; |
| 443 |
T out_arr[Size]; |
| 444 |
T out_mut_arr[Size]; |
| 445 |
#pragma omp target map(from: ok, out_arr[:Size], out_mut_arr[:Size]) \ |
| 446 |
map(to: arr[:Size]) |
| 447 |
{ |
| 448 |
bool inner_ok = true; |
| 449 |
{ |
| 450 |
std::array<T, Size> std_array{}; |
| 451 |
/* Special case for std::array since it can't be initialized |
| 452 |
with iterators. */ |
| 453 |
{ |
| 454 |
T zero_val = T{}; |
| 455 |
for (auto it = std_array.begin(); it != std_array.end(); ++it) |
| 456 |
VERIFY (*it == zero_val); |
| 457 |
} |
| 458 |
simple_copy(arr, arr + Size, std_array.begin()); |
| 459 |
VERIFY (validate_sequential_elements(std_array.begin(), std_array.end(), |
| 460 |
arr, arr + Size)); |
| 461 |
simple_copy(std_array.begin(), std_array.end(), out_arr); |
| 462 |
simple_mutate(std_array.begin(), std_array.end(), MutationFunc()); |
| 463 |
VERIFY (validate_sequential_elements(std_array.begin(), std_array.end(), |
| 464 |
arr, arr + Size, MutationFunc())); |
| 465 |
simple_copy(std_array.begin(), std_array.end(), out_mut_arr); |
| 466 |
} |
| 467 |
end: |
| 468 |
ok = inner_ok; |
| 469 |
} |
| 470 |
if (!ok) |
| 471 |
return false; |
| 472 |
VERIFY_NON_TARGET (validate_sequential_elements(out_arr, out_arr + Size, |
| 473 |
arr, arr + Size)); |
| 474 |
VERIFY_NON_TARGET (validate_sequential_elements(out_mut_arr, out_mut_arr + Size, |
| 475 |
arr, arr + Size, MutationFunc())); |
| 476 |
return true; |
| 477 |
} |
| 478 |
|
| 479 |
template<typename MutationFunc, typename T, std::size_t Size> |
| 480 |
bool forward_list_test(const T (&arr)[Size]) |
| 481 |
{ |
| 482 |
bool ok; |
| 483 |
T out_arr[Size]; |
| 484 |
T out_mut_arr[Size]; |
| 485 |
#pragma omp target map(from: ok, out_arr[:Size], out_mut_arr[:Size]) \ |
| 486 |
map(to: arr[:Size]) |
| 487 |
{ |
| 488 |
bool inner_ok = true; |
| 489 |
{ |
| 490 |
std::forward_list<T> fwd_list(arr, arr + Size); |
| 491 |
VERIFY (validate_sequential_elements(fwd_list.begin(), fwd_list.end(), |
| 492 |
arr, arr + Size)); |
| 493 |
simple_copy(fwd_list.begin(), fwd_list.end(), out_arr); |
| 494 |
simple_mutate(fwd_list.begin(), fwd_list.end(), MutationFunc()); |
| 495 |
VERIFY (validate_sequential_elements(fwd_list.begin(), fwd_list.end(), |
| 496 |
arr, arr + Size, MutationFunc())); |
| 497 |
simple_copy(fwd_list.begin(), fwd_list.end(), out_mut_arr); |
| 498 |
} |
| 499 |
end: |
| 500 |
ok = inner_ok; |
| 501 |
} |
| 502 |
if (!ok) |
| 503 |
return false; |
| 504 |
VERIFY_NON_TARGET (validate_sequential_elements(out_arr, out_arr + Size, |
| 505 |
arr, arr + Size)); |
| 506 |
VERIFY_NON_TARGET (validate_sequential_elements(out_mut_arr, out_mut_arr + Size, |
| 507 |
arr, arr + Size, MutationFunc())); |
| 508 |
return true; |
| 509 |
} |
| 510 |
|
| 511 |
template<typename MutationFunc, typename K, typename V, std::size_t Size> |
| 512 |
bool unordered_map_test(const std::pair<K, V> (&arr)[Size]) |
| 513 |
{ |
| 514 |
std::unordered_map<K, V> reference_map(arr, arr + Size); |
| 515 |
bool ok; |
| 516 |
/* Both sizes should be the same. */ |
| 517 |
std::pair<K, V> out_pairs[Size]; |
| 518 |
std::size_t out_size; |
| 519 |
std::pair<K, V> out_pairs_mut[Size]; |
| 520 |
std::size_t out_size_mut; |
| 521 |
#pragma omp target map(from: ok, out_pairs[:Size], out_size, \ |
| 522 |
out_pairs_mut[:Size], out_size_mut) \ |
| 523 |
map(to: arr[:Size]) |
| 524 |
{ |
| 525 |
bool inner_ok = true; |
| 526 |
{ |
| 527 |
std::vector<std::pair<K, V> > unique_elems; |
| 528 |
simple_copy_unique(arr, arr + Size, |
| 529 |
std::back_insert_iterator<std::vector<std::pair<K, V> > >(unique_elems)); |
| 530 |
|
| 531 |
std::unordered_map<K, V> map(arr, arr + Size); |
| 532 |
VERIFY (validate_associative(map, unique_elems.begin(), unique_elems.end())); |
| 533 |
simple_copy(map.begin(), map.end(), out_pairs); |
| 534 |
out_size = map.size(); |
| 535 |
simple_mutate_map(map.begin(), map.end(), MutationFunc()); |
| 536 |
VERIFY (validate_associative(map, unique_elems.begin(), unique_elems.end(), |
| 537 |
MutationFunc())); |
| 538 |
simple_copy(map.begin(), map.end(), out_pairs_mut); |
| 539 |
out_size_mut = map.size(); |
| 540 |
} |
| 541 |
end: |
| 542 |
ok = inner_ok; |
| 543 |
} |
| 544 |
if (!ok) |
| 545 |
return false; |
| 546 |
VERIFY_NON_TARGET (out_size == out_size_mut); |
| 547 |
VERIFY_NON_TARGET (validate_associative(reference_map, |
| 548 |
out_pairs, out_pairs + out_size)); |
| 549 |
simple_mutate_map(reference_map.begin(), reference_map.end(), MutationFunc()); |
| 550 |
VERIFY_NON_TARGET (validate_associative(reference_map, |
| 551 |
out_pairs_mut, out_pairs_mut + out_size_mut)); |
| 552 |
return true; |
| 553 |
} |
| 554 |
|
| 555 |
template<typename T, std::size_t Size> |
| 556 |
bool unordered_set_test(const T (&arr)[Size]) |
| 557 |
{ |
| 558 |
std::unordered_set<T> reference_set(arr, arr + Size); |
| 559 |
bool ok; |
| 560 |
/* Both sizes should be the same. */ |
| 561 |
T out_arr[Size]; |
| 562 |
std::size_t out_size; |
| 563 |
#pragma omp target map(from: ok, out_arr[:Size], out_size) \ |
| 564 |
map(to: arr[:Size]) |
| 565 |
{ |
| 566 |
bool inner_ok = true; |
| 567 |
{ |
| 568 |
std::vector<T> unique_elems; |
| 569 |
simple_copy_unique(arr, arr + Size, |
| 570 |
std::back_insert_iterator<std::vector<T> >(unique_elems)); |
| 571 |
|
| 572 |
std::unordered_set<T> set(arr, arr + Size); |
| 573 |
VERIFY (validate_associative(set, unique_elems.begin(), unique_elems.end())); |
| 574 |
simple_copy(set.begin(), set.end(), out_arr); |
| 575 |
out_size = set.size(); |
| 576 |
/* Sets can't be mutated, we could create another set with mutated |
| 577 |
but it gets a little annoying and probably isn't an interesting test. */ |
| 578 |
} |
| 579 |
end: |
| 580 |
ok = inner_ok; |
| 581 |
} |
| 582 |
if (!ok) |
| 583 |
return false; |
| 584 |
VERIFY_NON_TARGET (validate_associative(reference_set, |
| 585 |
out_arr, out_arr + out_size)); |
| 586 |
return true; |
| 587 |
} |
| 588 |
|
| 589 |
template<typename MutationFunc, typename K, typename V, std::size_t Size> |
| 590 |
bool unordered_multimap_test(const std::pair<K, V> (&arr)[Size]) |
| 591 |
{ |
| 592 |
std::unordered_multimap<K, V> reference_multimap(arr, arr + Size); |
| 593 |
bool ok; |
| 594 |
std::pair<K, V> out_pairs[Size]; |
| 595 |
std::pair<K, V> out_pairs_mut[Size]; |
| 596 |
#pragma omp target map(from: ok, out_pairs[:Size], out_pairs_mut[:Size]) \ |
| 597 |
map(to: arr[:Size]) |
| 598 |
{ |
| 599 |
bool inner_ok = true; |
| 600 |
{ |
| 601 |
std::unordered_multimap<K, V> multimap(arr, arr + Size); |
| 602 |
VERIFY (validate_multi_associative(multimap, arr, arr + Size)); |
| 603 |
simple_copy(multimap.begin(), multimap.end(), out_pairs); |
| 604 |
simple_mutate_map(multimap.begin(), multimap.end(), MutationFunc()); |
| 605 |
VERIFY (validate_multi_associative(multimap, arr, arr + Size, MutationFunc())); |
| 606 |
simple_copy(multimap.begin(), multimap.end(), out_pairs_mut); |
| 607 |
} |
| 608 |
end: |
| 609 |
ok = inner_ok; |
| 610 |
} |
| 611 |
if (!ok) |
| 612 |
return false; |
| 613 |
VERIFY_NON_TARGET (validate_multi_associative(reference_multimap, |
| 614 |
out_pairs, out_pairs + Size)); |
| 615 |
simple_mutate_map(reference_multimap.begin(), reference_multimap.end(), MutationFunc()); |
| 616 |
VERIFY_NON_TARGET (validate_multi_associative(reference_multimap, |
| 617 |
out_pairs_mut, out_pairs_mut + Size)); |
| 618 |
return true; |
| 619 |
} |
| 620 |
|
| 621 |
template<typename T, std::size_t Size> |
| 622 |
bool unordered_multiset_test(const T (&arr)[Size]) |
| 623 |
{ |
| 624 |
std::unordered_multiset<T> reference_multiset(arr, arr + Size); |
| 625 |
bool ok; |
| 626 |
T out_arr[Size]; |
| 627 |
#pragma omp target map(from: ok, out_arr[:Size]) \ |
| 628 |
map(to: arr[:Size]) |
| 629 |
{ |
| 630 |
bool inner_ok = true; |
| 631 |
{ |
| 632 |
std::unordered_multiset<T> set(arr, arr + Size); |
| 633 |
VERIFY (validate_multi_associative(set, arr, arr + Size)); |
| 634 |
simple_copy(set.begin(), set.end(), out_arr); |
| 635 |
/* Sets can't be mutated, we could create another set with mutated |
| 636 |
but it gets a little annoying and probably isn't an interesting test. */ |
| 637 |
} |
| 638 |
end: |
| 639 |
ok = inner_ok; |
| 640 |
} |
| 641 |
if (!ok) |
| 642 |
return false; |
| 643 |
VERIFY_NON_TARGET (validate_multi_associative(reference_multiset, |
| 644 |
out_arr, out_arr + Size)); |
| 645 |
return true; |
| 646 |
} |
| 647 |
|
| 648 |
#else |
| 649 |
template<typename, typename T, std::size_t Size> bool array_test(const T (&arr)[Size]) { return true; } |
| 650 |
template<typename, typename T, std::size_t Size> bool forward_list_test(const T (&arr)[Size]) { return true; } |
| 651 |
template<typename, typename T, std::size_t Size> bool unordered_map_test(const T (&arr)[Size]) { return true; } |
| 652 |
template<typename T, std::size_t Size> bool unordered_set_test(const T (&arr)[Size]) { return true; } |
| 653 |
template<typename, typename T, std::size_t Size> bool unordered_multimap_test(const T (&arr)[Size]) { return true; } |
| 654 |
template<typename T, std::size_t Size> bool unordered_multiset_test(const T (&arr)[Size]) { return true; } |
| 655 |
#endif |
| 656 |
|
| 657 |
/* This clamps to the maximum value to guard against overflowing, |
| 658 |
assuming std::numeric_limits is specialized for T. */ |
| 659 |
struct multiply_by_2 |
| 660 |
{ |
| 661 |
template<typename T> |
| 662 |
typename enable_if<std::numeric_limits<T>::is_specialized, T>::type |
| 663 |
operator()(T arg) const BL_NOEXCEPT { |
| 664 |
if (arg < static_cast<T>(0)) |
| 665 |
{ |
| 666 |
if (std::numeric_limits<T>::min() / static_cast<T>(2) >= arg) |
| 667 |
return std::numeric_limits<T>::min(); |
| 668 |
} |
| 669 |
else |
| 670 |
{ |
| 671 |
if (std::numeric_limits<T>::max() / static_cast<T>(2) <= arg) |
| 672 |
return std::numeric_limits<T>::max(); |
| 673 |
} |
| 674 |
return arg * 2; |
| 675 |
} |
| 676 |
template<typename T> |
| 677 |
typename enable_if<!std::numeric_limits<T>::is_specialized, T>::type |
| 678 |
operator()(T arg) const BL_NOEXCEPT { |
| 679 |
return arg * 2; |
| 680 |
} |
| 681 |
}; |
| 682 |
|
| 683 |
int main() |
| 684 |
{ |
| 685 |
int data[8] = {0, 1, 2, 3, 4, 5, 6, 7}; |
| 686 |
std::pair<int, int> pairs[10] = {std::pair<int, int>( 1, 2), |
| 687 |
std::pair<int, int>( 2, 4), |
| 688 |
std::pair<int, int>( 3, 6), |
| 689 |
std::pair<int, int>( 4, 8), |
| 690 |
std::pair<int, int>( 5, 10), |
| 691 |
std::pair<int, int>( 6, 12), |
| 692 |
std::pair<int, int>( 7, 14), |
| 693 |
std::pair<int, int>( 8, 16), |
| 694 |
std::pair<int, int>( 9, 18), |
| 695 |
std::pair<int, int>(10, 20)}; |
| 696 |
const bool vec_res = vector_test<multiply_by_2>(data); |
| 697 |
const bool deque_res = deque_test<multiply_by_2>(data); |
| 698 |
const bool list_res = list_test<multiply_by_2>(data); |
| 699 |
const bool map_res = map_test<multiply_by_2>(pairs); |
| 700 |
const bool set_res = set_test(data); |
| 701 |
const bool multimap_res = multimap_test<multiply_by_2>(pairs); |
| 702 |
const bool multiset_res = multiset_test(data); |
| 703 |
const bool array_res = array_test<multiply_by_2>(data); |
| 704 |
const bool forward_list_res = forward_list_test<multiply_by_2>(data); |
| 705 |
const bool unordered_map_res = unordered_map_test<multiply_by_2>(pairs); |
| 706 |
const bool unordered_set_res = unordered_set_test(data); |
| 707 |
const bool unordered_multimap_res = unordered_multimap_test<multiply_by_2>(pairs); |
| 708 |
const bool unordered_multiset_res = unordered_multiset_test(data); |
| 709 |
std::printf("vector : %s\n", vec_res ? "PASS" : "FAIL"); |
| 710 |
std::printf("deque : %s\n", deque_res ? "PASS" : "FAIL"); |
| 711 |
std::printf("list : %s\n", list_res ? "PASS" : "FAIL"); |
| 712 |
std::printf("map : %s\n", map_res ? "PASS" : "FAIL"); |
| 713 |
std::printf("set : %s\n", set_res ? "PASS" : "FAIL"); |
| 714 |
std::printf("multimap : %s\n", multimap_res ? "PASS" : "FAIL"); |
| 715 |
std::printf("multiset : %s\n", multiset_res ? "PASS" : "FAIL"); |
| 716 |
std::printf("array : %s\n", array_res ? "PASS" : "FAIL"); |
| 717 |
std::printf("forward_list : %s\n", forward_list_res ? "PASS" : "FAIL"); |
| 718 |
std::printf("unordered_map : %s\n", unordered_map_res ? "PASS" : "FAIL"); |
| 719 |
std::printf("unordered_set : %s\n", unordered_set_res ? "PASS" : "FAIL"); |
| 720 |
std::printf("unordered_multimap: %s\n", unordered_multimap_res ? "PASS" : "FAIL"); |
| 721 |
std::printf("unordered_multiset: %s\n", unordered_multiset_res ? "PASS" : "FAIL"); |
| 722 |
const bool ok = vec_res |
| 723 |
&& deque_res |
| 724 |
&& list_res |
| 725 |
&& map_res |
| 726 |
&& set_res |
| 727 |
&& multimap_res |
| 728 |
&& multiset_res |
| 729 |
&& array_res |
| 730 |
&& forward_list_res |
| 731 |
&& unordered_map_res |
| 732 |
&& unordered_set_res |
| 733 |
&& unordered_multimap_res |
| 734 |
&& unordered_multiset_res; |
| 735 |
return ok ? 0 : 1; |
| 736 |
} |