Extended CUDA Library (ecuda)  2.0
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros
allocators.hpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2014-2016, Scott Zuyderduyn
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7 
8 1. Redistributions of source code must retain the above copyright notice, this
9  list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright notice,
11  this list of conditions and the following disclaimer in the documentation
12  and/or other materials provided with the distribution.
13 
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 
25 The views and conclusions contained in the software and documentation are those
26 of the authors and should not be interpreted as representing official policies,
27 either expressed or implied, of the FreeBSD Project.
28 */
29 
30 //----------------------------------------------------------------------------
31 // allocators.hpp
32 //
33 // STL-compatible memory allocators using CUDA memory allocation routines.
34 //
35 // Author: Scott D. Zuyderduyn, Ph.D. (scott.zuyderduyn@utoronto.ca)
36 //----------------------------------------------------------------------------
37 
38 #pragma once
39 #ifndef ECUDA_ALLOCATORS_HPP
40 #define ECUDA_ALLOCATORS_HPP
41 
42 #include <limits>
43 #include <stdexcept>
44 
45 #include "global.hpp"
46 #include "type_traits.hpp"
47 #include "ptr/common.hpp"
48 #include "ptr/padded_ptr.hpp"
49 
50 namespace ecuda {
51 
81 template<typename T,unsigned Flags=cudaHostAllocDefault>
83 {
84 
85 public:
86  typedef T value_type;
87  typedef typename ecuda::add_pointer<T>::type pointer;
88  typedef typename ecuda::add_lvalue_reference<T>::type reference;
89  typedef typename make_const<pointer>::type const_pointer;
90  typedef typename ecuda::add_lvalue_reference<const T>::type const_reference;
91  typedef std::size_t size_type;
92  typedef std::ptrdiff_t difference_type;
93  template<typename U> struct rebind { typedef host_allocator<U> other; };
95 
97 public:
101  host_allocator() throw() {}
102 
107  host_allocator( const host_allocator& alloc ) throw() {}
108 
113  template<typename U>
114  host_allocator( const host_allocator<U>& alloc ) throw() {}
115 
119  ~host_allocator() throw() {}
120 
129  inline pointer address( reference x ) { return &x; }
130 
139  inline const_pointer address( const_reference x ) const { return &x; }
140 
161  pointer allocate( size_type n, std::allocator<void>::const_pointer hint = 0 )
162  {
163  pointer ptr = NULL;
164  const cudaError_t result = cudaHostAlloc( reinterpret_cast<void**>(&ptr), n*sizeof(T), Flags );
165  if( result != cudaSuccess ) throw std::bad_alloc();
166  return ptr;
167  }
168 
180  inline void deallocate( pointer ptr, size_type )
181  {
182  typedef typename ecuda::add_pointer<value_type>::type raw_pointer_type;
183  default_host_delete<value_type>()( naked_cast<raw_pointer_type>(ptr) );
184  }
185 
194  inline size_type max_size() const throw() { return std::numeric_limits<size_type>::max(); }
195 
203  inline void construct( pointer ptr, const_reference val ) { new ((void*)ptr) value_type (val); }
204 
210  inline void destroy( pointer ptr ) { ptr->~value_type(); }
211 
212 };
213 
226 template<typename T>
228 {
229 
230 public:
231  typedef T value_type;
232  typedef typename ecuda::add_pointer<T>::type pointer;
233  typedef typename ecuda::add_lvalue_reference<T>::type reference;
234  typedef typename make_const<pointer>::type const_pointer;
235  typedef typename ecuda::add_lvalue_reference<const T>::type const_reference;
236  typedef std::size_t size_type;
237  typedef std::ptrdiff_t difference_type;
238  template<typename U> struct rebind { typedef device_allocator<U> other; };
240 
242 public:
247 
253 
258  template<typename U>
260 
265 
274  __HOST__ __DEVICE__ inline pointer address( reference x ) { return &x; }
275 
284  __HOST__ __DEVICE__ inline const_pointer address( const_reference x ) const { return &x; }
285 
306  __HOST__ pointer allocate( size_type n, std::allocator<void>::const_pointer hint = 0 )
307  {
308  pointer ptr = NULL;
309  const cudaError_t result = cudaMalloc( reinterpret_cast<void**>(&ptr), n*sizeof(T) );
310  if( result != cudaSuccess ) throw std::bad_alloc();
311  return ptr;
312  }
313 
325  __HOST__ inline void deallocate( pointer ptr, size_type )
326  {
327  typedef typename ecuda::add_pointer<value_type>::type raw_pointer_type;
328  default_device_delete<value_type>()( naked_cast<raw_pointer_type>(ptr) );
329  }
330 
340 
348  __DEVICE__ inline void construct( pointer ptr, const_reference val ); // not supported on device
349 
355  __DEVICE__ inline void destroy( pointer ptr ); // not supported on device
356 
357 };
358 
378 template<typename T>
380 {
381 
382 public:
383  typedef T value_type;
385  typedef typename ecuda::add_lvalue_reference<T>::type reference;
386  typedef typename make_const<pointer>::type const_pointer;
387  typedef typename ecuda::add_lvalue_reference<const T>::type const_reference;
388  typedef std::size_t size_type;
389  typedef std::ptrdiff_t difference_type;
390  template<typename U> struct rebind { typedef device_allocator<U> other; };
392 
394 private:
395  template<typename U> struct char_cast;
396  template<typename U> struct char_cast<U*> { char* type; };
397  template<typename U> struct char_cast<const U*> { const char* type; };
398 
399 public:
404 
410 
415  template<typename U>
417 
422 
431  __HOST__ __DEVICE__ inline pointer address( reference x ) { return &x; }
432 
441  __HOST__ __DEVICE__ inline const_pointer address( const_reference x ) const { return &x; }
442 
464  __HOST__ pointer allocate( size_type w, size_type h, std::allocator<void>::const_pointer hint = 0 )
465  {
466  typename ecuda::add_pointer<value_type>::type ptr = NULL;
467  size_type pitch;
468  const cudaError_t result = cudaMallocPitch( reinterpret_cast<void**>(&ptr), &pitch, w*sizeof(value_type), h );
469  if( result != cudaSuccess ) throw std::bad_alloc();
470  return pointer( ptr, pitch );
471  }
472 
484  __HOST__ inline void deallocate( pointer ptr, size_type )
485  {
486  typedef typename ecuda::add_pointer<value_type>::type raw_pointer_type;
487  default_device_delete<value_type>()( naked_cast<raw_pointer_type>(ptr) );
488  }
489 
499 
507  __DEVICE__ inline void construct( pointer ptr, const_reference val ); // not supported on device
508 
514  __DEVICE__ inline void destroy( pointer ptr ); // not supported on device
515 
529  {
530  return reinterpret_cast<const_pointer>( naked_cast<const char*>(ptr) + x*pitch + y*sizeof(value_type) );
531  }
532 
545  {
546  // TODO: this is not general if this is padded_ptr<T,[some other specialized class]>
547  typedef typename ecuda::add_pointer<value_type>::type raw_pointer;
548  raw_pointer p = naked_cast<raw_pointer>(ptr);
549  typedef typename char_cast<raw_pointer>::type char_pointer;
550  char_pointer p2 = reinterpret_cast<char_pointer>(p);
551  p2 += ptr.get_pitch() * x;
552  p = p2;
553  p += y;
554  return pointer( p, ptr.get_pitch() );
555  }
556 
557 };
558 
559 } // namespace ecuda
560 
561 #endif
T value_type
element type
Definition: allocators.hpp:86
std::ptrdiff_t difference_type
Definition: allocators.hpp:92
ecuda::add_lvalue_reference< const T >::type const_reference
reference to constant element
Definition: allocators.hpp:387
void construct(pointer ptr, const_reference val)
Constructs an element object on the location pointed by ptr.
Definition: allocators.hpp:203
ecuda::add_pointer< T >::type pointer
pointer to element
Definition: allocators.hpp:87
__HOST__ pointer allocate(size_type n, std::allocator< void >::const_pointer hint=0)
Allocate block of storage.
Definition: allocators.hpp:306
__HOST__ __DEVICE__ device_allocator(const device_allocator< U > &alloc)
Constructs a device allocator object from another device allocator object with a different element ty...
Definition: allocators.hpp:259
pointer address(reference x)
Returns the address of x.
Definition: allocators.hpp:129
The default destruction policy used by smart pointers to device memory.
Definition: common.hpp:64
padded_ptr< T, typename ecuda::add_pointer< T >::type > pointer
pointer to element
Definition: allocators.hpp:384
std::ptrdiff_t difference_type
Definition: allocators.hpp:237
__HOST__ __DEVICE__ device_allocator(const device_allocator &alloc)
Constructs a device allocator object from another device allocator object.
Definition: allocators.hpp:252
void deallocate(pointer ptr, size_type)
Releases a block of storage previously allocated with member allocate and not yet released...
Definition: allocators.hpp:180
ecuda::add_pointer< T >::type pointer
pointer to element
Definition: allocators.hpp:232
host_allocator()
Constructs a host allocator object.
Definition: allocators.hpp:101
__HOST__ __DEVICE__ device_pitch_allocator()
Constructs a device pitched memory allocator object.
Definition: allocators.hpp:403
The default destruction policy used by smart pointers to page-locked host memory. ...
Definition: common.hpp:104
__HOST__ __DEVICE__ ~device_pitch_allocator()
Destructs the device pitched memory allocator object.
Definition: allocators.hpp:421
pointer allocate(size_type n, std::allocator< void >::const_pointer hint=0)
Allocate block of storage.
Definition: allocators.hpp:161
std::size_t size_type
quantities of elements
Definition: allocators.hpp:91
~host_allocator()
Destructs the host allocator object.
Definition: allocators.hpp:119
__HOST__ __DEVICE__ size_type max_size() const
Returns the maximum number of elements, each of member type value_type (an alias of allocator's templ...
Definition: allocators.hpp:339
__HOST__ __DEVICE__ pointer address(pointer ptr, size_type x, size_type y)
Returns the address of a given coordinate.
Definition: allocators.hpp:544
__DEVICE__ void construct(pointer ptr, const_reference val)
Constructs an element object on the location pointed by ptr.
void destroy(pointer ptr)
Destroys in-place the object pointed by ptr. Notice that this does not deallocate the storage for the...
Definition: allocators.hpp:210
__HOST__ __DEVICE__ const_pointer address(const_reference x) const
Returns the address of x.
Definition: allocators.hpp:284
__HOST__ void deallocate(pointer ptr, size_type)
Releases a block of storage previously allocated with member allocate and not yet released...
Definition: allocators.hpp:484
__HOST__ pointer allocate(size_type w, size_type h, std::allocator< void >::const_pointer hint=0)
Allocate block of storage.
Definition: allocators.hpp:464
__HOST__ __DEVICE__ const_pointer address(const_pointer ptr, size_type x, size_type y, size_type pitch) const
Returns the address of a given coordinate.
Definition: allocators.hpp:528
#define __HOST__
Definition: global.hpp:150
A specialized pointer to padded memory.
Definition: iterator.hpp:52
ecuda::add_lvalue_reference< const T >::type const_reference
reference to constant element
Definition: allocators.hpp:90
Allocator for hardware aligned device memory.
Definition: allocators.hpp:379
__DEVICE__ void destroy(pointer ptr)
Destroys in-place the object pointed by ptr. Notice that this does not deallocate the storage for the...
ecuda::add_lvalue_reference< T >::type reference
reference to element
Definition: allocators.hpp:385
size_type max_size() const
Returns the maximum number of elements, each of member type value_type (an alias of allocator's templ...
Definition: allocators.hpp:194
__HOST__ void deallocate(pointer ptr, size_type)
Releases a block of storage previously allocated with member allocate and not yet released...
Definition: allocators.hpp:325
Allocator for device memory.
Definition: allocators.hpp:227
std::size_t size_type
quantities of elements
Definition: allocators.hpp:388
const_pointer address(const_reference x) const
Returns the address of x.
Definition: allocators.hpp:139
ecuda::add_lvalue_reference< T >::type reference
reference to element
Definition: allocators.hpp:233
Allocator for page-locked host memory.
Definition: allocators.hpp:82
make_const< pointer >::type const_pointer
pointer to constant element
Definition: allocators.hpp:89
host_allocator(const host_allocator< U > &alloc)
Constructs a host allocator object from another host allocator object with a different element type...
Definition: allocators.hpp:114
__HOST__ __DEVICE__ device_allocator()
Constructs a device allocator object.
Definition: allocators.hpp:246
ecuda::add_lvalue_reference< T >::type reference
reference to element
Definition: allocators.hpp:88
__HOST__ __DEVICE__ device_pitch_allocator(const device_pitch_allocator &alloc)
Constructs a device pitched memory allocator object from another host allocator object.
Definition: allocators.hpp:409
__HOST__ __DEVICE__ size_type get_pitch() const
Definition: padded_ptr.hpp:125
__DEVICE__ void destroy(pointer ptr)
Destroys in-place the object pointed by ptr. Notice that this does not deallocate the storage for the...
__HOST__ __DEVICE__ const_pointer address(const_reference x) const
Returns the address of x.
Definition: allocators.hpp:441
__HOST__ __DEVICE__ device_pitch_allocator(const device_pitch_allocator< U > &alloc)
Constructs a device pitched memory allocator object from another device pitched memory allocator obje...
Definition: allocators.hpp:416
make_const< pointer >::type const_pointer
pointer to constant element
Definition: allocators.hpp:386
__HOST__ __DEVICE__ pointer address(reference x)
Returns the address of x.
Definition: allocators.hpp:274
ecuda::add_lvalue_reference< const T >::type const_reference
reference to constant element
Definition: allocators.hpp:235
__DEVICE__ void construct(pointer ptr, const_reference val)
Constructs an element object on the location pointed by ptr.
std::size_t size_type
quantities of elements
Definition: allocators.hpp:236
__HOST__ __DEVICE__ ~device_allocator()
Destructs the device allocator object.
Definition: allocators.hpp:264
__HOST__ __DEVICE__ const T & max(const T &a, const T &b)
Definition: algorithm.hpp:51
#define __DEVICE__
Definition: global.hpp:151
host_allocator(const host_allocator &alloc)
Constructs a host allocator object from another host allocator object.
Definition: allocators.hpp:107
__HOST__ __DEVICE__ size_type max_size() const
Returns the maximum number of elements, each of member type value_type (an alias of allocator's templ...
Definition: allocators.hpp:498
__HOST__ __DEVICE__ pointer address(reference x)
Returns the address of x.
Definition: allocators.hpp:431
T value_type
element type
Definition: allocators.hpp:231
make_const< pointer >::type const_pointer
pointer to constant element
Definition: allocators.hpp:234