KFPSimdAllocator.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //----------------------------------------------------------------------------
  2. // Allocator for SIMDised KF Particle
  3. // .
  4. // @author I.Kisel, I.Kulakov, M.Zyzak
  5. // @version 1.0
  6. // @since 20.08.13
  7. //
  8. //
  9. // -= Copyright &copy ALICE HLT and CBM L1 Groups =-
  10. //____________________________________________________________________________
  11. #ifndef KFPSimdAllocator_H
  12. #define KFPSimdAllocator_H
  13. #include <Vc/Vc>
  14. /** @class KFPSimdAllocator
  15. ** @brief Allocator which is needed to allocate memory in std::vector aligned by the size of SIMD vectors.
  16. ** @author M.Zyzak, I.Kisel
  17. ** @date 05.02.2019
  18. ** @version 1.0
  19. **/
  20. template <class T>
  21. class KFPSimdAllocator {
  22. public:
  23. // type definitions
  24. typedef T value_type;
  25. typedef T* pointer;
  26. typedef const T* const_pointer;
  27. typedef T& reference;
  28. typedef const T& const_reference;
  29. typedef std::size_t size_type;
  30. typedef std::ptrdiff_t difference_type;
  31. /** @class rebind
  32. ** @brief Rebind allocator to type U of the SIMD allocator.
  33. ** @author M.Zyzak, I.Kisel
  34. ** @date 05.02.2019
  35. ** @version 1.0
  36. **/
  37. template <class U>
  38. struct rebind {
  39. typedef KFPSimdAllocator<U> other;
  40. };
  41. /** Return address of "value". */
  42. pointer address (reference value) const {
  43. return &value;
  44. }
  45. /** Return address of "value". */
  46. const_pointer address (const_reference value) const {
  47. return &value;
  48. }
  49. /* constructors and destructor
  50. * - nothing to do because the allocator has no state
  51. */
  52. KFPSimdAllocator() throw() { }
  53. KFPSimdAllocator(const KFPSimdAllocator&) throw() { }
  54. template <class U>
  55. KFPSimdAllocator (const KFPSimdAllocator<U>&) throw() { }
  56. ~KFPSimdAllocator() throw() { }
  57. /** Return maximum number of elements that can be allocated. */
  58. size_type max_size () const throw() {
  59. return std::numeric_limits<std::size_t>::max() / sizeof(T);
  60. }
  61. /** Allocate but don't initialize num elements of type T. */
  62. pointer allocate (size_type num, const void* = 0) {
  63. // print message and allocate memory with global new
  64. pointer ret = reinterpret_cast<pointer>( /*T::*/operator new(num*sizeof(T)) );
  65. return ret;
  66. }
  67. /** Initialize elements of allocated storage "p" with an empty element. */
  68. void construct (pointer p) {
  69. // initialize memory with placement new
  70. new(p) T();
  71. }
  72. /** Initialize elements of allocated storage "p" with value "value". */
  73. void construct (pointer p, const T& value) {
  74. new(p) T(value);
  75. }
  76. /** Destroy elements of initialized storage "p". */
  77. void destroy (pointer p) {
  78. // destroy objects by calling their destructor
  79. p->~T();
  80. }
  81. /** Deallocate storage p of deleted elements. */
  82. void deallocate (pointer p, size_type num) {
  83. // print message and deallocate memory with global delete
  84. /*T::*/operator delete(static_cast<void*>(p), num*sizeof(T));
  85. }
  86. void *operator new(size_t size, void *ptr) { return ::operator new(size, ptr);} ///< new operator for allocation of the SIMD-alligned dynamic memory allocation
  87. void *operator new[](size_t size, void *ptr) { return ::operator new(size, ptr);} ///< new operator for allocation of the SIMD-alligned dynamic memory allocation
  88. void *operator new(size_t size) { return _mm_malloc(size, sizeof(Vc::float_v)); } ///< new operator for allocation of the SIMD-alligned dynamic memory allocation
  89. void *operator new[](size_t size) { return _mm_malloc(size, sizeof(Vc::float_v)); } ///< new operator for allocation of the SIMD-alligned dynamic memory allocation
  90. void operator delete(void *ptr, size_t) { _mm_free(ptr); } ///< delete operator for the SIMD-alligned dynamic memory release
  91. void operator delete[](void *ptr, size_t) { _mm_free(ptr); } ///< delete operator for the SIMD-alligned dynamic memory release
  92. }; // KFPSimdAllocator
  93. #endif //KFPSimdAllocator