ls1-MarDyn
ls1-MarDyn molecular dynamics code
AlignedAllocator.h
1
10#ifndef ALIGNEDALLOCATOR_H
11#define ALIGNEDALLOCATOR_H
12
13#ifdef __SSE3__
14#include <xmmintrin.h>
15#endif
16
17#include <limits>
18#include <stdlib.h>
19//#include <malloc.h>
20
21#define CACHE_LINE_SIZE 64
22
30template<typename T, size_t Alignment = CACHE_LINE_SIZE>
32
33 typedef T value_type;
34 typedef T* pointer;
35 typedef const T* const_pointer;
36 typedef T& reference;
37 typedef const T& const_reference;
38 typedef size_t size_type;
39
40 //dont know what rebind exactly does
41 //but makes the allocator compile with second template parameter
42 template<class U>
43 struct rebind {
45 };
46
50 AlignedAllocator() = default;
51
55 template<class U>
57 }
58
62 size_t max_size() const noexcept {
63 return (std::numeric_limits < size_t > ::max() - size_t(Alignment)) / sizeof(T);
64 }
65
70 T* allocate(std::size_t n) {
71 if (n <= max_size()) {
72#if defined(_SX)
73 T* ptr = static_cast<T*>(malloc(sizeof(T) * n));
74#elif defined(__SSE3__) && !defined(__PGI)
75 T* ptr = static_cast<T*>(_mm_malloc(sizeof(T) * n, Alignment));
76#else
77 T* ptr = static_cast<T*>(memalign(Alignment, sizeof(T) * n));
78 //T* ptr = static_cast<T*>(aligned_alloc(Alignment, sizeof(T) * n));
79 //T* ptr; posix_memalign(&ptr,Alignment, sizeof(T) * n);
80#endif
81 if (ptr == nullptr) {
82 throw std::bad_alloc();
83 }
84 return ptr;
85 }
86 throw std::bad_alloc();
87 }
88
92 void deallocate(T* ptr, std::size_t /*n*/) {
93#if defined(__SSE3__) && !defined(__PGI)
94 _mm_free(ptr);
95#else
96 free(ptr);
97#endif
98 }
99
103 template<class U, class ...Args>
104 void construct(U* p, Args&&... args) {
105 ::new ((void*) p) U(std::forward<Args>(args)...);
106 }
107
111 template<class U>
112 void destroy(U* p) {
113 p->~U();
114 }
115};
116
117template<typename T, size_t TAlignment, typename U, size_t UAlignment>
118inline bool operator ==(const AlignedAllocator<T, TAlignment>&,
120 return TAlignment == UAlignment;
121}
122
123template<typename T, size_t TAlignment, typename U, size_t UAlignment>
124inline bool operator !=(const AlignedAllocator<T, TAlignment>& a,
126 return !(a == b);
127}
128
129#endif /*ALIGNEDALLOCATOR_H*/
Definition: AlignedAllocator.h:43
A custom allocator to get aligned memory.
Definition: AlignedAllocator.h:31
AlignedAllocator()=default
Default constructor.
size_t max_size() const noexcept
Returns maximum possible value of n, with which we can call allocate(n)
Definition: AlignedAllocator.h:62
AlignedAllocator(const AlignedAllocator< U, Alignment > &)
Copy constructor.
Definition: AlignedAllocator.h:56
void construct(U *p, Args &&... args)
Construct object of type U at already allocated memory, pointed to by p.
Definition: AlignedAllocator.h:104
void destroy(U *p)
Destroy object pointed to by p, but does not deallocate the memory.
Definition: AlignedAllocator.h:112
T * allocate(std::size_t n)
Allocate aligned memory for n objects of type T.
Definition: AlignedAllocator.h:70
void deallocate(T *ptr, std::size_t)
Deallocate memory pointed to by ptr.
Definition: AlignedAllocator.h:92