8 #include "BaseProbe.hpp" 9 #include "ColumnEnergyProbe.hpp" 10 #include "layers/HyPerLayer.hpp" 16 BaseProbe::BaseProbe() {
21 BaseProbe::~BaseProbe() {
22 for (
auto &s : mOutputStreams) {
25 mOutputStreams.clear();
32 free(probeOutputFilename);
33 probeOutputFilename = NULL;
34 if (triggerLayerName) {
35 free(triggerLayerName);
36 triggerLayerName = NULL;
42 int BaseProbe::initialize_base() {
46 textOutputFlag =
true;
47 probeOutputFilename = NULL;
49 triggerLayerName = NULL;
65 int status = BaseObject::initialize(name, hc);
66 if (status != PV_SUCCESS) {
74 void BaseProbe::setObjectType() { mObjectType =
lookupKeyword(); }
90 parent->parameters()->ioParamStringRequired(ioFlag, name,
"targetName", &targetName);
94 parent->parameters()->ioParamString(
95 ioFlag, name,
"message", &msgparams, NULL,
false );
96 if (ioFlag == PARAMS_IO_READ) {
102 parent->parameters()->ioParamString(
103 ioFlag, name,
"energyProbe", &energyProbe, NULL,
false );
107 assert(!parent->parameters()->presentAndNotBeenRead(name,
"energyProbe"));
108 if (energyProbe && energyProbe[0]) {
109 parent->parameters()->ioParamValue(
110 ioFlag, name,
"coefficient", &coefficient, coefficient,
true );
115 parent->parameters()->ioParamValue(
116 ioFlag, name,
"textOutputFlag", &textOutputFlag, textOutputFlag);
120 assert(!parent->parameters()->presentAndNotBeenRead(name,
"textOutputFlag"));
121 if (textOutputFlag) {
122 parent->parameters()->ioParamString(
123 ioFlag, name,
"probeOutputFile", &probeOutputFilename, NULL,
false );
128 parent->parameters()->ioParamString(
129 ioFlag, name,
"triggerLayerName", &triggerLayerName, NULL,
false );
130 if (ioFlag == PARAMS_IO_READ) {
131 triggerFlag = (triggerLayerName != NULL && triggerLayerName[0] !=
'\0');
140 assert(!parent->parameters()->presentAndNotBeenRead(name,
"triggerLayerName"));
141 if (ioFlag == PARAMS_IO_READ && parent->parameters()->
present(name,
"triggerFlag")) {
142 bool flagFromParams =
false;
143 parent->parameters()->ioParamValue(
144 ioFlag, name,
"triggerFlag", &flagFromParams, flagFromParams);
145 if (parent->columnId() == 0) {
146 Fatal(triggerFlagDeprecated);
147 triggerFlagDeprecated.printf(
148 "%s: triggerFlag is obsolete for probes.\n", getDescription_c());
149 triggerFlagDeprecated.printf(
150 " If triggerLayerName is a nonempty string, triggering will be on;\n");
151 triggerFlagDeprecated.printf(
152 " if triggerLayerName is empty or null, triggering will be off.\n");
158 assert(!parent->parameters()->presentAndNotBeenRead(name,
"triggerFlag"));
160 parent->parameters()->ioParamValue(
161 ioFlag, name,
"triggerOffset", &triggerOffset, triggerOffset);
162 if (triggerOffset < 0) {
164 "%s \"%s\" error in rank %d process: TriggerOffset (%f) " 165 "must be positive\n",
166 parent->parameters()->groupKeywordFromName(name),
175 MPIBlock const *mpiBlock = checkpointer->getMPIBlock();
178 if (blockColumnIndex == 0 and blockRowIndex == 0) {
179 int localBatchWidth = parent->getNBatch();
181 int localBatchOffset = localBatchWidth * mpiBatchIndex;
182 mOutputStreams.resize(localBatchWidth);
184 if (probeOutputFilename) {
185 std::string path(probeOutputFilename);
186 auto extensionStart = path.rfind(
'.');
187 std::string extension;
188 if (extensionStart != std::string::npos) {
189 extension = path.substr(extensionStart);
190 path = path.substr(0, extensionStart);
192 std::ios_base::openmode mode = std::ios_base::out;
193 if (!checkpointer->getCheckpointReadDirectory().empty()) {
194 mode |= std::ios_base::app;
196 for (
int b = 0; b < localBatchWidth; b++) {
197 int globalBatchIndex = b + localBatchOffset;
198 std::string batchPath = path;
199 std::string batchIndexString = std::to_string(globalBatchIndex);
200 batchPath.append(
"_batchElement_").append(batchIndexString).append(extension);
201 if (batchPath[0] !=
'/') {
204 auto fs =
new FileStream(batchPath.c_str(), mode, checkpointer->doesVerifyWrites());
205 mOutputStreams[b] = fs;
209 for (
int b = 0; b < localBatchWidth; b++) {
210 mOutputStreams[b] =
new PrintStream(PV::getOutputStream());
215 mOutputStreams.clear();
223 double *newValuesBuffer = (
double *)realloc(probeValues, (
size_t)n *
sizeof(*probeValues));
225 newValuesBuffer ==
nullptr,
226 "%s unable to set number of values to %d\n",
230 probeValues = newValuesBuffer;
235 probeValues =
nullptr;
243 triggerLayer = message->lookup<
HyPerLayer>(std::string(triggerLayerName));
244 if (triggerLayer == NULL) {
245 if (parent->columnId() == 0) {
247 "%s \"%s\": triggerLayer \"%s\" is not a layer in the HyPerCol.\n",
248 parent->parameters()->groupKeywordFromName(name),
252 MPI_Barrier(parent->getCommunicator()->communicator());
258 if (energyProbe && energyProbe[0]) {
261 if (parent->columnId() == 0) {
263 "%s \"%s\": energyProbe \"%s\" is not a ColumnEnergyProbe in the " 265 parent->parameters()->groupKeywordFromName(getName()),
269 MPI_Barrier(parent->getCommunicator()->communicator());
272 int termAdded = probe->
addTerm(
this);
274 termAdded != PV_SUCCESS,
275 "Failed to add %s to %s.\n",
277 probe->getDescription_c());
279 return Response::SUCCESS;
283 assert(msgstring == NULL);
284 int status = PV_SUCCESS;
285 if (msg != NULL && msg[0] !=
'\0') {
286 size_t msglen = strlen(msg);
287 this->msgstring = (
char *)calloc(
290 if (this->msgstring) {
291 memcpy(this->msgstring, msg, msglen);
292 this->msgstring[msglen] =
':';
293 this->msgstring[msglen + 1] =
'\0';
297 this->msgstring = (
char *)calloc(1,
sizeof(
char));
298 if (this->msgstring) {
299 this->msgstring[0] =
'\0';
302 if (!this->msgstring) {
304 "%s \"%s\": Unable to allocate memory for probe's message.\n",
305 parent->parameters()->groupKeywordFromName(name),
309 assert(status == PV_SUCCESS);
315 return triggerLayer->
needUpdate(simTime + triggerOffset, dt);
321 auto status = BaseObject::registerData(checkpointer);
326 return Response::SUCCESS;
338 memcpy(values, probeValues,
sizeof(*probeValues) * (
size_t)
getNumValues());
343 getValues(timevalue, &valuesVector->front());
348 return std::numeric_limits<double>::signaling_NaN();
355 return probeValues[index];
359 auto status = Response::NO_ACTION;
360 if (textOutputFlag &&
needUpdate(timef, dt)) {
virtual void ioParam_coefficient(enum ParamsIOFlag ioFlag)
coefficient: If energyProbe is set, the coefficient parameter specifies that ColumnEnergyProbe multip...
double getValue(double timevalue, int index)
virtual void calcValues(double timevalue)=0
int present(const char *groupName, const char *paramName)
int addTerm(BaseProbe *probe)
Adds a probe to the energy calculation.
virtual int ioParamsFillGroup(enum ParamsIOFlag ioFlag) override
char const * getProbeOutputFilename()
virtual void ioParam_message(enum ParamsIOFlag ioFlag)
message: A string parameter that is typically included in the lines output by the outputState method ...
virtual double referenceUpdateTime() const =0
virtual Response::Status outputState(double timef)=0
virtual void ioParam_energyProbe(enum ParamsIOFlag ioFlag)
energyProbe: If nonblank, specifies the name of a ColumnEnergyProbe that this probe contributes an en...
static bool completed(Status &a)
virtual void ioParam_triggerFlag(enum ParamsIOFlag ioFlag)
triggerFlag: If false, the needUpdate method always returns true, so that outputState is called every...
virtual void initNumValues()
virtual bool needRecalc(double timevalue)=0
int getColumnIndex() const
virtual void ioParam_triggerOffset(enum ParamsIOFlag ioFlag)
triggerOffset: If triggerFlag is true, triggerOffset specifies the time interval before the triggerLa...
virtual void initOutputStreams(const char *filename, Checkpointer *checkpointer)
virtual bool needUpdate(double simTime, double dt)
virtual bool needUpdate(double time, double dt)
char const * lookupKeyword() const
virtual void ioParam_textOutputFlag(enum ParamsIOFlag ioFlag)
textOutputFlag: A boolean parameter that sets whether to generate an output file. Defaults to true...
virtual Response::Status registerData(Checkpointer *checkpointer) override
virtual Response::Status outputStateWrapper(double timef, double dt)
void getValues(double timevalue, double *valuesVector)
virtual void ioParam_probeOutputFile(enum ParamsIOFlag ioFlag)
probeOutputFile: If textOutputFlag is true, probeOutputFile specifies the name of the file that the o...
int getBatchIndex() const
int getStartBatch() const
virtual void ioParam_triggerLayerName(enum ParamsIOFlag ioFlag)
triggerLayerName: If triggerFlag is true, triggerLayerName specifies the layer to check for triggerin...
virtual void ioParam_targetName(enum ParamsIOFlag ioFlag)
targetName: the name of the object that the probe attaches to. In LayerProbe, targetName is used to d...
int initialize(const char *name, HyPerCol *hc)
virtual Response::Status communicateInitInfo(std::shared_ptr< CommunicateInitInfoMessage const > message) override=0
std::string makeOutputPathFilename(std::string const &path)
virtual int initMessage(const char *msg)