PetaVision  Alpha
KernelProbe.cpp
1 /*
2  * KernelPactchProbe.cpp
3  *
4  * Created on: Oct 21, 2011
5  * Author: pschultz
6  */
7 
8 #include "KernelProbe.hpp"
9 
10 namespace PV {
11 
12 KernelProbe::KernelProbe() { initialize_base(); }
13 
14 KernelProbe::KernelProbe(const char *probename, HyPerCol *hc) {
15  initialize_base();
16  int status = initialize(probename, hc);
17  assert(status == PV_SUCCESS);
18 }
19 
20 KernelProbe::~KernelProbe() {}
21 
22 int KernelProbe::initialize_base() { return PV_SUCCESS; }
23 
24 int KernelProbe::initialize(const char *probename, HyPerCol *hc) {
25  int status = BaseConnectionProbe::initialize(probename, hc);
26  assert(name && parent);
27 
28  return status;
29 }
30 
31 int KernelProbe::ioParamsFillGroup(enum ParamsIOFlag ioFlag) {
32  int status = BaseConnectionProbe::ioParamsFillGroup(ioFlag);
33  ioParam_kernelIndex(ioFlag);
34  ioParam_arborId(ioFlag);
35  ioParam_outputWeights(ioFlag);
36  ioParam_outputPlasticIncr(ioFlag);
37  ioParam_outputPatchIndices(ioFlag);
38  return status;
39 }
40 
41 void KernelProbe::ioParam_kernelIndex(enum ParamsIOFlag ioFlag) {
42  parent->parameters()->ioParamValue(ioFlag, name, "kernelIndex", &kernelIndex, 0);
43 }
44 
45 void KernelProbe::ioParam_arborId(enum ParamsIOFlag ioFlag) {
46  parent->parameters()->ioParamValue(ioFlag, name, "arborId", &arborID, 0);
47 }
48 
49 void KernelProbe::ioParam_outputWeights(enum ParamsIOFlag ioFlag) {
50  parent->parameters()->ioParamValue(
51  ioFlag, name, "outputWeights", &outputWeights, true /*default value*/);
52 }
53 
54 void KernelProbe::ioParam_outputPlasticIncr(enum ParamsIOFlag ioFlag) {
55  parent->parameters()->ioParamValue(
56  ioFlag, name, "outputPlasticIncr", &outputPlasticIncr, false /*default value*/);
57 }
58 
59 void KernelProbe::ioParam_outputPatchIndices(enum ParamsIOFlag ioFlag) {
60  parent->parameters()->ioParamValue(
61  ioFlag, name, "outputPatchIndices", &outputPatchIndices, false /*default value*/);
62 }
63 
65 
66 Response::Status
67 KernelProbe::communicateInitInfo(std::shared_ptr<CommunicateInitInfoMessage const> message) {
68  auto status = BaseHyPerConnProbe::communicateInitInfo(message);
69  if (!Response::completed(status)) {
70  return status;
71  }
72  auto *targetHyPerConn = getTargetHyPerConn();
73  assert(targetHyPerConn);
74  if (targetHyPerConn->getSharedWeights() == false) {
75  if (parent->getCommunicator()->globalCommRank() == 0) {
76  ErrorLog().printf(
77  "%s: %s is not using shared weights.\n",
78  getDescription_c(),
79  targetHyPerConn->getDescription_c());
80  }
81  MPI_Barrier(parent->getCommunicator()->communicator());
82  exit(EXIT_FAILURE);
83  }
84  return Response::SUCCESS;
85 }
86 
87 Response::Status KernelProbe::allocateDataStructures() {
88  auto status = BaseHyPerConnProbe::allocateDataStructures();
89  if (!Response::completed(status)) {
90  return status;
91  }
92  auto *targetHyPerConn = getTargetHyPerConn();
93  assert(targetHyPerConn);
94  if (getKernelIndex() < 0 || getKernelIndex() >= targetHyPerConn->getNumDataPatches()) {
95  Fatal().printf(
96  "KernelProbe \"%s\": kernelIndex %d is out of bounds. "
97  "(min 0, max %d)\n",
98  name,
99  getKernelIndex(),
100  targetHyPerConn->getNumDataPatches() - 1);
101  }
102  if (getArbor() < 0 || getArbor() >= getTargetHyPerConn()->getNumAxonalArbors()) {
103  Fatal().printf(
104  "KernelProbe \"%s\": arborId %d is out of bounds. (min 0, max %d)\n",
105  name,
106  getArbor(),
107  getTargetHyPerConn()->getNumAxonalArbors() - 1);
108  }
109 
110  if (!mOutputStreams.empty()) {
111  output(0) << "Probe \"" << name << "\", kernel index " << getKernelIndex() << ", arbor index "
112  << getArbor() << ".\n";
113  }
114  if (getOutputPatchIndices()) {
115  patchIndices(targetHyPerConn);
116  }
117 
118  return Response::SUCCESS;
119 }
120 
121 Response::Status KernelProbe::outputState(double timed) {
122  if (mOutputStreams.empty()) {
123  return Response::NO_ACTION;
124  }
125  Communicator *icComm = parent->getCommunicator();
126  const int rank = icComm->commRank();
127  auto *targetHyPerConn = getTargetHyPerConn();
128  assert(targetHyPerConn != nullptr);
129  int nxp = targetHyPerConn->getPatchSizeX();
130  int nyp = targetHyPerConn->getPatchSizeY();
131  int nfp = targetHyPerConn->getPatchSizeF();
132  int patchSize = nxp * nyp * nfp;
133 
134  const float *wdata = targetHyPerConn->getWeightsDataStart(arborID) + patchSize * kernelIndex;
135  const float *dwdata =
136  outputPlasticIncr
137  ? targetHyPerConn->getDeltaWeightsDataStart(arborID) + patchSize * kernelIndex
138  : NULL;
139  output(0) << "Time " << timed << ", Conn \"" << getTargetConn()->getName() << ", nxp=" << nxp
140  << ", nyp=" << nyp << ", nfp=" << nfp << "\n";
141  for (int f = 0; f < nfp; f++) {
142  for (int y = 0; y < nyp; y++) {
143  for (int x = 0; x < nxp; x++) {
144  int k = kIndex(x, y, f, nxp, nyp, nfp);
145  output(0) << " x=" << x << ", y=" << y << ", f=" << f << " (index " << k << "):";
146  if (getOutputWeights()) {
147  output(0) << " weight=" << wdata[k];
148  }
149  if (getOutputPlasticIncr()) {
150  output(0) << " dw=" << dwdata[k];
151  }
152  output(0) << "\n";
153  }
154  }
155  }
156 
157  return Response::SUCCESS;
158 }
159 
160 int KernelProbe::patchIndices(HyPerConn *conn) {
161  pvAssert(!mOutputStreams.empty());
162  int nxp = conn->getPatchSizeX();
163  int nyp = conn->getPatchSizeY();
164  int nfp = conn->getPatchSizeF();
165  int nPreExt = conn->getNumGeometryPatches();
166  assert(nPreExt == conn->getPre()->getNumExtended());
167  const PVLayerLoc *loc = conn->getPre()->getLayerLoc();
168  const PVHalo *halo = &loc->halo;
169  int nxPre = loc->nx;
170  int nyPre = loc->ny;
171  int nfPre = loc->nf;
172  int nxPreExt = nxPre + loc->halo.lt + loc->halo.rt;
173  int nyPreExt = nyPre + loc->halo.dn + loc->halo.up;
174  for (int kPre = 0; kPre < nPreExt; kPre++) {
175  Patch const *patch = conn->getPatch(kPre);
176  int xOffset = kxPos(patch->offset, nxp, nyp, nfp);
177  int yOffset = kyPos(patch->offset, nxp, nyp, nfp);
178  int kxPre = kxPos(kPre, nxPreExt, nyPreExt, nfPre) - loc->halo.lt;
179  int kyPre = kyPos(kPre, nxPreExt, nyPreExt, nfPre) - loc->halo.up;
180  int kfPre = featureIndex(kPre, nxPreExt, nyPreExt, nfPre);
181  output(0) << " presynaptic neuron " << kPre;
182  output(0) << " (x=" << kxPre << ", y=" << kyPre << ", f=" << kfPre;
183  output(0) << ") uses kernel index " << conn->calcDataIndexFromPatchIndex(kPre);
184  output(0) << ", starting at x=" << xOffset << ", y=" << yOffset << "\n";
185  }
186  return PV_SUCCESS;
187 }
188 
189 } // end of namespace PV block
virtual int ioParamsFillGroup(enum ParamsIOFlag ioFlag) override
Definition: KernelProbe.cpp:31
virtual Response::Status communicateInitInfo(std::shared_ptr< CommunicateInitInfoMessage const > message) override
void setNumValues(int n)
Definition: BaseProbe.cpp:221
virtual int ioParamsFillGroup(enum ParamsIOFlag ioFlag) override
Definition: BaseProbe.cpp:76
PrintStream & output(int b)
Definition: BaseProbe.hpp:291
static bool completed(Status &a)
Definition: Response.hpp:49
virtual Response::Status outputState(double timef) override
virtual void initNumValues() override
Definition: KernelProbe.cpp:64
virtual Response::Status communicateInitInfo(std::shared_ptr< CommunicateInitInfoMessage const > message) override
Definition: KernelProbe.cpp:67