PetaVision  Alpha
RequireAllZeroActivityProbe.cpp
1 /*
2  * RequireAllZeroActivityProbe.cpp
3  *
4  * Created on: Mar 26, 2014
5  * Author: pschultz
6  */
7 
8 #include "RequireAllZeroActivityProbe.hpp"
9 
10 namespace PV {
11 
12 RequireAllZeroActivityProbe::RequireAllZeroActivityProbe(const char *name, HyPerCol *hc) {
13  initialize_base();
14  initialize(name, hc);
15 }
16 
17 RequireAllZeroActivityProbe::RequireAllZeroActivityProbe() { initialize_base(); }
18 
19 int RequireAllZeroActivityProbe::initialize_base() { return PV_SUCCESS; }
20 
21 int RequireAllZeroActivityProbe::initialize(const char *name, HyPerCol *hc) {
22  int status = StatsProbe::initialize(name, hc);
23  return status;
24 }
25 
26 int RequireAllZeroActivityProbe::ioParamsFillGroup(enum ParamsIOFlag ioFlag) {
27  int status = StatsProbe::ioParamsFillGroup(ioFlag);
28  ioParam_exitOnFailure(ioFlag);
30  return status;
31 }
32 
33 void RequireAllZeroActivityProbe::ioParam_buffer(enum ParamsIOFlag ioFlag) {
34  requireType(BufActivity);
35 }
36 
38  parent->parameters()->ioParamValue(ioFlag, name, "exitOnFailure", &exitOnFailure, exitOnFailure);
39 }
40 
42  pvAssert(!parent->parameters()->presentAndNotBeenRead(name, "exitOnFailure"));
43  if (exitOnFailure) {
44  parent->parameters()->ioParamValue(
45  ioFlag,
46  name,
47  "immediateExitOnFailure",
48  &immediateExitOnFailure,
49  immediateExitOnFailure);
50  }
51  else {
52  immediateExitOnFailure = false;
53  }
54 }
55 
56 Response::Status RequireAllZeroActivityProbe::outputState(double timed) {
57  auto status = StatsProbe::outputState(timed);
58  if (!Response::completed(status)) {
59  Fatal() << getDescription() << ": StatsProbe::outputState failed at time " << timed << ".\n";
60  }
61  for (int b = 0; b < parent->getNBatch(); b++) {
62  if (nnz[b] != 0) {
63  if (!nonzeroFound) {
64  nonzeroTime = timed;
65  }
66  nonzeroFound = true;
67  nonzeroFoundMessage(nonzeroTime, parent->columnId() == 0, immediateExitOnFailure);
68  }
69  }
70  return Response::SUCCESS;
71 }
72 
73 void RequireAllZeroActivityProbe::nonzeroFoundMessage(
74  double badTime,
75  bool isRoot,
76  bool fatalError) {
77  if (isRoot) {
78  std::stringstream message("");
79  message << getDescription_c() << ": Nonzero activity found at time " << badTime << "\n";
80  if (fatalError) {
81  Fatal() << message.str();
82  }
83  else {
84  WarnLog() << message.str();
85  }
86  }
87  if (fatalError) {
88  MPI_Barrier(parent->getCommunicator()->communicator());
89  exit(EXIT_FAILURE);
90  }
91 }
92 
93 RequireAllZeroActivityProbe::~RequireAllZeroActivityProbe() {
94  // We check for exits on failure in destructor
95  if (exitOnFailure && getNonzeroFound()) {
96  nonzeroFoundMessage(nonzeroTime, parent->columnId() == 0, true /*fatalError*/);
97  }
98 }
99 
100 } /* namespace PV */
virtual int ioParamsFillGroup(enum ParamsIOFlag ioFlag) override
Definition: StatsProbe.cpp:92
virtual void ioParam_immediateExitOnFailure(enum ParamsIOFlag ioFlag)
immediateExitOnFailure: determines when finding a nonzero value causes an exit with an error...
virtual Response::Status outputState(double timef) override
Definition: StatsProbe.cpp:193
virtual Response::Status outputState(double timed) override
static bool completed(Status &a)
Definition: Response.hpp:49
int ioParamsFillGroup(enum ParamsIOFlag ioFlag) override
virtual void ioParam_exitOnFailure(enum ParamsIOFlag ioFlag)
exitOnFailure: If true, will error out if a nonzero value is encountered. Default is true...