Extended CUDA Library (ecuda)  2.0
 All Classes Namespaces Files Functions Variables Typedefs Friends Macros
Public Types | Public Member Functions | List of all members
ecuda::host_allocator< T, Flags > Class Template Reference

Allocator for page-locked host memory. More...

#include <allocators.hpp>

Public Types

typedef T value_type
 element type More...
 
typedef ecuda::add_pointer< T >
::type 
pointer
 pointer to element More...
 
typedef
ecuda::add_lvalue_reference< T >
::type 
reference
 reference to element More...
 
typedef make_const< pointer >::type const_pointer
 pointer to constant element More...
 
typedef
ecuda::add_lvalue_reference
< const T >::type 
const_reference
 reference to constant element More...
 
typedef std::size_t size_type
 quantities of elements More...
 
typedef std::ptrdiff_t difference_type
 

Public Member Functions

 host_allocator () throw ()
 Constructs a host allocator object. More...
 
 host_allocator (const host_allocator &alloc) throw ()
 Constructs a host allocator object from another host allocator object. More...
 
template<typename U >
 host_allocator (const host_allocator< U > &alloc) throw ()
 Constructs a host allocator object from another host allocator object with a different element type. More...
 
 ~host_allocator () throw ()
 Destructs the host allocator object. More...
 
pointer address (reference x)
 Returns the address of x. More...
 
const_pointer address (const_reference x) const
 Returns the address of x. More...
 
pointer allocate (size_type n, std::allocator< void >::const_pointer hint=0)
 Allocate block of storage. More...
 
void deallocate (pointer ptr, size_type)
 Releases a block of storage previously allocated with member allocate and not yet released. More...
 
size_type max_size () const throw ()
 Returns the maximum number of elements, each of member type value_type (an alias of allocator's template parameter) that could potentially be allocated by a call to member allocate. More...
 
void construct (pointer ptr, const_reference val)
 Constructs an element object on the location pointed by ptr. More...
 
void destroy (pointer ptr)
 Destroys in-place the object pointed by ptr. Notice that this does not deallocate the storage for the element (see member deallocate to release storage space). More...
 

Detailed Description

template<typename T, unsigned Flags = cudaHostAllocDefault>
class ecuda::host_allocator< T, Flags >

Allocator for page-locked host memory.

Implementation follows the specification of an STL allocator. The main difference is that the CUDA API functions cudaHostAlloc and cudaFreeHost are used internally to allocate/deallocate memory.

Page-locked or "pinned" memory makes copying memory from the GPU (device) to the CPU (host) faster. Using STL containers with this allocator makes them better at acting as "staging" points when moving data from the device memory to the host memory. This is used internally to optimize host <=> device transfers that involve any kind of temporary staging memory, but can be used effectively by an end-user of the library as well.

For example:

std::vector< int, ecuda::host_allocator<int> > v;

This would instantiate a vector whose underlying contents would be stored in page-locked host memory. Then a call to, for example:

ecuda::vector<int> deviceVector(1000);
// do work on device vector using the GPU...
std::vector< int, ecuda::host_allocator<int> > hostVector( 1000 );
ecuda::copy( deviceVector.begin(), deviceVector.end(), hostVector.begin() ); // copy results from device to host

This would potentially be a faster transfer than one would get using a std::vector with the default STL allocator.

Definition at line 82 of file allocators.hpp.

Member Typedef Documentation

template<typename T, unsigned Flags = cudaHostAllocDefault>
typedef make_const<pointer>::type ecuda::host_allocator< T, Flags >::const_pointer

pointer to constant element

Definition at line 89 of file allocators.hpp.

template<typename T, unsigned Flags = cudaHostAllocDefault>
typedef ecuda::add_lvalue_reference<const T>::type ecuda::host_allocator< T, Flags >::const_reference

reference to constant element

Definition at line 90 of file allocators.hpp.

template<typename T, unsigned Flags = cudaHostAllocDefault>
typedef std::ptrdiff_t ecuda::host_allocator< T, Flags >::difference_type

difference between two pointers

Definition at line 92 of file allocators.hpp.

template<typename T, unsigned Flags = cudaHostAllocDefault>
typedef ecuda::add_pointer<T>::type ecuda::host_allocator< T, Flags >::pointer

pointer to element

Definition at line 87 of file allocators.hpp.

template<typename T, unsigned Flags = cudaHostAllocDefault>
typedef ecuda::add_lvalue_reference<T>::type ecuda::host_allocator< T, Flags >::reference

reference to element

Definition at line 88 of file allocators.hpp.

template<typename T, unsigned Flags = cudaHostAllocDefault>
typedef std::size_t ecuda::host_allocator< T, Flags >::size_type

quantities of elements

Definition at line 91 of file allocators.hpp.

template<typename T, unsigned Flags = cudaHostAllocDefault>
typedef T ecuda::host_allocator< T, Flags >::value_type

element type

Definition at line 86 of file allocators.hpp.

Constructor & Destructor Documentation

template<typename T, unsigned Flags = cudaHostAllocDefault>
ecuda::host_allocator< T, Flags >::host_allocator ( )
throw (
)
inline

Constructs a host allocator object.

Definition at line 101 of file allocators.hpp.

template<typename T, unsigned Flags = cudaHostAllocDefault>
ecuda::host_allocator< T, Flags >::host_allocator ( const host_allocator< T, Flags > &  alloc)
throw (
)
inline

Constructs a host allocator object from another host allocator object.

Parameters
allocAllocator object.

Definition at line 107 of file allocators.hpp.

template<typename T, unsigned Flags = cudaHostAllocDefault>
template<typename U >
ecuda::host_allocator< T, Flags >::host_allocator ( const host_allocator< U > &  alloc)
throw (
)
inline

Constructs a host allocator object from another host allocator object with a different element type.

Parameters
allocAllocator object.

Definition at line 114 of file allocators.hpp.

template<typename T, unsigned Flags = cudaHostAllocDefault>
ecuda::host_allocator< T, Flags >::~host_allocator ( )
throw (
)
inline

Destructs the host allocator object.

Definition at line 119 of file allocators.hpp.

Member Function Documentation

template<typename T, unsigned Flags = cudaHostAllocDefault>
pointer ecuda::host_allocator< T, Flags >::address ( reference  x)
inline

Returns the address of x.

This effectively means returning &x.

Parameters
xReference to object.
Returns
A pointer to the object.

Definition at line 129 of file allocators.hpp.

template<typename T, unsigned Flags = cudaHostAllocDefault>
const_pointer ecuda::host_allocator< T, Flags >::address ( const_reference  x) const
inline

Returns the address of x.

This effectively means returning &x.

Parameters
xReference to object.
Returns
A pointer to the object.

Definition at line 139 of file allocators.hpp.

template<typename T, unsigned Flags = cudaHostAllocDefault>
pointer ecuda::host_allocator< T, Flags >::allocate ( size_type  n,
std::allocator< void >::const_pointer  hint = 0 
)
inline

Allocate block of storage.

Attempts to allocate a block of storage with a size large enough to contain n elements of member type value_type, and returns a pointer to the first element.

The storage is aligned appropriately for object of type value_type, but they are not constructed.

The block of storage is allocated using cudaHostAlloc and throws std::bad_alloc if it cannot allocate the total amount of storage requested.

Parameters
nNumber of elements (each of size sizeof(value_type)) to be allocated.
hintEither 0 or a value previously obtained by another call to allocate and not yet freed with deallocate. For standard memory allocation, a non-zero value may used as a hint to improve performance by allocating the new block near the one specified. The address of an adjacent element is often a good choice. In this case, hint is always ignored since the CUDA host memory allocator cannot take advantage of it.
Returns
A pointer to the initial element in the block of storage.

Definition at line 161 of file allocators.hpp.

template<typename T, unsigned Flags = cudaHostAllocDefault>
void ecuda::host_allocator< T, Flags >::construct ( pointer  ptr,
const_reference  val 
)
inline

Constructs an element object on the location pointed by ptr.

Parameters
ptrPointer to a location with enough storage space to contain an element of type value_type. pointer is a member type (defined as an alias of T* in ecuda::host_allocator<T>).
valValue to initialize the constructed element to. const_reference is a member type (defined as an alias of T& in ecuda::host_allocator<T>).

Definition at line 203 of file allocators.hpp.

template<typename T, unsigned Flags = cudaHostAllocDefault>
void ecuda::host_allocator< T, Flags >::deallocate ( pointer  ptr,
size_type   
)
inline

Releases a block of storage previously allocated with member allocate and not yet released.

The elements in the array are not destroyed by a call to this member function.

In the default allocator, the block of storage is at some point deallocated using ::operator delete (either during the function call, or later).

Parameters
ptrPointer to a block of storage previously allocated with allocate. pointer is a member type (defined as an alias of T* in ecuda::host_allocator<T>).

Definition at line 180 of file allocators.hpp.

template<typename T, unsigned Flags = cudaHostAllocDefault>
void ecuda::host_allocator< T, Flags >::destroy ( pointer  ptr)
inline

Destroys in-place the object pointed by ptr. Notice that this does not deallocate the storage for the element (see member deallocate to release storage space).

Parameters
ptrPointer to the object to be destroyed.

Definition at line 210 of file allocators.hpp.

template<typename T, unsigned Flags = cudaHostAllocDefault>
size_type ecuda::host_allocator< T, Flags >::max_size ( ) const
throw (
)
inline

Returns the maximum number of elements, each of member type value_type (an alias of allocator's template parameter) that could potentially be allocated by a call to member allocate.

A call to member allocate with the value returned by this function can still fail to allocate the requested storage.

Returns
The nubmer of elements that might be allcoated as maximum by a call to member allocate.

Definition at line 194 of file allocators.hpp.


The documentation for this class was generated from the following file: