curfil  ..
 All Classes Functions Variables Typedefs Friends Groups Pages
Public Types | Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | Friends | List of all members
cuv::ndarray< V, M, L > Class Template Reference

represents an n-dimensional array on GPU or CPU. More...

#include <ndarray.hpp>

Inheritance diagram for cuv::ndarray< V, M, L >:
cuv::ndarray_view< V, M, L >

Public Types

typedef memory< V, M > memory_type
 type of stored memory
typedef memory_type::reference_type reference_type
 values returned by operator() and []
typedef
memory_type::const_reference_type 
const_reference_type
 values returned by operator()
typedef
memory_type::memory_space_type 
memory_space_type
 dev/host
typedef memory_type::value_type value_type
 type of stored values
typedef memory_type::size_type size_type
 type shapes
typedef memory_type::index_type index_type
 type strides
typedef L memory_layout_type
 column/row major
typedef ndarray_info< M, L > info_type
 type of shape info struct
typedef ndarray_view< V, M, L > view_type
 type of views on this ndarray

Public Member Functions

template<size_t D>
size_type index_of (const extent_gen< D > &eg) const
 determine linear index in memory of an index array
template<class T >
ndarray copy (T tag=linear_memory_tag(), cudaStream_t stream=0) const
 copy memory using given allocator tag (linear/pitched)
ndarray copy () const
 copy memory using linear memory
template<int D, int E>
ndarray_view< V, M, L > operator[] (const index_gen< D, E > &idx) const
 create a sub-ndarray of the current ndarray
template<size_t D>
void reshape (const extent_gen< D > &eg)
 reshape the ndarray (in place)
void reshape (const std::vector< size_type > &shape)
 reshape the ndarray (in place)
void reshape (size_type r, size_type c)
 convenience wrapper for reshape(extents[r][c])
void resize (const std::vector< size_type > &shape)
 resize the ndarray (deallocates memory if product changes, otherwise equivalent to reshape)
template<size_t D>
void resize (const extent_gen< D > &eg)
 resize the ndarray (deallocates memory if product changes, otherwise equivalent to reshape)
void resize (size_type size)
 convenience wrapper for resize(extents[size])
void resize (size_type r, size_type c)
 convenience wrapper for resize(extents[r][c])
void dealloc ()
 force deallocation of memory if possible
template<class OM , class OL >
bool copy_memory (const ndarray< V, OM, OL > &src, bool force_dst_contiguous, cudaStream_t stream)
 tries to copy memory, succeeds if shapes match AND both ndarrays are c_contiguous or 2d-copyable.
template<class OM , class OL >
void copy_memory (const ndarray< V, OM, OL > &src, linear_memory_tag, cudaStream_t stream)
 copies between different memory spaces
template<class OM , class OL >
void copy_memory (const ndarray< V, OM, OL > &src, pitched_memory_tag, cudaStream_t stream)
 copies between different memory spaces
Accessors
index_type ndim () const
 return the number of dimensions
size_type shape (const size_t i) const
 return the size of the i-th dimension
index_type stride (const size_t i) const
 return the stride of the i-th dimension
V * ptr ()
const V * ptr () const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
void set_ptr_offset (long int i)
 set the pointer offset (used in deserialization)
boost::shared_ptr< memory_type > & mem ()
const boost::shared_ptr
< memory_type > & 
mem () const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
size_type size () const
size_type memsize () const
 determine size in bytes
std::vector< size_typeshape () const
 return the shape of the ndarray (as a vector for backward compatibility)
std::vector< size_typeeffective_shape () const
 return the effective shape of the ndarray (as a vector for backward compatibility)
const info_typeinfo () const
info_typeinfo ()
bool is_c_contiguous () const
 true iff there are no "holes" in memory
bool is_2dcopyable () const
 true iff it can be copied as a 2d array (only one dimension is pitched)
accessing stored values
reference_type operator[] (index_type idx)
 member access: "flat" access as if memory was linear
const_reference_type operator[] (index_type idx) const
reference_type operator() (index_type i0)
 get a reference to the datum at an index
const_reference_type operator() (index_type i0) const
const_reference_type operator() (index_type i0, index_type i1) const
reference_type operator() (index_type i0, index_type i1)
const_reference_type operator() (index_type i0, index_type i1, index_type i2) const
reference_type operator() (index_type i0, index_type i1, index_type i2)
const_reference_type operator() (index_type i0, index_type i1, index_type i2, index_type i3) const
reference_type operator() (index_type i0, index_type i1, index_type i2, index_type i3)
const_reference_type operator() (index_type i0, index_type i1, index_type i2, index_type i3, index_type i4) const
reference_type operator() (index_type i0, index_type i1, index_type i2, index_type i3, index_type i4)
constructors
 ndarray (const boost::shared_ptr< allocator > _allocator=boost::make_shared< default_allocator >())
 default constructor (does nothing)
 ndarray (const ndarray &o)
 construct ndarray from ndarray of exact same type
template<class OM >
 ndarray (const ndarray< value_type, OM, L > &o, cudaStream_t stream=0)
 construct ndarray from ndarray of other memory space in (dense) /linear/ memory.
 ndarray (const ndarray &o, pitched_memory_tag, cudaStream_t stream=0)
 construct ndarray from ndarray of same memory space in /pitched/ memory.
template<class OM >
 ndarray (const ndarray< value_type, OM, L > &o, pitched_memory_tag, cudaStream_t stream=0)
 construct ndarray from ndarray of other memory space in /pitched/ memory.
 ndarray (const ndarray &o, linear_memory_tag, cudaStream_t stream=0)
 construct ndarray from ndarray of same memory space in (dense) /linear/ memory.
template<class OM >
 ndarray (const ndarray< value_type, OM, L > &o, linear_memory_tag, cudaStream_t stream=0)
 construct ndarray from ndarray of other memory space in (dense) /linear/ memory.
template<class OL >
 ndarray (const ndarray< value_type, M, OL > &o)
 construct ndarray from other memory layout
 ndarray (const size_type i, const boost::shared_ptr< allocator > _allocator=boost::make_shared< default_allocator >())
 construct one-dimensional ndarray
 ndarray (const size_type i, const int j, const boost::shared_ptr< allocator > _allocator=boost::make_shared< default_allocator >())
 construct two-dimensional ndarray
template<size_t D>
 ndarray (const extent_gen< D > &eg, const boost::shared_ptr< allocator > _allocator=boost::make_shared< default_allocator >())
 construct ndarray from a shape
 ndarray (const std::vector< size_type > &eg, const boost::shared_ptr< allocator > _allocator=boost::make_shared< default_allocator >())
 construct ndarray from a shape
 ndarray (const std::vector< size_type > &eg, pitched_memory_tag, const boost::shared_ptr< allocator > _allocator=boost::make_shared< default_allocator >())
 construct ndarray from a shape
template<size_t D>
 ndarray (const extent_gen< D > &eg, pitched_memory_tag, const boost::shared_ptr< allocator > _allocator=boost::make_shared< default_allocator >())
 construct ndarray from a shape (pitched)
template<size_t D>
 ndarray (const extent_gen< D > &eg, value_type *ptr, const boost::shared_ptr< allocator > _allocator=boost::make_shared< default_allocator >())
 construct ndarray from a shape and a pointer (does not copy memory)
 ndarray (const std::vector< size_type > &shape, value_type *ptr, const boost::shared_ptr< allocator > _allocator=boost::make_shared< default_allocator >())
template<int D, int E>
 ndarray (const index_gen< D, E > &idx, value_type *ptr, const boost::shared_ptr< allocator > _allocator=boost::make_shared< default_allocator >())
 construct ndarray from a shape and a pointer (does not copy memory)
assigning other values to a ndarray object
template<class _M , class _L >
ndarrayassign (const ndarray< V, _M, _L > &o, cudaStream_t stream=0)
 explicitly assign by copying memory
ndarrayoperator= (const ndarray &o)
 assign from ndarray of same type
template<class _V >
boost::enable_if_c
< boost::is_convertible< _V,
value_type >::value, ndarray & >
::type 
operator= (const _V &scalar)
 assign from value (sets all elements equal to one scalar)
template<class OM >
ndarrayassign (const ndarray< value_type, OM, L > &o, cudaStream_t stream=0)
 assign from ndarray of different memory space type.
template<class OM >
ndarrayoperator= (const ndarray< value_type, OM, L > &o)
 assign from ndarray of different memory space type.
template<class OL >
ndarrayoperator= (const ndarray< value_type, M, OL > &o)
 assign from ndarray of different memory layout type.

Public Attributes

boost::shared_ptr< allocatorm_allocator

Protected Member Functions

size_type index_of (int D, index_type *arr) const
 determine linear index in memory of an index array
void allocate (ndarray &t, linear_memory_tag)
 allocate linear memory (c-contiguous version)
void allocate (ndarray &t, pitched_memory_tag)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. pitched version.

Protected Attributes

info_type m_info
 information about shape, strides
boost::shared_ptr< memory_typem_memory
 points to (possibly shared) memory
V * m_ptr
 points to start of actually referenced memory (within m_memory)

Friends

class ndarray_view
 ndarray views are our friends

Detailed Description

template<class V, class M, class L = row_major>
class cuv::ndarray< V, M, L >

represents an n-dimensional array on GPU or CPU.

Definition at line 205 of file ndarray.hpp.

Constructor & Destructor Documentation

template<class V, class M, class L = row_major>
cuv::ndarray< V, M, L >::ndarray ( const ndarray< V, M, L > &  o)
inline

construct ndarray from ndarray of exact same type

time O(1)

Definition at line 609 of file ndarray.hpp.

template<class V, class M, class L = row_major>
template<class OM >
cuv::ndarray< V, M, L >::ndarray ( const ndarray< value_type, OM, L > &  o,
cudaStream_t  stream = 0 
)
inline

construct ndarray from ndarray of other memory space in (dense) /linear/ memory.

Note: this /copies/ the memory!

Definition at line 621 of file ndarray.hpp.

template<class V, class M, class L = row_major>
cuv::ndarray< V, M, L >::ndarray ( const ndarray< V, M, L > &  o,
pitched_memory_tag  ,
cudaStream_t  stream = 0 
)
inlineexplicit

construct ndarray from ndarray of same memory space in /pitched/ memory.

Note: this /copies/ the memory!

Definition at line 633 of file ndarray.hpp.

template<class V, class M, class L = row_major>
template<class OM >
cuv::ndarray< V, M, L >::ndarray ( const ndarray< value_type, OM, L > &  o,
pitched_memory_tag  ,
cudaStream_t  stream = 0 
)
inlineexplicit

construct ndarray from ndarray of other memory space in /pitched/ memory.

Note: this /copies/ the memory!

Definition at line 646 of file ndarray.hpp.

template<class V, class M, class L = row_major>
cuv::ndarray< V, M, L >::ndarray ( const ndarray< V, M, L > &  o,
linear_memory_tag  ,
cudaStream_t  stream = 0 
)
inlineexplicit

construct ndarray from ndarray of same memory space in (dense) /linear/ memory.

Note: this /copies/ the memory!

Definition at line 658 of file ndarray.hpp.

template<class V, class M, class L = row_major>
template<class OM >
cuv::ndarray< V, M, L >::ndarray ( const ndarray< value_type, OM, L > &  o,
linear_memory_tag  ,
cudaStream_t  stream = 0 
)
inlineexplicit

construct ndarray from ndarray of other memory space in (dense) /linear/ memory.

Note: this /copies/ the memory!

Definition at line 671 of file ndarray.hpp.

template<class V, class M, class L = row_major>
template<class OL >
cuv::ndarray< V, M, L >::ndarray ( const ndarray< value_type, M, OL > &  o)
inlineexplicit

construct ndarray from other memory layout

this does not copy memory, but reverses dimensions and strides (and therefore only takes O(1) time)

Definition at line 686 of file ndarray.hpp.

template<class V, class M, class L = row_major>
cuv::ndarray< V, M, L >::ndarray ( const std::vector< size_type > &  eg,
const boost::shared_ptr< allocator _allocator = boost::make_shared<default_allocator>() 
)
inlineexplicit

construct ndarray from a shape

Deprecated:

Definition at line 748 of file ndarray.hpp.

template<class V, class M, class L = row_major>
cuv::ndarray< V, M, L >::ndarray ( const std::vector< size_type > &  eg,
pitched_memory_tag  ,
const boost::shared_ptr< allocator _allocator = boost::make_shared<default_allocator>() 
)
inlineexplicit

construct ndarray from a shape

Deprecated:

Definition at line 764 of file ndarray.hpp.

template<class V, class M, class L = row_major>
template<size_t D>
cuv::ndarray< V, M, L >::ndarray ( const extent_gen< D > &  eg,
value_type ptr,
const boost::shared_ptr< allocator _allocator = boost::make_shared<default_allocator>() 
)
inlineexplicit

construct ndarray from a shape and a pointer (does not copy memory)

Warning
You have to ensure that the memory lives as long as this object.

Definition at line 800 of file ndarray.hpp.

template<class V, class M, class L = row_major>
template<int D, int E>
cuv::ndarray< V, M, L >::ndarray ( const index_gen< D, E > &  idx,
value_type ptr,
const boost::shared_ptr< allocator _allocator = boost::make_shared<default_allocator>() 
)
inlineexplicit

construct ndarray from a shape and a pointer (does not copy memory)

Warning
You have to ensure that the memory lives as long as this object.
Deprecated:

Definition at line 851 of file ndarray.hpp.

Member Function Documentation

template<class V, class M, class L = row_major>
void cuv::ndarray< V, M, L >::allocate ( ndarray< V, M, L > &  t,
linear_memory_tag   
)
inlineprotected

allocate linear memory (c-contiguous version)

Parameters
tndarray to allocate

Definition at line 274 of file ndarray.hpp.

template<class V, class M, class L = row_major>
template<class OM >
ndarray& cuv::ndarray< V, M, L >::assign ( const ndarray< value_type, OM, L > &  o,
cudaStream_t  stream = 0 
)
inline

assign from ndarray of different memory space type.

If shapes do not match, it defaults to linear memory.

this copies memory (obviously) but tries to avoid reallocation

Definition at line 926 of file ndarray.hpp.

template<class V, class M, class L = row_major>
std::vector<size_type> cuv::ndarray< V, M, L >::effective_shape ( ) const
inline

return the effective shape of the ndarray (as a vector for backward compatibility)

the effective shape removes all degenerate dimensions (i.e. shape(i)==1).

Definition at line 416 of file ndarray.hpp.

template<class V, class M, class L = row_major>
size_type cuv::ndarray< V, M, L >::index_of ( int  D,
index_type arr 
) const
inlineprotected

determine linear index in memory of an index array

this function takes strides etc. into account, so that indices are interpreted as relative to the (strided) sub-ndarray we're referring to.

Parameters
Dsize of index array
arrindex array
Returns
linear index in memory of index array

Definition at line 258 of file ndarray.hpp.

template<class V, class M, class L = row_major>
template<size_t D>
size_type cuv::ndarray< V, M, L >::index_of ( const extent_gen< D > &  eg) const
inline

determine linear index in memory of an index array

this function takes strides etc. into account, so that indices are interpreted as relative to the (strided) sub-ndarray we're referring to.

Template Parameters
Dsize of index array
Parameters
egposition in array
Returns
linear index in memory of index array

Definition at line 310 of file ndarray.hpp.

template<class V, class M, class L = row_major>
const info_type& cuv::ndarray< V, M, L >::info ( ) const
inline
Returns
the ndarray info struct (const)

Definition at line 427 of file ndarray.hpp.

template<class V, class M, class L = row_major>
info_type& cuv::ndarray< V, M, L >::info ( )
inline
Returns
the ndarray info struct

Definition at line 432 of file ndarray.hpp.

template<class V, class M, class L = row_major>
boost::shared_ptr<memory_type>& cuv::ndarray< V, M, L >::mem ( )
inline
  • Returns
    pointer to allocated memory

Definition at line 363 of file ndarray.hpp.

template<class V, class M, class L = row_major>
const boost::shared_ptr<memory_type>& cuv::ndarray< V, M, L >::mem ( ) const
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Returns
the const pointer to the allocated memory

Definition at line 370 of file ndarray.hpp.

template<class V, class M, class L = row_major>
size_type cuv::ndarray< V, M, L >::memsize ( ) const
inline

determine size in bytes

assumes that the memory is c_contiguous!

Returns
the size in bytes

Definition at line 392 of file ndarray.hpp.

template<class V, class M, class L = row_major>
reference_type cuv::ndarray< V, M, L >::operator() ( index_type  i0)
inline

get a reference to the datum at an index

Parameters
i0index for a 1-dimensional ndarray
Returns
reference to datum at i0

Definition at line 500 of file ndarray.hpp.

template<class V, class M, class L = row_major>
const_reference_type cuv::ndarray< V, M, L >::operator() ( index_type  i0) const
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 513 of file ndarray.hpp.

template<class V, class M, class L = row_major>
const_reference_type cuv::ndarray< V, M, L >::operator() ( index_type  i0,
index_type  i1 
) const
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 518 of file ndarray.hpp.

template<class V, class M, class L = row_major>
reference_type cuv::ndarray< V, M, L >::operator() ( index_type  i0,
index_type  i1 
)
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 523 of file ndarray.hpp.

template<class V, class M, class L = row_major>
const_reference_type cuv::ndarray< V, M, L >::operator() ( index_type  i0,
index_type  i1,
index_type  i2 
) const
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 534 of file ndarray.hpp.

template<class V, class M, class L = row_major>
reference_type cuv::ndarray< V, M, L >::operator() ( index_type  i0,
index_type  i1,
index_type  i2 
)
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 539 of file ndarray.hpp.

template<class V, class M, class L = row_major>
const_reference_type cuv::ndarray< V, M, L >::operator() ( index_type  i0,
index_type  i1,
index_type  i2,
index_type  i3 
) const
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 551 of file ndarray.hpp.

template<class V, class M, class L = row_major>
reference_type cuv::ndarray< V, M, L >::operator() ( index_type  i0,
index_type  i1,
index_type  i2,
index_type  i3 
)
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 556 of file ndarray.hpp.

template<class V, class M, class L = row_major>
const_reference_type cuv::ndarray< V, M, L >::operator() ( index_type  i0,
index_type  i1,
index_type  i2,
index_type  i3,
index_type  i4 
) const
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 569 of file ndarray.hpp.

template<class V, class M, class L = row_major>
reference_type cuv::ndarray< V, M, L >::operator() ( index_type  i0,
index_type  i1,
index_type  i2,
index_type  i3,
index_type  i4 
)
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 575 of file ndarray.hpp.

template<class V, class M, class L = row_major>
ndarray& cuv::ndarray< V, M, L >::operator= ( const ndarray< V, M, L > &  o)
inline

assign from ndarray of same type

always an O(1) operation.

Reimplemented in cuv::ndarray_view< V, M, L >.

Definition at line 897 of file ndarray.hpp.

template<class V, class M, class L = row_major>
template<class OM >
ndarray& cuv::ndarray< V, M, L >::operator= ( const ndarray< value_type, OM, L > &  o)
inline

assign from ndarray of different memory space type.

If shapes do not match, it defaults to linear memory.

this copies memory (obviously) but tries to avoid reallocation

Definition at line 944 of file ndarray.hpp.

template<class V, class M, class L = row_major>
template<class OL >
ndarray& cuv::ndarray< V, M, L >::operator= ( const ndarray< value_type, M, OL > &  o)
inline

assign from ndarray of different memory layout type.

this does not copy memory, but reverses strides and shapes.

Definition at line 954 of file ndarray.hpp.

template<class V, class M, class L = row_major>
const_reference_type cuv::ndarray< V, M, L >::operator[] ( index_type  idx) const
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 491 of file ndarray.hpp.

template<class V, class M, class L = row_major>
template<int D, int E>
ndarray_view<V, M, L> cuv::ndarray< V, M, L >::operator[] ( const index_gen< D, E > &  idx) const
inline

create a sub-ndarray of the current ndarray

this works in O(1).

Definition at line 985 of file ndarray.hpp.

template<class V, class M, class L = row_major>
V* cuv::ndarray< V, M, L >::ptr ( )
inline
Returns
the pointer to the referenced memory

Definition at line 345 of file ndarray.hpp.

template<class V, class M, class L = row_major>
const V* cuv::ndarray< V, M, L >::ptr ( ) const
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Returns
the const pointer to the referenced memory

Definition at line 353 of file ndarray.hpp.

template<class V, class M, class L = row_major>
template<size_t D>
void cuv::ndarray< V, M, L >::reshape ( const extent_gen< D > &  eg)
inline

reshape the ndarray (in place)

works only for c_contiguous memory!

Parameters
egnew shape

Definition at line 1034 of file ndarray.hpp.

template<class V, class M, class L = row_major>
void cuv::ndarray< V, M, L >::reshape ( const std::vector< size_type > &  shape)
inline

reshape the ndarray (in place)

works only for c_contiguous memory!

Parameters
shapenew shape

Definition at line 1047 of file ndarray.hpp.

template<class V, class M, class L = row_major>
void cuv::ndarray< V, M, L >::reshape ( size_type  r,
size_type  c 
)
inline

convenience wrapper for reshape(extents[r][c])

Parameters
rleading index of new shape
cfollowing index of new shape

Definition at line 1073 of file ndarray.hpp.

template<class V, class M, class L = row_major>
void cuv::ndarray< V, M, L >::resize ( const std::vector< size_type > &  shape)
inline

resize the ndarray (deallocates memory if product changes, otherwise equivalent to reshape)

Parameters
shapenew shape

Definition at line 1082 of file ndarray.hpp.

template<class V, class M, class L = row_major>
template<size_t D>
void cuv::ndarray< V, M, L >::resize ( const extent_gen< D > &  eg)
inline

resize the ndarray (deallocates memory if product changes, otherwise equivalent to reshape)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Parameters
egnew shape

Definition at line 1103 of file ndarray.hpp.

template<class V, class M, class L = row_major>
void cuv::ndarray< V, M, L >::resize ( size_type  size)
inline

convenience wrapper for resize(extents[size])

Parameters
sizesize of the new shape

Definition at line 1114 of file ndarray.hpp.

template<class V, class M, class L = row_major>
void cuv::ndarray< V, M, L >::resize ( size_type  r,
size_type  c 
)
inline

convenience wrapper for resize(extents[r][c])

Parameters
rleading index of new shape
cfollowing index of new shape

Definition at line 1123 of file ndarray.hpp.

template<class V, class M, class L = row_major>
size_type cuv::ndarray< V, M, L >::shape ( const size_t  i) const
inline

return the size of the i-th dimension

Parameters
ithe index of the queried dimension

Definition at line 333 of file ndarray.hpp.

template<class V, class M, class L = row_major>
size_type cuv::ndarray< V, M, L >::size ( ) const
inline
Returns
the number of stored elements

Definition at line 376 of file ndarray.hpp.

template<class V, class M, class L = row_major>
index_type cuv::ndarray< V, M, L >::stride ( const size_t  i) const
inline

return the stride of the i-th dimension

Parameters
ithe index of the queried dimension

Definition at line 340 of file ndarray.hpp.


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