PetaVision  Alpha
DataStore.cpp
1 /*
2  * DataStore.cpp
3  *
4  * Created on: Sep 10, 2008
5  * Author: Craig Rasmussen
6  */
7 
8 #include "DataStore.hpp"
9 #include "include/pv_common.h"
10 #include "utils/PVAssert.hpp"
11 #include "utils/PVLog.hpp"
12 
13 #include <limits>
14 
15 namespace PV {
16 
17 DataStore::DataStore(int numBuffers, int numItems, int numLevels, bool isSparse_flag) {
18  assert(numLevels > 0 && numBuffers > 0);
19  mCurrentLevel = 0; // Publisher::publish decrements levels when writing, so
20  // first level written
21  // to is numLevels - 1;
22  mNumItems = numItems;
23  mNumLevels = numLevels;
24  mNumBuffers = numBuffers;
25 
26  mBuffer = new RingBuffer<float>(numLevels, numBuffers * numItems);
27  mLastUpdateTimes = new RingBuffer<double>(
28  numLevels, numBuffers, -std::numeric_limits<double>::infinity() /*initial value*/);
29 
30  mSparseFlag = isSparse_flag;
31  if (mSparseFlag) {
32  mActiveIndices =
33  new RingBuffer<SparseList<float>::Entry>(numLevels, numBuffers * numItems, {0, 0});
34  mNumActive = new RingBuffer<long>(numLevels, numBuffers);
35  }
36 }
37 
38 void DataStore::markActiveIndicesOutOfSync(int bufferId, int level) {
39  if (!mSparseFlag) {
40  return;
41  }
42  long *numActiveBuf = numActiveBuffer(bufferId, level);
43  *numActiveBuf = -1;
44 }
45 
46 void DataStore::updateActiveIndices(int bufferId, int level) {
47  if (!mSparseFlag) {
48  return;
49  }
50  int numActive = 0;
51  float *activity = buffer(bufferId, level);
52 
53  SparseList<float>::Entry *activeIndices = activeIndicesBuffer(bufferId, level);
54  // unsigned int *activeIndices = activeIndicesBuffer(bufferId, level);
55  for (int kex = 0; kex < getNumItems(); kex++) {
56  float a = activity[kex];
57  if (a != 0.0f) {
58  activeIndices[numActive].index = kex;
59  activeIndices[numActive].value = a;
60  numActive++;
61  }
62  }
63 
64  long *numActiveBuf = numActiveBuffer(bufferId, level);
65  *numActiveBuf = numActive;
66 }
67 
69  PVLayerCube cube;
70  cube.size = sizeof(PVLayerCube);
71  cube.numItems = mNumItems * mNumBuffers;
72  cube.data = buffer(0 /*batch element*/, delay);
73  // All batch elements allocated contiguously, so the numItems and data fields cover all
74  // batch elements.
75  cube.loc = loc;
76  cube.isSparse = isSparse();
77  if (isSparse()) {
78  cube.numActive = numActiveBuffer(0, delay);
79  cube.activeIndices = activeIndicesBuffer(0, delay);
80  }
81  return cube;
82 }
83 
84 } // end namespace PV
PVLayerCube createCube(PVLayerLoc const &loc, int delay)
Definition: DataStore.cpp:68