Auryn simulator  v0.8.1-206-gb56e451
Plastic Spiking Neural Network Simulator
Public Member Functions | Public Attributes | Protected Member Functions | Friends | List of all members
auryn::AurynVector< T, IndexType > Class Template Reference

Default Auryn vector template. More...

#include <auryn_definitions.h>

Collaboration diagram for auryn::AurynVector< T, IndexType >:
Collaboration graph
[legend]

Public Member Functions

 AurynVector (IndexType n)
 Default constructor. More...
 
 AurynVector (AurynVector *vec)
 Copy constructor. More...
 
virtual ~AurynVector ()
 Default destructor. More...
 
virtual void resize (IndexType new_size)
 resize data array to new_size More...
 
void copy (AurynVector *v)
 Copies vector v. More...
 
get (IndexType i)
 Gets element i from vector. More...
 
T * ptr (IndexType i=0)
 Gets pointer to element i from vector. More...
 
void set (IndexType i, T value)
 Sets element i in vector to value. More...
 
void set_all (const T v)
 Set all elements to value v. More...
 
void set_zero ()
 Set all elements to zero. More...
 
void scale (const T a)
 Scales all vector elements by a. More...
 
void add (const T c)
 Adds constant c to each vector element. More...
 
void add_specific (const IndexType i, const T c)
 Adds the value c to specific vector element i. More...
 
void mul_specific (const IndexType i, const T c)
 Multiply to specific vector element with data index i with the constant c. More...
 
void add (AurynVector *v)
 Adds a vector v to the vector. More...
 
void sub (const T c)
 Subtract constant c to each vector element. More...
 
void sub (AurynVector *v)
 Elementwise subtraction. More...
 
void mul (const T a)
 Multiply all vector elements by constant. More...
 
void mul (AurynVector *v)
 Element-wise vector multiply. More...
 
void div (const T a)
 Element-wise division. More...
 
void div (AurynVector *v)
 Element-wise vector division. More...
 
void div (AurynVector *a, AurynVector *b)
 Element-wise vector division which stores the result in this. More...
 
void saxpy (const T a, AurynVector *x)
 SAXPY operation as in GSL. More...
 
void follow (AurynVector< T, IndexType > *v, const T rate)
 Follows target vector v with rate. More...
 
void follow_scalar (const T a, const T rate)
 Like follow but with a scalar target value a. More...
 
void elementwise_max (AurynVector *v1, AurynVector *v2)
 Elementwise max operation. More...
 
void elementwise_max (AurynVector *v1)
 Elementwise max operation with another vector. More...
 
void pow (const unsigned int n)
 Takes each element to the n-th power. More...
 
void fast_exp ()
 Computes an approximation of exp(x) for each vector element. More...
 
void exp ()
 Computes exp(x) for each vector element. More...
 
void sigmoid (AurynVector *x, const T beta, const T thr)
 Computes sigmoid(beta*(x-thr)) for each vector element and stores result in this instance. More...
 
void sqrt ()
 Takes the square root of each element. More...
 
void neg ()
 Flips the sign of all elements. More...
 
void inv ()
 Computes 1./x of each element. More...
 
void sum (AurynVector *a, AurynVector *b)
 Computes the sum a+b and stores the result in this instance. More...
 
void sum (AurynVector *a, const T b)
 Computes the sum a+b and stores the result in this instance. More...
 
void diff (AurynVector *a, AurynVector *b)
 Computes the difference a-b and stores the result in this instance. More...
 
void diff (AurynVector *a, const T b)
 Computes the difference a-b and stores the result in this instance. More...
 
void diff (const T a, AurynVector *b)
 Computes the difference a-b and stores the result in this instance. More...
 
void sqr ()
 Squares each element. More...
 
void abs ()
 Takes absolute value of each element. More...
 
void rect ()
 Rectifies all elements. More...
 
void neg_rect ()
 Negatively rectifies all elements. More...
 
void clip (T min, T max)
 Clips all vector elements to the range min max. More...
 
double var ()
 Computes the variance of the vector elements on this rank. More...
 
double std ()
 Computes the standard deviation of all elements on this rank. More...
 
double mean ()
 Computes the mean of the vector elements on this rank. More...
 
double element_sum ()
 Computes the sum of the vector elements. More...
 
double l1norm ()
 Computes the l1 norm of the vector. More...
 
double l2norm ()
 Computes the l2 norm of the vector. More...
 
double max ()
 Returns the max of the vector elements. More...
 
double min ()
 Returns the min of the vector elements. More...
 
IndexType nonzero ()
 Computes number of nonzero elements on this rank. More...
 
void zero_effective_zeros (const T epsilon=1e-3)
 Sets all values whose absolute value is smaller than epsilon to zero. More...
 
void add_random_normal (AurynState mean=0.0, AurynState sigma=1.0, unsigned int seed=8721)
 
void set_random_normal (AurynState mean=0.0, AurynState sigma=1.0, unsigned int seed=8721)
 
void set_random (unsigned int seed=0)
 Initializes vector elements with Gaussian of unit varince and a seed derived from system time if no seed or seed of 0 is given. More...
 
bool any ()
 Returns true if any element is nonzero. More...
 
bool any (T eps)
 Returns true if any element is nonzero. More...
 
void print ()
 Print vector elements to stdout for debugging. More...
 
void write_to_file (std::string filename)
 Print vector elements to a text file for debugging. More...
 

Public Attributes

IndexType size
 Size of the vector. More...
 
T * data
 Pointer to the array housing the data. More...
 

Protected Member Functions

void check_size (IndexType x)
 Checks if argument is larger than size and throws and exception if so. More...
 
void check_size (AurynVector *v)
 Checks if vector size matches to this instance. More...
 
void allocate (const NeuronID n)
 Implements aligned memory allocation. More...
 
void freebuf ()
 
fast_exp256 (T x)
 Computes approximation of exp(x) via fast series approximation up to n=256. More...
 

Friends

class boost::serialization::access
 

Detailed Description

template<typename T, typename IndexType = NeuronID>
class auryn::AurynVector< T, IndexType >

Default Auryn vector template.

This class implements the base vector class for Auryn. Neuronal or synaptic state variables are stored as AurynVectors. For performance reasons, time critical functions of this template have to be reimplemented in derived classes with a specific template parameter T. For instance I will always provide a derived type AurynVectorFloat (T=float) which per default is synonymous to AurynStateVector which implements SSE instructions for labour intensive operations on the vectors. When using the AurynStateVector in NeuronGroups etc this will speed up computation performance automatically. Note, that all Auryn vectors should initialized with multiple of 4 elements (later that number might change) when we add AVX support to the code. This is done automatically

Constructor & Destructor Documentation

◆ AurynVector() [1/2]

template<typename T, typename IndexType = NeuronID>
auryn::AurynVector< T, IndexType >::AurynVector ( IndexType  n)
inline

Default constructor.

158  {
159  allocate(n);
160  }
void allocate(const NeuronID n)
Implements aligned memory allocation.
Definition: AurynVector.h:100
int n
Definition: mkpat.py:5

◆ AurynVector() [2/2]

template<typename T, typename IndexType = NeuronID>
auryn::AurynVector< T, IndexType >::AurynVector ( AurynVector< T, IndexType > *  vec)
inline

Copy constructor.

Constructs vector as a copy of argument vector.

166  {
167  allocate(vec->size);
168  copy(vec);
169  }
void allocate(const NeuronID n)
Implements aligned memory allocation.
Definition: AurynVector.h:100
void copy(AurynVector *v)
Copies vector v.
Definition: AurynVector.h:200

◆ ~AurynVector()

template<typename T, typename IndexType = NeuronID>
virtual auryn::AurynVector< T, IndexType >::~AurynVector ( )
inlinevirtual

Default destructor.

174  {
175  freebuf();
176  }
void freebuf()
Definition: AurynVector.h:122

Member Function Documentation

◆ abs()

template<typename T, typename IndexType = NeuronID>
void auryn::AurynVector< T, IndexType >::abs ( )
inline

Takes absolute value of each element.

542  {
543  sqr();
544  sqrt();
545  }
void sqr()
Squares each element.
Definition: AurynVector.h:533
void sqrt()
Takes the square root of each element.
Definition: AurynVector.h:453

◆ add() [1/2]

template<typename T, typename IndexType = NeuronID>
void auryn::AurynVector< T, IndexType >::add ( const T  c)
inline

Adds constant c to each vector element.

256  {
257  for ( IndexType i = 0 ; i < size ; ++i ) {
258  data[i] += c;
259  }
260  }
T * data
Pointer to the array housing the data.
Definition: AurynVector.h:154
IndexType size
Size of the vector.
Definition: AurynVector.h:151

◆ add() [2/2]

template<typename T, typename IndexType = NeuronID>
void auryn::AurynVector< T, IndexType >::add ( AurynVector< T, IndexType > *  v)
inline

Adds a vector v to the vector.

No checking of the dimensions match!

280  {
281  check_size(v);
282  for ( IndexType i = 0 ; i < size ; ++i ) {
283  data[i] += v->data[i];
284  }
285  }
void check_size(IndexType x)
Checks if argument is larger than size and throws and exception if so.
Definition: AurynVector.h:78
T * data
Pointer to the array housing the data.
Definition: AurynVector.h:154
IndexType size
Size of the vector.
Definition: AurynVector.h:151

◆ add_random_normal()

template<typename T, typename IndexType = NeuronID>
void auryn::AurynVector< T, IndexType >::add_random_normal ( AurynState  mean = 0.0,
AurynState  sigma = 1.0,
unsigned int  seed = 8721 
)
inline
717  {
718  if ( seed == 0 )
719  seed = static_cast<unsigned int>(std::time(0));
720  boost::mt19937 randgen(seed);
721  boost::normal_distribution<> dist((double)mean, (double)sigma);
722  boost::variate_generator<boost::mt19937&, boost::normal_distribution<> > die(randgen, dist);
723  AurynState rv;
724  for ( IndexType i = 0 ; i<size ; ++i ) {
725  rv = die();
726  data[i] += rv;
727  }
728  }
T * data
Pointer to the array housing the data.
Definition: AurynVector.h:154
double mean()
Computes the mean of the vector elements on this rank.
Definition: AurynVector.h:622
IndexType size
Size of the vector.
Definition: AurynVector.h:151
AurynFloat AurynState
Type for Auryn state variables (default single precision since it needs to be compatible with auryn_v...
Definition: auryn_definitions.h:160

◆ add_specific()

template<typename T, typename IndexType = NeuronID>
void auryn::AurynVector< T, IndexType >::add_specific ( const IndexType  i,
const T  c 
)
inline

Adds the value c to specific vector element i.

264  {
265  check_size(i);
266  data[i] += c;
267  }
void check_size(IndexType x)
Checks if argument is larger than size and throws and exception if so.
Definition: AurynVector.h:78
T * data
Pointer to the array housing the data.
Definition: AurynVector.h:154

◆ allocate()

template<typename T, typename IndexType = NeuronID>
void auryn::AurynVector< T, IndexType >::allocate ( const NeuronID  n)
inlineprotected

Implements aligned memory allocation.

Todo:
TODO Replace above alignment code with boost code once boost 1.56 is commonly available with the dists
100  {
101 #if defined(CODE_USE_SIMD_INSTRUCTIONS_EXPLICITLY) && defined(CODE_ALIGNED_SIMD_INSTRUCTIONS)
102  std::size_t mem_alignment = sizeof(T)*SIMD_NUM_OF_PARALLEL_FLOAT_OPERATIONS;
103  std::size_t mem_size = sizeof(T)*n;
104  mem = malloc(mem_size+mem_alignment-1); // adds padding to allocated memory
105  T * ptr = (T*)mem;
106  if ( (unsigned long)mem%mem_alignment ) ptr = (T*)(((unsigned long)mem/mem_alignment+1)*mem_alignment);
108  // T * ptr = (T*)boost::alignment::aligned_alloc(sizeof(T)*SIMD_NUM_OF_PARALLEL_FLOAT_OPERATIONS,sizeof(T)*n);
109  if ( mem == NULL ) {
110  // TODO implement proper exception handling
111  throw AurynMemoryAlignmentException();
112  }
113  assert(((unsigned long)ptr % mem_alignment) == 0);
114 #else
115  T * ptr = new T[n];
116 #endif
117  data = ptr;
118  size = n;
119  set_zero();
120  }
T * data
Pointer to the array housing the data.
Definition: AurynVector.h:154
void set_zero()
Set all elements to zero.
Definition: AurynVector.h:240
int n
Definition: mkpat.py:5
IndexType size
Size of the vector.
Definition: AurynVector.h:151
#define SIMD_NUM_OF_PARALLEL_FLOAT_OPERATIONS
Use Intel Cilk Plus – only has an effect when CODE_USE_SIMD_INSTRUCTIONS_EXPLICITLY is enabled...
Definition: auryn_definitions.h:64
T * ptr(IndexType i=0)
Gets pointer to element i from vector.
Definition: AurynVector.h:217

◆ any() [1/2]

template<typename T, typename IndexType = NeuronID>
bool auryn::AurynVector< T, IndexType >::any ( )
inline

Returns true if any element is nonzero.

744  {
745  for ( IndexType i = 0 ; i < size ; ++i ) {
746  if ( get(i) ) return true;
747  }
748  return false;
749  }
IndexType size
Size of the vector.
Definition: AurynVector.h:151

◆ any() [2/2]

template<typename T, typename IndexType = NeuronID>
bool auryn::AurynVector< T, IndexType >::any ( eps)
inline

Returns true if any element is nonzero.

752  {
753  for ( IndexType i = 0 ; i < size ; ++i ) {
754  if ( std::abs(get(i))>eps ) return true;
755  }
756  return false;
757  }
IndexType size
Size of the vector.
Definition: AurynVector.h:151

◆ check_size() [1/2]

template<typename T, typename IndexType = NeuronID>
void auryn::AurynVector< T, IndexType >::check_size ( IndexType  x)
inlineprotected

Checks if argument is larger than size and throws and exception if so.

Check only enabled if NDEBUG is not defined.

79  {
80 #ifndef NDEBUG
81  if ( x >= size ) {
82  throw AurynVectorDimensionalityException();
83  }
84 #endif
85  }
IndexType size
Size of the vector.
Definition: AurynVector.h:151

◆ check_size() [2/2]

template<typename T, typename IndexType = NeuronID>
void auryn::AurynVector< T, IndexType >::check_size ( AurynVector< T, IndexType > *  v)
inlineprotected

Checks if vector size matches to this instance.

Check only enabled if NDEBUG is not defined.

91  {
92 #ifndef NDEBUG
93  if ( v->size != size ) {
94  throw AurynVectorDimensionalityException();
95  }
96 #endif
97  }
IndexType size
Size of the vector.
Definition: AurynVector.h:151

◆ clip()

template<typename T, typename IndexType = NeuronID>
void auryn::AurynVector< T, IndexType >::clip ( min,
max 
)
inline

Clips all vector elements to the range min max.

Parameters
minMinimum value
maxMaximum value
575  {
576  for ( IndexType i = 0 ; i < size ; ++i ) {
577  if ( data[i] < min ) {
578  data[i] = min;
579  } else
580  if ( data[i] > max )
581  data[i] = max;
582  }
583  }
T * data
Pointer to the array housing the data.
Definition: AurynVector.h:154
IndexType size
Size of the vector.
Definition: AurynVector.h:151
double min()
Returns the min of the vector elements.
Definition: AurynVector.h:682
double max()
Returns the max of the vector elements.
Definition: AurynVector.h:669

◆ copy()

template<typename T, typename IndexType = NeuronID>
void auryn::AurynVector< T, IndexType >::copy ( AurynVector< T, IndexType > *  v)
inline

Copies vector v.

201  {
202  check_size(v);
203  std::copy(v->data, v->data+v->size, data);
204  }
void check_size(IndexType x)
Checks if argument is larger than size and throws and exception if so.
Definition: AurynVector.h:78
T * data
Pointer to the array housing the data.
Definition: AurynVector.h:154

◆ diff() [1/3]

template<typename T, typename IndexType = NeuronID>
void auryn::AurynVector< T, IndexType >::diff ( AurynVector< T, IndexType > *  a,
AurynVector< T, IndexType > *  b 
)
inline

Computes the difference a-b and stores the result in this instance.

503  {
504  check_size(a);
505  check_size(b);
506  for ( IndexType i = 0 ; i < size ; ++i ) {
507  data[i] = a->data[i]-b->data[i];
508  }
509  }
void check_size(IndexType x)
Checks if argument is larger than size and throws and exception if so.
Definition: AurynVector.h:78
T * data
Pointer to the array housing the data.
Definition: AurynVector.h:154
IndexType size
Size of the vector.
Definition: AurynVector.h:151

◆ diff() [2/3]

template<typename T, typename IndexType = NeuronID>
void auryn::AurynVector< T, IndexType >::diff ( AurynVector< T, IndexType > *  a,
const T  b 
)
inline

Computes the difference a-b and stores the result in this instance.

515  {
516  sum(a,-b);
517  }
void sum(AurynVector *a, AurynVector *b)
Computes the sum a+b and stores the result in this instance.
Definition: AurynVector.h:479

◆ diff() [3/3]

template<typename T, typename IndexType = NeuronID>
void auryn::AurynVector< T, IndexType >::diff ( const T  a,
AurynVector< T, IndexType > *  b 
)
inline

Computes the difference a-b and stores the result in this instance.

523  {
524  check_size(b);
525  sum(b,-a);
526  neg();
527  }
void sum(AurynVector *a, AurynVector *b)
Computes the sum a+b and stores the result in this instance.
Definition: AurynVector.h:479
void check_size(IndexType x)
Checks if argument is larger than size and throws and exception if so.
Definition: AurynVector.h:78
void neg()
Flips the sign of all elements.
Definition: AurynVector.h:461

◆ div() [1/3]

template<typename T, typename IndexType = NeuronID>
void auryn::AurynVector< T, IndexType >::div ( const T  a)
inline

Element-wise division.

323  {
324  scale(1.0/a);
325  }
void scale(const T a)
Scales all vector elements by a.
Definition: AurynVector.h:246

◆ div() [2/3]

template<typename T, typename IndexType = NeuronID>
void auryn::AurynVector< T, IndexType >::div ( AurynVector< T, IndexType > *  v)
inline

Element-wise vector division.

331  {
332  check_size(v);
333  for ( IndexType i = 0 ; i < size ; ++i ) {
334  data[i] /= v->data[i];
335  }
336  }
void check_size(IndexType x)
Checks if argument is larger than size and throws and exception if so.
Definition: AurynVector.h:78
T * data
Pointer to the array housing the data.
Definition: AurynVector.h:154
IndexType size
Size of the vector.
Definition: AurynVector.h:151

◆ div() [3/3]

template<typename T, typename IndexType = NeuronID>
void auryn::AurynVector< T, IndexType >::div ( AurynVector< T, IndexType > *  a,
AurynVector< T, IndexType > *  b 
)
inline

Element-wise vector division which stores the result in this.

Computes elemntwise a/b and stores the result in current instance.

344  {
345  check_size(a);
346  for ( IndexType i = 0 ; i < size ; ++i ) {
347  data[i] = a->data[i]/b->data[i];
348  }
349  }
void check_size(IndexType x)
Checks if argument is larger than size and throws and exception if so.
Definition: AurynVector.h:78
T * data
Pointer to the array housing the data.
Definition: AurynVector.h:154
IndexType size
Size of the vector.
Definition: AurynVector.h:151

◆ element_sum()

template<typename T, typename IndexType = NeuronID>
double auryn::AurynVector< T, IndexType >::element_sum ( )
inline

Computes the sum of the vector elements.

631  {
632  double sum = 0.0;
633  for ( IndexType i = 0 ; i < size ; ++i ) {
634  sum += get(i);
635  }
636  return sum;
637  }
void sum(AurynVector *a, AurynVector *b)
Computes the sum a+b and stores the result in this instance.
Definition: AurynVector.h:479
IndexType size
Size of the vector.
Definition: AurynVector.h:151

◆ elementwise_max() [1/2]

template<typename T, typename IndexType = NeuronID>
void auryn::AurynVector< T, IndexType >::elementwise_max ( AurynVector< T, IndexType > *  v1,
AurynVector< T, IndexType > *  v2 
)
inline

Elementwise max operation.

389  {
390  check_size(v1);
391  check_size(v2);
392  for ( IndexType i = 0 ; i < size ; ++i ) {
393  data[i] = std::max(v1->data[i],v2->data[i]);
394  }
395  }
void check_size(IndexType x)
Checks if argument is larger than size and throws and exception if so.
Definition: AurynVector.h:78
T * data
Pointer to the array housing the data.
Definition: AurynVector.h:154
IndexType size
Size of the vector.
Definition: AurynVector.h:151

◆ elementwise_max() [2/2]

template<typename T, typename IndexType = NeuronID>
void auryn::AurynVector< T, IndexType >::elementwise_max ( AurynVector< T, IndexType > *  v1)
inline

Elementwise max operation with another vector.

399  {
400  elementwise_max(this,v1);
401  }
void elementwise_max(AurynVector *v1, AurynVector *v2)
Elementwise max operation.
Definition: AurynVector.h:388

◆ exp()

template<typename T, typename IndexType = NeuronID>
void auryn::AurynVector< T, IndexType >::exp ( )
inline

Computes exp(x) for each vector element.

431  {
432  for ( IndexType i = 0 ; i < size ; ++i ) {
433  data[i] = std::exp(data[i]);
434  }
435  }
T * data
Pointer to the array housing the data.
Definition: AurynVector.h:154
IndexType size
Size of the vector.
Definition: AurynVector.h:151

◆ fast_exp()

template<typename T, typename IndexType = NeuronID>
void auryn::AurynVector< T, IndexType >::fast_exp ( )
inline

Computes an approximation of exp(x) for each vector element.

417  {
418  // mul(0.00390625); // ie. 1.0/256.0
419  // add(1.0);
420  // for ( int i = 0 ; i < 8 ; ++i ) {
421  // sqr();
422  // }
423  // seems as if the naive version is faster
424  for ( IndexType i = 0 ; i < size ; ++i ) {
425  data[i] = fast_exp256(data[i]);
426  }
427  }
T fast_exp256(T x)
Computes approximation of exp(x) via fast series approximation up to n=256.
Definition: AurynVector.h:133
T * data
Pointer to the array housing the data.
Definition: AurynVector.h:154
IndexType size
Size of the vector.
Definition: AurynVector.h:151

◆ fast_exp256()

template<typename T, typename IndexType = NeuronID>
T auryn::AurynVector< T, IndexType >::fast_exp256 ( x)
inlineprotected

Computes approximation of exp(x) via fast series approximation up to n=256.

134  {
135  x = 1.0 + x / 256.0;
136  x *= x; x *= x; x *= x; x *= x;
137  x *= x; x *= x; x *= x; x *= x;
138  return x;
139  }

◆ follow()

template<typename T, typename IndexType = NeuronID>
void auryn::AurynVector< T, IndexType >::follow ( AurynVector< T, IndexType > *  v,
const T  rate 
)
inline

Follows target vector v with rate.

Example to implement the ODE: tau dx/dt = -(x-v)

the rate parameter should be set to auryn_timestep/tau.

373  {
374  for ( IndexType i = 0 ; i < size ; ++i ) {
375  data[i] += rate*(v->data[i]-data[i]);
376  }
377  }
T * data
Pointer to the array housing the data.
Definition: AurynVector.h:154
IndexType size
Size of the vector.
Definition: AurynVector.h:151

◆ follow_scalar()

template<typename T, typename IndexType = NeuronID>
void auryn::AurynVector< T, IndexType >::follow_scalar ( const T  a,
const T  rate 
)
inline

Like follow but with a scalar target value a.

381  {
382  for ( IndexType i = 0 ; i < size ; ++i ) {
383  data[i] += rate*(a-data[i]);
384  }
385  }
T * data
Pointer to the array housing the data.
Definition: AurynVector.h:154
IndexType size
Size of the vector.
Definition: AurynVector.h:151

◆ freebuf()

template<typename T, typename IndexType = NeuronID>
void auryn::AurynVector< T, IndexType >::freebuf ( )
inlineprotected
Todo:
TODO Replace above alignment code with boost code once boost 1.56 is commonly available with the dists
122  {
123 #if defined(CODE_USE_SIMD_INSTRUCTIONS_EXPLICITLY) && defined(CODE_ALIGNED_SIMD_INSTRUCTIONS)
124  free(mem);
126  // boost::alignment::aligned_free(data);
127 #else
128  delete [] data;
129 #endif
130  }
T * data
Pointer to the array housing the data.
Definition: AurynVector.h:154

◆ get()

template<typename T, typename IndexType = NeuronID>
T auryn::AurynVector< T, IndexType >::get ( IndexType  i)
inline

Gets element i from vector.

208  {
209  check_size(i);
210  return data[i];
211  }
void check_size(IndexType x)
Checks if argument is larger than size and throws and exception if so.
Definition: AurynVector.h:78
T * data
Pointer to the array housing the data.
Definition: AurynVector.h:154

◆ inv()

template<typename T, typename IndexType = NeuronID>
void auryn::AurynVector< T, IndexType >::inv ( )
inline

Computes 1./x of each element.

470  {
471  for ( IndexType i = 0 ; i < size ; ++i ) {
472  data[i] = 1.0/data[i];
473  }
474  }
T * data
Pointer to the array housing the data.
Definition: AurynVector.h:154
IndexType size
Size of the vector.
Definition: AurynVector.h:151

◆ l1norm()

template<typename T, typename IndexType = NeuronID>
double auryn::AurynVector< T, IndexType >::l1norm ( )
inline

Computes the l1 norm of the vector.

643  {
644  double sum = 0.0;
645  for ( IndexType i = 0 ; i < size ; ++i ) {
646  double e = get(i);
647  sum += std::abs(e);
648  }
649  return sum;
650  }
void sum(AurynVector *a, AurynVector *b)
Computes the sum a+b and stores the result in this instance.
Definition: AurynVector.h:479
IndexType size
Size of the vector.
Definition: AurynVector.h:151

◆ l2norm()

template<typename T, typename IndexType = NeuronID>
double auryn::AurynVector< T, IndexType >::l2norm ( )
inline

Computes the l2 norm of the vector.

657  {
658  double sum = 0.0;
659  for ( IndexType i = 0 ; i < size ; ++i ) {
660  double e = get(i);
661  sum += e*e;
662  }
663  return std::sqrt(sum);
664  }
void sum(AurynVector *a, AurynVector *b)
Computes the sum a+b and stores the result in this instance.
Definition: AurynVector.h:479
IndexType size
Size of the vector.
Definition: AurynVector.h:151

◆ max()

template<typename T, typename IndexType = NeuronID>
double auryn::AurynVector< T, IndexType >::max ( )
inline

Returns the max of the vector elements.

670  {
671  double max = -1e64; // TODO use numeric limits for this
672  for ( IndexType i = 0 ; i < size ; ++i ) {
673  double el = get(i);
674  if ( el > max ) max = el;
675  }
676  return max;
677  }
IndexType size
Size of the vector.
Definition: AurynVector.h:151
double max()
Returns the max of the vector elements.
Definition: AurynVector.h:669

◆ mean()

template<typename T, typename IndexType = NeuronID>
double auryn::AurynVector< T, IndexType >::mean ( )
inline

Computes the mean of the vector elements on this rank.

Warning: Note that AurynVector can only compute the mean of all the subset of elements stored on rank it runs on.

623  {
624  return element_sum()/size;
625  }
IndexType size
Size of the vector.
Definition: AurynVector.h:151
double element_sum()
Computes the sum of the vector elements.
Definition: AurynVector.h:630

◆ min()

template<typename T, typename IndexType = NeuronID>
double auryn::AurynVector< T, IndexType >::min ( )
inline

Returns the min of the vector elements.

683  {
684  double min = 1e64; // TODO use numeric limits for this
685  for ( IndexType i = 0 ; i < size ; ++i ) {
686  double el = get(i);
687  if ( el < min ) min = el;
688  }
689  return min;
690  }
IndexType size
Size of the vector.
Definition: AurynVector.h:151
double min()
Returns the min of the vector elements.
Definition: AurynVector.h:682

◆ mul() [1/2]

template<typename T, typename IndexType = NeuronID>
void auryn::AurynVector< T, IndexType >::mul ( const T  a)
inline

Multiply all vector elements by constant.

304  {
305  scale(a);
306  }
void scale(const T a)
Scales all vector elements by a.
Definition: AurynVector.h:246

◆ mul() [2/2]

template<typename T, typename IndexType = NeuronID>
void auryn::AurynVector< T, IndexType >::mul ( AurynVector< T, IndexType > *  v)
inline

Element-wise vector multiply.

312  {
313  check_size(v);
314  for ( IndexType i = 0 ; i < size ; ++i ) {
315  data[i] *= v->data[i];
316  }
317  }
void check_size(IndexType x)
Checks if argument is larger than size and throws and exception if so.
Definition: AurynVector.h:78
T * data
Pointer to the array housing the data.
Definition: AurynVector.h:154
IndexType size
Size of the vector.
Definition: AurynVector.h:151

◆ mul_specific()

template<typename T, typename IndexType = NeuronID>
void auryn::AurynVector< T, IndexType >::mul_specific ( const IndexType  i,
const T  c 
)
inline

Multiply to specific vector element with data index i with the constant c.

271  {
272  check_size(i);
273  data[i] *= c;
274  }
void check_size(IndexType x)
Checks if argument is larger than size and throws and exception if so.
Definition: AurynVector.h:78
T * data
Pointer to the array housing the data.
Definition: AurynVector.h:154

◆ neg()

template<typename T, typename IndexType = NeuronID>
void auryn::AurynVector< T, IndexType >::neg ( )
inline

Flips the sign of all elements.

462  {
463  for ( IndexType i = 0 ; i < size ; ++i ) {
464  data[i] = -data[i];
465  }
466  }
T * data
Pointer to the array housing the data.
Definition: AurynVector.h:154
IndexType size
Size of the vector.
Definition: AurynVector.h:151

◆ neg_rect()

template<typename T, typename IndexType = NeuronID>
void auryn::AurynVector< T, IndexType >::neg_rect ( )
inline

Negatively rectifies all elements.

561  {
562  for ( IndexType i = 0 ; i < size ; ++i ) {
563  if ( data[i] > 0.0 ) {
564  data[i] = 0.0;
565  }
566  }
567  }
T * data
Pointer to the array housing the data.
Definition: AurynVector.h:154
IndexType size
Size of the vector.
Definition: AurynVector.h:151

◆ nonzero()

template<typename T, typename IndexType = NeuronID>
IndexType auryn::AurynVector< T, IndexType >::nonzero ( )
inline

Computes number of nonzero elements on this rank.

Warning: Note that AurynVector can only compute the mean of all the subset of elements stored on rank it runs on.

698  {
699  IndexType sum = 0;
700  for ( IndexType i = 0 ; i < size ; ++i ) {
701  if ( get(i) != 0 ) ++sum;
702  }
703  return sum;
704  }
void sum(AurynVector *a, AurynVector *b)
Computes the sum a+b and stores the result in this instance.
Definition: AurynVector.h:479
IndexType size
Size of the vector.
Definition: AurynVector.h:151

◆ pow()

template<typename T, typename IndexType = NeuronID>
void auryn::AurynVector< T, IndexType >::pow ( const unsigned int  n)
inline

Takes each element to the n-th power.

Parameters
nthe exponent
407  {
408  for ( IndexType i = 0 ; i < size ; ++i ) {
409  data[i] = std::pow(data[i],n);
410  }
411  }
T * data
Pointer to the array housing the data.
Definition: AurynVector.h:154
int n
Definition: mkpat.py:5
IndexType size
Size of the vector.
Definition: AurynVector.h:151

◆ print()

template<typename T, typename IndexType = NeuronID>
void auryn::AurynVector< T, IndexType >::print ( )
inline

Print vector elements to stdout for debugging.

760  {
761  for ( IndexType i = 0 ; i < size ; ++i ) {
762  std::cout << get(i) << " ";
763  }
764  std::cout << std::endl;
765  }
IndexType size
Size of the vector.
Definition: AurynVector.h:151

◆ ptr()

template<typename T, typename IndexType = NeuronID>
T* auryn::AurynVector< T, IndexType >::ptr ( IndexType  i = 0)
inline

Gets pointer to element i from vector.

When no argument is given the function returns the first element of data array of the vector.

218  {
219  check_size(i);
220  return data+i;
221  }
void check_size(IndexType x)
Checks if argument is larger than size and throws and exception if so.
Definition: AurynVector.h:78
T * data
Pointer to the array housing the data.
Definition: AurynVector.h:154

◆ rect()

template<typename T, typename IndexType = NeuronID>
void auryn::AurynVector< T, IndexType >::rect ( )
inline

Rectifies all elements.

550  {
551  for ( IndexType i = 0 ; i < size ; ++i ) {
552  if ( data[i] < 0.0 ) {
553  data[i] = 0.0;
554  }
555  }
556  }
T * data
Pointer to the array housing the data.
Definition: AurynVector.h:154
IndexType size
Size of the vector.
Definition: AurynVector.h:151

◆ resize()

template<typename T, typename IndexType = NeuronID>
virtual void auryn::AurynVector< T, IndexType >::resize ( IndexType  new_size)
inlinevirtual

resize data array to new_size

The function tries to preserve data while resizing. If a vector is downsized elements at the end are simply dropped. When the vector size is increased the new elements at the end are intialized with zeros.

Reimplemented in auryn::AurynVectorFloat, and auryn::AurynDelayVector.

185  {
186  if ( size != new_size ) {
187  T * old_data = data;
188  IndexType old_size = size;
189  allocate(new_size);
190  // copy old data
191  const size_t copy_size = std::min(old_size,new_size) * sizeof(T);
192  memcpy(data, old_data, copy_size);
193  free(old_data);
194  }
195  }
T * data
Pointer to the array housing the data.
Definition: AurynVector.h:154
void allocate(const NeuronID n)
Implements aligned memory allocation.
Definition: AurynVector.h:100
IndexType size
Size of the vector.
Definition: AurynVector.h:151

◆ saxpy()

template<typename T, typename IndexType = NeuronID>
void auryn::AurynVector< T, IndexType >::saxpy ( const T  a,
AurynVector< T, IndexType > *  x 
)
inline

SAXPY operation as in GSL.

Computes a*x + y and stores the result to y where y is the present instance.

Parameters
aThe scaling factor for the additional vector
xThe additional vector to add
358  {
359  check_size(x);
360  for ( IndexType i = 0 ; i < size ; ++i ) {
361  data[i] += a * x->data[i];
362  }
363  }
void check_size(IndexType x)
Checks if argument is larger than size and throws and exception if so.
Definition: AurynVector.h:78
T * data
Pointer to the array housing the data.
Definition: AurynVector.h:154
IndexType size
Size of the vector.
Definition: AurynVector.h:151

◆ scale()

template<typename T, typename IndexType = NeuronID>
void auryn::AurynVector< T, IndexType >::scale ( const T  a)
inline

Scales all vector elements by a.

247  {
248  for ( IndexType i = 0 ; i < size ; ++i ) {
249  data[i] *= a;
250  }
251  }
T * data
Pointer to the array housing the data.
Definition: AurynVector.h:154
IndexType size
Size of the vector.
Definition: AurynVector.h:151

◆ set()

template<typename T, typename IndexType = NeuronID>
void auryn::AurynVector< T, IndexType >::set ( IndexType  i,
value 
)
inline

Sets element i in vector to value.

225  {
226  check_size(i);
227  data[i] = value;
228  }
void check_size(IndexType x)
Checks if argument is larger than size and throws and exception if so.
Definition: AurynVector.h:78
T * data
Pointer to the array housing the data.
Definition: AurynVector.h:154

◆ set_all()

template<typename T, typename IndexType = NeuronID>
void auryn::AurynVector< T, IndexType >::set_all ( const T  v)
inline

Set all elements to value v.

233  {
234  for ( IndexType i = 0 ; i < size ; ++i ) {
235  data[i] = v;
236  }
237  }
T * data
Pointer to the array housing the data.
Definition: AurynVector.h:154
IndexType size
Size of the vector.
Definition: AurynVector.h:151

◆ set_random()

template<typename T, typename IndexType = NeuronID>
void auryn::AurynVector< T, IndexType >::set_random ( unsigned int  seed = 0)
inline

Initializes vector elements with Gaussian of unit varince and a seed derived from system time if no seed or seed of 0 is given.

739  {
740  set_random_normal(0.0,1.0,seed);
741  }
void set_random_normal(AurynState mean=0.0, AurynState sigma=1.0, unsigned int seed=8721)
Definition: AurynVector.h:730

◆ set_random_normal()

template<typename T, typename IndexType = NeuronID>
void auryn::AurynVector< T, IndexType >::set_random_normal ( AurynState  mean = 0.0,
AurynState  sigma = 1.0,
unsigned int  seed = 8721 
)
inline
731  {
732  set_all(0.0);
733  add_random_normal(mean, sigma);
734  }
void set_all(const T v)
Set all elements to value v.
Definition: AurynVector.h:232
double mean()
Computes the mean of the vector elements on this rank.
Definition: AurynVector.h:622
void add_random_normal(AurynState mean=0.0, AurynState sigma=1.0, unsigned int seed=8721)
Definition: AurynVector.h:716

◆ set_zero()

template<typename T, typename IndexType = NeuronID>
void auryn::AurynVector< T, IndexType >::set_zero ( )
inline

Set all elements to zero.

241  {
242  set_all(0.0);
243  }
void set_all(const T v)
Set all elements to value v.
Definition: AurynVector.h:232

◆ sigmoid()

template<typename T, typename IndexType = NeuronID>
void auryn::AurynVector< T, IndexType >::sigmoid ( AurynVector< T, IndexType > *  x,
const T  beta,
const T  thr 
)
inline

Computes sigmoid(beta*(x-thr)) for each vector element and stores result in this instance.

441  {
442  diff(x,thr);
443  mul(-beta);
444  exp();
445  add(1.0);
446  inv();
447  }
void diff(AurynVector *a, AurynVector *b)
Computes the difference a-b and stores the result in this instance.
Definition: AurynVector.h:502
void inv()
Computes 1./x of each element.
Definition: AurynVector.h:469
void exp()
Computes exp(x) for each vector element.
Definition: AurynVector.h:430
void add(const T c)
Adds constant c to each vector element.
Definition: AurynVector.h:255
void mul(const T a)
Multiply all vector elements by constant.
Definition: AurynVector.h:303

◆ sqr()

template<typename T, typename IndexType = NeuronID>
void auryn::AurynVector< T, IndexType >::sqr ( )
inline

Squares each element.

534  {
535  this->mul(this);
536  }
void mul(const T a)
Multiply all vector elements by constant.
Definition: AurynVector.h:303

◆ sqrt()

template<typename T, typename IndexType = NeuronID>
void auryn::AurynVector< T, IndexType >::sqrt ( )
inline

Takes the square root of each element.

454  {
455  for ( IndexType i = 0 ; i < size ; ++i ) {
456  data[i] = std::sqrt(data[i]);
457  }
458  }
T * data
Pointer to the array housing the data.
Definition: AurynVector.h:154
IndexType size
Size of the vector.
Definition: AurynVector.h:151

◆ std()

template<typename T, typename IndexType = NeuronID>
double auryn::AurynVector< T, IndexType >::std ( )
inline

Computes the standard deviation of all elements on this rank.

Warning: Note that AurynVector can only compute the mean of all the subset of elements stored on rank it runs on.

613  {
614  return std::sqrt(var());
615  }
double var()
Computes the variance of the vector elements on this rank.
Definition: AurynVector.h:593

◆ sub() [1/2]

template<typename T, typename IndexType = NeuronID>
void auryn::AurynVector< T, IndexType >::sub ( const T  c)
inline

Subtract constant c to each vector element.

289  {
290  add(-c);
291  }
void add(const T c)
Adds constant c to each vector element.
Definition: AurynVector.h:255

◆ sub() [2/2]

template<typename T, typename IndexType = NeuronID>
void auryn::AurynVector< T, IndexType >::sub ( AurynVector< T, IndexType > *  v)
inline

Elementwise subtraction.

295  {
296  check_size(v);
297  for ( IndexType i = 0 ; i < size ; ++i ) {
298  data[i] -= v->data[i];
299  }
300  }
void check_size(IndexType x)
Checks if argument is larger than size and throws and exception if so.
Definition: AurynVector.h:78
T * data
Pointer to the array housing the data.
Definition: AurynVector.h:154
IndexType size
Size of the vector.
Definition: AurynVector.h:151

◆ sum() [1/2]

template<typename T, typename IndexType = NeuronID>
void auryn::AurynVector< T, IndexType >::sum ( AurynVector< T, IndexType > *  a,
AurynVector< T, IndexType > *  b 
)
inline

Computes the sum a+b and stores the result in this instance.

480  {
481  check_size(a);
482  check_size(b);
483  for ( IndexType i = 0 ; i < size ; ++i ) {
484  data[i] = a->data[i]+b->data[i];
485  }
486  }
void check_size(IndexType x)
Checks if argument is larger than size and throws and exception if so.
Definition: AurynVector.h:78
T * data
Pointer to the array housing the data.
Definition: AurynVector.h:154
IndexType size
Size of the vector.
Definition: AurynVector.h:151

◆ sum() [2/2]

template<typename T, typename IndexType = NeuronID>
void auryn::AurynVector< T, IndexType >::sum ( AurynVector< T, IndexType > *  a,
const T  b 
)
inline

Computes the sum a+b and stores the result in this instance.

492  {
493  check_size(a);
494  for ( IndexType i = 0 ; i < size ; ++i ) {
495  data[i] = a->data[i]+b;
496  }
497  }
void check_size(IndexType x)
Checks if argument is larger than size and throws and exception if so.
Definition: AurynVector.h:78
T * data
Pointer to the array housing the data.
Definition: AurynVector.h:154
IndexType size
Size of the vector.
Definition: AurynVector.h:151

◆ var()

template<typename T, typename IndexType = NeuronID>
double auryn::AurynVector< T, IndexType >::var ( )
inline

Computes the variance of the vector elements on this rank.

Uses Bessel's correction to calculate an unbiased estimate of the population variance which requires n > 1 otherwise the output is not defined.

Warning: Note that AurynVector can only compute the mean of all the subset of elements stored on rank it runs on.

594  {
595  double sum = 0.0;
596  double sum2 = 0.0;
597  for ( IndexType i = 0 ; i < size ; ++i ) {
598  double elem = get(i);
599  sum += elem;
600  sum2 += std::pow(elem,2);
601  }
602  double var = (sum2-(sum*sum)/size)/(size-1);
603  return var;
604  }
void sum(AurynVector *a, AurynVector *b)
Computes the sum a+b and stores the result in this instance.
Definition: AurynVector.h:479
IndexType size
Size of the vector.
Definition: AurynVector.h:151
double var()
Computes the variance of the vector elements on this rank.
Definition: AurynVector.h:593

◆ write_to_file()

template<typename T, typename IndexType = NeuronID>
void auryn::AurynVector< T, IndexType >::write_to_file ( std::string  filename)
inline

Print vector elements to a text file for debugging.

768  {
769 
770  std::ofstream outfile;
771  outfile.open(filename.c_str(),std::ios::out);
772  if (!outfile) {
773  std::stringstream oss;
774  throw AurynOpenFileException();
775  }
776 
777  outfile << std::setprecision(7);
778 
779  for ( IndexType i = 0 ; i < size ; ++i ) {
780  outfile << get(i) << "\n";
781  }
782 
783  outfile.close();
784  }
IndexType size
Size of the vector.
Definition: AurynVector.h:151

◆ zero_effective_zeros()

template<typename T, typename IndexType = NeuronID>
void auryn::AurynVector< T, IndexType >::zero_effective_zeros ( const T  epsilon = 1e-3)
inline

Sets all values whose absolute value is smaller than epsilon to zero.

710  {
711  for ( IndexType i = 0 ; i < size ; ++i ) {
712  if ( std::abs(get(i)) < epsilon ) set(i, 0.0);
713  }
714  }
IndexType size
Size of the vector.
Definition: AurynVector.h:151

Friends And Related Function Documentation

◆ boost::serialization::access

template<typename T, typename IndexType = NeuronID>
friend class boost::serialization::access
friend

Member Data Documentation

◆ data

template<typename T, typename IndexType = NeuronID>
T* auryn::AurynVector< T, IndexType >::data

Pointer to the array housing the data.

◆ size

template<typename T, typename IndexType = NeuronID>
IndexType auryn::AurynVector< T, IndexType >::size

Size of the vector.

Todo:
Consider including a non_zero size paramter too, because we are using this template also in sparse matrices now for complex synaptic dynamics in which not all elements are necessarily used...

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