PetaVision  Alpha
BaseConnectionProbe.cpp
1 /*
2  * BaseConnectionProbe.cpp
3  *
4  * Created on: Oct 20, 2011
5  * Author: pschultz
6  */
7 
8 #include "BaseConnectionProbe.hpp"
9 
10 namespace PV {
11 
12 BaseConnectionProbe::BaseConnectionProbe() {}
13 
14 BaseConnectionProbe::BaseConnectionProbe(const char *name, HyPerCol *hc) { initialize(name, hc); }
15 
16 BaseConnectionProbe::~BaseConnectionProbe() { delete mIOTimer; }
17 
18 int BaseConnectionProbe::initialize(const char *name, HyPerCol *hc) {
19  int status = BaseProbe::initialize(name, hc);
20  return status;
21 }
22 
23 void BaseConnectionProbe::ioParam_targetName(enum ParamsIOFlag ioFlag) {
24  parent->parameters()->ioParamString(ioFlag, name, "targetConnection", &targetName, NULL, false);
25  if (targetName == NULL) {
27  }
28 }
29 
30 Response::Status BaseConnectionProbe::respond(std::shared_ptr<BaseMessage const> message) {
31  Response::Status status = BaseProbe::respond(message);
32  if (status != Response::SUCCESS) {
33  return status;
34  }
35  else if (
36  auto castMessage =
37  std::dynamic_pointer_cast<ConnectionProbeWriteParamsMessage const>(message)) {
38  return respondConnectionProbeWriteParams(castMessage);
39  }
40  else if (auto castMessage = std::dynamic_pointer_cast<ConnectionOutputMessage const>(message)) {
41  return respondConnectionOutput(castMessage);
42  }
43  else {
44  return status;
45  }
46 }
47 
48 Response::Status BaseConnectionProbe::respondConnectionProbeWriteParams(
49  std::shared_ptr<ConnectionProbeWriteParamsMessage const> message) {
50  writeParams();
51  return Response::SUCCESS;
52 }
53 
54 Response::Status BaseConnectionProbe::respondConnectionOutput(
55  std::shared_ptr<ConnectionOutputMessage const> message) {
56  mIOTimer->start();
57  Response::Status status = outputStateWrapper(message->mTime, message->mDeltaT);
58  mIOTimer->stop();
59  return status;
60 }
61 
63  std::shared_ptr<CommunicateInitInfoMessage const> message) {
64  auto status = BaseProbe::communicateInitInfo(message);
65  if (status != Response::SUCCESS) {
66  return status;
67  }
68 
69  bool failed = false;
70  mTargetConn = message->lookup<BaseConnection>(std::string(targetName));
71  if (mTargetConn == nullptr) {
72  ErrorLog().printf(
73  "%s, rank %d process: targetConnection \"%s\" is not a connection in the column.\n",
74  getDescription_c(),
75  parent->columnId(),
76  targetName);
77  failed = true;
78  ;
79  }
80  MPI_Barrier(parent->getCommunicator()->communicator());
81  if (failed) {
82  exit(EXIT_FAILURE);
83  }
84  return Response::SUCCESS;
85 }
86 
87 Response::Status BaseConnectionProbe::registerData(Checkpointer *checkpointer) {
88  auto status = BaseProbe::registerData(checkpointer);
89  if (!Response::completed(status)) {
90  return status;
91  }
92  mIOTimer = new Timer(getName(), "probe", "update");
93  checkpointer->registerTimer(mIOTimer);
94  return Response::SUCCESS;
95 }
96 
97 void BaseConnectionProbe::initOutputStreams(const char *filename, Checkpointer *checkpointer) {
98  MPIBlock const *mpiBlock = checkpointer->getMPIBlock();
99  if (getMPIBlock()->getRank() == 0) {
100  char const *probeOutputFilename = getProbeOutputFilename();
101  if (probeOutputFilename) {
102  std::string path(probeOutputFilename);
103  std::ios_base::openmode mode = std::ios_base::out;
104  if (!checkpointer->getCheckpointReadDirectory().empty()) {
105  mode |= std::ios_base::app;
106  }
107  if (path[0] != '/') {
108  path = checkpointer->makeOutputPathFilename(path);
109  }
110  auto stream = new FileStream(path.c_str(), mode, checkpointer->doesVerifyWrites());
111  mOutputStreams.push_back(stream);
112  }
113  else {
114  auto stream = new PrintStream(PV::getOutputStream());
115  mOutputStreams.push_back(stream);
116  }
117  }
118  else {
119  mOutputStreams.clear();
120  }
121 }
122 
123 } // end of namespace PV
virtual Response::Status registerData(Checkpointer *checkpointer) override
char const * getProbeOutputFilename()
Definition: BaseProbe.hpp:314
static bool completed(Status &a)
Definition: Response.hpp:49
virtual Response::Status registerData(Checkpointer *checkpointer) override
Definition: BaseProbe.cpp:320
virtual Response::Status outputStateWrapper(double timef, double dt)
Definition: BaseProbe.cpp:358
virtual void ioParam_targetName(enum ParamsIOFlag ioFlag) override
targetName: the name of the object that the probe attaches to. In LayerProbe, targetName is used to d...
virtual Response::Status communicateInitInfo(std::shared_ptr< CommunicateInitInfoMessage const > message) override
virtual void initOutputStreams(const char *filename, Checkpointer *checkpointer) override
void writeParams()
Definition: BaseObject.hpp:69
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...
Definition: BaseProbe.cpp:89
int initialize(const char *name, HyPerCol *hc)
Definition: BaseProbe.cpp:64
virtual Response::Status communicateInitInfo(std::shared_ptr< CommunicateInitInfoMessage const > message) override=0
Definition: BaseProbe.cpp:240
std::string makeOutputPathFilename(std::string const &path)