PetaVision  Alpha
L1NormProbe.cpp
1 /*
2  * L1NormProbe.cpp
3  *
4  * Created on: Aug 11, 2015
5  * Author: pschultz
6  */
7 
8 #include <cmath>
9 
10 #include "L1NormProbe.hpp"
11 #include "columns/HyPerCol.hpp"
12 #include "layers/HyPerLayer.hpp"
13 
14 namespace PV {
15 
16 L1NormProbe::L1NormProbe() : AbstractNormProbe() { initialize_base(); }
17 
18 L1NormProbe::L1NormProbe(const char *name, HyPerCol *hc) : AbstractNormProbe() {
19  initialize_base();
20  initialize(name, hc);
21 }
22 
23 L1NormProbe::~L1NormProbe() {}
24 
25 int L1NormProbe::initialize(const char *name, HyPerCol *hc) {
26  return AbstractNormProbe::initialize(name, hc);
27 }
28 
29 double L1NormProbe::getValueInternal(double timevalue, int index) {
30  if (index < 0 || index >= parent->getNBatch()) {
31  return PV_FAILURE;
32  }
33  PVLayerLoc const *loc = getTargetLayer()->getLayerLoc();
34  int const nx = loc->nx;
35  int const ny = loc->ny;
36  int const nf = loc->nf;
37  PVHalo const *halo = &loc->halo;
38  int const lt = halo->lt;
39  int const rt = halo->rt;
40  int const dn = halo->dn;
41  int const up = halo->up;
42  double sum = 0.0;
43  float const *aBuffer =
44  getTargetLayer()->getLayerData() + index * getTargetLayer()->getNumExtended();
45  if (getMaskLayer()) {
46  PVLayerLoc const *maskLoc = getMaskLayer()->getLayerLoc();
47  PVHalo const *maskHalo = &maskLoc->halo;
48  float const *maskLayerData =
50  + index * getMaskLayer()->getNumExtended(); // Is there a DataStore method to return the
51  // part of the layer data for a given batch
52  // index?
53  int const maskLt = maskHalo->lt;
54  int const maskRt = maskHalo->rt;
55  int const maskDn = maskHalo->dn;
56  int const maskUp = maskHalo->up;
57  if (maskHasSingleFeature()) {
58  assert(getTargetLayer()->getNumNeurons() == nx * ny * nf);
59  int nxy = nx * ny;
60 #ifdef PV_USE_OPENMP_THREADS
61 #pragma omp parallel for reduction(+ : sum)
62 #endif // PV_USE_OPENMP_THREADS
63  for (int kxy = 0; kxy < nxy; kxy++) {
64  int kexMask = kIndexExtended(kxy, nx, ny, 1, maskLt, maskRt, maskDn, maskUp);
65  if (maskLayerData[kexMask]) {
66  int featureBase = kxy * nf;
67  for (int f = 0; f < nf; f++) {
68  int kex = kIndexExtended(featureBase++, nx, ny, nf, lt, rt, dn, up);
69  float val = aBuffer[kex];
70  sum += (double)std::fabs(val);
71  }
72  }
73  }
74  }
75  else {
76 #ifdef PV_USE_OPENMP_THREADS
77 #pragma omp parallel for reduction(+ : sum)
78 #endif // PV_USE_OPENMP_THREADS
79  for (int k = 0; k < getTargetLayer()->getNumNeurons(); k++) {
80  int kex = kIndexExtended(k, nx, ny, nf, lt, rt, dn, up);
81  int kexMask = kIndexExtended(k, nx, ny, nf, maskLt, maskRt, maskDn, maskUp);
82  if (maskLayerData[kexMask]) {
83  float val = aBuffer[kex];
84  sum += std::fabs((double)val);
85  }
86  }
87  }
88  }
89  else {
90  if (getTargetLayer()->getSparseFlag()) {
91  PVLayerCube cube = getTargetLayer()->getPublisher()->createCube();
92  long int numActive = cube.numActive[index];
93  int numItems = cube.numItems / cube.loc.nbatch;
94  SparseList<float>::Entry const *activeList =
95  &((SparseList<float>::Entry *)cube.activeIndices)[index * numItems];
96 #ifdef PV_USE_OPENMP_THREADS
97 #pragma omp parallel for reduction(+ : sum)
98 #endif // PV_USE_OPENMP_THREADS
99  for (int k = 0; k < numActive; k++) {
100  int extIndex = activeList[k].index;
101  int inRestricted = !extendedIndexInBorderRegion(
102  extIndex, nx, ny, nf, halo->lt, halo->rt, halo->dn, halo->up);
103  double val = inRestricted * (double)fabs(aBuffer[extIndex]);
104  sum += fabs(val);
105  }
106  }
107  else {
108 #ifdef PV_USE_OPENMP_THREADS
109 #pragma omp parallel for reduction(+ : sum)
110 #endif // PV_USE_OPENMP_THREADS
111  for (int k = 0; k < getTargetLayer()->getNumNeurons(); k++) {
112  int kex = kIndexExtended(k, nx, ny, nf, lt, rt, dn, up);
113  sum += (double)fabs(aBuffer[kex]);
114  }
115  }
116  }
117 
118  return sum;
119 }
120 
122 
123 } // end namespace PV
PVLayerCube createCube(int delay=0)
Definition: Publisher.cpp:60
int setNormDescriptionToString(char const *s)
virtual double getValueInternal(double timevalue, int index) override
Definition: L1NormProbe.cpp:29
virtual int setNormDescription() override
const float * getLayerData(int delay=0)