Vc  0.7.5-dev
SIMD Vector Classes for C++
Memory< V, 0u, 0u > Class Template Reference

Detailed Description

template<typename V>
class Vc::Memory< V, 0u, 0u >

A helper class that is very similar to Memory<V, Size> but with dynamically allocated memory and thus dynamic size.

Example:

size_t size = 11;
Vc::Memory<int_v> array(size);
// scalar access:
for (size_t i = 0; i < array.entriesCount(); ++i) {
array[i] = i;
}
// vector access:
for (size_t i = 0; i < array.vectorsCount(); ++i) {
}

This code allocates a small array with 11 scalar entries and implements two equivalent loops that initialize the memory. The scalar loop writes each individual int. The vectorized loop writes int_v::Size values to memory per iteration. 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. (Note: the scalar loop can be auto-vectorized, except for the last three assignments.)

Note
The internal data pointer is not declared with the __restrict__ keyword. Therefore modifying memory of V::EntryType will require the compiler to assume aliasing. If you want to use the __restrict__ keyword you need to use a standard pointer to memory and do the vector address calculation and loads and stores manually.
Parameters
VThe vector type you want to operate on. (e.g. float_v or uint_v)
See Also
Memory<V, Size>

#include <Vc/Memory>

Inherits MemoryBase< V, Memory< V, 0u, 0u >, 1, void >.

Public Member Functions

 Memory (size_t size)
 Allocate enough memory to access size values of type V::EntryType.
template<typename Parent , typename RM >
 Memory (const MemoryBase< V, Parent, 1, RM > &rhs)
 Copy the memory into a new memory area.
 Memory (const Memory &rhs)
 Overload of the above function.
 ~Memory ()
 Frees the memory which was allocated in the constructor.
void swap (Memory &rhs)
 Swap the contents and size information of two Memory objects.
size_t entriesCount () const
size_t vectorsCount () const
template<typename Parent , typename RM >
Memoryoperator= (const MemoryBase< V, Parent, 1, RM > &rhs)
 Overwrite all entries with the values stored in rhs.
Memoryoperator= (const EntryType *rhs)
 Overwrite all entries with the values stored in the memory at rhs.
VectorPointerHelper< V,
AlignedFlag > 
vector (size_t i)
const VectorPointerHelperConst
< V, AlignedFlag > 
vector (size_t i) const
 Const overload of the above function.
VectorPointerHelper< V,
UnalignedFlag > 
vector (size_t i, int shift)
const VectorPointerHelperConst
< V, UnalignedFlag > 
vector (size_t i, int shift) const
 Const overload of the above function.
VectorPointerHelper< V, A > vectorAt (size_t i, A align=Vc::Aligned)
const VectorPointerHelperConst
< V, A > 
vectorAt (size_t i, A align=Vc::Aligned) const
 Const overload of the above function.
VectorPointerHelper< V,
AlignedFlag > 
firstVector ()
const VectorPointerHelperConst
< V, AlignedFlag > 
firstVector () const
 Const overload of the above function.
VectorPointerHelper< V,
AlignedFlag > 
lastVector ()
const VectorPointerHelperConst
< V, AlignedFlag > 
lastVector () const
 Const overload of the above function.

Constructor & Destructor Documentation

Memory ( size_t  size)

Allocate enough memory to access size values of type V::EntryType.

The allocated memory is aligned and padded correctly for fully vectorized access.

Parameters
sizeDetermines how many scalar values will fit into the allocated memory.
Memory ( const MemoryBase< V, Parent, 1, RM > &  rhs)

Copy the memory into a new memory area.

The allocated memory is aligned and padded correctly for fully vectorized access.

Parameters
rhsThe Memory object to copy from.
Memory ( const Memory< V, 0u, 0u > &  rhs)

Overload of the above function.

(Because C++ would otherwise not use the templated cctor and use a default-constructed cctor instead.)

Parameters
rhsThe Memory object to copy from.

Member Function Documentation

void swap ( Memory< V, 0u, 0u > &  rhs)

Swap the contents and size information of two Memory objects.

Parameters
rhsThe other Memory object to swap.
size_t entriesCount ( ) const
Returns
the number of scalar entries in the whole array.

Reimplemented from MemoryBase< V, Memory< V, 0u, 0u >, 1, void >.

size_t vectorsCount ( ) const
Returns
the number of vectors in the whole array.

Reimplemented from MemoryBase< V, Memory< V, 0u, 0u >, 1, void >.

Memory& operator= ( const MemoryBase< V, Parent, 1, RM > &  rhs)

Overwrite all entries with the values stored in rhs.

Parameters
rhsThe object to copy the data from.
Returns
reference to the modified Memory object.
Note
this function requires the vectorsCount() of both Memory objects to be equal.
Memory& operator= ( const EntryType *  rhs)

Overwrite all entries with the values stored in the memory at rhs.

Parameters
rhsThe array to copy the data from.
Returns
reference to the modified Memory object.
Note
this function requires that there are entriesCount() many values accessible from rhs.
VectorPointerHelper<V, AlignedFlag> vector ( size_t  i)
inherited
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.

const VectorPointerHelperConst<V, AlignedFlag> vector ( size_t  i) const
inherited

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.
VectorPointerHelper<V, UnalignedFlag> vector ( size_t  i,
int  shift 
)
inherited
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;
VectorPointerHelper<V, A> vectorAt ( size_t  i,
align = Vc::Aligned 
)
inherited
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.
alignYou must take care to determine whether an unaligned load/store is required. Per default an aligned load/store is used. If i is not a multiple of V::Size you must pass Vc::Unaligned here.
const VectorPointerHelperConst<V, A> vectorAt ( size_t  i,
align = Vc::Aligned 
) const
inherited

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.
alignYou must take care to determine whether an unaligned load/store is required. Per default an aligned load/store is used. If i is not a multiple of V::Size you must pass Vc::Unaligned here.
VectorPointerHelper<V, AlignedFlag> firstVector ( )
inherited
Returns
the first vector in the allocated memory.

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

VectorPointerHelper<V, AlignedFlag> lastVector ( )
inherited
Returns
the last vector in the allocated memory.

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