/usr/include/deal.II/base/aligned_vector.h is in libdeal.ii-dev 8.4.2-2+b1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 | // ---------------------------------------------------------------------
//
// Copyright (C) 2011 - 2016 by the deal.II authors
//
// This file is part of the deal.II library.
//
// The deal.II library is free software; you can use it, redistribute
// it, and/or modify it under the terms of the GNU Lesser General
// Public License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// The full text of the license can be found in the file LICENSE at
// the top level of the deal.II distribution.
//
// ---------------------------------------------------------------------
#ifndef dealii__aligned_vector_h
#define dealii__aligned_vector_h
#include <deal.II/base/config.h>
#include <deal.II/base/std_cxx11/type_traits.h>
#include <deal.II/base/exceptions.h>
#include <deal.II/base/memory_consumption.h>
#include <deal.II/base/utilities.h>
#include <deal.II/base/parallel.h>
#include <boost/serialization/array.hpp>
#include <boost/serialization/split_member.hpp>
#include <cstring>
DEAL_II_NAMESPACE_OPEN
/**
* This is a replacement class for std::vector to be used in combination with
* VectorizedArray and derived data types. It allocates memory aligned to
* addresses of a vectorized data type (in order to avoid segmentation faults
* when a variable of type VectorizedArray which the compiler assumes to be
* aligned to certain memory addresses does not actually follow these rules).
* This could also be achieved by proving std::vector with a user-defined
* allocator. On the other hand, writing an own small vector class lets us
* implement parallel copy and move operations with TBB, insert deal.II-style
* assertions, and cut some unnecessary functionality. Note that this vector
* is a bit more memory-consuming than std::vector because of alignment, so it
* is recommended to only use this vector on long vectors.
*
* @p author Katharina Kormann, Martin Kronbichler, 2011
*/
template < class T >
class AlignedVector
{
public:
/**
* Declare standard types used in all containers. These types parallel those
* in the <tt>C++</tt> standard libraries <tt>vector<...></tt> class.
*/
typedef T value_type;
typedef value_type *pointer;
typedef const value_type *const_pointer;
typedef value_type *iterator;
typedef const value_type *const_iterator;
typedef value_type &reference;
typedef const value_type &const_reference;
typedef std::size_t size_type;
/**
* Empty constructor. Sets the vector size to zero.
*/
AlignedVector ();
/**
* Sets the vector size to the given size and initializes all elements with
* T().
*
* @dealiiOperationIsMultithreaded
*/
AlignedVector (const size_type size,
const T &init = T());
/**
* Destructor.
*/
~AlignedVector ();
/**
* Copy constructor.
*
* @dealiiOperationIsMultithreaded
*/
AlignedVector (const AlignedVector<T> &vec);
/**
* Assignment to the input vector @p vec.
*
* @dealiiOperationIsMultithreaded
*/
AlignedVector &
operator = (const AlignedVector<T> &vec);
/**
* Change the size of the vector. It keeps old elements previously available
* but does not initialize the newly allocated memory, leaving it in an
* undefined state.
*
* @note This method can only be invoked for classes @p T that define a
* default constructor, @p T(). Otherwise, compilation will fail.
*/
void resize_fast (const size_type size);
/**
* Change the size of the vector. It keeps old elements previously
* available, and initializes each element with the specified data. If the
* new vector size is shorter than the old one, the memory is not released
* unless the new size is zero.
*
* @dealiiOperationIsMultithreaded
*/
void resize (const size_type size_in,
const T &init = T());
/**
* Reserve memory space for @p size elements. If the argument @p size is set
* to zero, all previously allocated memory is released.
*
* In order to avoid too frequent reallocation (which involves copy of the
* data), this function doubles the amount of memory occupied when the given
* size is larger than the previously allocated size.
*/
void reserve (const size_type size_alloc);
/**
* Releases all previously allocated memory and leaves the vector in a state
* equivalent to the state after the default constructor has been called.
*/
void clear ();
/**
* Inserts an element at the end of the vector, increasing the vector size
* by one. Note that the allocated size will double whenever the previous
* space is not enough to hold the new element.
*/
void push_back (const T in_data);
/**
* Returns the last element of the vector (read and write access).
*/
reference back ();
/**
* Returns the last element of the vector (read-only access).
*/
const_reference back () const;
/**
* Inserts several elements at the end of the vector given by a range of
* elements.
*/
template <typename ForwardIterator>
void insert_back (ForwardIterator begin,
ForwardIterator end);
/**
* Fills the vector with size() copies of the given input.
*
* @note This method can only be invoked for classes that define the copy
* assignment operator. Otherwise, compilation will fail.
*
* @dealiiOperationIsMultithreaded
*/
void fill (const T &element);
/**
* Swaps the given vector with the calling vector.
*/
void swap (AlignedVector<T> &vec);
/**
* Returns whether the vector is empty, i.e., its size is zero.
*/
bool empty () const;
/**
* Returns the size of the vector.
*/
size_type size () const;
/**
* Returns the capacity of the vector, i.e., the size this vector can hold
* without reallocation. Note that capacity() >= size().
*/
size_type capacity () const;
/**
* Read-write access to entry @p index in the vector.
*/
reference
operator [] (const size_type index);
/**
* Read-only access to entry @p index in the vector.
*/
const_reference operator [] (const size_type index) const;
/**
* Returns a read and write pointer to the beginning of the data array.
*/
iterator begin ();
/**
* Returns a read and write pointer to the end of the data array.
*/
iterator end ();
/**
* Returns a read-only pointer to the beginning of the data array.
*/
const_iterator begin () const;
/**
* Returns a read-only pointer to the end of the data array.
*/
const_iterator end () const;
/**
* Returns the memory consumption of the allocated memory in this class. If
* the underlying type @p T allocates memory by itself, this memory is not
* counted.
*/
size_type memory_consumption () const;
/**
* Write the data of this object to a stream for the purpose of
* serialization.
*/
template <class Archive>
void save (Archive &ar, const unsigned int version) const;
/**
* Read the data of this object from a stream for the purpose of
* serialization.
*/
template <class Archive>
void load (Archive &ar, const unsigned int version);
BOOST_SERIALIZATION_SPLIT_MEMBER()
private:
/**
* Pointer to actual class data.
*/
T *_data;
/**
* Pointer to the end of valid data fields.
*/
T *_end_data;
/**
* Pointer to the end of the allocated memory.
*/
T *_end_allocated;
};
// ------------------------------- inline functions --------------------------
/**
* This namespace defines the copy and set functions used in AlignedVector.
* These functions operate in parallel when there are enough elements in the
* vector.
*/
namespace internal
{
/**
* Move and class that actually issues the copy commands in AlignedVector.
* This class is based on the specialized for loop base class
* ParallelForLoop in parallel.h whose purpose is the following: When
* calling a parallel for loop on AlignedVector with apply_to_subranges, it
* generates different code for every different argument we might choose (as
* it is templated). This gives a lot of code (e.g. it triples the memory
* required for compiling the file matrix_free.cc and the final object size
* is several times larger) which is completely useless. Therefore, this
* class channels all copy commands through one call to apply_to_subrange
* for all possible types, which makes the copy operation much cleaner
* (thanks to a virtual function, whose cost is negligible in this context).
*
* @relates AlignedVector
*/
template <typename T>
class AlignedVectorMove : private parallel::ParallelForInteger
{
static const std::size_t minimum_parallel_grain_size = 160000/sizeof(T)+1;
public:
/**
* Constructor. Issues a parallel call if there are sufficiently many
* elements, otherwise works in serial. Copies the data from the half-open
* interval between @p source_begin and @p source_end to array starting at
* @p destination (by calling the copy constructor with placement new). If
* the flag copy_source is set to @p true, the elements from the source
* array are simply copied. If it is set to @p false, the data is moved
* between the two arrays by invoking the destructor on the source range
* (preparing for a subsequent call to free).
*/
AlignedVectorMove (T *source_begin,
T *source_end,
T *destination,
const bool copy_source)
:
source_ (source_begin),
destination_ (destination),
copy_source_ (copy_source)
{
Assert (source_end >= source_begin, ExcInternalError());
const std::size_t size = source_end - source_begin;
if (size < minimum_parallel_grain_size)
apply_to_subrange (0, size);
else
apply_parallel (0, size, minimum_parallel_grain_size);
}
/**
* This method moves elements from the source to the destination given in
* the constructor on a subrange given by two integers.
*/
virtual void apply_to_subrange (const std::size_t begin,
const std::size_t end) const
{
// for classes trivial assignment can use memcpy. cast element to
// (void*) to silence compiler warning for virtual classes (they will
// never arrive here because they are non-trivial).
if (std_cxx11::is_trivial<T>::value == true)
std::memcpy ((void *)(destination_+begin), source_+begin,
(end-begin)*sizeof(T));
else if (copy_source_ == false)
for (std::size_t i=begin; i<end; ++i)
{
// initialize memory (copy construct by placement new), and
// destruct the source
new (&destination_[i]) T(source_[i]);
source_[i].~T();
}
else
for (std::size_t i=begin; i<end; ++i)
new (&destination_[i]) T(source_[i]);
}
private:
T *source_;
T *destination_;
const bool copy_source_;
};
/**
* Class that issues the set commands for AlignedVector.
*
* @tparam initialize_memory Sets whether the the set command should
* initialize memory (with a call to the copy constructor) or rather use the
* copy assignment operator. A template is necessary to select the
* appropriate operation since some classes might define only one of those
* two operations.
*
* @relates AlignedVector
*/
template <typename T, bool initialize_memory>
class AlignedVectorSet : private parallel::ParallelForInteger
{
static const std::size_t minimum_parallel_grain_size = 160000/sizeof(T)+1;
public:
/**
* Constructor. Issues a parallel call if there are sufficiently many
* elements, otherwise work in serial.
*/
AlignedVectorSet (const std::size_t size,
const T &element,
T *destination)
:
element_ (element),
destination_ (destination),
trivial_element (false)
{
if (size == 0)
return;
// do not use memcmp for long double because on some systems it does not
// completely fill its memory and may lead to false positives in
// e.g. valgrind
if (std_cxx11::is_trivial<T>::value == true &&
types_are_equal<T,long double>::value == false)
{
const unsigned char zero [sizeof(T)] = {};
// cast element to (void*) to silence compiler warning for virtual
// classes (they will never arrive here because they are
// non-trivial).
if (std::memcmp(zero, (void *)&element, sizeof(T)) == 0)
trivial_element = true;
}
if (size < minimum_parallel_grain_size)
apply_to_subrange (0, size);
else
apply_parallel (0, size, minimum_parallel_grain_size);
}
/**
* This sets elements on a subrange given by two integers.
*/
virtual void apply_to_subrange (const std::size_t begin,
const std::size_t end) const
{
// for classes with trivial assignment of zero can use memset. cast
// element to (void*) to silence compiler warning for virtual
// classes (they will never arrive here because they are
// non-trivial).
if (std_cxx11::is_trivial<T>::value == true && trivial_element)
std::memset ((void *)(destination_+begin), 0, (end-begin)*sizeof(T));
else
copy_construct_or_assign(begin, end,
::dealii::internal::bool2type<initialize_memory>());
}
private:
const T &element_;
mutable T *destination_;
bool trivial_element;
// copy assignment operation
void copy_construct_or_assign(const std::size_t begin,
const std::size_t end,
::dealii::internal::bool2type<false>) const
{
for (std::size_t i=begin; i<end; ++i)
destination_[i] = element_;
}
// copy constructor (memory initialization)
void copy_construct_or_assign(const std::size_t begin,
const std::size_t end,
::dealii::internal::bool2type<true>) const
{
for (std::size_t i=begin; i<end; ++i)
new (&destination_[i]) T(element_);
}
};
} // end of namespace internal
#ifndef DOXYGEN
template < class T >
inline
AlignedVector<T>::AlignedVector ()
:
_data (0),
_end_data (0),
_end_allocated (0)
{}
template < class T >
inline
AlignedVector<T>::AlignedVector (const size_type size,
const T &init)
:
_data (0),
_end_data (0),
_end_allocated (0)
{
if (size > 0)
resize (size, init);
}
template < class T >
inline
AlignedVector<T>::~AlignedVector ()
{
clear();
}
template < class T >
inline
AlignedVector<T>::AlignedVector (const AlignedVector<T> &vec)
:
_data (0),
_end_data (0),
_end_allocated (0)
{
// copy the data from vec
reserve (vec._end_data - vec._data);
_end_data = _end_allocated;
internal::AlignedVectorMove<T> (vec._data, vec._end_data, _data, true);
}
template < class T >
inline
AlignedVector<T> &
AlignedVector<T>::operator = (const AlignedVector<T> &vec)
{
resize(0);
resize_fast (vec._end_data - vec._data);
internal::AlignedVectorMove<T> (vec._data, vec._end_data, _data, true);
return *this;
}
template < class T >
inline
void
AlignedVector<T>::resize_fast (const size_type size_in)
{
const size_type old_size = size();
if (std_cxx11::is_trivial<T>::value == false && size_in < old_size)
{
// call destructor on fields that are released. doing it backward
// releases the elements in reverse order as compared to how they were
// created
while (_end_data != _data+size_in)
(--_end_data)->~T();
}
reserve (size_in);
_end_data = _data + size_in;
// need to still set the values in case the class is non-trivial because
// virtual classes etc. need to run their (default) constructor
if (std_cxx11::is_trivial<T>::value == false && size_in > old_size)
dealii::internal::AlignedVectorSet<T,true> (size_in-old_size, T(), _data+old_size);
}
template < class T >
inline
void
AlignedVector<T>::resize (const size_type size_in,
const T &init)
{
const size_type old_size = size();
if (std_cxx11::is_trivial<T>::value == false && size_in < old_size)
{
// call destructor on fields that are released. doing it backward
// releases the elements in reverse order as compared to how they were
// created
while (_end_data != _data+size_in)
(--_end_data)->~T();
}
reserve (size_in);
_end_data = _data + size_in;
// finally set the desired init values
if (size_in > old_size)
dealii::internal::AlignedVectorSet<T,true> (size_in-old_size, init, _data+old_size);
}
template < class T >
inline
void
AlignedVector<T>::reserve (const size_type size_alloc)
{
const size_type old_size = _end_data - _data;
const size_type allocated_size = _end_allocated - _data;
if (size_alloc > allocated_size)
{
// if we continuously increase the size of the vector, we might be
// reallocating a lot of times. therefore, try to increase the size more
// aggressively
size_type new_size = size_alloc;
if (size_alloc < (2 * allocated_size))
new_size = 2 * allocated_size;
const size_type size_actual_allocate = new_size * sizeof(T);
// allocate and align along 64-byte boundaries (this is enough for all
// levels of vectorization currently supported by deal.II)
T *new_data;
Utilities::System::posix_memalign ((void **)&new_data, 64, size_actual_allocate);
// copy data in case there was some content before and release the old
// memory with the function corresponding to the one used for allocating
std::swap (_data, new_data);
_end_data = _data + old_size;
_end_allocated = _data + new_size;
if (_end_data != _data)
{
dealii::internal::AlignedVectorMove<T>(new_data, new_data + old_size,
_data, false);
free(new_data);
}
else
Assert(new_data == 0, ExcInternalError());
}
else if (size_alloc == 0)
clear();
}
template < class T >
inline
void
AlignedVector<T>::clear ()
{
if (_data != 0)
{
if (std_cxx11::is_trivial<T>::value == false)
while (_end_data != _data)
(--_end_data)->~T();
free(_data);
}
_data = 0;
_end_data = 0;
_end_allocated = 0;
}
template < class T >
inline
void
AlignedVector<T>::push_back (const T in_data)
{
Assert (_end_data <= _end_allocated, ExcInternalError());
if (_end_data == _end_allocated)
reserve (std::max(2*capacity(),static_cast<size_type>(16)));
if (std_cxx11::is_trivial<T>::value == false)
new (_end_data) T;
*_end_data++ = in_data;
}
template < class T >
inline
typename AlignedVector<T>::reference
AlignedVector<T>::back ()
{
AssertIndexRange (0, size());
T *field = _end_data - 1;
return *field;
}
template < class T >
inline
typename AlignedVector<T>::const_reference
AlignedVector<T>::back () const
{
AssertIndexRange (0, size());
const T *field = _end_data - 1;
return *field;
}
template < class T >
template <typename ForwardIterator>
inline
void
AlignedVector<T>::insert_back (ForwardIterator begin,
ForwardIterator end)
{
const unsigned int old_size = size();
reserve (old_size + (end-begin));
for ( ; begin != end; ++begin, ++_end_data)
{
if (std_cxx11::is_trivial<T>::value == false)
new (_end_data) T;
*_end_data = *begin;
}
}
template < class T >
inline
void
AlignedVector<T>::fill (const T &value)
{
dealii::internal::AlignedVectorSet<T,false> (size(), value, _data);
}
template < class T >
inline
void
AlignedVector<T>::swap (AlignedVector<T> &vec)
{
std::swap (_data, vec._data);
std::swap (_end_data, vec._end_data);
std::swap (_end_allocated, vec._end_allocated);
}
template < class T >
inline
bool
AlignedVector<T>::empty () const
{
return _end_data == _data;
}
template < class T >
inline
typename AlignedVector<T>::size_type
AlignedVector<T>::size () const
{
return _end_data - _data;
}
template < class T >
inline
typename AlignedVector<T>::size_type
AlignedVector<T>::capacity () const
{
return _end_allocated - _data;
}
template < class T >
inline
typename AlignedVector<T>::reference
AlignedVector<T>::operator [] (const size_type index)
{
AssertIndexRange (index, size());
return _data[index];
}
template < class T >
inline
typename AlignedVector<T>::const_reference
AlignedVector<T>::operator [] (const size_type index) const
{
AssertIndexRange (index, size());
return _data[index];
}
template < class T >
inline
typename AlignedVector<T>::iterator
AlignedVector<T>::begin ()
{
return _data;
}
template < class T >
inline
typename AlignedVector<T>::iterator
AlignedVector<T>::end ()
{
return _end_data;
}
template < class T >
inline
typename AlignedVector<T>::const_iterator
AlignedVector<T>::begin () const
{
return _data;
}
template < class T >
inline
typename AlignedVector<T>::const_iterator
AlignedVector<T>::end () const
{
return _end_data;
}
template < class T >
template < class Archive >
inline
void
AlignedVector<T>::save (Archive &ar, const unsigned int) const
{
size_type vec_size (size());
ar &vec_size;
if (vec_size > 0)
ar &boost::serialization::make_array(_data, vec_size);
}
template < class T >
template < class Archive >
inline
void
AlignedVector<T>::load (Archive &ar, const unsigned int)
{
size_type vec_size = 0;
ar &vec_size ;
if (vec_size > 0)
{
reserve(vec_size);
ar &boost::serialization::make_array(_data, vec_size);
_end_data = _data + vec_size;
}
}
template < class T >
inline
typename AlignedVector<T>::size_type
AlignedVector<T>::memory_consumption () const
{
size_type memory = sizeof(*this);
for (const T *t = _data ; t != _end_data; ++t)
memory += dealii::MemoryConsumption::memory_consumption(*t);
memory += sizeof(T) * (_end_allocated-_end_data);
return memory;
}
#endif // ifndef DOXYGEN
/**
* Relational operator == for AlignedVector
*
* @relates AlignedVector
*/
template < class T >
bool operator == (const AlignedVector<T> &lhs,
const AlignedVector<T> &rhs)
{
if (lhs.size() != rhs.size())
return false;
for (typename AlignedVector<T>::const_iterator lit = lhs.begin(),
rit = rhs.begin(); lit != lhs.end(); ++lit, ++rit)
if (*lit != *rit)
return false;
return true;
}
/**
* Relational operator != for AlignedVector
*
* @relates AlignedVector
*/
template < class T >
bool operator != (const AlignedVector<T> &lhs,
const AlignedVector<T> &rhs)
{
return !(operator==(lhs, rhs));
}
DEAL_II_NAMESPACE_CLOSE
#endif
|