PetaVision  Alpha
QuotientColProbe.cpp
1 /*
2  * QuotientColProbe.cpp
3  *
4  * Created on: Aug 12, 2015
5  * Author: pschultz
6  */
7 
8 #include "QuotientColProbe.hpp"
9 #include "columns/HyPerCol.hpp"
10 #include <limits>
11 
12 namespace PV {
13 
15  : ColProbe() { // Default constructor to be called by derived classes.
16  // They should call QuotientColProbe::initialize from their own initialization
17  // routine
18  // instead of calling a non-default constructor.
20 } // end QuotientColProbe::QuotientColProbe(const char *)
21 
22 QuotientColProbe::QuotientColProbe(const char *probename, HyPerCol *hc) : ColProbe() {
24  initializeQuotientColProbe(probename, hc);
25 }
26 
28  free(valueDescription);
29  free(numerator);
30  free(denominator);
31  // Don't free numerProbe or denomProbe; they don't belong to the
32  // QuotientColProbe.
33 }
34 
36  valueDescription = NULL;
37  numerator = NULL;
38  denominator = NULL;
39  numerProbe = NULL;
40  denomProbe = NULL;
41  return PV_SUCCESS;
42 }
43 
44 int QuotientColProbe::initializeQuotientColProbe(const char *probename, HyPerCol *hc) {
45  return ColProbe::initialize(probename, hc);
46 }
47 
49  for (auto &s : mOutputStreams) {
50  *s << "Probe_name,time,index," << valueDescription;
51  }
52 }
53 
54 int QuotientColProbe::ioParamsFillGroup(enum ParamsIOFlag ioFlag) {
55  int status = ColProbe::ioParamsFillGroup(ioFlag);
57  ioParam_numerator(ioFlag);
58  ioParam_denominator(ioFlag);
59  return status;
60 }
61 
62 void QuotientColProbe::ioParam_valueDescription(enum ParamsIOFlag ioFlag) {
63  parent->parameters()->ioParamString(
64  ioFlag, name, "valueDescription", &valueDescription, "value", true /*warnIfAbsent*/);
65 }
66 
67 void QuotientColProbe::ioParam_numerator(enum ParamsIOFlag ioFlag) {
68  parent->parameters()->ioParamStringRequired(ioFlag, name, "numerator", &numerator);
69 }
70 
71 void QuotientColProbe::ioParam_denominator(enum ParamsIOFlag ioFlag) {
72  parent->parameters()->ioParamStringRequired(ioFlag, name, "denominator", &denominator);
73 }
74 
75 Response::Status
76 QuotientColProbe::communicateInitInfo(std::shared_ptr<CommunicateInitInfoMessage const> message) {
77  Response::Status status = ColProbe::communicateInitInfo(message);
78  if (!Response::completed(status)) {
79  return status;
80  }
81  numerProbe = message->lookup<BaseProbe>(std::string(numerator));
82  denomProbe = message->lookup<BaseProbe>(std::string(denominator));
83  bool failed = false;
84  if (numerProbe == NULL || denomProbe == NULL) {
85  failed = true;
86  if (parent->columnId() == 0) {
87  if (numerProbe == NULL) {
88  ErrorLog().printf(
89  "%s: numerator probe \"%s\" could not be found.\n",
90  getDescription_c(),
91  numerator);
92  }
93  if (denomProbe == NULL) {
94  ErrorLog().printf(
95  "%s: denominator probe \"%s\" could not be found.\n",
96  getDescription_c(),
97  denominator);
98  }
99  }
100  }
101  int nNumValues, dNumValues;
102  if (!failed) {
103  nNumValues = numerProbe->getNumValues();
104  dNumValues = denomProbe->getNumValues();
105  if (nNumValues != dNumValues) {
106  if (parent->columnId() == 0) {
107  ErrorLog().printf(
108  "%s: numerator probe \"%s\" and denominator "
109  "probe \"%s\" have differing numbers "
110  "of values (%d vs. %d)\n",
111  getDescription_c(),
112  numerator,
113  denominator,
114  nNumValues,
115  dNumValues);
116  }
117  failed = true;
118  }
119  }
120  if (!failed) {
121  setNumValues(nNumValues);
122  }
123  if (failed) {
124  MPI_Barrier(parent->getCommunicator()->communicator());
125  exit(EXIT_FAILURE);
126  }
127  return Response::SUCCESS;
128 }
129 
130 void QuotientColProbe::calcValues(double timeValue) {
131  int numValues = this->getNumValues();
132  double *valuesBuffer = getValuesBuffer();
133  if (parent->simulationTime() == 0.0) {
134  for (int b = 0; b < numValues; b++) {
135  valuesBuffer[b] = 1.0;
136  }
137  return;
138  }
139  double n[numValues];
140  numerProbe->getValues(timeValue, n);
141  double d[numValues];
142  denomProbe->getValues(timeValue, d);
143  for (int b = 0; b < numValues; b++) {
144  valuesBuffer[b] = n[b] / d[b];
145  }
146 }
147 
148 double QuotientColProbe::referenceUpdateTime() const { return parent->simulationTime(); }
149 
150 Response::Status QuotientColProbe::outputState(double timevalue) {
151  getValues(timevalue);
152  if (mOutputStreams.empty()) {
153  return Response::SUCCESS;
154  }
155  double *valuesBuffer = getValuesBuffer();
156  int numValues = this->getNumValues();
157  for (int b = 0; b < numValues; b++) {
158  if (isWritingToFile()) {
159  output(b) << "\"" << valueDescription << "\",";
160  }
161  output(b) << timevalue << "," << b << "," << valuesBuffer[b] << std::endl;
162  }
163  return Response::SUCCESS;
164 } // end QuotientColProbe::outputState(float, HyPerCol *)
165 
166 } // end namespace PV
virtual void calcValues(double timeValue) override
void setNumValues(int n)
Definition: BaseProbe.cpp:221
PrintStream & output(int b)
Definition: BaseProbe.hpp:291
static bool completed(Status &a)
Definition: Response.hpp:49
virtual void outputHeader() override
virtual void ioParam_valueDescription(enum ParamsIOFlag ioFlag)
valueDescription: a short description of what the quantities computed by getValues() represent...
int initialize(const char *name, HyPerCol *hc)
Definition: ColProbe.cpp:31
int initializeQuotientColProbe(const char *probename, HyPerCol *hc)
double * getValuesBuffer()
Definition: BaseProbe.hpp:319
bool isWritingToFile() const
Definition: BaseProbe.hpp:330
void getValues(double timevalue, double *valuesVector)
Definition: BaseProbe.cpp:336
virtual void ioParam_numerator(enum ParamsIOFlag ioFlag)
numerator: A probe whose values are used in the numerators of the quotients. numerator can be a layer...
virtual Response::Status communicateInitInfo(std::shared_ptr< CommunicateInitInfoMessage const > message) override
Definition: ColProbe.cpp:82
virtual int ioParamsFillGroup(enum ParamsIOFlag ioFlag) override
int getNumValues()
Definition: BaseProbe.hpp:61
virtual double referenceUpdateTime() const override
virtual void ioParam_denominator(enum ParamsIOFlag ioFlag)
denominator: A probe whose values are used in the denominators of the quotients. denominator can be a...
virtual Response::Status communicateInitInfo(std::shared_ptr< CommunicateInitInfoMessage const > message) override
virtual int ioParamsFillGroup(enum ParamsIOFlag ioFlag) override
Definition: ColProbe.cpp:36
virtual Response::Status outputState(double timevalue) override