deal.II version GIT relicensing-7182-g951947b1c4 2026-11-26 10:30:01+00:00
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
Loading...
Searching...
No Matches
aligned_vector.h
Go to the documentation of this file.
1// -----------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
4// Copyright (C) 2014 - 2026 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Detailed license information governing the source code and contributions
9// can be found in LICENSE.md and CONTRIBUTING.md at the top level directory.
10//
11// -----------------------------------------------------------------------------
12
13
14#ifndef dealii_aligned_vector_h
15#define dealii_aligned_vector_h
16
17#include <deal.II/base/config.h>
18
21#include <deal.II/base/mpi.h>
24
25// boost::serialization::make_array used to be in array.hpp, but was
26// moved to a different file in BOOST 1.64
27#include <boost/version.hpp>
28#if BOOST_VERSION >= 106400
29# include <boost/serialization/array_wrapper.hpp>
30#else
31# include <boost/serialization/array.hpp>
32#endif
33#include <boost/serialization/binary_object.hpp>
34#include <boost/serialization/split_member.hpp>
35
36#include <cstring>
37#include <functional>
38#include <iterator>
39#include <memory>
40#include <type_traits>
41
42
43
45
46
60template <class T>
62{
63public:
68 using value_type = T;
70 using const_pointer = const value_type *;
72 using const_iterator = const value_type *;
74 using const_reference = const value_type &;
75 using size_type = std::size_t;
76
81
90 template <
91 typename RandomAccessIterator,
92 typename = std::enable_if_t<std::is_convertible_v<
93 typename std::iterator_traits<RandomAccessIterator>::iterator_category,
94 std::random_access_iterator_tag>>>
95 AlignedVector(RandomAccessIterator begin, RandomAccessIterator end);
96
103 explicit AlignedVector(const size_type size, const T &init = T());
104
108 ~AlignedVector() = default;
109
116
122
130
136
156 void
157 resize_fast(const size_type new_size);
158
171 void
172 resize(const size_type new_size);
173
189 void
190 resize(const size_type new_size, const T &init);
191
212 void
213 reserve(const size_type new_allocated_size);
214
218 void
220
225 void
227
233 void
234 push_back(const T in_data);
235
241
246 back() const;
247
252 template <typename ForwardIterator>
253 void
254 insert_back(ForwardIterator begin, ForwardIterator end);
255
274 template <
275 typename RandomAccessIterator,
276 typename = std::enable_if_t<std::is_convertible_v<
277 typename std::iterator_traits<RandomAccessIterator>::iterator_category,
278 std::random_access_iterator_tag>>>
281 RandomAccessIterator begin,
282 RandomAccessIterator end);
283
293 void
295
304 void
305 fill(const T &element);
306
394 void
396 const unsigned int root_process);
397
401 void
402 swap(AlignedVector<T> &vec) noexcept;
403
407 bool
408 empty() const;
409
414 size() const;
415
421 capacity() const;
422
427 operator[](const size_type index);
428
433 operator[](const size_type index) const;
434
438 pointer
440
445 data() const;
446
452
458
463 begin() const;
464
469 end() const;
470
478
484 template <class Archive>
485 void
486 save(Archive &ar, const unsigned int version) const;
487
493 template <class Archive>
494 void
495 load(Archive &ar, const unsigned int version);
496
497#ifdef DOXYGEN
503 template <class Archive>
504 void
505 serialize(Archive &archive, const unsigned int version);
506#else
507 // This macro defines the serialize() method that is compatible with
508 // the templated save() and load() method that have been implemented.
509 BOOST_SERIALIZATION_SPLIT_MEMBER()
510#endif
511
519 "Changing the vector after a call to "
520 "replicate_across_communicator() is not allowed.");
521
522private:
527 void
528 allocate_and_move(const std::size_t old_size,
529 const std::size_t new_size,
530 const std::size_t new_allocated_size);
531
624 {
625 public:
631 Deleter(AlignedVector<T> *owning_object);
632
633#ifdef DEAL_II_WITH_MPI
641 Deleter(AlignedVector<T> *owning_object,
642 const bool is_shmem_root,
643 T *aligned_shmem_pointer,
644 MPI_Comm shmem_group_communicator,
645 MPI_Win shmem_window);
646#endif
647
653 void
654 operator()(T *ptr);
655
663 void
664 reset_owning_object(const AlignedVector<T> *new_aligned_vector_ptr);
665
666 private:
671 {
672 public:
676 virtual ~DeleterActionBase() = default;
677
683 virtual void
685 };
686
687#ifdef DEAL_II_WITH_MPI
688
694 {
695 public:
703 MPI_Win shmem_window);
704
710 virtual void
711 delete_array(const AlignedVector<T> *aligned_vector, T *ptr) override;
712
713 private:
718 const bool is_shmem_root;
722 };
723#endif
724
729 std::unique_ptr<DeleterActionBase> deleter_action_object;
730
736 };
737
741 std::unique_ptr<T[], Deleter> elements;
742
747
752
757};
758
759
760// ------------------------------- inline functions --------------------------
761
767namespace internal
768{
787 template <typename RandomAccessIterator, typename T>
790 {
791 static const std::size_t minimum_parallel_grain_size =
792 160000 / sizeof(T) + 1;
793
794 public:
804 AlignedVectorCopyConstruct(RandomAccessIterator source_begin,
805 RandomAccessIterator source_end,
806 T *const destination)
807 : source_(source_begin)
808 , destination_(destination)
809 {
810 Assert(source_end >= source_begin, ExcInternalError());
811 Assert(source_end == source_begin || destination != nullptr,
813 const std::size_t size = source_end - source_begin;
816 else
818 }
819
824 virtual void
825 apply_to_subrange(const std::size_t begin,
826 const std::size_t end) const override
827 {
828 if (end == begin)
829 return;
830
831 // We can use memcpy() with trivially copyable objects.
832 if constexpr (std::is_trivially_copyable_v<T> == true &&
833 (std::is_same_v<T *, RandomAccessIterator> ||
834 std::is_same_v<const T *, RandomAccessIterator>) == true)
835 std::memcpy(destination_ + begin,
836 source_ + begin,
837 (end - begin) * sizeof(T));
838 else
839 for (std::size_t i = begin; i < end; ++i)
840 new (&destination_[i]) T(*(source_ + i));
841 }
842
843 private:
844 RandomAccessIterator source_;
845 T *const destination_;
846 };
847
848
855 template <typename RandomAccessIterator, typename T>
858 {
859 static const std::size_t minimum_parallel_grain_size =
860 160000 / sizeof(T) + 1;
861
862 public:
872 AlignedVectorMoveConstruct(RandomAccessIterator source_begin,
873 RandomAccessIterator source_end,
874 T *const destination)
875 : source_(source_begin)
876 , destination_(destination)
877 {
878 Assert(source_end >= source_begin, ExcInternalError());
879 Assert(source_end == source_begin || destination != nullptr,
881 const std::size_t size = source_end - source_begin;
884 else
886 }
887
892 virtual void
893 apply_to_subrange(const std::size_t begin,
894 const std::size_t end) const override
895 {
896 if (end == begin)
897 return;
898
899 // We can use memcpy() with trivially copyable objects.
900 if constexpr (std::is_trivially_copyable_v<T> == true &&
901 (std::is_same_v<T *, RandomAccessIterator> ||
902 std::is_same_v<const T *, RandomAccessIterator>) == true)
903 std::memcpy(destination_ + begin,
904 source_ + begin,
905 (end - begin) * sizeof(T));
906 else
907 // For everything else just use the move constructor. The original
908 // object remains alive and will be destroyed elsewhere.
909 for (std::size_t i = begin; i < end; ++i)
910 new (&destination_[i]) T(std::move(*(source_ + i)));
911 }
912
913 private:
914 RandomAccessIterator source_;
915 T *const destination_;
916 };
917
918
936 template <typename T, bool initialize_memory>
938 {
939 static const std::size_t minimum_parallel_grain_size =
940 160000 / sizeof(T) + 1;
941
942 public:
947 AlignedVectorInitialize(const std::size_t size,
948 const T &element,
949 T *const destination)
950 : element_(element)
951 , destination_(destination)
952 , trivial_element(false)
953 {
954 if (size == 0)
955 return;
956 Assert(destination != nullptr, ExcInternalError());
957
958 // do not use memcmp() for long double because on some systems it does not
959 // completely fill its memory and may lead to false positives in e.g.
960 // valgrind
961 if constexpr (std::is_trivially_default_constructible_v<T> == true &&
962 std::is_same_v<T, long double> == false)
963 {
964 const unsigned char zero[sizeof(T)] = {};
965 if (std::memcmp(zero, &element, sizeof(T)) == 0)
966 trivial_element = true;
967 }
970 else
972 }
973
977 virtual void
978 apply_to_subrange(const std::size_t begin,
979 const std::size_t end) const override
980 {
981 // Only use memset() with types whose default constructors don't do
982 // anything.
983 if constexpr (std::is_trivially_default_constructible_v<T> == true)
984 if (trivial_element)
985 {
986 std::memset(destination_ + begin, 0, (end - begin) * sizeof(T));
987 return;
988 }
989
991 end,
992 std::bool_constant<initialize_memory>());
993 }
994
995 private:
996 const T &element_;
997 mutable T *destination_;
999
1000 // copy assignment operation
1001 void
1003 const std::size_t end,
1004 std::bool_constant<false>) const
1005 {
1006 for (std::size_t i = begin; i < end; ++i)
1008 }
1009
1010 // copy constructor (memory initialization)
1011 void
1013 const std::size_t end,
1014 std::bool_constant<true>) const
1015 {
1016 for (std::size_t i = begin; i < end; ++i)
1017 new (&destination_[i]) T(element_);
1018 }
1019 };
1020
1021
1022
1035 template <typename T, bool initialize_memory>
1038 {
1039 static const std::size_t minimum_parallel_grain_size =
1040 160000 / sizeof(T) + 1;
1041
1042 public:
1047 AlignedVectorDefaultInitialize(const std::size_t size, T *const destination)
1048 : destination_(destination)
1049 {
1050 if (size == 0)
1051 return;
1052 Assert(destination != nullptr, ExcInternalError());
1053
1056 else
1058 }
1059
1063 virtual void
1064 apply_to_subrange(const std::size_t begin,
1065 const std::size_t end) const override
1066 {
1067 // Only use memset() with types whose default constructors don't do
1068 // anything.
1069 if constexpr (std::is_trivially_default_constructible_v<T> == true)
1070 std::memset(destination_ + begin, 0, (end - begin) * sizeof(T));
1071 else
1073 end,
1074 std::bool_constant<initialize_memory>());
1075 }
1076
1077 private:
1078 mutable T *destination_;
1079
1080 // copy assignment operation
1081 void
1083 const std::size_t end,
1084 std::bool_constant<false>) const
1085 {
1086 for (std::size_t i = begin; i < end; ++i)
1087 destination_[i] = std::move(T());
1088 }
1089
1090 // copy constructor (memory initialization)
1091 void
1093 const std::size_t end,
1094 std::bool_constant<true>) const
1095 {
1096 for (std::size_t i = begin; i < end; ++i)
1097 new (&destination_[i]) T;
1098 }
1099 };
1100
1101} // end of namespace internal
1102
1103
1104#ifndef DOXYGEN
1105
1106
1107
1108template <typename T>
1110 : deleter_action_object(nullptr) // encode default action by using a nullptr
1111 , owning_aligned_vector(owning_object)
1112{}
1113
1114
1115# ifdef DEAL_II_WITH_MPI
1116
1117template <typename T>
1119 const bool is_shmem_root,
1120 T *aligned_shmem_pointer,
1121 MPI_Comm shmem_group_communicator,
1122 MPI_Win shmem_window)
1123 : deleter_action_object(
1124 std::make_unique<MPISharedMemDeleterAction>(is_shmem_root,
1125 aligned_shmem_pointer,
1126 shmem_group_communicator,
1127 shmem_window))
1128 , owning_aligned_vector(owning_object)
1129{}
1130# endif
1131
1132
1133template <typename T>
1134inline void
1136{
1137 // If no special action has been registered (i.e., if the action pointer is
1138 // nullptr), then just perform the default action right here.
1139 if (deleter_action_object == nullptr)
1140 {
1141 if (ptr != nullptr)
1142 {
1143 Assert(owning_aligned_vector->used_elements_end != nullptr,
1145
1146 if (std::is_trivially_destructible_v<T> == false)
1147 for (T *p = owning_aligned_vector->used_elements_end - 1; p >= ptr;
1148 --p)
1149 p->~T();
1150
1151 std::free(ptr);
1152 }
1153 }
1154 else
1155 // Otherwise, let the action object do what is necessary
1156 deleter_action_object->delete_array(owning_aligned_vector, ptr);
1157}
1158
1159
1160
1161template <typename T>
1162inline void
1164 const AlignedVector<T> *new_aligned_vector_ptr)
1165{
1166 owning_aligned_vector = new_aligned_vector_ptr;
1167}
1168
1169
1170# ifdef DEAL_II_WITH_MPI
1171
1172template <typename T>
1174 MPISharedMemDeleterAction(const bool is_shmem_root,
1175 T *aligned_shmem_pointer,
1176 MPI_Comm shmem_group_communicator,
1177 MPI_Win shmem_window)
1178 : is_shmem_root(is_shmem_root)
1179 , aligned_shmem_pointer(aligned_shmem_pointer)
1180 , shmem_group_communicator(shmem_group_communicator)
1181 , shmem_window(shmem_window)
1182{}
1183
1184
1185
1186template <typename T>
1187inline void
1189 const AlignedVector<T> *aligned_vector,
1190 T *ptr)
1191{
1192 (void)ptr;
1193 // It would be nice to assert that aligned_vector->elements.get() equals ptr,
1194 // but it is not guaranteed to work: clang, for example, sets elements.get()
1195 // to nullptr and then calls the deleter on a previously made copy. Hence we
1196 // must assume here that elements.get() (which is managed by the unique_ptr)
1197 // may be nullptr at this point.
1198 //
1199 // used_elements_end is a member variable of AlignedVector (i.e., we control
1200 // it, not unique_ptr) so it is still set to its correct value.
1201
1202 if (is_shmem_root)
1203 if (std::is_trivially_destructible_v<T> == false)
1204 for (T *p = aligned_vector->used_elements_end - 1; p >= ptr; --p)
1205 p->~T();
1206
1207 int ierr;
1208 ierr = MPI_Win_free(&shmem_window);
1209 AssertThrowMPI(ierr);
1210
1211 Utilities::MPI::free_communicator(shmem_group_communicator);
1212}
1213
1214# endif
1215
1216
1217template <class T>
1219 : elements(nullptr, Deleter(this))
1220 , used_elements_end(nullptr)
1221 , allocated_elements_end(nullptr)
1222 , replicated_across_communicator(false)
1223{}
1224
1225
1226
1227template <class T>
1228template <typename RandomAccessIterator, typename>
1229inline AlignedVector<T>::AlignedVector(RandomAccessIterator begin,
1230 RandomAccessIterator end)
1231 : elements(nullptr, Deleter(this))
1232 , used_elements_end(nullptr)
1233 , allocated_elements_end(nullptr)
1234 , replicated_across_communicator(false)
1235{
1236 allocate_and_move(0u, end - begin, end - begin);
1237 used_elements_end = allocated_elements_end;
1239 end,
1240 data());
1241}
1242
1243
1244template <class T>
1245inline AlignedVector<T>::AlignedVector(const size_type size, const T &init)
1246 : elements(nullptr, Deleter(this))
1247 , used_elements_end(nullptr)
1248 , allocated_elements_end(nullptr)
1249 , replicated_across_communicator(false)
1250{
1251 if (size > 0)
1252 resize(size, init);
1253}
1254
1255
1256
1257template <class T>
1259 : elements(nullptr, Deleter(this))
1260 , used_elements_end(nullptr)
1261 , allocated_elements_end(nullptr)
1262 , replicated_across_communicator(false)
1263{
1264 // copy the data from vec
1265 reserve(vec.size());
1266 used_elements_end = allocated_elements_end;
1269 elements.get());
1270}
1271
1272
1273
1274template <class T>
1277{
1278 // forward to the move operator
1279 *this = std::move(vec);
1280}
1281
1282
1283
1284template <class T>
1285inline AlignedVector<T> &
1287{
1288 const size_type new_size = vec.used_elements_end - vec.elements.get();
1289
1290 // First throw away everything and re-allocate memory but leave that
1291 // memory uninitialized for now:
1292 resize(0);
1293 reserve(new_size);
1294
1295 // Then copy the elements over by using the copy constructor on these
1296 // elements:
1299 elements.get());
1300
1301 // Finally adjust the pointer to the end of the elements that are used:
1302 used_elements_end = elements.get() + new_size;
1303
1304 return *this;
1305}
1306
1307
1308
1309template <class T>
1310inline AlignedVector<T> &
1312{
1313 clear();
1314
1315 // Move the actual data in the 'elements' object. One problem is that this
1316 // also moves the deleter object, but the deleter object
1317 // references 'this' (i.e., the 'this' pointer of the *moved-from*
1318 // object). The way this is implemented is that we have to move the
1319 // deleter as well, and then reset the pointer inside the deleter
1320 // that references the outer object.
1321 elements = std::move(vec.elements);
1322 elements.get_deleter().reset_owning_object(this);
1323
1324 // Then also steal the other pointers and clear them in the original object:
1325 used_elements_end = vec.used_elements_end;
1326 allocated_elements_end = vec.allocated_elements_end;
1327
1328 vec.used_elements_end = nullptr;
1329 vec.allocated_elements_end = nullptr;
1330
1331 return *this;
1332}
1333
1334
1335
1336template <class T>
1337inline void
1338AlignedVector<T>::resize_fast(const size_type new_size)
1339{
1340 const size_type old_size = size();
1341
1342 if (new_size == 0)
1343 clear();
1344 else if (new_size == old_size)
1345 {
1346 } // nothing to do here
1347 else if (new_size < old_size)
1348 {
1349 // call destructor on fields that are released, if the type requires it.
1350 // doing it backward releases the elements in reverse order as compared to
1351 // how they were created
1352 if (std::is_trivially_destructible_v<T> == false)
1353 for (T *p = used_elements_end - 1; p >= elements.get() + new_size; --p)
1354 p->~T();
1355 used_elements_end = elements.get() + new_size;
1356 }
1357 else // new_size > old_size
1358 {
1359 // Allocate more space, and claim that space as used
1360 reserve(new_size);
1361 used_elements_end = elements.get() + new_size;
1362
1363 // Leave the new array entries as-is (with undefined values) unless T's
1364 // default constructor is nontrivial (i.e., it is not a no-op)
1365 if (std::is_trivially_default_constructible_v<T> == false)
1367 new_size - old_size, elements.get() + old_size);
1368 }
1369}
1370
1371
1372
1373template <class T>
1374inline void
1375AlignedVector<T>::resize(const size_type new_size)
1376{
1377 const size_type old_size = size();
1378
1379 if (new_size == 0)
1380 clear();
1381 else if (new_size == old_size)
1382 {
1383 } // nothing to do here
1384 else if (new_size < old_size)
1385 {
1386 // call destructor on fields that are released, if the type requires it.
1387 // doing it backward releases the elements in reverse order as compared to
1388 // how they were created
1389 if (std::is_trivially_destructible_v<T> == false)
1390 for (T *p = used_elements_end - 1; p >= elements.get() + new_size; --p)
1391 p->~T();
1392 used_elements_end = elements.get() + new_size;
1393 }
1394 else // new_size > old_size
1395 {
1396 // Allocate more space, and claim that space as used
1397 reserve(new_size);
1398 used_elements_end = elements.get() + new_size;
1399
1400 // finally set the values to the default initializer
1402 new_size - old_size, elements.get() + old_size);
1403 }
1404}
1405
1406
1407
1408template <class T>
1409inline void
1410AlignedVector<T>::resize(const size_type new_size, const T &init)
1411{
1412 const size_type old_size = size();
1413
1414 if (new_size == 0)
1415 clear();
1416 else if (new_size == old_size)
1417 {
1418 } // nothing to do here
1419 else if (new_size < old_size)
1420 {
1421 // call destructor on fields that are released, if the type requires it.
1422 // doing it backward releases the elements in reverse order as compared to
1423 // how they were created
1424 if (std::is_trivially_destructible_v<T> == false)
1425 for (T *p = used_elements_end - 1; p >= elements.get() + new_size; --p)
1426 p->~T();
1427 used_elements_end = elements.get() + new_size;
1428 }
1429 else // new_size > old_size
1430 {
1431 // Allocate more space, and claim that space as used
1432 reserve(new_size);
1433 used_elements_end = elements.get() + new_size;
1434
1435 // finally set the desired init values
1437 new_size - old_size, init, elements.get() + old_size);
1438 }
1439}
1440
1441
1442
1443template <class T>
1444inline void
1445AlignedVector<T>::allocate_and_move(const std::size_t old_size,
1446 const std::size_t new_size,
1447 const std::size_t new_allocated_size)
1448{
1449 // allocate and align along 64-byte boundaries (this is enough for all
1450 // levels of vectorization currently supported by deal.II)
1451 T *new_data_ptr;
1452 Utilities::System::posix_memalign(reinterpret_cast<void **>(&new_data_ptr),
1453 64,
1454 new_size * sizeof(T));
1455
1456 // Now create a deleter that encodes what should happen when the object is
1457 // released: We need to destroy the objects that are currently alive (in
1458 // reverse order, and then release the memory. Note that we catch the
1459 // 'this' pointer because the number of elements currently alive might
1460 // change over time.
1461 Deleter deleter(this);
1462
1463 // copy whatever elements we need to retain
1464 if (new_allocated_size > 0)
1466 elements.get(), elements.get() + old_size, new_data_ptr);
1467
1468 // Now reset all the member variables of the current object
1469 // based on the allocation above. Assigning to a std::unique_ptr
1470 // object also releases the previously pointed to memory.
1471 //
1472 // Note that at the time of releasing the old memory, 'used_elements_end'
1473 // still points to its previous value, and this is important for the
1474 // deleter object of the previously allocated array (see how it loops over
1475 // the to-be-destroyed elements at the Deleter::DefaultDeleterAction
1476 // class).
1477 elements = decltype(elements)(new_data_ptr, std::move(deleter));
1478 used_elements_end = elements.get() + old_size;
1479 allocated_elements_end = elements.get() + new_size;
1480}
1481
1482
1483
1484template <class T>
1485inline void
1486AlignedVector<T>::reserve(const size_type new_allocated_size)
1487{
1488 const size_type old_size = used_elements_end - elements.get();
1489 const size_type old_allocated_size = allocated_elements_end - elements.get();
1490 if (new_allocated_size > old_allocated_size)
1491 {
1492 // if we continuously increase the size of the vector, we might be
1493 // reallocating a lot of times. therefore, try to increase the size more
1494 // aggressively
1495 const size_type new_size =
1496 std::max(new_allocated_size, 2 * old_allocated_size);
1497
1498 allocate_and_move(old_size, new_size, new_allocated_size);
1499 }
1500 else if (new_allocated_size == 0)
1501 clear();
1502 else // size_alloc < allocated_size
1503 {
1504 } // nothing to do here
1505}
1506
1507
1508
1509template <class T>
1510inline void
1512{
1513 if constexpr (running_in_debug_mode())
1514 {
1515 Assert(replicated_across_communicator == false,
1516 ExcAlignedVectorChangeAfterReplication());
1517 }
1518 const size_type used_size = used_elements_end - elements.get();
1519 const size_type allocated_size = allocated_elements_end - elements.get();
1520 if (allocated_size > used_size)
1521 allocate_and_move(used_size, used_size, used_size);
1522}
1523
1524
1525
1526template <class T>
1527inline void
1529{
1530 // Just release the memory (which also calls the destructor of the elements),
1531 // and then set the auxiliary pointers to invalid values.
1532 //
1533 // Note that at the time of releasing the old memory, 'used_elements_end'
1534 // still points to its previous value, and this is important for the
1535 // deleter object of the previously allocated array (see how it loops over
1536 // the to-be-destroyed elements a few lines above).
1537 elements.reset();
1538 used_elements_end = nullptr;
1539 allocated_elements_end = nullptr;
1540}
1541
1542
1543
1544template <class T>
1545inline void
1546AlignedVector<T>::push_back(const T in_data)
1547{
1548 Assert(used_elements_end <= allocated_elements_end, ExcInternalError());
1549 if (used_elements_end == allocated_elements_end)
1550 reserve(std::max(2 * capacity(), static_cast<size_type>(16)));
1551 new (used_elements_end++) T(in_data);
1552}
1553
1554
1555
1556template <class T>
1557inline typename AlignedVector<T>::reference
1559{
1560 AssertIndexRange(0, size());
1561 T *field = used_elements_end - 1;
1562 return *field;
1563}
1564
1565
1566
1567template <class T>
1570{
1571 AssertIndexRange(0, size());
1572 const T *field = used_elements_end - 1;
1573 return *field;
1574}
1575
1576
1577
1578template <class T>
1579template <typename ForwardIterator>
1580inline void
1581AlignedVector<T>::insert_back(ForwardIterator begin, ForwardIterator end)
1582{
1583 const size_type old_size = size();
1584 reserve(old_size + (end - begin));
1585 for (; begin != end; ++begin, ++used_elements_end)
1586 new (used_elements_end) T(*begin);
1587}
1588
1589
1590
1591template <class T>
1592template <typename RandomAccessIterator, typename>
1593inline typename AlignedVector<T>::iterator
1594AlignedVector<T>::insert(const_iterator position,
1595 RandomAccessIterator begin,
1596 RandomAccessIterator end)
1597{
1598 Assert(replicated_across_communicator == false,
1599 ExcAlignedVectorChangeAfterReplication());
1600 // Compare with std::less_equal (and, later on, with std::less): unlike
1601 // operator<=, it is well-defined also for pointers into unrelated arrays,
1602 // i.e., for invalid positions.
1603 {
1604 const std::less_equal<const T *> less_equal;
1605 Assert(less_equal(this->begin(), position) &&
1606 less_equal(position, this->end()),
1607 ExcMessage("The position iterator is not valid."));
1608 }
1609 const auto offset = position - this->begin();
1610
1611 const size_type old_size = size();
1612 const size_type range_size = end - begin;
1613 const size_type new_size = old_size + range_size;
1614 if (range_size != 0)
1615 {
1616 // Like for std::vector, the range may not refer to elements of this
1617 // vector: reallocating or shifting elements below would invalidate it.
1618 // In debug mode, check this in O(1) for ranges of T (other types cannot
1619 // refer to our elements) via the addresses of the first and the last
1620 // element in the range. This catches every overlapping contiguous range
1621 // and every range that lies entirely inside the vector, whatever the
1622 // iterator type.
1623 using iterator_reference =
1624 typename std::iterator_traits<RandomAccessIterator>::reference;
1625 constexpr bool same_iterator_reference_type =
1626 std::is_same_v<iterator_reference, reference> ||
1627 std::is_same_v<iterator_reference, const T &>;
1628 if constexpr (running_in_debug_mode() && same_iterator_reference_type)
1629 if (old_size != 0)
1630 {
1631 const std::less<const T *> less;
1632 const auto is_own_element = [&](const T &element) {
1633 const T *const address = std::addressof(element);
1634 return !less(address, elements.get()) &&
1635 less(address, used_elements_end);
1636 };
1637 Assert(!is_own_element(*begin) && !is_own_element(*(end - 1)),
1638 ExcMessage("The range [begin, end) passed to "
1639 "AlignedVector::insert() must not refer to "
1640 "elements of the vector itself."));
1641 }
1642
1643 // Only reallocate if the current capacity is not sufficient. reserve()
1644 // grows the allocation geometrically, so repeated insertions have
1645 // amortized cost proportional to the number of elements moved and do
1646 // not reallocate (and copy the whole array) every time.
1647 if (new_size > capacity())
1648 reserve(new_size);
1649
1650 T *const data = elements.get();
1651
1652 // Shift the elements after position back by range_size. Mark only the
1653 // elements before position as alive while doing so: should a move
1654 // constructor throw, the other elements are leaked rather than
1655 // destroyed twice.
1656 used_elements_end = data + offset;
1657 if constexpr (std::is_trivially_copyable_v<T>)
1658 std::memmove(data + offset + range_size,
1659 data + offset,
1660 (old_size - offset) * sizeof(T));
1661 else
1662 // Go from back to front: the destination of each element is either
1663 // past the old end of the array, or the slot of an element that was
1664 // already moved and destroyed.
1665 for (size_type i = old_size; i > static_cast<size_type>(offset); --i)
1666 {
1667 new (data + i - 1 + range_size) T(std::move(data[i - 1]));
1668 data[i - 1].~T();
1669 }
1670
1671 // Fill the gap.
1673 begin, end, data + offset);
1674 used_elements_end = data + new_size;
1675 }
1676 return this->begin() + offset;
1677}
1678
1679
1680
1681template <class T>
1682inline void
1684{
1686 elements.get());
1687}
1688
1689
1690
1691template <class T>
1692inline void
1694{
1696 value,
1697 elements.get());
1698}
1699
1700
1701
1702template <class T>
1703inline void
1705 const unsigned int root_process)
1706{
1707# ifdef DEAL_II_WITH_MPI
1708
1709 // Let the root process broadcast its size. If it is zero, then all
1710 // processes just clear() their memory and reset themselves to a non-shared
1711 // empty object -- there is no point to run through complicated MPI
1712 // calls if the end result is an empty array. Otherwise, we continue on.
1713 const size_type new_size =
1714 Utilities::MPI::broadcast(communicator, size(), root_process);
1715 if (new_size == 0)
1716 {
1717 clear();
1718 return;
1719 }
1720
1721
1722 // **** Step 0 ****
1723 // All but the root process no longer need their data, so release the memory
1724 // used to store the previous elements.
1725 if (Utilities::MPI::this_mpi_process(communicator) != root_process)
1726 {
1727 elements.reset();
1728 used_elements_end = nullptr;
1729 allocated_elements_end = nullptr;
1730 }
1731
1732 // **** Step 1 ****
1733 // Create communicators for each group of processes that can use
1734 // shared memory areas. Within each of these groups, we don't care about
1735 // which rank each of the old processes gets except that we would like to
1736 // make sure that the (global) root process will have rank=0 within
1737 // its own sub-communicator. We can do that through the third argument of
1738 // MPI_Comm_split_type (the "key") which is an integer meant to indicate the
1739 // order of processes within the split communicators, and we should set it to
1740 // zero for the root processes and one for all others -- which means that
1741 // for all of these other processes, MPI can choose whatever order it
1742 // wants because they have the same key (MPI then documents that these ties
1743 // will be broken according to these processes' rank in the old group).
1744 //
1745 // At least that's the theory. In practice, the MPI implementation where
1746 // this function was developed on does not seem to do that. (Bug report
1747 // is here: https://github.com/open-mpi/ompi/issues/8854)
1748 // We work around this by letting MPI_Comm_split_type choose whatever
1749 // rank it wants, and then reshuffle with MPI_Comm_split in a second
1750 // step -- not elegant, nor efficient, but seems to work:
1751 MPI_Comm shmem_group_communicator;
1752 {
1753 MPI_Comm shmem_group_communicator_temp;
1754 int ierr = MPI_Comm_split_type(communicator,
1755 MPI_COMM_TYPE_SHARED,
1756 /* key */ 0,
1757 MPI_INFO_NULL,
1758 &shmem_group_communicator_temp);
1759 AssertThrowMPI(ierr);
1760
1761 const int key =
1762 (Utilities::MPI::this_mpi_process(communicator) == root_process ? 0 : 1);
1763 ierr = MPI_Comm_split(shmem_group_communicator_temp,
1764 /* color */ 0,
1765 key,
1766 &shmem_group_communicator);
1767 AssertThrowMPI(ierr);
1768
1769 // Verify the explanation from above
1770 if (Utilities::MPI::this_mpi_process(communicator) == root_process)
1771 Assert(Utilities::MPI::this_mpi_process(shmem_group_communicator) == 0,
1773
1774 // And get rid of the temporary communicator
1775 Utilities::MPI::free_communicator(shmem_group_communicator_temp);
1776 }
1777 const bool is_shmem_root =
1778 Utilities::MPI::this_mpi_process(shmem_group_communicator) == 0;
1779
1780 // **** Step 2 ****
1781 // We then have to send the state of the current object from the
1782 // root process to one exemplar in each shmem group. To this end,
1783 // we create another subcommunicator that includes the ranks zero
1784 // of all shmem groups, and because of the trick above, we know
1785 // that this also includes the original root process.
1786 //
1787 // There are different ways of creating a "shmem_roots_communicator".
1788 // The conceptually easiest way is to create an MPI_Group that only
1789 // includes the shmem roots and then create a communicator from this
1790 // via MPI_Comm_create or MPI_Comm_create_group. The problem
1791 // with this is that we would have to exchange among all processes
1792 // which ones are shmem roots and which are not. This is awkward.
1793 //
1794 // A simpler way is to use MPI_Comm_split that uses "colors" to
1795 // indicate which sub-communicator each process wants to be in.
1796 // We use color=0 to indicate the group of shmem roots, and color=1
1797 // for all other processes -- the latter will simply not ever do
1798 // anything among themselves with the communicator so created.
1799 //
1800 // Using MPI_Comm_split has the additional benefit that, just as above,
1801 // we can choose where each rank will end up in shmem_roots_communicator.
1802 // We again set key=0 for the original root_process, and key=1 for all other
1803 // ranks; then, the global root becomes rank=0 on the
1804 // shmem_roots_communicator. We don't care how the other processes are
1805 // ordered.
1806 MPI_Comm shmem_roots_communicator;
1807 {
1808 const int key =
1809 (Utilities::MPI::this_mpi_process(communicator) == root_process ? 0 : 1);
1810
1811 const int ierr = MPI_Comm_split(communicator,
1812 /*color=*/
1813 (is_shmem_root ? 0 : 1),
1814 key,
1815 &shmem_roots_communicator);
1816 AssertThrowMPI(ierr);
1817
1818 // Again verify the explanation from above
1819 if (Utilities::MPI::this_mpi_process(communicator) == root_process)
1820 Assert(Utilities::MPI::this_mpi_process(shmem_roots_communicator) == 0,
1822 }
1823
1824 const unsigned int shmem_roots_root_rank = 0;
1825 const bool is_shmem_roots_root =
1826 (is_shmem_root && (Utilities::MPI::this_mpi_process(
1827 shmem_roots_communicator) == shmem_roots_root_rank));
1828
1829 // Now let the original root_process broadcast the current object to all
1830 // shmem roots. We know that the last rank is the original root process that
1831 // has all of the data.
1832 if (is_shmem_root)
1833 {
1834 if constexpr (Utilities::MPI::is_mpi_type<T>)
1835 {
1836 // The data is an MPI type, so it can be sent as an array
1837 // without having to go through the
1838 // serialization/deserialization machinery. We want to use
1839 // Utilities::MPI::broadcast() over MPI_Bcast() because the
1840 // former can deal with more elements than just a signed int
1841 // can hold. That's useful for very large tables.
1842 //
1843 // In that case, first tell all of the other shmem roots how many
1844 // elements we will have to deal with, and let them resize their
1845 // (non-shared) arrays.
1846 const size_type new_size =
1847 Utilities::MPI::broadcast(shmem_roots_communicator,
1848 size(),
1849 shmem_roots_root_rank);
1850 if (is_shmem_roots_root == false)
1851 resize(new_size);
1852
1853 // Then directly copy from the root process into these buffers
1854
1855 Utilities::MPI::broadcast(elements.get(),
1856 new_size,
1857 shmem_roots_root_rank,
1858 shmem_roots_communicator);
1859 }
1860 else
1861 {
1862 // The objects to be sent around are not "trivial", and so we have
1863 // to go through the serialization/deserialization machinery. On all
1864 // but the sending process, overwrite the current state with the
1865 // vector just broadcast.
1866 //
1867 // On the root rank, this would lead to resetting the 'entries'
1868 // pointer, which would trigger the deleter which would lead to a
1869 // deadlock. So we just send the result of the broadcast() call to
1870 // nirvana on the root process and keep our current state.
1871 if (Utilities::MPI::this_mpi_process(shmem_roots_communicator) == 0)
1872 std::ignore = Utilities::MPI::broadcast(shmem_roots_communicator,
1873 *this,
1874 shmem_roots_root_rank);
1875 else
1876 *this = Utilities::MPI::broadcast(shmem_roots_communicator,
1877 *this,
1878 shmem_roots_root_rank);
1879 }
1880 }
1881
1882 // We no longer need the shmem roots communicator, so get rid of it
1883 Utilities::MPI::free_communicator(shmem_roots_communicator);
1884
1885
1886 // **** Step 3 ****
1887 // At this point, all shmem groups have one shmem root process that has
1888 // a copy of the data. This is the point where each shmem group should
1889 // establish a shmem area to put the data into. As mentioned above,
1890 // we know that the shmem roots are the last rank in their respective
1891 // shmem_group_communicator.
1892 //
1893 // The process for all of this works as follows: While all processes in
1894 // the shmem group participate in the generation of the shmem memory window,
1895 // only the shmem root actually allocates any memory -- the rest just
1896 // allocate zero bytes of their own. We allocate space for exactly
1897 // size() elements (computed on the shmem_root that already has the data)
1898 // and add however many bytes are necessary so that we know that we can align
1899 // things to 64-byte boundaries. The worst case happens if the memory system
1900 // gives us a pointer to an address one byte past a desired alignment
1901 // boundary, and in that case aligning the memory will require us to waste the
1902 // first (align_by-1) bytes. So we have to ask for
1903 // size() * sizeof(T) + (align_by - 1)
1904 // bytes.
1905 //
1906 // Before MPI 4.0, there was no way to specify that we want memory aligned to
1907 // a certain number of bytes. This is going to come back to bite us further
1908 // down below when we try to get a properly aligned pointer to our memory
1909 // region, see the commentary there. Starting with MPI 4.0, one can set a
1910 // flag in an MPI_Info structure that requests a desired alignment, so we do
1911 // this for forward compatibility; MPI implementations ignore flags they don't
1912 // know anything about, and so setting this flag is backward compatible also
1913 // to older MPI versions.
1914 MPI_Win shmem_window;
1915 void *base_ptr;
1916 const MPI_Aint align_by = 64;
1917 const MPI_Aint alloc_size =
1918 Utilities::MPI::broadcast(shmem_group_communicator,
1919 (size() * sizeof(T) + (align_by - 1)),
1920 0);
1921
1922 {
1923 int ierr;
1924
1925 MPI_Info mpi_info;
1926 ierr = MPI_Info_create(&mpi_info);
1927 AssertThrowMPI(ierr);
1928 ierr = MPI_Info_set(mpi_info,
1929 "mpi_minimum_memory_alignment",
1930 std::to_string(align_by).c_str());
1931 AssertThrowMPI(ierr);
1932 ierr = MPI_Win_allocate_shared((is_shmem_root ? alloc_size : 0),
1933 /* disp_unit = */ 1,
1934 mpi_info,
1935 shmem_group_communicator,
1936 &base_ptr,
1937 &shmem_window);
1938 AssertThrowMPI(ierr);
1939
1940 ierr = MPI_Info_free(&mpi_info);
1941 AssertThrowMPI(ierr);
1942 }
1943
1944
1945 // **** Step 4 ****
1946 // The next step is to teach all non-shmem root processes what the pointer to
1947 // the array is that the shmem-root created. MPI has a nifty way for this
1948 // given that only a single process actually allocated memory in the window:
1949 // When calling MPI_Win_shared_query, the MPI documentation says that
1950 // "When rank is MPI_PROC_NULL, the pointer, disp_unit, and size returned are
1951 // the pointer, disp_unit, and size of the memory segment belonging the lowest
1952 // rank that specified size > 0. If all processes in the group attached to the
1953 // window specified size = 0, then the call returns size = 0 and a baseptr as
1954 // if MPI_ALLOC_MEM was called with size = 0."
1955 //
1956 // This will allow us to obtain the pointer to the shmem root's memory area,
1957 // which is the only one we care about. (None of the other processes have
1958 // even allocated any memory.)
1959 //
1960 // We don't need to do this on the shmem root process: This process has
1961 // already gotten its base_ptr correctly set above, and we can determine the
1962 // array size by just calling size().
1963 if (is_shmem_root == false)
1964 {
1965 int disp_unit;
1966 MPI_Aint alloc_size; // not actually used
1967 const int ierr = MPI_Win_shared_query(
1968 shmem_window, MPI_PROC_NULL, &alloc_size, &disp_unit, &base_ptr);
1969 AssertThrowMPI(ierr);
1970
1971 // Make sure we actually got a pointer, and check that the disp_unit is
1972 // equal to 1 (as set above)
1973 Assert(base_ptr != nullptr, ExcInternalError());
1974 Assert(disp_unit == 1, ExcInternalError());
1975 }
1976
1977
1978 // **** Step 5 ****
1979 // Now that all processes know the address of the space that is visible to
1980 // everyone, we need to figure out whether it is properly aligned and if not,
1981 // find the next aligned address.
1982 //
1983 // std::align does that, but it also modifies its last two arguments. The
1984 // documentation of that function at
1985 // https://en.cppreference.com/w/cpp/memory/align is not entirely clear, but I
1986 // *think* that the following should do given that we do not use base_ptr and
1987 // available_space any further after the call to std::align.
1988 std::size_t available_space = alloc_size;
1989 void *base_ptr_backup = base_ptr;
1990 T *aligned_shmem_pointer = static_cast<T *>(
1991 std::align(align_by, new_size * sizeof(T), base_ptr, available_space));
1992 Assert(aligned_shmem_pointer != nullptr, ExcInternalError());
1993
1994 // There is one step to guard against. It is *conceivable* that the base_ptr
1995 // we have previously obtained from MPI_Win_shared_query is mapped so
1996 // awkwardly into the different MPI processes' memory spaces that it is
1997 // aligned in one memory space, but not another. In that case, different
1998 // processes would align base_ptr differently, and adjust available_space
1999 // differently. We can check that by making sure that the max (or min) over
2000 // all processes is equal to every process's value. If that's not the case,
2001 // then the whole idea of aligning above is wrong and we need to rethink what
2002 // it means to align data in a shared memory space.
2003 //
2004 // One might be tempted to think that this is not how MPI implementations
2005 // actually arrange things. Alas, when developing this functionality in 2021,
2006 // this is really how at least OpenMPI ends up doing things. (This is with an
2007 // OpenMPI implementation of MPI 3.1, so it does not support the flag we set
2008 // in the MPI_Info structure above when allocating the memory window.) Indeed,
2009 // when running this code on three processes, one ends up with base_ptr values
2010 // of
2011 // base_ptr=0x7f0842f02108
2012 // base_ptr=0x7fc0a47881d0
2013 // base_ptr=0x7f64872db108
2014 // which, most annoyingly, are aligned to 8 and 16 byte boundaries -- so there
2015 // is no common offset std::align could find that leads to a 64-byte
2016 // aligned memory address in all three memory spaces. That's a tremendous
2017 // nuisance and there is really nothing we can do about this other than just
2018 // fall back on the (unaligned) base_ptr in that case.
2019 if (Utilities::MPI::min(available_space, shmem_group_communicator) !=
2020 Utilities::MPI::max(available_space, shmem_group_communicator))
2021 aligned_shmem_pointer = static_cast<T *>(base_ptr_backup);
2022
2023
2024 // **** Step 6 ****
2025 // If this is the shmem root process, we need to copy the data into the
2026 // shared memory space.
2027 if (is_shmem_root)
2028 {
2029 if (std::is_trivially_copyable_v<T> == true)
2030 std::memcpy(aligned_shmem_pointer, elements.get(), sizeof(T) * size());
2031 else
2032 for (std::size_t i = 0; i < size(); ++i)
2033 new (&aligned_shmem_pointer[i]) T(std::move(elements[i]));
2034 }
2035
2036 // Make sure that the shared memory host has copied the data before we try to
2037 // access it.
2038 const int ierr = MPI_Barrier(shmem_group_communicator);
2039 AssertThrowMPI(ierr);
2040
2041 // **** Step 7 ****
2042 // Finally, we need to set the pointers of this object to what we just
2043 // learned. This also releases all memory that may have been in use
2044 // previously.
2045 //
2046 // The part that is a bit tricky is how to write the deleter of this
2047 // shared memory object. When we want to get rid of it, we need to
2048 // also release the MPI_Win object along with the shmem_group_communicator
2049 // object. That's because as long as we use the shared memory, we still need
2050 // to hold on to the MPI_Win object, and the MPI_Win object is based on the
2051 // communicator. (The former is definitely true, the latter is not quite clear
2052 // from the MPI documentation, but seems reasonable.) So we need to have a
2053 // deleter for the pointer that ensures that upon release of the memory, we
2054 // not only call the destructor of these memory elements (but only once, on
2055 // the shmem root!) but also destroy the MPI_Win and the communicator. All of
2056 // that is encapsulated in the following call where the deleter makes copies
2057 // of the arguments in the lambda capture.
2058 elements = decltype(elements)(aligned_shmem_pointer,
2059 Deleter(this,
2060 is_shmem_root,
2061 aligned_shmem_pointer,
2062 shmem_group_communicator,
2063 shmem_window));
2064
2065 // We then also have to set the other two pointers that define the state of
2066 // the current object. Note that the new buffer size is exactly as large as
2067 // necessary, i.e., can store size() elements, regardless of the number of
2068 // allocated elements in the original objects.
2069 used_elements_end = elements.get() + new_size;
2070 allocated_elements_end = used_elements_end;
2071
2072 // **** Consistency check ****
2073 // At this point, each process should have a copy of the data.
2074 // Verify this in some sort of round-about way
2075 if constexpr (running_in_debug_mode())
2076 {
2077 replicated_across_communicator = true;
2078 const std::vector<char> packed_data = Utilities::pack(*this);
2079 const int hash =
2080 std::accumulate(packed_data.begin(), packed_data.end(), int(0));
2081 Assert(Utilities::MPI::max(hash, communicator) == hash,
2083 }
2084
2085# else
2086 // No MPI -> nothing to replicate
2087 (void)communicator;
2088 (void)root_process;
2089# endif
2090}
2091
2092
2093
2094template <class T>
2095inline void
2097{
2098 // Swap the data in the 'elements' objects. Then also make sure that
2099 // their respective deleter objects point to the right place.
2100 std::swap(elements, vec.elements);
2101 elements.get_deleter().reset_owning_object(this);
2102 vec.elements.get_deleter().reset_owning_object(&vec);
2103
2104 // Now also swap the remaining members.
2105 std::swap(used_elements_end, vec.used_elements_end);
2106 std::swap(allocated_elements_end, vec.allocated_elements_end);
2107}
2108
2109
2110
2111template <class T>
2112inline bool
2114{
2115 return used_elements_end == elements.get();
2116}
2117
2118
2119
2120template <class T>
2121inline typename AlignedVector<T>::size_type
2123{
2124 return used_elements_end - elements.get();
2125}
2126
2127
2128
2129template <class T>
2130inline typename AlignedVector<T>::size_type
2132{
2133 return allocated_elements_end - elements.get();
2134}
2135
2136
2137
2138template <class T>
2139inline typename AlignedVector<T>::reference
2140AlignedVector<T>::operator[](const size_type index)
2141{
2143 return elements[index];
2144}
2145
2146
2147
2148template <class T>
2150AlignedVector<T>::operator[](const size_type index) const
2151{
2153 return elements[index];
2154}
2155
2156
2157
2158template <typename T>
2159inline typename AlignedVector<T>::pointer
2161{
2162 return elements.get();
2163}
2164
2165
2166
2167template <typename T>
2168inline typename AlignedVector<T>::const_pointer
2170{
2171 return elements.get();
2172}
2173
2174
2175
2176template <class T>
2177inline typename AlignedVector<T>::iterator
2179{
2180 return elements.get();
2181}
2182
2183
2184
2185template <class T>
2186inline typename AlignedVector<T>::iterator
2188{
2189 return used_elements_end;
2190}
2191
2192
2193
2194template <class T>
2197{
2198 return elements.get();
2199}
2200
2201
2202
2203template <class T>
2206{
2207 return used_elements_end;
2208}
2209
2210
2211
2212template <class T>
2213template <class Archive>
2214inline void
2215AlignedVector<T>::save(Archive &ar, const unsigned int) const
2216{
2217 size_type vec_size = size();
2218 ar & vec_size;
2219 if (vec_size > 0)
2220 {
2221 if constexpr (std::is_trivially_copyable_v<T>)
2222 ar &boost::serialization::make_binary_object(elements.get(),
2223 vec_size * sizeof(T));
2224 else
2225 ar &boost::serialization::make_array(elements.get(), vec_size);
2226 }
2227}
2228
2229
2230
2231template <class T>
2232template <class Archive>
2233inline void
2234AlignedVector<T>::load(Archive &ar, const unsigned int)
2235{
2236 size_type vec_size = 0;
2237 ar & vec_size;
2238
2239 resize(vec_size);
2240
2241 if (vec_size > 0)
2242 {
2243 if constexpr (std::is_trivially_copyable_v<T>)
2244 ar &boost::serialization::make_binary_object(elements.get(),
2245 vec_size * sizeof(T));
2246 else
2247 ar &boost::serialization::make_array(elements.get(), vec_size);
2248 }
2249}
2250
2251
2252
2253template <class T>
2254inline typename AlignedVector<T>::size_type
2256{
2257 size_type memory = sizeof(*this);
2258 for (const T *t = elements.get(); t != used_elements_end; ++t)
2260 memory += sizeof(T) * (allocated_elements_end - used_elements_end);
2261 return memory;
2262}
2263
2264
2265#endif // ifndef DOXYGEN
2266
2267
2273template <class T>
2274bool
2276{
2277 if (lhs.size() != rhs.size())
2278 return false;
2279 for (typename AlignedVector<T>::const_iterator lit = lhs.begin(),
2280 rit = rhs.begin();
2281 lit != lhs.end();
2282 ++lit, ++rit)
2283 if (*lit != *rit)
2284 return false;
2285 return true;
2286}
2287
2288
2289
2295template <class T>
2296bool
2298{
2299 return !(operator==(lhs, rhs));
2300}
2301
2302
2304
2305#endif
*  iterator end()
*  *  iterator begin()
bool operator==(const AlignedVector< T > &lhs, const AlignedVector< T > &rhs)
virtual void delete_array(const AlignedVector< T > *owning_aligned_vector, T *ptr)=0
virtual void delete_array(const AlignedVector< T > *aligned_vector, T *ptr) override
MPISharedMemDeleterAction(const bool is_shmem_root, T *aligned_shmem_pointer, MPI_Comm shmem_group_communicator, MPI_Win shmem_window)
void operator()(T *ptr)
Deleter(AlignedVector< T > *owning_object, const bool is_shmem_root, T *aligned_shmem_pointer, MPI_Comm shmem_group_communicator, MPI_Win shmem_window)
Deleter(AlignedVector< T > *owning_object)
std::unique_ptr< DeleterActionBase > deleter_action_object
void reset_owning_object(const AlignedVector< T > *new_aligned_vector_ptr)
const AlignedVector< T > * owning_aligned_vector
iterator end()
void replicate_across_communicator(const MPI_Comm communicator, const unsigned int root_process)
size_type memory_consumption() const
void resize_fast(const size_type new_size)
std::unique_ptr< T[], Deleter > elements
reference operator[](const size_type index)
void fill(const T &element)
iterator begin()
const_iterator end() const
void allocate_and_move(const std::size_t old_size, const std::size_t new_size, const std::size_t new_allocated_size)
~AlignedVector()=default
AlignedVector(AlignedVector< T > &&vec) noexcept
void reserve(const size_type new_allocated_size)
void serialize(Archive &archive, const unsigned int version)
void shrink_to_fit()
bool operator!=(const AlignedVector< T > &lhs, const AlignedVector< T > &rhs)
const_reference operator[](const size_type index) const
pointer data()
void resize(const size_type new_size, const T &init)
AlignedVector & operator=(AlignedVector< T > &&vec) noexcept
AlignedVector(RandomAccessIterator begin, RandomAccessIterator end)
size_type capacity() const
value_type & reference
AlignedVector & operator=(const AlignedVector< T > &vec)
void swap(AlignedVector< T > &vec) noexcept
const value_type * const_pointer
bool replicated_across_communicator
void push_back(const T in_data)
iterator insert(const_iterator position, RandomAccessIterator begin, RandomAccessIterator end)
const_iterator begin() const
AlignedVector(const size_type size, const T &init=T())
bool empty() const
AlignedVector(const AlignedVector< T > &vec)
size_type size() const
const_reference back() const
std::size_t size_type
bool operator==(const AlignedVector< T > &lhs, const AlignedVector< T > &rhs)
const value_type * const_iterator
void resize(const size_type new_size)
void load(Archive &ar, const unsigned int version)
void save(Archive &ar, const unsigned int version) const
void insert_back(ForwardIterator begin, ForwardIterator end)
const value_type & const_reference
reference back()
const_pointer data() const
value_type * pointer
value_type * iterator
AlignedVectorCopyConstruct(RandomAccessIterator source_begin, RandomAccessIterator source_end, T *const destination)
static const std::size_t minimum_parallel_grain_size
virtual void apply_to_subrange(const std::size_t begin, const std::size_t end) const override
AlignedVectorDefaultInitialize(const std::size_t size, T *const destination)
static const std::size_t minimum_parallel_grain_size
virtual void apply_to_subrange(const std::size_t begin, const std::size_t end) const override
void default_construct_or_assign(const std::size_t begin, const std::size_t end, std::bool_constant< false >) const
void default_construct_or_assign(const std::size_t begin, const std::size_t end, std::bool_constant< true >) const
void copy_construct_or_assign(const std::size_t begin, const std::size_t end, std::bool_constant< false >) const
static const std::size_t minimum_parallel_grain_size
virtual void apply_to_subrange(const std::size_t begin, const std::size_t end) const override
void copy_construct_or_assign(const std::size_t begin, const std::size_t end, std::bool_constant< true >) const
AlignedVectorInitialize(const std::size_t size, const T &element, T *const destination)
AlignedVectorMoveConstruct(RandomAccessIterator source_begin, RandomAccessIterator source_end, T *const destination)
virtual void apply_to_subrange(const std::size_t begin, const std::size_t end) const override
static const std::size_t minimum_parallel_grain_size
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:38
constexpr bool running_in_debug_mode()
Definition config.h:76
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:39
#define Assert(cond, exc)
#define AssertThrowMPI(error_code)
#define AssertIndexRange(index, range)
#define DeclExceptionMsg(Exception, defaulttext)
static ::ExceptionBase & ExcInternalError()
static ::ExceptionBase & ExcMessage(std::string arg1)
static ::ExceptionBase & ExcAlignedVectorChangeAfterReplication()
std::vector< index_type > data
Definition mpi.cc:734
std::size_t size
Definition mpi.cc:733
constexpr char T
std::enable_if_t< std::is_fundamental_v< T >, std::size_t > memory_consumption(const T &t)
*  state_parameters reserve(get_material_parameter_count())
T max(const T &t, const MPI_Comm mpi_communicator)
T min(const T &t, const MPI_Comm mpi_communicator)
unsigned int this_mpi_process(const MPI_Comm mpi_communicator)
Definition mpi.cc:118
void free_communicator(MPI_Comm mpi_communicator)
Definition mpi.cc:165
T broadcast(const MPI_Comm comm, const T &object_to_send, const unsigned int root_process=0)
void posix_memalign(void **memptr, std::size_t alignment, std::size_t size)
std::size_t pack(const T &object, std::vector< char > &dest_buffer, const bool allow_compression=true)
Definition utilities.h:1352
STL namespace.
::VectorizedArray< Number, width > max(const ::VectorizedArray< Number, width > &, const ::VectorizedArray< Number, width > &)
void apply_parallel(const std::size_t begin, const std::size_t end, const std::size_t minimum_parallel_grain_size) const
Definition parallel.h:636