PetaVision  Alpha
ConnectionData.cpp
1 /*
2  * ConnectionData.cpp
3  *
4  * Created on: Nov 17, 2017
5  * Author: pschultz
6  */
7 
8 #include "ConnectionData.hpp"
9 #include "columns/HyPerCol.hpp"
10 #include "columns/ObjectMapComponent.hpp"
11 #include "utils/MapLookupByType.hpp"
12 
13 namespace PV {
14 
15 ConnectionData::ConnectionData(char const *name, HyPerCol *hc) { initialize(name, hc); }
16 
17 ConnectionData::ConnectionData() {}
18 
19 ConnectionData::~ConnectionData() {
20  free(mPreLayerName);
21  free(mPostLayerName);
22 }
23 
24 int ConnectionData::initialize(char const *name, HyPerCol *hc) {
25  return BaseObject::initialize(name, hc);
26 }
27 
28 void ConnectionData::setObjectType() { mObjectType = "ConnectionData"; }
29 
30 int ConnectionData::ioParamsFillGroup(enum ParamsIOFlag ioFlag) {
31  ioParam_preLayerName(ioFlag);
32  ioParam_postLayerName(ioFlag);
33  return PV_SUCCESS;
34 }
35 
36 void ConnectionData::ioParam_preLayerName(enum ParamsIOFlag ioFlag) {
37  this->parent->parameters()->ioParamString(
38  ioFlag, this->getName(), "preLayerName", &mPreLayerName, NULL, false /*warnIfAbsent*/);
39 }
40 
41 void ConnectionData::ioParam_postLayerName(enum ParamsIOFlag ioFlag) {
42  this->parent->parameters()->ioParamString(
43  ioFlag, this->getName(), "postLayerName", &mPostLayerName, NULL, false /*warnIfAbsent*/);
44 }
45 
46 Response::Status
47 ConnectionData::communicateInitInfo(std::shared_ptr<CommunicateInitInfoMessage const> message) {
48  if (getPreLayerName() == nullptr and getPostLayerName() == nullptr) {
49  std::string preLayerNameString, postLayerNameString;
51  getName(),
52  parent->getCommunicator()->globalCommRank(),
53  preLayerNameString,
54  postLayerNameString);
55  mPreLayerName = strdup(preLayerNameString.c_str());
56  mPostLayerName = strdup(postLayerNameString.c_str());
57  }
58  MPI_Barrier(this->parent->getCommunicator()->globalCommunicator());
59  if (getPreLayerName() == nullptr or getPostLayerName() == nullptr) {
60  if (parent->getCommunicator()->globalCommRank() == 0) {
61  ErrorLog().printf(
62  "%s: Unable to determine pre- and post-layer names. Exiting.\n", getDescription_c());
63  }
64  exit(EXIT_FAILURE);
65  }
66 
67  auto hierarchy = message->mHierarchy;
68  ObjectMapComponent *objectMapComponent =
69  mapLookupByType<ObjectMapComponent>(hierarchy, getDescription());
70  FatalIf(
71  objectMapComponent == nullptr,
72  "CommunicateInitInfo called for %s with no ObjectMapComponent object.\n",
73  getDescription_c());
74 
75  bool failed = false;
76  mPre = objectMapComponent->lookup<HyPerLayer>(std::string(getPreLayerName()));
77  if (getPre() == nullptr) {
78  if (parent->getCommunicator()->globalCommRank() == 0) {
79  ErrorLog().printf(
80  "%s: preLayerName \"%s\" does not correspond to a layer in the column.\n",
81  getDescription_c(),
82  getPreLayerName());
83  }
84  failed = true;
85  }
86 
87  mPost = objectMapComponent->lookup<HyPerLayer>(std::string(getPostLayerName()));
88  if (getPost() == nullptr) {
89  if (parent->getCommunicator()->globalCommRank() == 0) {
90  ErrorLog().printf(
91  "%s: postLayerName \"%s\" does not correspond to a layer in the column.\n",
92  getDescription_c(),
94  }
95  failed = true;
96  }
97  MPI_Barrier(parent->getCommunicator()->globalCommunicator());
98  if (failed) {
99  exit(EXIT_FAILURE);
100  }
101 
102  return Response::SUCCESS;
103 }
104 
106  const char *name,
107  int rank,
108  std::string &preLayerNameString,
109  std::string &postLayerNameString) {
110  pvAssert(name);
111  preLayerNameString.clear();
112  postLayerNameString.clear();
113  std::string nameString(name);
114  auto locto = nameString.find("To");
115  if (locto == std::string::npos) {
116  if (rank == 0) {
117  ErrorLog(errorMessage);
118  errorMessage.printf("Unable to infer pre and post from connection name \"%s\".\n", name);
119  errorMessage.printf(
120  "The connection name must have the form \"AbcToXyz\", to infer the names,\n");
121  errorMessage.printf("but the string \"To\" does not appear.\n");
122  return;
123  }
124  }
125  auto secondto = nameString.find("To", locto + 1);
126  if (secondto != std::string::npos) {
127  if (rank == 0) {
128  ErrorLog(errorMessage);
129  errorMessage.printf("Unable to infer pre and post from connection name \"%s\":\n", name);
130  errorMessage.printf("The string \"To\" cannot appear in the name more than once.\n");
131  }
132  }
133  preLayerNameString.append(nameString.substr(0, locto));
134  postLayerNameString.append(nameString.substr(locto + 2, std::string::npos));
135 }
136 
137 } // namespace PV
char const * getPostLayerName() const
HyPerLayer * getPre()
virtual void ioParam_preLayerName(enum ParamsIOFlag ioFlag)
preLayerName: Specifies the connection&#39;s pre layer
virtual void ioParam_postLayerName(enum ParamsIOFlag ioFlag)
preLayerName: Specifies the connection&#39;s post layer
char const * getPreLayerName() const
HyPerLayer * getPost()
static void inferPreAndPostFromConnName(const char *name, int rank, std::string &preLayerNameString, std::string &postLayerNameString)
virtual int ioParamsFillGroup(enum ParamsIOFlag ioFlag) override