42 #ifndef STOKHOS_MEMORY_TRAITS_HPP 43 #define STOKHOS_MEMORY_TRAITS_HPP 47 #include "Kokkos_Core_fwd.hpp" 50 #define STOKHOS_ALIGN_MEMORY 1 55 template <
typename MemorySpace>
60 KOKKOS_INLINE_FUNCTION
63 static void*
alloc(
const size_t size) {
return operator new(size); }
66 KOKKOS_INLINE_FUNCTION
67 static void free(
void *ptr) {
operator delete(ptr); }
75 #if STOKHOS_ALIGN_MEMORY 78 #elif defined(__AVX__) 80 #elif defined(__SSE2__) 98 KOKKOS_INLINE_FUNCTION
99 static void*
alloc(
const size_t size) {
102 #if STOKHOS_ALIGN_MEMORY 104 const size_t total_size = size + mask +
sizeof(std::ptrdiff_t);
105 char *ptr_alloc =
reinterpret_cast<char*
>(std::malloc(total_size));
106 char *ptr_storage = ptr_alloc +
sizeof(std::ptrdiff_t);
107 char *ptr_body =
reinterpret_cast<char*
>(
108 (
reinterpret_cast<size_t>(ptr_storage) + mask ) & ~mask );
109 char *ptr_header = ptr_body -
sizeof(std::ptrdiff_t);
110 const std::ptrdiff_t offset = ptr_body - ptr_alloc;
111 *
reinterpret_cast<std::ptrdiff_t*
>(ptr_header) = offset;
112 ptr =
reinterpret_cast<void*
>(ptr_body);
114 ptr =
operator new(size);
121 KOKKOS_INLINE_FUNCTION
124 #if STOKHOS_ALIGN_MEMORY 125 void *ptr_header =
reinterpret_cast<char*
>(ptr) -
sizeof(std::ptrdiff_t);
126 const std::ptrdiff_t offset = *
reinterpret_cast<std::ptrdiff_t*
>(ptr_header);
127 void *ptr_alloc =
reinterpret_cast<char*
>(ptr) - offset;
128 std::free(ptr_alloc);
130 operator delete(ptr);
137 template <
typename T>
168 if (ptr == 0)
throw std::bad_alloc();
169 return reinterpret_cast<pointer>(ptr);
180 template <
typename T>
209 if (ptr == 0)
throw std::bad_alloc();
210 return reinterpret_cast<pointer>(ptr);
220 template <
typename T,
typename U>
225 template <
typename T,
typename U>
232 #endif // STOKHOS_MEMORY_TRAITS_HPP static KOKKOS_INLINE_FUNCTION void free(void *ptr)
Free memory allocated by alloc()
An aligned STL allocator.
Specialization of MemoryTraits for host memory spaces.
pointer allocate(size_type n, const void *=0)
const_pointer address(const_reference x) const
bool operator==(const aligned_allocator< T > &, const aligned_allocator< U > &)
static KOKKOS_INLINE_FUNCTION void * alloc(const size_t size)
Allocate aligned memory of given size.
size_type max_size() const
static KOKKOS_INLINE_FUNCTION void free(void *ptr)
Free memory allocated by alloc()
const T & const_reference
const IndexType const IndexType const IndexType const IndexType const ValueType const ValueType * x
size_type max_size() const
const T & const_reference
Stokhos::MemoryTraits< Kokkos::HostSpace > Traits
const_pointer address(const_reference x) const
Top-level namespace for Stokhos classes and functions.
Traits class encapsulting memory alignment.
void deallocate(pointer p, size_type)
pointer allocate(size_type n, const void *=0)
aligned_allocator< U > other
static const unsigned Alignment
Bytes to which memory allocations are aligned.
void deallocate(pointer p, size_type)
std::ptrdiff_t difference_type
pointer address(reference x) const
aligned_allocator< U > other
void construct(pointer p, const_reference val)
void construct(pointer p, const_reference val)
Stokhos::MemoryTraits< Kokkos::HostSpace > Traits
static KOKKOS_INLINE_FUNCTION void * alloc(const size_t size)
Allocate aligned memory.
aligned_allocator(const aligned_allocator< U > &)
static const unsigned Alignment
Bytes to which memory allocations are aligned.
std::ptrdiff_t difference_type
aligned_allocator(const aligned_allocator< U > &)
bool operator!=(const aligned_allocator< T > &, const aligned_allocator< U > &)