Grid 0.7.0
AlignedAllocator.h
Go to the documentation of this file.
1/*************************************************************************************
2
3 Grid physics library, www.github.com/paboyle/Grid
4
5 Source file: ./lib/AlignedAllocator.h
6
7 Copyright (C) 2015
8
9Author: Azusa Yamaguchi <ayamaguc@staffmail.ed.ac.uk>
10Author: Peter Boyle <paboyle@ph.ed.ac.uk>
11
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License along
23 with this program; if not, write to the Free Software Foundation, Inc.,
24 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25
26 See the full license in the file "LICENSE" in the top level distribution directory
27*************************************************************************************/
28/* END LEGAL */
29#pragma once
30
32
33template<typename _Tp>
35public:
36 typedef std::size_t size_type;
37 typedef std::ptrdiff_t difference_type;
38 typedef _Tp* pointer;
39 typedef const _Tp* const_pointer;
40 typedef _Tp& reference;
41 typedef const _Tp& const_reference;
42 typedef _Tp value_type;
43
44 template<typename _Tp1> struct rebind { typedef alignedAllocator<_Tp1> other; };
45 alignedAllocator() throw() { }
47 template<typename _Tp1> alignedAllocator(const alignedAllocator<_Tp1>&) throw() { }
48 ~alignedAllocator() throw() { }
49 pointer address(reference __x) const { return &__x; }
50 size_type max_size() const throw() { return size_t(-1) / sizeof(_Tp); }
51
52 pointer allocate(size_type __n, const void* _p= 0)
53 {
54 size_type bytes = __n*sizeof(_Tp);
55 profilerAllocate(bytes);
56 _Tp *ptr = (_Tp*) MemoryManager::CpuAllocate(bytes);
57 if ( (_Tp*)ptr == (_Tp *) NULL ) {
58 printf("Grid CPU Allocator got NULL for %lu bytes\n",(unsigned long) bytes );
59 }
60 assert( ( (_Tp*)ptr != (_Tp *)NULL ) );
61 return ptr;
62 }
63
65 {
66 size_type bytes = __n * sizeof(_Tp);
67 profilerFree(bytes);
68 MemoryManager::CpuFree((void *)__p,bytes);
69 }
70
71 // FIXME: hack for the copy constructor: it must be avoided to avoid single thread loop
72 void construct(pointer __p, const _Tp& __val) { };
73 void construct(pointer __p) { };
74 void destroy(pointer __p) { };
75};
76template<typename _Tp> inline bool operator==(const alignedAllocator<_Tp>&, const alignedAllocator<_Tp>&){ return true; }
77template<typename _Tp> inline bool operator!=(const alignedAllocator<_Tp>&, const alignedAllocator<_Tp>&){ return false; }
78
80// Unified virtual memory
82template<typename _Tp>
84public:
85 typedef std::size_t size_type;
86 typedef std::ptrdiff_t difference_type;
87 typedef _Tp* pointer;
88 typedef const _Tp* const_pointer;
89 typedef _Tp& reference;
90 typedef const _Tp& const_reference;
91 typedef _Tp value_type;
92
93 template<typename _Tp1> struct rebind { typedef uvmAllocator<_Tp1> other; };
94 uvmAllocator() throw() { }
95 uvmAllocator(const uvmAllocator&) throw() { }
96 template<typename _Tp1> uvmAllocator(const uvmAllocator<_Tp1>&) throw() { }
97 ~uvmAllocator() throw() { }
98 pointer address(reference __x) const { return &__x; }
99 size_type max_size() const throw() { return size_t(-1) / sizeof(_Tp); }
100
101 pointer allocate(size_type __n, const void* _p= 0)
102 {
103 size_type bytes = __n*sizeof(_Tp);
104 profilerAllocate(bytes);
105 _Tp *ptr = (_Tp*) MemoryManager::SharedAllocate(bytes);
106 if ( (_Tp*)ptr == (_Tp *) NULL ) {
107 printf("Grid Shared Allocator got NULL for %lu bytes\n",(unsigned long) bytes );
108 }
109 assert( ( (_Tp*)ptr != (_Tp *)NULL ) );
110 return ptr;
111 }
112
114 {
115 size_type bytes = __n * sizeof(_Tp);
116 profilerFree(bytes);
117 MemoryManager::SharedFree((void *)__p,bytes);
118 }
119
120 void construct(pointer __p, const _Tp& __val) { new((void *)__p) _Tp(__val); };
121 void construct(pointer __p) { };
122 void destroy(pointer __p) { };
123};
124template<typename _Tp> inline bool operator==(const uvmAllocator<_Tp>&, const uvmAllocator<_Tp>&){ return true; }
125template<typename _Tp> inline bool operator!=(const uvmAllocator<_Tp>&, const uvmAllocator<_Tp>&){ return false; }
126
128// Device memory
130template<typename _Tp>
132public:
133 typedef std::size_t size_type;
134 typedef std::ptrdiff_t difference_type;
135 typedef _Tp* pointer;
136 typedef const _Tp* const_pointer;
137 typedef _Tp& reference;
138 typedef const _Tp& const_reference;
139 typedef _Tp value_type;
140
141 template<typename _Tp1> struct rebind { typedef devAllocator<_Tp1> other; };
142 devAllocator() throw() { }
143 devAllocator(const devAllocator&) throw() { }
144 template<typename _Tp1> devAllocator(const devAllocator<_Tp1>&) throw() { }
145 ~devAllocator() throw() { }
146 pointer address(reference __x) const { return &__x; }
147 size_type max_size() const throw() { return size_t(-1) / sizeof(_Tp); }
148
149 pointer allocate(size_type __n, const void* _p= 0)
150 {
151 size_type bytes = __n*sizeof(_Tp);
152 profilerAllocate(bytes);
153 _Tp *ptr = (_Tp*) MemoryManager::AcceleratorAllocate(bytes);
154 if ( (_Tp*)ptr == (_Tp *) NULL ) {
155 printf("Grid Device Allocator got NULL for %lu bytes\n",(unsigned long) bytes );
156 }
157 assert( ( (_Tp*)ptr != (_Tp *)NULL ) );
158 return ptr;
159 }
160
162 {
163 size_type bytes = __n * sizeof(_Tp);
164 profilerFree(bytes);
165 MemoryManager::AcceleratorFree((void *)__p,bytes);
166 }
167 void construct(pointer __p, const _Tp& __val) { };
168 void construct(pointer __p) { };
169 void destroy(pointer __p) { };
170};
171template<typename _Tp> inline bool operator==(const devAllocator<_Tp>&, const devAllocator<_Tp>&){ return true; }
172template<typename _Tp> inline bool operator!=(const devAllocator<_Tp>&, const devAllocator<_Tp>&){ return false; }
173
175// Template typedefs
177template<class T> using hostVector = std::vector<T,alignedAllocator<T> >; // Needs autoview
178template<class T> using Vector = std::vector<T,uvmAllocator<T> >; // Really want to deprecate
179template<class T> using uvmVector = std::vector<T,uvmAllocator<T> >; // auto migrating page
180template<class T> using deviceVector = std::vector<T,devAllocator<T> >; // device vector
181
182/*
183template<class T> class vecView
184{
185 protected:
186 T * data;
187 uint64_t size;
188 ViewMode mode;
189 void * cpu_ptr;
190 public:
191 // Rvalue accessor
192 accelerator_inline T & operator[](size_t i) const { return this->data[i]; };
193 vecView(Vector<T> &refer_to_me,ViewMode _mode)
194 {
195 cpu_ptr = &refer_to_me[0];
196 size = refer_to_me.size();
197 mode = _mode;
198 data =(T *) MemoryManager::ViewOpen(cpu_ptr,
199 size*sizeof(T),
200 mode,
201 AdviseDefault);
202 }
203 void ViewClose(void)
204 { // Inform the manager
205 MemoryManager::ViewClose(this->cpu_ptr,this->mode);
206 }
207};
208
209template<class T> vecView<T> VectorView(Vector<T> &vec,ViewMode _mode)
210{
211 vecView<T> ret(vec,_mode); // does the open
212 return ret; // must be closed
213}
214
215#define autoVecView(v_v,v,mode) \
216 auto v_v = VectorView(v,mode); \
217 ViewCloser<decltype(v_v)> _autoView##v_v(v_v);
218*/
219
221
222
std::vector< T, devAllocator< T > > deviceVector
bool operator==(const alignedAllocator< _Tp > &, const alignedAllocator< _Tp > &)
std::vector< T, uvmAllocator< T > > Vector
std::vector< T, uvmAllocator< T > > uvmVector
std::vector< T, alignedAllocator< T > > hostVector
bool operator!=(const alignedAllocator< _Tp > &, const alignedAllocator< _Tp > &)
#define profilerAllocate(bytes)
Definition MemoryStats.h:65
#define profilerFree(bytes)
Definition MemoryStats.h:79
#define NAMESPACE_BEGIN(A)
Definition Namespace.h:35
#define NAMESPACE_END(A)
Definition Namespace.h:36
static void CpuFree(void *ptr, size_t bytes)
static void * CpuAllocate(size_t bytes)
static void SharedFree(void *ptr, size_t bytes)
static void * SharedAllocate(size_t bytes)
static void * AcceleratorAllocate(size_t bytes)
static void AcceleratorFree(void *ptr, size_t bytes)
size_type max_size() const
pointer address(reference __x) const
void construct(pointer __p)
std::ptrdiff_t difference_type
void construct(pointer __p, const _Tp &__val)
void deallocate(pointer __p, size_type __n)
const _Tp * const_pointer
std::size_t size_type
const _Tp & const_reference
pointer allocate(size_type __n, const void *_p=0)
alignedAllocator(const alignedAllocator &)
void destroy(pointer __p)
alignedAllocator(const alignedAllocator< _Tp1 > &)
void destroy(pointer __p)
const _Tp * const_pointer
std::size_t size_type
devAllocator(const devAllocator< _Tp1 > &)
pointer address(reference __x) const
const _Tp & const_reference
size_type max_size() const
pointer allocate(size_type __n, const void *_p=0)
void construct(pointer __p)
void construct(pointer __p, const _Tp &__val)
devAllocator(const devAllocator &)
std::ptrdiff_t difference_type
void deallocate(pointer __p, size_type __n)
void construct(pointer __p)
pointer address(reference __x) const
std::size_t size_type
uvmAllocator(const uvmAllocator< _Tp1 > &)
void destroy(pointer __p)
const _Tp * const_pointer
void construct(pointer __p, const _Tp &__val)
uvmAllocator(const uvmAllocator &)
const _Tp & const_reference
pointer allocate(size_type __n, const void *_p=0)
std::ptrdiff_t difference_type
size_type max_size() const
void deallocate(pointer __p, size_type __n)
alignedAllocator< _Tp1 > other
devAllocator< _Tp1 > other
uvmAllocator< _Tp1 > other