Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-03 08:20:15

0001 /*
0002  * This file is part of KFParticle package
0003  * Copyright (C) 2007-2019 FIAS Frankfurt Institute for Advanced Studies
0004  *               2007-2019 Goethe University of Frankfurt
0005  *               2007-2019 Ivan Kisel <I.Kisel@compeng.uni-frankfurt.de>
0006  *               2007-2019 Maksym Zyzak
0007  *
0008  * KFParticle is free software: you can redistribute it and/or modify
0009  * it under the terms of the GNU General Public License as published by
0010  * the Free Software Foundation, either version 3 of the License, or
0011  * (at your option) any later version.
0012  *
0013  * KFParticle is distributed in the hope that it will be useful,
0014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0016  * GNU General Public License for more details.
0017  *
0018  * You should have received a copy of the GNU General Public License
0019  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
0020  */
0021 
0022 #ifndef KFPSimdAllocator_H
0023 #define KFPSimdAllocator_H
0024 
0025 #include <Vc/Vc>
0026 
0027 /** @class KFPSimdAllocator
0028  ** @brief Allocator which is needed to allocate memory in std::vector aligned by the size of SIMD vectors.
0029  ** @author  M.Zyzak, I.Kisel
0030  ** @date 05.02.2019
0031  ** @version 1.0
0032  **/
0033 
0034 template <class T>
0035 class KFPSimdAllocator {
0036  public:
0037  // type definitions
0038   typedef T        value_type;
0039   typedef T*       pointer;
0040   typedef const T* const_pointer;
0041   typedef T&       reference;
0042   typedef const T& const_reference;
0043   typedef std::size_t    size_type;
0044   typedef std::ptrdiff_t difference_type;
0045 
0046   /** @class rebind
0047    ** @brief Rebind allocator to type U of the SIMD allocator.
0048    ** @author  M.Zyzak, I.Kisel
0049    ** @date 05.02.2019
0050    ** @version 1.0
0051    **/
0052   template <class U>
0053   struct rebind {
0054     typedef KFPSimdAllocator<U> other;
0055   };
0056 
0057   /** Return address of "value". */
0058   pointer address (reference value) const {
0059     return &value;
0060   }
0061   /** Return address of "value". */
0062   const_pointer address (const_reference value) const {
0063     return &value;
0064   }
0065 
0066   /* constructors and destructor
0067         * - nothing to do because the allocator has no state
0068   */
0069   KFPSimdAllocator() throw() { }
0070   KFPSimdAllocator(const KFPSimdAllocator&) throw() {  }
0071   template <class U>
0072   KFPSimdAllocator (const KFPSimdAllocator<U>&) throw() {  }
0073   ~KFPSimdAllocator() throw() {  }
0074 
0075   /** Return maximum number of elements that can be allocated. */
0076   size_type max_size () const throw() {
0077     return std::numeric_limits<std::size_t>::max() / sizeof(T);
0078   }
0079 
0080   /** Allocate but don't initialize num elements of type T. */
0081   pointer allocate (size_type num, const void* = 0) {
0082 //               print message and allocate memory with global new
0083     pointer ret = reinterpret_cast<pointer>( /*T::*/operator new(num*sizeof(T)) );
0084     return ret;
0085   }
0086 
0087   /** Initialize elements of allocated storage "p" with an empty element. */
0088   void construct (pointer p) {
0089   // initialize memory with placement new
0090     new(p) T();
0091   }
0092   
0093   /** Initialize elements of allocated storage "p" with value "value". */
0094   void construct (pointer p, const T& value) {
0095     new(p) T(value);
0096   }
0097 
0098   /** Destroy elements of initialized storage "p". */
0099   void destroy (pointer p) {
0100   // destroy objects by calling their destructor
0101     p->~T();
0102   }
0103 
0104   /** Deallocate storage p of deleted elements. */
0105   void deallocate (pointer p, size_type num) {
0106   // print message and deallocate memory with global delete
0107     /*T::*/operator delete(static_cast<void*>(p), num*sizeof(T));
0108 
0109   }
0110 
0111   void *operator new(size_t size, void *ptr) { return ::operator new(size, ptr);}      ///< new operator for allocation of the SIMD-alligned dynamic memory allocation
0112   void *operator new[](size_t size, void *ptr) { return ::operator new(size, ptr);}    ///< new operator for allocation of the SIMD-alligned dynamic memory allocation
0113   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
0114   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
0115   void operator delete(void *ptr, size_t) { _mm_free(ptr); }                           ///< delete operator for the SIMD-alligned dynamic memory release
0116   void operator delete[](void *ptr, size_t) { _mm_free(ptr); }                         ///< delete operator for the SIMD-alligned dynamic memory release
0117 }; // KFPSimdAllocator
0118       
0119 #endif //KFPSimdAllocator