Vc  1.1.0
SIMD Vector Classes for C++
Memory< V, Size, 0u, InitPadding > Class Template Reference

Detailed Description

template<typename V, size_t Size, bool InitPadding>
class Vc::Common::Memory< V, Size, 0u, InitPadding >

A helper class to simplify usage of correctly aligned and padded memory, allowing both vector and scalar access.

Example:

Vc::Memory<int_v, 11> array;
// scalar access:
for (size_t i = 0; i < array.entriesCount(); ++i) {
int x = array[i]; // read
array[i] = x; // write
}
// more explicit alternative:
for (size_t i = 0; i < array.entriesCount(); ++i) {
int x = array.scalar(i); // read
array.scalar(i) = x; // write
}
// vector access:
for (size_t i = 0; i < array.vectorsCount(); ++i) {
int_v x = array.vector(i); // read
array.vector(i) = x; // write
}

This code allocates a small array and implements three equivalent loops (that do nothing useful). The loops show how scalar and vector read/write access is best implemented.

Since the size of 11 is not a multiple of int_v::Size (unless you use the scalar Vc implementation) the last write access of the vector loop would normally be out of bounds. But the Memory class automatically pads the memory such that the whole array can be accessed with correctly aligned memory addresses.

Parameters
VThe vector type you want to operate on. (e.g. float_v or uint_v)
SizeThe number of entries of the scalar base type the memory should hold. This is thus the same number as you would use for a normal C array (e.g. float mem[11] becomes Memory<float_v, 11> mem).
See also
Memory<V, 0u>

Definition at line 270 of file memory.h.

#include <Vc/Memory>

Inherits AlignedBase< V::MemoryAlignment >, and MemoryBase< V, Memory< V, Size, 0u, InitPadding >, 1, void >.

Public Member Functions

size_t entriesCount () const
 
size_t vectorsCount () const
 
MemoryVectorIterator< V, Flags > begin (Flags flags=Flags())
 Return a (vectorized) iterator to the start of this memory object.
 
MemoryVectorIterator< const V, Flags > begin (Flags flags=Flags()) const
 const overload of the above
 
MemoryVectorIterator< V, Flags > end (Flags flags=Flags())
 Return a (vectorized) iterator to the end of this memory object.
 
MemoryVectorIterator< const V, Flags > end (Flags flags=Flags()) const
 const overload of the above
 
std::enable_if<!std::is_convertible< Flags, int >::value, MemoryVector< V, Flags > >::type & vector (size_t i, Flags=Flags())
 
std::enable_if<!std::is_convertible< Flags, int >::value, MemoryVector< const V, Flags > >::type & vector (size_t i, Flags=Flags()) const
 Const overload of the above function. More...
 
std::enable_if< std::is_convertible< ShiftT, int >::value, MemoryVector< V, decltype(std::declval< Flags >)|Unaligned)> >::type & vector (size_t i, ShiftT shift, Flags=Flags())
 
std::enable_if< std::is_convertible< ShiftT, int >::value, MemoryVector< const V, decltype(std::declval< Flags >)|Unaligned)> >::type & vector (size_t i, ShiftT shift, Flags=Flags()) const
 Const overload of the above function.
 
MemoryVector< V, Flags > & vectorAt (size_t i, Flags flags=Flags())
 
MemoryVector< const V, Flags > & vectorAt (size_t i, Flags flags=Flags()) const
 Const overload of the above function. More...
 
MemoryVector< V, Flags > & firstVector (Flags=Flags())
 
MemoryVector< const V, Flags > & firstVector (Flags=Flags()) const
 Const overload of the above function.
 
MemoryVector< V, Flags > & lastVector (Flags=Flags())
 
MemoryVector< const V, Flags > & lastVector (Flags=Flags()) const
 Const overload of the above function.
 
void setZero ()
 Zero the whole memory area.
 
Memory< V, Size, 0u, InitPadding > & operator+= (const MemoryBase< V, P2, Dimension, RM > &rhs)
 (Inefficient) shorthand to add up two arrays.
 
Memory< V, Size, 0u, InitPadding > & operator+= (EntryType rhs)
 (Inefficient) shorthand to add a value to an array.
 
Memory< V, Size, 0u, InitPadding > & operator-= (const MemoryBase< V, P2, Dimension, RM > &rhs)
 (Inefficient) shorthand to subtract two arrays.
 
Memory< V, Size, 0u, InitPadding > & operator-= (EntryType rhs)
 (Inefficient) shorthand to subtract a value from an array.
 
Memory< V, Size, 0u, InitPadding > & operator*= (const MemoryBase< V, P2, Dimension, RM > &rhs)
 (Inefficient) shorthand to multiply two arrays.
 
Memory< V, Size, 0u, InitPadding > & operator*= (EntryType rhs)
 (Inefficient) shorthand to multiply a value to an array.
 
Memory< V, Size, 0u, InitPadding > & operator/= (const MemoryBase< V, P2, Dimension, RM > &rhs)
 (Inefficient) shorthand to divide two arrays.
 
Memory< V, Size, 0u, InitPadding > & operator/= (EntryType rhs)
 (Inefficient) shorthand to divide an array with a value.
 
bool operator== (const MemoryBase< V, P2, Dimension, RM > &rhs) const
 (Inefficient) shorthand compare equality of two arrays.
 
bool operator!= (const MemoryBase< V, P2, Dimension, RM > &rhs) const
 (Inefficient) shorthand compare two arrays.
 
bool operator< (const MemoryBase< V, P2, Dimension, RM > &rhs) const
 (Inefficient) shorthand compare two arrays.
 
bool operator<= (const MemoryBase< V, P2, Dimension, RM > &rhs) const
 (Inefficient) shorthand compare two arrays.
 
bool operator> (const MemoryBase< V, P2, Dimension, RM > &rhs) const
 (Inefficient) shorthand compare two arrays.
 
bool operator>= (const MemoryBase< V, P2, Dimension, RM > &rhs) const
 (Inefficient) shorthand compare two arrays.
 

Static Public Member Functions

static Memory< V, Size, 0u, false > & fromRawData (EntryType *ptr)
 Wrap existing data with the Memory convenience class. More...
 
static constexpr size_t entriesCount ()
 
static constexpr size_t vectorsCount ()
 

Member Function Documentation

static Memory<V, Size, 0u, false>& fromRawData ( EntryType *  ptr)
inlinestatic

Wrap existing data with the Memory convenience class.

This function returns a reference to a Memory<V, Size, 0> object that you must capture to avoid a copy of the whole data:

Memory<float_v, 16> &m = Memory<float_v, 16>::fromRawData(someAlignedPointerToFloat)
Parameters
ptrAn aligned pointer to memory of type V::EntryType (e.g. float for Vc::float_v).
Returns
A Memory object placed at the given location in memory.
Warning
The pointer ptr passed to this function must be aligned according to the alignment restrictions of V.
The size of the accessible memory must match Size. This includes the required padding at the end to allow the last entries to be accessed via vectors. If you know what you are doing you might violate this constraint.
It is your responsibility to ensure that the memory is released correctly (not too early/not leaked). This function simply adds convenience functions to access the memory.

Definition at line 335 of file memory.h.

static constexpr size_t entriesCount ( )
inlinestatic
Returns
the number of scalar entries in the whole array.
Note
This function can be optimized into a compile-time constant.

Definition at line 351 of file memory.h.

static constexpr size_t vectorsCount ( )
inlinestatic
Returns
the number of vectors in the whole array.
Note
This function can be optimized into a compile-time constant.

Definition at line 358 of file memory.h.

size_t entriesCount ( ) const
inlineinherited
Returns
the number of scalar entries in the array. This function is optimized away if a constant size array is used.

Definition at line 368 of file memorybase.h.

size_t vectorsCount ( ) const
inlineinherited
Returns
the number of vector entries that span the array. This function is optimized away if a constant size array is used.

Definition at line 373 of file memorybase.h.

std::enable_if<!std::is_convertible<Flags, int>::value, MemoryVector<V, Flags> >::type& vector ( size_t  i,
Flags  = Flags() 
)
inlineinherited
Parameters
iSelects the offset, where the vector should be read.
Returns
a smart object to wrap the i-th vector in the memory.

The return value can be used as any other vector object. I.e. you can substitute something like

float_v a = ..., b = ...;
a += b;

with

mem.vector(i) += b;

This function ensures that only aligned loads and stores are used. Thus it only allows to access memory at fixed strides. If access to known offsets from the aligned vectors is needed the vector(size_t, int) function can be used.

Definition at line 417 of file memorybase.h.

std::enable_if<!std::is_convertible<Flags, int>::value, MemoryVector<const V, Flags> >::type& vector ( size_t  i,
Flags  = Flags() 
) const
inlineinherited

Const overload of the above function.

Parameters
iSelects the offset, where the vector should be read.
Returns
a smart object to wrap the i-th vector in the memory.

Definition at line 427 of file memorybase.h.

std::enable_if< std::is_convertible<ShiftT, int>::value, MemoryVector<V, decltype(std::declval<Flags>) | Unaligned)> >::type& vector ( size_t  i,
ShiftT  shift,
Flags  = Flags() 
)
inlineinherited
Returns
a smart object to wrap the i-th vector + shift in the memory.

This function ensures that only unaligned loads and stores are used. It allows to access memory at any location aligned to the entry type.

Parameters
iSelects the memory location of the i-th vector. Thus if V::Size == 4 and i is set to 3 the base address for the load/store will be the 12th entry (same as &mem[12]).
shiftShifts the base address determined by parameter i by shift many entries. Thus vector(3, 1) for V::Size == 4 will load/store the 13th - 16th entries (same as &mem[13]).
Note
Any shift value is allowed as long as you make sure it stays within bounds of the allocated memory. Shift values that are a multiple of V::Size will not result in aligned loads. You have to use the above vector(size_t) function for aligned loads instead.
Thus a simple way to access vectors randomly is to set i to 0 and use shift as the parameter to select the memory address:
// don't use:
mem.vector(i / V::Size, i % V::Size) += 1;
// instead use:
mem.vector(0, i) += 1;

Definition at line 501 of file memorybase.h.

MemoryVector<V, Flags>& vectorAt ( size_t  i,
Flags  flags = Flags() 
)
inlineinherited
Returns
a smart object to wrap the vector starting from the i-th scalar entry in the memory.

Example:

Memory<float_v, N> mem;
mem.setZero();
for (int i = 0; i < mem.entriesCount(); i += float_v::Size) {
mem.vectorAt(i) += b;
}
Parameters
iSpecifies the scalar entry from where the vector will be loaded/stored. I.e. the values scalar(i), scalar(i + 1), ..., scalar(i + V::Size - 1) will be read/overwritten.
flagsYou must take care to determine whether an unaligned load/store is required. Per default an unaligned load/store is used. If i is a multiple of V::Size you may want to pass Vc::Aligned here.

Definition at line 451 of file memorybase.h.

MemoryVector<const V, Flags>& vectorAt ( size_t  i,
Flags  flags = Flags() 
) const
inlineinherited

Const overload of the above function.

Returns
a smart object to wrap the vector starting from the i-th scalar entry in the memory.
Parameters
iSpecifies the scalar entry from where the vector will be loaded/stored. I.e. the values scalar(i), scalar(i + 1), ..., scalar(i + V::Size - 1) will be read/overwritten.
flagsYou must take care to determine whether an unaligned load/store is required. Per default an unaligned load/store is used. If i is a multiple of V::Size you may want to pass Vc::Aligned here.

Definition at line 466 of file memorybase.h.

MemoryVector<V, Flags>& firstVector ( Flags  = Flags())
inlineinherited
Returns
the first vector in the allocated memory.

This function is simply a shorthand for vector(0).

Definition at line 523 of file memorybase.h.

MemoryVector<V, Flags>& lastVector ( Flags  = Flags())
inlineinherited
Returns
the last vector in the allocated memory.

This function is simply a shorthand for vector(vectorsCount() - 1).

Definition at line 538 of file memorybase.h.


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