Memory.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2008-2015 Gael Guennebaud <gael.guennebaud@inria.fr>
5 // Copyright (C) 2008-2009 Benoit Jacob <jacob.benoit.1@gmail.com>
6 // Copyright (C) 2009 Kenneth Riddile <kfriddile@yahoo.com>
7 // Copyright (C) 2010 Hauke Heibel <hauke.heibel@gmail.com>
8 // Copyright (C) 2010 Thomas Capricelli <orzel@freehackers.org>
9 // Copyright (C) 2013 Pavel Holoborodko <pavel@holoborodko.com>
10 //
11 // This Source Code Form is subject to the terms of the Mozilla
12 // Public License v. 2.0. If a copy of the MPL was not distributed
13 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
14 
15 /*****************************************************************************
16 *** Platform checks for aligned malloc functions ***
17 *****************************************************************************/
18 
19 #ifndef EIGEN_MEMORY_H
20 #define EIGEN_MEMORY_H
21 
22 #ifndef EIGEN_MALLOC_ALREADY_ALIGNED
23 
24 // Try to determine automatically if malloc is already aligned.
25 
26 // On 64-bit systems, glibc's malloc returns 16-byte-aligned pointers, see:
27 // http://www.gnu.org/s/libc/manual/html_node/Aligned-Memory-Blocks.html
28 // This is true at least since glibc 2.8.
29 // This leaves the question how to detect 64-bit. According to this document,
30 // http://gcc.fyxm.net/summit/2003/Porting%20to%2064%20bit.pdf
31 // page 114, "[The] LP64 model [...] is used by all 64-bit UNIX ports" so it's indeed
32 // quite safe, at least within the context of glibc, to equate 64-bit with LP64.
33 #if defined(__GLIBC__) && ((__GLIBC__ >= 2 && __GLIBC_MINOR__ >= 8) || __GLIBC__ > 2) && defined(__LP64__) && \
34  !defined(__SANITIZE_ADDRESS__) && (EIGEN_DEFAULT_ALIGN_BYTES == 16)
35 #define EIGEN_GLIBC_MALLOC_ALREADY_ALIGNED 1
36 #else
37 #define EIGEN_GLIBC_MALLOC_ALREADY_ALIGNED 0
38 #endif
39 
40 // FreeBSD 6 seems to have 16-byte aligned malloc
41 // See http://svn.freebsd.org/viewvc/base/stable/6/lib/libc/stdlib/malloc.c?view=markup
42 // FreeBSD 7 seems to have 16-byte aligned malloc except on ARM and MIPS architectures
43 // See http://svn.freebsd.org/viewvc/base/stable/7/lib/libc/stdlib/malloc.c?view=markup
44 #if defined(__FreeBSD__) && !(EIGEN_ARCH_ARM || EIGEN_ARCH_MIPS) && (EIGEN_DEFAULT_ALIGN_BYTES == 16)
45 #define EIGEN_FREEBSD_MALLOC_ALREADY_ALIGNED 1
46 #else
47 #define EIGEN_FREEBSD_MALLOC_ALREADY_ALIGNED 0
48 #endif
49 
50 #if (EIGEN_OS_MAC && (EIGEN_DEFAULT_ALIGN_BYTES == 16)) || (EIGEN_OS_WIN64 && (EIGEN_DEFAULT_ALIGN_BYTES == 16)) || \
51  EIGEN_GLIBC_MALLOC_ALREADY_ALIGNED || EIGEN_FREEBSD_MALLOC_ALREADY_ALIGNED
52 #define EIGEN_MALLOC_ALREADY_ALIGNED 1
53 #else
54 #define EIGEN_MALLOC_ALREADY_ALIGNED 0
55 #endif
56 
57 #endif
58 
59 #ifndef EIGEN_MALLOC_CHECK_THREAD_LOCAL
60 
61 // Check whether we can use the thread_local keyword to allow or disallow
62 // allocating memory with per-thread granularity, by means of the
63 // set_is_malloc_allowed() function.
64 #ifndef EIGEN_AVOID_THREAD_LOCAL
65 
66 #if ((EIGEN_COMP_GNUC) || __has_feature(cxx_thread_local) || EIGEN_COMP_MSVC >= 1900) && \
67  !defined(EIGEN_GPU_COMPILE_PHASE)
68 #define EIGEN_MALLOC_CHECK_THREAD_LOCAL thread_local
69 #else
70 #define EIGEN_MALLOC_CHECK_THREAD_LOCAL
71 #endif
72 
73 #else // EIGEN_AVOID_THREAD_LOCAL
74 #define EIGEN_MALLOC_CHECK_THREAD_LOCAL
75 #endif // EIGEN_AVOID_THREAD_LOCAL
76 
77 #endif
78 
79 // IWYU pragma: private
80 #include "../InternalHeaderCheck.h"
81 
82 namespace Eigen {
83 
84 namespace internal {
85 
86 /*****************************************************************************
87 *** Implementation of portable aligned versions of malloc/free/realloc ***
88 *****************************************************************************/
89 
90 #ifdef EIGEN_NO_MALLOC
92  eigen_assert(false && "heap allocation is forbidden (EIGEN_NO_MALLOC is defined)");
93 }
94 #elif defined EIGEN_RUNTIME_NO_MALLOC
95 EIGEN_DEVICE_FUNC inline bool is_malloc_allowed_impl(bool update, bool new_value = false) {
96  EIGEN_MALLOC_CHECK_THREAD_LOCAL static bool value = true;
97  if (update == 1) value = new_value;
98  return value;
99 }
100 EIGEN_DEVICE_FUNC inline bool is_malloc_allowed() { return is_malloc_allowed_impl(false); }
101 EIGEN_DEVICE_FUNC inline bool set_is_malloc_allowed(bool new_value) { return is_malloc_allowed_impl(true, new_value); }
103  eigen_assert(is_malloc_allowed() &&
104  "heap allocation is forbidden (EIGEN_RUNTIME_NO_MALLOC is defined and g_is_malloc_allowed is false)");
105 }
106 #else
108 #endif
109 
111 #ifdef EIGEN_EXCEPTIONS
112  throw std::bad_alloc();
113 #else
114  std::size_t huge = static_cast<std::size_t>(-1);
115 #if defined(EIGEN_HIPCC)
116  //
117  // calls to "::operator new" are to be treated as opaque function calls (i.e no inlining),
118  // and as a consequence the code in the #else block triggers the hipcc warning :
119  // "no overloaded function has restriction specifiers that are compatible with the ambient context"
120  //
121  // "throw_std_bad_alloc" has the EIGEN_DEVICE_FUNC attribute, so it seems that hipcc expects
122  // the same on "operator new"
123  // Reverting code back to the old version in this #if block for the hipcc compiler
124  //
125  new int[huge];
126 #else
127  void* unused = ::operator new(huge);
128  EIGEN_UNUSED_VARIABLE(unused);
129 #endif
130 #endif
131 }
132 
133 /*****************************************************************************
134 *** Implementation of handmade aligned functions ***
135 *****************************************************************************/
136 
137 /* ----- Hand made implementations of aligned malloc/free and realloc ----- */
138 
143  std::size_t alignment = EIGEN_DEFAULT_ALIGN_BYTES) {
144  eigen_assert(alignment >= sizeof(void*) && alignment <= 128 && (alignment & (alignment - 1)) == 0 &&
145  "Alignment must be at least sizeof(void*), less than or equal to 128, and a power of 2");
146 
148  EIGEN_USING_STD(malloc)
149  void* original = malloc(size + alignment);
150  if (original == nullptr) return nullptr;
151  uint8_t offset = static_cast<uint8_t>(alignment - (reinterpret_cast<std::size_t>(original) & (alignment - 1)));
152  void* aligned = static_cast<void*>(static_cast<uint8_t*>(original) + offset);
153  *(static_cast<uint8_t*>(aligned) - 1) = offset;
154  return aligned;
155 }
156 
158 EIGEN_DEVICE_FUNC inline void handmade_aligned_free(void* ptr) {
159  if (ptr != nullptr) {
160  uint8_t offset = static_cast<uint8_t>(*(static_cast<uint8_t*>(ptr) - 1));
161  void* original = static_cast<void*>(static_cast<uint8_t*>(ptr) - offset);
162 
164  EIGEN_USING_STD(free)
165  free(original);
166  }
167 }
168 
174 EIGEN_DEVICE_FUNC inline void* handmade_aligned_realloc(void* ptr, std::size_t new_size, std::size_t old_size,
175  std::size_t alignment = EIGEN_DEFAULT_ALIGN_BYTES) {
176  if (ptr == nullptr) return handmade_aligned_malloc(new_size, alignment);
177  uint8_t old_offset = *(static_cast<uint8_t*>(ptr) - 1);
178  void* old_original = static_cast<uint8_t*>(ptr) - old_offset;
179 
181  EIGEN_USING_STD(realloc)
182  void* original = realloc(old_original, new_size + alignment);
183  if (original == nullptr) return nullptr;
184  if (original == old_original) return ptr;
185  uint8_t offset = static_cast<uint8_t>(alignment - (reinterpret_cast<std::size_t>(original) & (alignment - 1)));
186  void* aligned = static_cast<void*>(static_cast<uint8_t*>(original) + offset);
187  if (offset != old_offset) {
188  const void* src = static_cast<const void*>(static_cast<uint8_t*>(original) + old_offset);
189  std::size_t count = (std::min)(new_size, old_size);
190  std::memmove(aligned, src, count);
191  }
192  *(static_cast<uint8_t*>(aligned) - 1) = offset;
193  return aligned;
194 }
195 
199 EIGEN_DEVICE_FUNC inline void* aligned_malloc(std::size_t size) {
200  if (size == 0) return nullptr;
201 
202  void* result;
203 #if (EIGEN_DEFAULT_ALIGN_BYTES == 0) || EIGEN_MALLOC_ALREADY_ALIGNED
204 
206  EIGEN_USING_STD(malloc)
207  result = malloc(size);
208 
209 #if EIGEN_DEFAULT_ALIGN_BYTES == 16
210  eigen_assert((size < 16 || (std::size_t(result) % 16) == 0) &&
211  "System's malloc returned an unaligned pointer. Compile with EIGEN_MALLOC_ALREADY_ALIGNED=0 to fallback "
212  "to handmade aligned memory allocator.");
213 #endif
214 #else
215  result = handmade_aligned_malloc(size);
216 #endif
217 
218  if (!result && size) throw_std_bad_alloc();
219 
220  return result;
221 }
222 
224 EIGEN_DEVICE_FUNC inline void aligned_free(void* ptr) {
225 #if (EIGEN_DEFAULT_ALIGN_BYTES == 0) || EIGEN_MALLOC_ALREADY_ALIGNED
226 
227  if (ptr != nullptr) {
229  EIGEN_USING_STD(free)
230  free(ptr);
231  }
232 
233 #else
235 #endif
236 }
237 
243 EIGEN_DEVICE_FUNC inline void* aligned_realloc(void* ptr, std::size_t new_size, std::size_t old_size) {
244  if (ptr == nullptr) return aligned_malloc(new_size);
245  if (old_size == new_size) return ptr;
246  if (new_size == 0) {
247  aligned_free(ptr);
248  return nullptr;
249  }
250 
251  void* result;
252 #if (EIGEN_DEFAULT_ALIGN_BYTES == 0) || EIGEN_MALLOC_ALREADY_ALIGNED
253  EIGEN_UNUSED_VARIABLE(old_size)
254 
256  EIGEN_USING_STD(realloc)
257  result = realloc(ptr, new_size);
258 #else
259  result = handmade_aligned_realloc(ptr, new_size, old_size);
260 #endif
261 
262  if (!result && new_size) throw_std_bad_alloc();
263 
264  return result;
265 }
266 
267 /*****************************************************************************
268 *** Implementation of conditionally aligned functions ***
269 *****************************************************************************/
270 
274 template <bool Align>
276  return aligned_malloc(size);
277 }
278 
279 template <>
281  if (size == 0) return nullptr;
282 
284  EIGEN_USING_STD(malloc)
285  void* result = malloc(size);
286 
287  if (!result && size) throw_std_bad_alloc();
288  return result;
289 }
290 
292 template <bool Align>
294  aligned_free(ptr);
295 }
296 
297 template <>
299  if (ptr != nullptr) {
301  EIGEN_USING_STD(free)
302  free(ptr);
303  }
304 }
305 
306 template <bool Align>
307 EIGEN_DEVICE_FUNC inline void* conditional_aligned_realloc(void* ptr, std::size_t new_size, std::size_t old_size) {
308  return aligned_realloc(ptr, new_size, old_size);
309 }
310 
311 template <>
312 EIGEN_DEVICE_FUNC inline void* conditional_aligned_realloc<false>(void* ptr, std::size_t new_size,
313  std::size_t old_size) {
314  if (ptr == nullptr) return conditional_aligned_malloc<false>(new_size);
315  if (old_size == new_size) return ptr;
316  if (new_size == 0) {
318  return nullptr;
319  }
320 
322  EIGEN_USING_STD(realloc)
323  return realloc(ptr, new_size);
324 }
325 
326 /*****************************************************************************
327 *** Construction/destruction of array elements ***
328 *****************************************************************************/
329 
333 template <typename T>
334 EIGEN_DEVICE_FUNC inline void destruct_elements_of_array(T* ptr, std::size_t size) {
335  // always destruct an array starting from the end.
336  if (ptr)
337  while (size) ptr[--size].~T();
338 }
339 
343 template <typename T>
345  std::size_t i = 0;
346  EIGEN_TRY {
347  for (i = 0; i < size; ++i) ::new (ptr + i) T;
348  }
349  EIGEN_CATCH(...) {
351  EIGEN_THROW;
352  }
353  return ptr;
354 }
355 
359 template <typename T>
360 EIGEN_DEVICE_FUNC inline T* copy_construct_elements_of_array(T* ptr, const T* src, std::size_t size) {
361  std::size_t i = 0;
362  EIGEN_TRY {
363  for (i = 0; i < size; ++i) ::new (ptr + i) T(*(src + i));
364  }
365  EIGEN_CATCH(...) {
367  EIGEN_THROW;
368  }
369  return ptr;
370 }
371 
375 template <typename T>
376 EIGEN_DEVICE_FUNC inline T* move_construct_elements_of_array(T* ptr, T* src, std::size_t size) {
377  std::size_t i = 0;
378  EIGEN_TRY {
379  for (i = 0; i < size; ++i) ::new (ptr + i) T(std::move(*(src + i)));
380  }
381  EIGEN_CATCH(...) {
383  EIGEN_THROW;
384  }
385  return ptr;
386 }
387 
388 /*****************************************************************************
389 *** Implementation of aligned new/delete-like functions ***
390 *****************************************************************************/
391 
392 template <typename T>
394  constexpr std::size_t max_elements = (std::numeric_limits<std::ptrdiff_t>::max)() / sizeof(T);
395  if (size > max_elements) throw_std_bad_alloc();
396 }
397 
402 template <typename T>
403 EIGEN_DEVICE_FUNC inline T* aligned_new(std::size_t size) {
404  check_size_for_overflow<T>(size);
405  T* result = static_cast<T*>(aligned_malloc(sizeof(T) * size));
407  EIGEN_CATCH(...) {
408  aligned_free(result);
409  EIGEN_THROW;
410  }
411  return result;
412 }
413 
414 template <typename T, bool Align>
416  check_size_for_overflow<T>(size);
417  T* result = static_cast<T*>(conditional_aligned_malloc<Align>(sizeof(T) * size));
419  EIGEN_CATCH(...) {
420  conditional_aligned_free<Align>(result);
421  EIGEN_THROW;
422  }
423  return result;
424 }
425 
429 template <typename T>
430 EIGEN_DEVICE_FUNC inline void aligned_delete(T* ptr, std::size_t size) {
431  destruct_elements_of_array<T>(ptr, size);
432  aligned_free(ptr);
433 }
434 
438 template <typename T, bool Align>
439 EIGEN_DEVICE_FUNC inline void conditional_aligned_delete(T* ptr, std::size_t size) {
440  destruct_elements_of_array<T>(ptr, size);
441  conditional_aligned_free<Align>(ptr);
442 }
443 
444 template <typename T, bool Align>
445 EIGEN_DEVICE_FUNC inline T* conditional_aligned_realloc_new(T* pts, std::size_t new_size, std::size_t old_size) {
446  check_size_for_overflow<T>(new_size);
447  check_size_for_overflow<T>(old_size);
448 
449  // If elements need to be explicitly initialized, we cannot simply realloc
450  // (or memcpy) the memory block - each element needs to be reconstructed.
451  // Otherwise, objects that contain internal pointers like mpfr or
452  // AnnoyingScalar can be pointing to the wrong thing.
453  T* result = static_cast<T*>(conditional_aligned_malloc<Align>(sizeof(T) * new_size));
454  EIGEN_TRY {
455  // Move-construct initial elements.
456  std::size_t copy_size = (std::min)(old_size, new_size);
457  move_construct_elements_of_array(result, pts, copy_size);
458 
459  // Default-construct remaining elements.
460  if (new_size > old_size) {
461  default_construct_elements_of_array(result + copy_size, new_size - old_size);
462  }
463 
464  // Delete old elements.
465  conditional_aligned_delete<T, Align>(pts, old_size);
466  }
467  EIGEN_CATCH(...) {
468  conditional_aligned_free<Align>(result);
469  EIGEN_THROW;
470  }
471 
472  return result;
473 }
474 
475 template <typename T, bool Align>
477  if (size == 0) return nullptr; // short-cut. Also fixes Bug 884
478  check_size_for_overflow<T>(size);
479  T* result = static_cast<T*>(conditional_aligned_malloc<Align>(sizeof(T) * size));
482  EIGEN_CATCH(...) {
483  conditional_aligned_free<Align>(result);
484  EIGEN_THROW;
485  }
486  }
487  return result;
488 }
489 
490 template <typename T, bool Align>
491 EIGEN_DEVICE_FUNC inline T* conditional_aligned_realloc_new_auto(T* pts, std::size_t new_size, std::size_t old_size) {
493  return conditional_aligned_realloc_new<T, Align>(pts, new_size, old_size);
494  }
495 
496  check_size_for_overflow<T>(new_size);
497  check_size_for_overflow<T>(old_size);
498  return static_cast<T*>(
499  conditional_aligned_realloc<Align>(static_cast<void*>(pts), sizeof(T) * new_size, sizeof(T) * old_size));
500 }
501 
502 template <typename T, bool Align>
503 EIGEN_DEVICE_FUNC inline void conditional_aligned_delete_auto(T* ptr, std::size_t size) {
504  if (NumTraits<T>::RequireInitialization) destruct_elements_of_array<T>(ptr, size);
505  conditional_aligned_free<Align>(ptr);
506 }
507 
508 /****************************************************************************/
509 
528 template <int Alignment, typename Scalar, typename Index>
530  const Index ScalarSize = sizeof(Scalar);
531  const Index AlignmentSize = Alignment / ScalarSize;
532  const Index AlignmentMask = AlignmentSize - 1;
533 
534  if (AlignmentSize <= 1) {
535  // Either the requested alignment if smaller than a scalar, or it exactly match a 1 scalar
536  // so that all elements of the array have the same alignment.
537  return 0;
538  } else if ((std::uintptr_t(array) & (sizeof(Scalar) - 1)) || (Alignment % ScalarSize) != 0) {
539  // The array is not aligned to the size of a single scalar, or the requested alignment is not a multiple of the
540  // scalar size. Consequently, no element of the array is well aligned.
541  return size;
542  } else {
543  Index first = (AlignmentSize - (Index((std::uintptr_t(array) / sizeof(Scalar))) & AlignmentMask)) & AlignmentMask;
544  return (first < size) ? first : size;
545  }
546 }
547 
550 template <typename Scalar, typename Index>
552  typedef typename packet_traits<Scalar>::type DefaultPacketType;
553  return first_aligned<unpacket_traits<DefaultPacketType>::alignment>(array, size);
554 }
555 
558 template <typename Index>
560  return ((size + base - 1) / base) * base;
561 }
562 
563 // std::copy is much slower than memcpy, so let's introduce a smart_copy which
564 // use memcpy on trivial types, i.e., on types that does not require an initialization ctor.
565 template <typename T, bool UseMemcpy>
567 
568 template <typename T>
569 EIGEN_DEVICE_FUNC void smart_copy(const T* start, const T* end, T* target) {
571 }
572 
573 template <typename T>
574 struct smart_copy_helper<T, true> {
575  EIGEN_DEVICE_FUNC static inline void run(const T* start, const T* end, T* target) {
576  std::intptr_t size = std::intptr_t(end) - std::intptr_t(start);
577  if (size == 0) return;
578  eigen_internal_assert(start != 0 && end != 0 && target != 0);
579  EIGEN_USING_STD(memcpy)
580  memcpy(target, start, size);
581  }
582 };
583 
584 template <typename T>
585 struct smart_copy_helper<T, false> {
586  EIGEN_DEVICE_FUNC static inline void run(const T* start, const T* end, T* target) { std::copy(start, end, target); }
587 };
588 
589 // intelligent memmove. falls back to std::memmove for POD types, uses std::copy otherwise.
590 template <typename T, bool UseMemmove>
592 
593 template <typename T>
594 void smart_memmove(const T* start, const T* end, T* target) {
596 }
597 
598 template <typename T>
599 struct smart_memmove_helper<T, true> {
600  static inline void run(const T* start, const T* end, T* target) {
601  std::intptr_t size = std::intptr_t(end) - std::intptr_t(start);
602  if (size == 0) return;
603  eigen_internal_assert(start != 0 && end != 0 && target != 0);
604  std::memmove(target, start, size);
605  }
606 };
607 
608 template <typename T>
609 struct smart_memmove_helper<T, false> {
610  static inline void run(const T* start, const T* end, T* target) {
611  if (std::uintptr_t(target) < std::uintptr_t(start)) {
612  std::copy(start, end, target);
613  } else {
614  std::ptrdiff_t count = (std::ptrdiff_t(end) - std::ptrdiff_t(start)) / sizeof(T);
615  std::copy_backward(start, end, target + count);
616  }
617  }
618 };
619 
620 template <typename T>
622  return std::move(start, end, target);
623 }
624 
625 /*****************************************************************************
626 *** Implementation of runtime stack allocation (falling back to malloc) ***
627 *****************************************************************************/
628 
629 // you can overwrite Eigen's default behavior regarding alloca by defining EIGEN_ALLOCA
630 // to the appropriate stack allocation function
631 #if !defined EIGEN_ALLOCA && !defined EIGEN_GPU_COMPILE_PHASE
632 #if EIGEN_OS_LINUX || EIGEN_OS_MAC || (defined alloca)
633 #define EIGEN_ALLOCA alloca
634 #elif EIGEN_COMP_MSVC
635 #define EIGEN_ALLOCA _alloca
636 #endif
637 #endif
638 
639 // With clang -Oz -mthumb, alloca changes the stack pointer in a way that is
640 // not allowed in Thumb2. -DEIGEN_STACK_ALLOCATION_LIMIT=0 doesn't work because
641 // the compiler still emits bad code because stack allocation checks use "<=".
642 // TODO: Eliminate after https://bugs.llvm.org/show_bug.cgi?id=23772
643 // is fixed.
644 #if defined(__clang__) && defined(__thumb__)
645 #undef EIGEN_ALLOCA
646 #endif
647 
648 // This helper class construct the allocated memory, and takes care of destructing and freeing the handled data
649 // at destruction time. In practice this helper class is mainly useful to avoid memory leak in case of exceptions.
650 template <typename T>
652  public:
653  /* Creates a stack_memory_handler responsible for the buffer \a ptr of size \a size.
654  * Note that \a ptr can be 0 regardless of the other parameters.
655  * This constructor takes care of constructing/initializing the elements of the buffer if required by the scalar type
656  *T (see NumTraits<T>::RequireInitialization). In this case, the buffer elements will also be destructed when this
657  *handler will be destructed. Finally, if \a dealloc is true, then the pointer \a ptr is freed.
658  **/
659  EIGEN_DEVICE_FUNC aligned_stack_memory_handler(T* ptr, std::size_t size, bool dealloc)
660  : m_ptr(ptr), m_size(size), m_deallocate(dealloc) {
662  }
664  if (NumTraits<T>::RequireInitialization && m_ptr) Eigen::internal::destruct_elements_of_array<T>(m_ptr, m_size);
666  }
667 
668  protected:
670  std::size_t m_size;
672 };
673 
674 #ifdef EIGEN_ALLOCA
675 
676 template <typename Xpr, int NbEvaluations,
677  bool MapExternalBuffer = nested_eval<Xpr, NbEvaluations>::Evaluate && Xpr::MaxSizeAtCompileTime == Dynamic>
678 struct local_nested_eval_wrapper {
679  static constexpr bool NeedExternalBuffer = false;
680  typedef typename Xpr::Scalar Scalar;
681  typedef typename nested_eval<Xpr, NbEvaluations>::type ObjectType;
682  ObjectType object;
683 
684  EIGEN_DEVICE_FUNC local_nested_eval_wrapper(const Xpr& xpr, Scalar* ptr) : object(xpr) {
686  eigen_internal_assert(ptr == 0);
687  }
688 };
689 
690 template <typename Xpr, int NbEvaluations>
691 struct local_nested_eval_wrapper<Xpr, NbEvaluations, true> {
692  static constexpr bool NeedExternalBuffer = true;
693  typedef typename Xpr::Scalar Scalar;
694  typedef typename plain_object_eval<Xpr>::type PlainObject;
695  typedef Map<PlainObject, EIGEN_DEFAULT_ALIGN_BYTES> ObjectType;
696  ObjectType object;
697 
698  EIGEN_DEVICE_FUNC local_nested_eval_wrapper(const Xpr& xpr, Scalar* ptr)
699  : object(ptr == 0 ? reinterpret_cast<Scalar*>(Eigen::internal::aligned_malloc(sizeof(Scalar) * xpr.size())) : ptr,
700  xpr.rows(), xpr.cols()),
701  m_deallocate(ptr == 0) {
704  object = xpr;
705  }
706 
707  EIGEN_DEVICE_FUNC ~local_nested_eval_wrapper() {
710  if (m_deallocate) Eigen::internal::aligned_free(object.data());
711  }
712 
713  private:
714  bool m_deallocate;
715 };
716 
717 #endif // EIGEN_ALLOCA
718 
719 template <typename T>
722 
723  public:
724  explicit scoped_array(std::ptrdiff_t size) { m_ptr = new T[size]; }
725  ~scoped_array() { delete[] m_ptr; }
726  T& operator[](std::ptrdiff_t i) { return m_ptr[i]; }
727  const T& operator[](std::ptrdiff_t i) const { return m_ptr[i]; }
728  T*& ptr() { return m_ptr; }
729  const T* ptr() const { return m_ptr; }
730  operator const T*() const { return m_ptr; }
731 };
732 
733 template <typename T>
735  std::swap(a.ptr(), b.ptr());
736 }
737 
738 } // end namespace internal
739 
763 #ifdef EIGEN_ALLOCA
764 
765 #if EIGEN_DEFAULT_ALIGN_BYTES > 0
766 // We always manually re-align the result of EIGEN_ALLOCA.
767 // If alloca is already aligned, the compiler should be smart enough to optimize away the re-alignment.
768 
769 #if ((EIGEN_COMP_GNUC || EIGEN_COMP_CLANG) && !EIGEN_COMP_NVHPC)
770 #define EIGEN_ALIGNED_ALLOCA(SIZE) __builtin_alloca_with_align(SIZE, CHAR_BIT* EIGEN_DEFAULT_ALIGN_BYTES)
771 #else
772 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void* eigen_aligned_alloca_helper(void* ptr) {
773  constexpr std::uintptr_t mask = EIGEN_DEFAULT_ALIGN_BYTES - 1;
774  std::uintptr_t ptr_int = std::uintptr_t(ptr);
775  std::uintptr_t aligned_ptr_int = (ptr_int + mask) & ~mask;
776  std::uintptr_t offset = aligned_ptr_int - ptr_int;
777  return static_cast<void*>(static_cast<uint8_t*>(ptr) + offset);
778 }
779 #define EIGEN_ALIGNED_ALLOCA(SIZE) eigen_aligned_alloca_helper(EIGEN_ALLOCA(SIZE + EIGEN_DEFAULT_ALIGN_BYTES - 1))
780 #endif
781 
782 #else
783 #define EIGEN_ALIGNED_ALLOCA(SIZE) EIGEN_ALLOCA(SIZE)
784 #endif
785 
786 #define ei_declare_aligned_stack_constructed_variable(TYPE, NAME, SIZE, BUFFER) \
787  Eigen::internal::check_size_for_overflow<TYPE>(SIZE); \
788  TYPE* NAME = (BUFFER) != 0 ? (BUFFER) \
789  : reinterpret_cast<TYPE*>((sizeof(TYPE) * SIZE <= EIGEN_STACK_ALLOCATION_LIMIT) \
790  ? EIGEN_ALIGNED_ALLOCA(sizeof(TYPE) * SIZE) \
791  : Eigen::internal::aligned_malloc(sizeof(TYPE) * SIZE)); \
792  Eigen::internal::aligned_stack_memory_handler<TYPE> EIGEN_CAT(NAME, _stack_memory_destructor)( \
793  (BUFFER) == 0 ? NAME : 0, SIZE, sizeof(TYPE) * SIZE > EIGEN_STACK_ALLOCATION_LIMIT)
794 
795 #define ei_declare_local_nested_eval(XPR_T, XPR, N, NAME) \
796  Eigen::internal::local_nested_eval_wrapper<XPR_T, N> EIGEN_CAT(NAME, _wrapper)( \
797  XPR, reinterpret_cast<typename XPR_T::Scalar*>( \
798  ((Eigen::internal::local_nested_eval_wrapper<XPR_T, N>::NeedExternalBuffer) && \
799  ((sizeof(typename XPR_T::Scalar) * XPR.size()) <= EIGEN_STACK_ALLOCATION_LIMIT)) \
800  ? EIGEN_ALIGNED_ALLOCA(sizeof(typename XPR_T::Scalar) * XPR.size()) \
801  : 0)); \
802  typename Eigen::internal::local_nested_eval_wrapper<XPR_T, N>::ObjectType NAME(EIGEN_CAT(NAME, _wrapper).object)
803 
804 #else
805 
806 #define ei_declare_aligned_stack_constructed_variable(TYPE, NAME, SIZE, BUFFER) \
807  Eigen::internal::check_size_for_overflow<TYPE>(SIZE); \
808  TYPE* NAME = (BUFFER) != 0 ? BUFFER : reinterpret_cast<TYPE*>(Eigen::internal::aligned_malloc(sizeof(TYPE) * SIZE)); \
809  Eigen::internal::aligned_stack_memory_handler<TYPE> EIGEN_CAT(NAME, _stack_memory_destructor)( \
810  (BUFFER) == 0 ? NAME : 0, SIZE, true)
811 
812 #define ei_declare_local_nested_eval(XPR_T, XPR, N, NAME) \
813  typename Eigen::internal::nested_eval<XPR_T, N>::type NAME(XPR)
814 
815 #endif
816 
817 /*****************************************************************************
818 *** Implementation of EIGEN_MAKE_ALIGNED_OPERATOR_NEW [_IF] ***
819 *****************************************************************************/
820 
821 #if EIGEN_HAS_CXX17_OVERALIGN
822 
823 // C++17 -> no need to bother about alignment anymore :)
824 
825 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_NOTHROW(NeedsToAlign)
826 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign)
827 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW
828 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(Scalar, Size)
829 
830 #else
831 
832 // HIP does not support new/delete on device.
833 #if EIGEN_MAX_ALIGN_BYTES != 0 && !defined(EIGEN_HIP_DEVICE_COMPILE)
834 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_NOTHROW(NeedsToAlign) \
835  EIGEN_DEVICE_FUNC void* operator new(std::size_t size, const std::nothrow_t&) EIGEN_NO_THROW { \
836  EIGEN_TRY { return Eigen::internal::conditional_aligned_malloc<NeedsToAlign>(size); } \
837  EIGEN_CATCH(...) { return 0; } \
838  }
839 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign) \
840  EIGEN_DEVICE_FUNC void* operator new(std::size_t size) { \
841  return Eigen::internal::conditional_aligned_malloc<NeedsToAlign>(size); \
842  } \
843  EIGEN_DEVICE_FUNC void* operator new[](std::size_t size) { \
844  return Eigen::internal::conditional_aligned_malloc<NeedsToAlign>(size); \
845  } \
846  EIGEN_DEVICE_FUNC void operator delete(void* ptr) EIGEN_NO_THROW { \
847  Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); \
848  } \
849  EIGEN_DEVICE_FUNC void operator delete[](void* ptr) EIGEN_NO_THROW { \
850  Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); \
851  } \
852  EIGEN_DEVICE_FUNC void operator delete(void* ptr, std::size_t /* sz */) EIGEN_NO_THROW { \
853  Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); \
854  } \
855  EIGEN_DEVICE_FUNC void operator delete[](void* ptr, std::size_t /* sz */) EIGEN_NO_THROW { \
856  Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); \
857  } \
858  /* in-place new and delete. since (at least afaik) there is no actual */ \
859  /* memory allocated we can safely let the default implementation handle */ \
860  /* this particular case. */ \
861  EIGEN_DEVICE_FUNC static void* operator new(std::size_t size, void* ptr) { return ::operator new(size, ptr); } \
862  EIGEN_DEVICE_FUNC static void* operator new[](std::size_t size, void* ptr) { return ::operator new[](size, ptr); } \
863  EIGEN_DEVICE_FUNC void operator delete(void* memory, void* ptr) EIGEN_NO_THROW { \
864  return ::operator delete(memory, ptr); \
865  } \
866  EIGEN_DEVICE_FUNC void operator delete[](void* memory, void* ptr) EIGEN_NO_THROW { \
867  return ::operator delete[](memory, ptr); \
868  } \
869  /* nothrow-new (returns zero instead of std::bad_alloc) */ \
870  EIGEN_MAKE_ALIGNED_OPERATOR_NEW_NOTHROW(NeedsToAlign) \
871  EIGEN_DEVICE_FUNC void operator delete(void* ptr, const std::nothrow_t&) EIGEN_NO_THROW { \
872  Eigen::internal::conditional_aligned_free<NeedsToAlign>(ptr); \
873  } \
874  typedef void eigen_aligned_operator_new_marker_type;
875 #else
876 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign)
877 #endif
878 
879 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(true)
880 #define EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF_VECTORIZABLE_FIXED_SIZE(Scalar, Size) \
881  EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF( \
882  bool(((Size) != Eigen::Dynamic) && \
883  (((EIGEN_MAX_ALIGN_BYTES >= 16) && ((sizeof(Scalar) * (Size)) % (EIGEN_MAX_ALIGN_BYTES) == 0)) || \
884  ((EIGEN_MAX_ALIGN_BYTES >= 32) && ((sizeof(Scalar) * (Size)) % (EIGEN_MAX_ALIGN_BYTES / 2) == 0)) || \
885  ((EIGEN_MAX_ALIGN_BYTES >= 64) && ((sizeof(Scalar) * (Size)) % (EIGEN_MAX_ALIGN_BYTES / 4) == 0)))))
886 
887 #endif
888 
889 /****************************************************************************/
890 
915 template <class T>
917  public:
918  typedef std::size_t size_type;
919  typedef std::ptrdiff_t difference_type;
920  typedef T* pointer;
921  typedef const T* const_pointer;
922  typedef T& reference;
923  typedef const T& const_reference;
924  typedef T value_type;
925 
926  template <class U>
927  struct rebind {
929  };
930 
931  aligned_allocator() = default;
932 
934 
935  template <class U>
937 
938  template <class U>
939  constexpr bool operator==(const aligned_allocator<U>&) const noexcept {
940  return true;
941  }
942  template <class U>
943  constexpr bool operator!=(const aligned_allocator<U>&) const noexcept {
944  return false;
945  }
946 
947 #if EIGEN_COMP_GNUC_STRICT && EIGEN_GNUC_STRICT_AT_LEAST(7, 0, 0)
948  // In gcc std::allocator::max_size() is bugged making gcc triggers a warning:
949  // eigen/Eigen/src/Core/util/Memory.h:189:12: warning: argument 1 value '18446744073709551612' exceeds maximum object
950  // size 9223372036854775807 See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87544
951  size_type max_size() const { return (std::numeric_limits<std::ptrdiff_t>::max)() / sizeof(T); }
952 #endif
953 
954  pointer allocate(size_type num, const void* /*hint*/ = 0) {
955  internal::check_size_for_overflow<T>(num);
956  return static_cast<pointer>(internal::aligned_malloc(num * sizeof(T)));
957  }
958 
960 };
961 
962 //---------- Cache sizes ----------
963 
964 #if !defined(EIGEN_NO_CPUID)
965 #if EIGEN_COMP_GNUC && EIGEN_ARCH_i386_OR_x86_64
966 #if defined(__PIC__) && EIGEN_ARCH_i386
967 // Case for x86 with PIC
968 #define EIGEN_CPUID(abcd, func, id) \
969  __asm__ __volatile__("xchgl %%ebx, %k1;cpuid; xchgl %%ebx,%k1" \
970  : "=a"(abcd[0]), "=&r"(abcd[1]), "=c"(abcd[2]), "=d"(abcd[3]) \
971  : "a"(func), "c"(id));
972 #elif defined(__PIC__) && EIGEN_ARCH_x86_64
973 // Case for x64 with PIC. In theory this is only a problem with recent gcc and with medium or large code model, not with
974 // the default small code model. However, we cannot detect which code model is used, and the xchg overhead is negligible
975 // anyway.
976 #define EIGEN_CPUID(abcd, func, id) \
977  __asm__ __volatile__("xchg{q}\t{%%}rbx, %q1; cpuid; xchg{q}\t{%%}rbx, %q1" \
978  : "=a"(abcd[0]), "=&r"(abcd[1]), "=c"(abcd[2]), "=d"(abcd[3]) \
979  : "0"(func), "2"(id));
980 #else
981 // Case for x86_64 or x86 w/o PIC
982 #define EIGEN_CPUID(abcd, func, id) \
983  __asm__ __volatile__("cpuid" : "=a"(abcd[0]), "=b"(abcd[1]), "=c"(abcd[2]), "=d"(abcd[3]) : "0"(func), "2"(id));
984 #endif
985 #elif EIGEN_COMP_MSVC
986 #if EIGEN_ARCH_i386_OR_x86_64
987 #define EIGEN_CPUID(abcd, func, id) __cpuidex((int*)abcd, func, id)
988 #endif
989 #endif
990 #endif
991 
992 namespace internal {
993 
994 #ifdef EIGEN_CPUID
995 
996 inline bool cpuid_is_vendor(int abcd[4], const int vendor[3]) {
997  return abcd[1] == vendor[0] && abcd[3] == vendor[1] && abcd[2] == vendor[2];
998 }
999 
1000 inline void queryCacheSizes_intel_direct(int& l1, int& l2, int& l3) {
1001  int abcd[4];
1002  l1 = l2 = l3 = 0;
1003  int cache_id = 0;
1004  int cache_type = 0;
1005  do {
1006  abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
1007  EIGEN_CPUID(abcd, 0x4, cache_id);
1008  cache_type = (abcd[0] & 0x0F) >> 0;
1009  if (cache_type == 1 || cache_type == 3) // data or unified cache
1010  {
1011  int cache_level = (abcd[0] & 0xE0) >> 5; // A[7:5]
1012  int ways = (abcd[1] & 0xFFC00000) >> 22; // B[31:22]
1013  int partitions = (abcd[1] & 0x003FF000) >> 12; // B[21:12]
1014  int line_size = (abcd[1] & 0x00000FFF) >> 0; // B[11:0]
1015  int sets = (abcd[2]); // C[31:0]
1016 
1017  int cache_size = (ways + 1) * (partitions + 1) * (line_size + 1) * (sets + 1);
1018 
1019  switch (cache_level) {
1020  case 1:
1021  l1 = cache_size;
1022  break;
1023  case 2:
1024  l2 = cache_size;
1025  break;
1026  case 3:
1027  l3 = cache_size;
1028  break;
1029  default:
1030  break;
1031  }
1032  }
1033  cache_id++;
1034  } while (cache_type > 0 && cache_id < 16);
1035 }
1036 
1037 inline void queryCacheSizes_intel_codes(int& l1, int& l2, int& l3) {
1038  int abcd[4];
1039  abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
1040  l1 = l2 = l3 = 0;
1041  EIGEN_CPUID(abcd, 0x00000002, 0);
1042  unsigned char* bytes = reinterpret_cast<unsigned char*>(abcd) + 2;
1043  bool check_for_p2_core2 = false;
1044  for (int i = 0; i < 14; ++i) {
1045  switch (bytes[i]) {
1046  case 0x0A:
1047  l1 = 8;
1048  break; // 0Ah data L1 cache, 8 KB, 2 ways, 32 byte lines
1049  case 0x0C:
1050  l1 = 16;
1051  break; // 0Ch data L1 cache, 16 KB, 4 ways, 32 byte lines
1052  case 0x0E:
1053  l1 = 24;
1054  break; // 0Eh data L1 cache, 24 KB, 6 ways, 64 byte lines
1055  case 0x10:
1056  l1 = 16;
1057  break; // 10h data L1 cache, 16 KB, 4 ways, 32 byte lines (IA-64)
1058  case 0x15:
1059  l1 = 16;
1060  break; // 15h code L1 cache, 16 KB, 4 ways, 32 byte lines (IA-64)
1061  case 0x2C:
1062  l1 = 32;
1063  break; // 2Ch data L1 cache, 32 KB, 8 ways, 64 byte lines
1064  case 0x30:
1065  l1 = 32;
1066  break; // 30h code L1 cache, 32 KB, 8 ways, 64 byte lines
1067  case 0x60:
1068  l1 = 16;
1069  break; // 60h data L1 cache, 16 KB, 8 ways, 64 byte lines, sectored
1070  case 0x66:
1071  l1 = 8;
1072  break; // 66h data L1 cache, 8 KB, 4 ways, 64 byte lines, sectored
1073  case 0x67:
1074  l1 = 16;
1075  break; // 67h data L1 cache, 16 KB, 4 ways, 64 byte lines, sectored
1076  case 0x68:
1077  l1 = 32;
1078  break; // 68h data L1 cache, 32 KB, 4 ways, 64 byte lines, sectored
1079  case 0x1A:
1080  l2 = 96;
1081  break; // code and data L2 cache, 96 KB, 6 ways, 64 byte lines (IA-64)
1082  case 0x22:
1083  l3 = 512;
1084  break; // code and data L3 cache, 512 KB, 4 ways (!), 64 byte lines, dual-sectored
1085  case 0x23:
1086  l3 = 1024;
1087  break; // code and data L3 cache, 1024 KB, 8 ways, 64 byte lines, dual-sectored
1088  case 0x25:
1089  l3 = 2048;
1090  break; // code and data L3 cache, 2048 KB, 8 ways, 64 byte lines, dual-sectored
1091  case 0x29:
1092  l3 = 4096;
1093  break; // code and data L3 cache, 4096 KB, 8 ways, 64 byte lines, dual-sectored
1094  case 0x39:
1095  l2 = 128;
1096  break; // code and data L2 cache, 128 KB, 4 ways, 64 byte lines, sectored
1097  case 0x3A:
1098  l2 = 192;
1099  break; // code and data L2 cache, 192 KB, 6 ways, 64 byte lines, sectored
1100  case 0x3B:
1101  l2 = 128;
1102  break; // code and data L2 cache, 128 KB, 2 ways, 64 byte lines, sectored
1103  case 0x3C:
1104  l2 = 256;
1105  break; // code and data L2 cache, 256 KB, 4 ways, 64 byte lines, sectored
1106  case 0x3D:
1107  l2 = 384;
1108  break; // code and data L2 cache, 384 KB, 6 ways, 64 byte lines, sectored
1109  case 0x3E:
1110  l2 = 512;
1111  break; // code and data L2 cache, 512 KB, 4 ways, 64 byte lines, sectored
1112  case 0x40:
1113  l2 = 0;
1114  break; // no integrated L2 cache (P6 core) or L3 cache (P4 core)
1115  case 0x41:
1116  l2 = 128;
1117  break; // code and data L2 cache, 128 KB, 4 ways, 32 byte lines
1118  case 0x42:
1119  l2 = 256;
1120  break; // code and data L2 cache, 256 KB, 4 ways, 32 byte lines
1121  case 0x43:
1122  l2 = 512;
1123  break; // code and data L2 cache, 512 KB, 4 ways, 32 byte lines
1124  case 0x44:
1125  l2 = 1024;
1126  break; // code and data L2 cache, 1024 KB, 4 ways, 32 byte lines
1127  case 0x45:
1128  l2 = 2048;
1129  break; // code and data L2 cache, 2048 KB, 4 ways, 32 byte lines
1130  case 0x46:
1131  l3 = 4096;
1132  break; // code and data L3 cache, 4096 KB, 4 ways, 64 byte lines
1133  case 0x47:
1134  l3 = 8192;
1135  break; // code and data L3 cache, 8192 KB, 8 ways, 64 byte lines
1136  case 0x48:
1137  l2 = 3072;
1138  break; // code and data L2 cache, 3072 KB, 12 ways, 64 byte lines
1139  case 0x49:
1140  if (l2 != 0)
1141  l3 = 4096;
1142  else {
1143  check_for_p2_core2 = true;
1144  l3 = l2 = 4096;
1145  }
1146  break; // code and data L3 cache, 4096 KB, 16 ways, 64 byte lines (P4) or L2 for core2
1147  case 0x4A:
1148  l3 = 6144;
1149  break; // code and data L3 cache, 6144 KB, 12 ways, 64 byte lines
1150  case 0x4B:
1151  l3 = 8192;
1152  break; // code and data L3 cache, 8192 KB, 16 ways, 64 byte lines
1153  case 0x4C:
1154  l3 = 12288;
1155  break; // code and data L3 cache, 12288 KB, 12 ways, 64 byte lines
1156  case 0x4D:
1157  l3 = 16384;
1158  break; // code and data L3 cache, 16384 KB, 16 ways, 64 byte lines
1159  case 0x4E:
1160  l2 = 6144;
1161  break; // code and data L2 cache, 6144 KB, 24 ways, 64 byte lines
1162  case 0x78:
1163  l2 = 1024;
1164  break; // code and data L2 cache, 1024 KB, 4 ways, 64 byte lines
1165  case 0x79:
1166  l2 = 128;
1167  break; // code and data L2 cache, 128 KB, 8 ways, 64 byte lines, dual-sectored
1168  case 0x7A:
1169  l2 = 256;
1170  break; // code and data L2 cache, 256 KB, 8 ways, 64 byte lines, dual-sectored
1171  case 0x7B:
1172  l2 = 512;
1173  break; // code and data L2 cache, 512 KB, 8 ways, 64 byte lines, dual-sectored
1174  case 0x7C:
1175  l2 = 1024;
1176  break; // code and data L2 cache, 1024 KB, 8 ways, 64 byte lines, dual-sectored
1177  case 0x7D:
1178  l2 = 2048;
1179  break; // code and data L2 cache, 2048 KB, 8 ways, 64 byte lines
1180  case 0x7E:
1181  l2 = 256;
1182  break; // code and data L2 cache, 256 KB, 8 ways, 128 byte lines, sect. (IA-64)
1183  case 0x7F:
1184  l2 = 512;
1185  break; // code and data L2 cache, 512 KB, 2 ways, 64 byte lines
1186  case 0x80:
1187  l2 = 512;
1188  break; // code and data L2 cache, 512 KB, 8 ways, 64 byte lines
1189  case 0x81:
1190  l2 = 128;
1191  break; // code and data L2 cache, 128 KB, 8 ways, 32 byte lines
1192  case 0x82:
1193  l2 = 256;
1194  break; // code and data L2 cache, 256 KB, 8 ways, 32 byte lines
1195  case 0x83:
1196  l2 = 512;
1197  break; // code and data L2 cache, 512 KB, 8 ways, 32 byte lines
1198  case 0x84:
1199  l2 = 1024;
1200  break; // code and data L2 cache, 1024 KB, 8 ways, 32 byte lines
1201  case 0x85:
1202  l2 = 2048;
1203  break; // code and data L2 cache, 2048 KB, 8 ways, 32 byte lines
1204  case 0x86:
1205  l2 = 512;
1206  break; // code and data L2 cache, 512 KB, 4 ways, 64 byte lines
1207  case 0x87:
1208  l2 = 1024;
1209  break; // code and data L2 cache, 1024 KB, 8 ways, 64 byte lines
1210  case 0x88:
1211  l3 = 2048;
1212  break; // code and data L3 cache, 2048 KB, 4 ways, 64 byte lines (IA-64)
1213  case 0x89:
1214  l3 = 4096;
1215  break; // code and data L3 cache, 4096 KB, 4 ways, 64 byte lines (IA-64)
1216  case 0x8A:
1217  l3 = 8192;
1218  break; // code and data L3 cache, 8192 KB, 4 ways, 64 byte lines (IA-64)
1219  case 0x8D:
1220  l3 = 3072;
1221  break; // code and data L3 cache, 3072 KB, 12 ways, 128 byte lines (IA-64)
1222 
1223  default:
1224  break;
1225  }
1226  }
1227  if (check_for_p2_core2 && l2 == l3) l3 = 0;
1228  l1 *= 1024;
1229  l2 *= 1024;
1230  l3 *= 1024;
1231 }
1232 
1233 inline void queryCacheSizes_intel(int& l1, int& l2, int& l3, int max_std_funcs) {
1234  if (max_std_funcs >= 4)
1235  queryCacheSizes_intel_direct(l1, l2, l3);
1236  else if (max_std_funcs >= 2)
1237  queryCacheSizes_intel_codes(l1, l2, l3);
1238  else
1239  l1 = l2 = l3 = 0;
1240 }
1241 
1242 inline void queryCacheSizes_amd(int& l1, int& l2, int& l3) {
1243  int abcd[4];
1244  abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
1245 
1246  // First query the max supported function.
1247  EIGEN_CPUID(abcd, 0x80000000, 0);
1248  if (static_cast<numext::uint32_t>(abcd[0]) >= static_cast<numext::uint32_t>(0x80000006)) {
1249  EIGEN_CPUID(abcd, 0x80000005, 0);
1250  l1 = (abcd[2] >> 24) * 1024; // C[31:24] = L1 size in KB
1251  abcd[0] = abcd[1] = abcd[2] = abcd[3] = 0;
1252  EIGEN_CPUID(abcd, 0x80000006, 0);
1253  l2 = (abcd[2] >> 16) * 1024; // C[31;16] = l2 cache size in KB
1254  l3 = ((abcd[3] & 0xFFFC000) >> 18) * 512 * 1024; // D[31;18] = l3 cache size in 512KB
1255  } else {
1256  l1 = l2 = l3 = 0;
1257  }
1258 }
1259 #endif
1260 
1263 inline void queryCacheSizes(int& l1, int& l2, int& l3) {
1264 #ifdef EIGEN_CPUID
1265  int abcd[4];
1266  const int GenuineIntel[] = {0x756e6547, 0x49656e69, 0x6c65746e};
1267  const int AuthenticAMD[] = {0x68747541, 0x69746e65, 0x444d4163};
1268  const int AMDisbetter_[] = {0x69444d41, 0x74656273, 0x21726574}; // "AMDisbetter!"
1269 
1270  // identify the CPU vendor
1271  EIGEN_CPUID(abcd, 0x0, 0);
1272  int max_std_funcs = abcd[0];
1273  if (cpuid_is_vendor(abcd, GenuineIntel))
1274  queryCacheSizes_intel(l1, l2, l3, max_std_funcs);
1275  else if (cpuid_is_vendor(abcd, AuthenticAMD) || cpuid_is_vendor(abcd, AMDisbetter_))
1276  queryCacheSizes_amd(l1, l2, l3);
1277  else
1278  // by default let's use Intel's API
1279  queryCacheSizes_intel(l1, l2, l3, max_std_funcs);
1280 
1281  // here is the list of other vendors:
1282  // ||cpuid_is_vendor(abcd,"VIA VIA VIA ")
1283  // ||cpuid_is_vendor(abcd,"CyrixInstead")
1284  // ||cpuid_is_vendor(abcd,"CentaurHauls")
1285  // ||cpuid_is_vendor(abcd,"GenuineTMx86")
1286  // ||cpuid_is_vendor(abcd,"TransmetaCPU")
1287  // ||cpuid_is_vendor(abcd,"RiseRiseRise")
1288  // ||cpuid_is_vendor(abcd,"Geode by NSC")
1289  // ||cpuid_is_vendor(abcd,"SiS SiS SiS ")
1290  // ||cpuid_is_vendor(abcd,"UMC UMC UMC ")
1291  // ||cpuid_is_vendor(abcd,"NexGenDriven")
1292 #else
1293  l1 = l2 = l3 = -1;
1294 #endif
1295 }
1296 
1299 inline int queryL1CacheSize() {
1300  int l1(-1), l2, l3;
1301  queryCacheSizes(l1, l2, l3);
1302  return l1;
1303 }
1304 
1308  int l1, l2(-1), l3(-1);
1309  queryCacheSizes(l1, l2, l3);
1310  return (std::max)(l2, l3);
1311 }
1312 
1317 #if EIGEN_COMP_CXXVER >= 20
1318 using std::construct_at;
1319 #else
1320 template <class T, class... Args>
1322  return ::new (const_cast<void*>(static_cast<const volatile void*>(p))) T(std::forward<Args>(args)...);
1323 }
1324 #endif
1325 
1331 #if EIGEN_COMP_CXXVER >= 17
1332 using std::destroy_at;
1333 #else
1334 template <class T>
1336  p->~T();
1337 }
1338 #endif
1339 
1340 } // end namespace internal
1341 
1342 } // end namespace Eigen
1343 
1344 #endif // EIGEN_MEMORY_H
int i
Definition: BiCGSTAB_step_by_step.cpp:9
#define EIGEN_DEFAULT_ALIGN_BYTES
Definition: ConfigureVectorization.h:169
Eigen::Triplet< double > T
Definition: EigenUnitTest.cpp:11
#define EIGEN_ALWAYS_INLINE
Definition: Macros.h:845
#define EIGEN_USING_STD(FUNC)
Definition: Macros.h:1090
#define EIGEN_CATCH(X)
Definition: Macros.h:1264
#define EIGEN_THROW
Definition: Macros.h:1261
#define eigen_internal_assert(x)
Definition: Macros.h:916
#define EIGEN_TRY
Definition: Macros.h:1263
#define EIGEN_UNUSED_VARIABLE(var)
Definition: Macros.h:966
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:892
#define eigen_assert(x)
Definition: Macros.h:910
#define EIGEN_STRONG_INLINE
Definition: Macros.h:834
int data[]
Definition: Map_placement_new.cpp:1
#define EIGEN_MALLOC_CHECK_THREAD_LOCAL
Definition: Memory.h:70
float * p
Definition: Tutorial_Map_using.cpp:9
int rows
Definition: Tutorial_commainit_02.cpp:1
int cols
Definition: Tutorial_commainit_02.cpp:1
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
Scalar * b
Definition: benchVecAdd.cpp:17
SCALAR Scalar
Definition: bench_gemm.cpp:45
STL compatible allocator to use with types requiring a non-standard alignment.
Definition: Memory.h:916
aligned_allocator(const aligned_allocator< U > &)
Definition: Memory.h:936
T * pointer
Definition: Memory.h:920
T & reference
Definition: Memory.h:922
pointer allocate(size_type num, const void *=0)
Definition: Memory.h:954
T value_type
Definition: Memory.h:924
std::size_t size_type
Definition: Memory.h:918
constexpr bool operator==(const aligned_allocator< U > &) const noexcept
Definition: Memory.h:939
const T & const_reference
Definition: Memory.h:923
std::ptrdiff_t difference_type
Definition: Memory.h:919
const T * const_pointer
Definition: Memory.h:921
aligned_allocator(const aligned_allocator &)=default
void deallocate(pointer p, size_type)
Definition: Memory.h:959
constexpr bool operator!=(const aligned_allocator< U > &) const noexcept
Definition: Memory.h:943
EIGEN_DEVICE_FUNC aligned_stack_memory_handler(T *ptr, std::size_t size, bool dealloc)
Definition: Memory.h:659
EIGEN_DEVICE_FUNC ~aligned_stack_memory_handler()
Definition: Memory.h:663
std::size_t m_size
Definition: Memory.h:670
bool m_deallocate
Definition: Memory.h:671
Definition: Meta.h:281
Definition: Memory.h:720
const T * ptr() const
Definition: Memory.h:729
~scoped_array()
Definition: Memory.h:725
T *& ptr()
Definition: Memory.h:728
scoped_array(std::ptrdiff_t size)
Definition: Memory.h:724
T * m_ptr
Definition: Memory.h:721
const T & operator[](std::ptrdiff_t i) const
Definition: Memory.h:727
T & operator[](std::ptrdiff_t i)
Definition: Memory.h:726
#define min(a, b)
Definition: datatypes.h:22
#define max(a, b)
Definition: datatypes.h:23
static constexpr lastp1_t end
Definition: IndexedViewHelper.h:79
EIGEN_BLAS_FUNC() copy(int *n, RealScalar *px, int *incx, RealScalar *py, int *incy)
Definition: level1_impl.h:32
const Scalar * a
Definition: level2_cplx_impl.h:32
EIGEN_DEVICE_FUNC void * conditional_aligned_realloc(void *ptr, std::size_t new_size, std::size_t old_size)
Definition: Memory.h:307
EIGEN_DEVICE_FUNC void * handmade_aligned_realloc(void *ptr, std::size_t new_size, std::size_t old_size, std::size_t alignment=EIGEN_DEFAULT_ALIGN_BYTES)
Reallocates aligned memory. Since we know that our handmade version is based on std::malloc we can us...
Definition: Memory.h:174
EIGEN_DEVICE_FUNC void * conditional_aligned_malloc< false >(std::size_t size)
Definition: Memory.h:280
EIGEN_DEVICE_FUNC void aligned_delete(T *ptr, std::size_t size)
Definition: Memory.h:430
EIGEN_DEVICE_FUNC T * conditional_aligned_new(std::size_t size)
Definition: Memory.h:415
EIGEN_DEVICE_FUNC void conditional_aligned_free(void *ptr)
Definition: Memory.h:293
void queryCacheSizes(int &l1, int &l2, int &l3)
Definition: Memory.h:1263
EIGEN_DEVICE_FUNC void conditional_aligned_delete(T *ptr, std::size_t size)
Definition: Memory.h:439
EIGEN_DEVICE_FUNC void * aligned_malloc(std::size_t size)
Definition: Memory.h:199
EIGEN_DEVICE_FUNC T * smart_move(T *start, T *end, T *target)
Definition: Memory.h:621
EIGEN_DEVICE_FUNC T * construct_at(T *p, Args &&... args)
Definition: Memory.h:1321
EIGEN_DEVICE_FUNC void conditional_aligned_delete_auto(T *ptr, std::size_t size)
Definition: Memory.h:503
Index first_multiple(Index size, Index base)
Definition: Memory.h:559
EIGEN_DEVICE_FUNC void destroy_at(T *p)
Definition: Memory.h:1335
EIGEN_DEVICE_FUNC T * default_construct_elements_of_array(T *ptr, std::size_t size)
Definition: Memory.h:344
EIGEN_DEVICE_FUNC void destruct_elements_of_array(T *ptr, std::size_t size)
Definition: Memory.h:334
EIGEN_DEVICE_FUNC void handmade_aligned_free(void *ptr)
Definition: Memory.h:158
EIGEN_DEVICE_FUNC void conditional_aligned_free< false >(void *ptr)
Definition: Memory.h:298
EIGEN_DEVICE_FUNC T * conditional_aligned_realloc_new(T *pts, std::size_t new_size, std::size_t old_size)
Definition: Memory.h:445
EIGEN_DEVICE_FUNC T * conditional_aligned_realloc_new_auto(T *pts, std::size_t new_size, std::size_t old_size)
Definition: Memory.h:491
EIGEN_DEVICE_FUNC T * aligned_new(std::size_t size)
Definition: Memory.h:403
static Index first_default_aligned(const DenseBase< Derived > &m)
Definition: DenseCoeffsBase.h:539
EIGEN_DEVICE_FUNC void * aligned_realloc(void *ptr, std::size_t new_size, std::size_t old_size)
Reallocates an aligned block of memory.
Definition: Memory.h:243
EIGEN_DEVICE_FUNC void throw_std_bad_alloc()
Definition: Memory.h:110
EIGEN_DEVICE_FUNC void smart_copy(const T *start, const T *end, T *target)
Definition: Memory.h:569
EIGEN_DEVICE_FUNC void * conditional_aligned_realloc< false >(void *ptr, std::size_t new_size, std::size_t old_size)
Definition: Memory.h:312
EIGEN_DEVICE_FUNC void check_that_malloc_is_allowed()
Definition: Memory.h:107
EIGEN_DEVICE_FUNC T * copy_construct_elements_of_array(T *ptr, const T *src, std::size_t size)
Definition: Memory.h:360
int queryTopLevelCacheSize()
Definition: Memory.h:1307
EIGEN_DEVICE_FUNC void * handmade_aligned_malloc(std::size_t size, std::size_t alignment=EIGEN_DEFAULT_ALIGN_BYTES)
Definition: Memory.h:142
void smart_memmove(const T *start, const T *end, T *target)
Definition: Memory.h:594
static Index first_aligned(const DenseBase< Derived > &m)
Definition: DenseCoeffsBase.h:533
EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE void check_size_for_overflow(std::size_t size)
Definition: Memory.h:393
int queryL1CacheSize()
Definition: Memory.h:1299
EIGEN_DEVICE_FUNC void * conditional_aligned_malloc(std::size_t size)
Definition: Memory.h:275
EIGEN_DEVICE_FUNC T * conditional_aligned_new_auto(std::size_t size)
Definition: Memory.h:476
EIGEN_DEVICE_FUNC void aligned_free(void *ptr)
Definition: Memory.h:224
EIGEN_DEVICE_FUNC T * move_construct_elements_of_array(T *ptr, T *src, std::size_t size)
Definition: Memory.h:376
void swap(scoped_array< T > &a, scoped_array< T > &b)
Definition: Memory.h:734
std::uint8_t uint8_t
Definition: Meta.h:36
std::uint32_t uint32_t
Definition: Meta.h:40
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:70
auto run(Kernel kernel, Args &&... args) -> decltype(kernel(args...))
Definition: gpu_test_helper.h:414
std::array< T, N > array
Definition: EmulateArray.h:231
squared absolute value
Definition: GlobalFunctions.h:87
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83
const int Dynamic
Definition: Constants.h:25
args
Definition: compute_granudrum_aor.py:143
type
Definition: compute_granudrum_aor.py:141
Definition: Eigen_Colamd.h:49
void start(const unsigned &i)
(Re-)start i-th timer
Definition: oomph_utilities.cc:243
@ RequireInitialization
Definition: NumTraits.h:177
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:217
Definition: Memory.h:927
aligned_allocator< U > other
Definition: Memory.h:928
Definition: XprHelper.h:533
std::conditional_t< Evaluate, PlainObject, typename ref_selector< T >::type > type
Definition: XprHelper.h:549
Definition: GenericPacketMath.h:108
static EIGEN_DEVICE_FUNC void run(const T *start, const T *end, T *target)
Definition: Memory.h:586
static EIGEN_DEVICE_FUNC void run(const T *start, const T *end, T *target)
Definition: Memory.h:575
Definition: Memory.h:566
static void run(const T *start, const T *end, T *target)
Definition: Memory.h:610
static void run(const T *start, const T *end, T *target)
Definition: Memory.h:600