PetaVision  Alpha
DependentPatchSize.cpp
1 /*
2  * DependentPatchSize.cpp
3  *
4  * Created on: Jan 5, 2018
5  * Author: pschultz
6  */
7 
8 #include "DependentPatchSize.hpp"
9 #include "columns/HyPerCol.hpp"
10 #include "columns/ObjectMapComponent.hpp"
11 #include "components/OriginalConnNameParam.hpp"
12 #include "connections/BaseConnection.hpp"
13 #include "utils/MapLookupByType.hpp"
14 
15 namespace PV {
16 
17 DependentPatchSize::DependentPatchSize(char const *name, HyPerCol *hc) { initialize(name, hc); }
18 
19 DependentPatchSize::DependentPatchSize() {}
20 
21 DependentPatchSize::~DependentPatchSize() {}
22 
23 int DependentPatchSize::initialize(char const *name, HyPerCol *hc) {
24  return PatchSize::initialize(name, hc);
25 }
26 
27 void DependentPatchSize::setObjectType() { mObjectType = "DependentPatchSize"; }
28 
29 int DependentPatchSize::ioParamsFillGroup(enum ParamsIOFlag ioFlag) {
30  return PatchSize::ioParamsFillGroup(ioFlag);
31 }
32 
33 void DependentPatchSize::ioParam_nxp(enum ParamsIOFlag ioFlag) {
34  if (ioFlag == PARAMS_IO_READ) {
35  parent->parameters()->handleUnnecessaryParameter(name, "nxp");
36  }
37  // During the communication phase, nxp will be copied from originalConn
38 }
39 
40 void DependentPatchSize::ioParam_nyp(enum ParamsIOFlag ioFlag) {
41  if (ioFlag == PARAMS_IO_READ) {
42  parent->parameters()->handleUnnecessaryParameter(name, "nyp");
43  }
44  // During the communication phase, nyp will be copied from originalConn
45 }
46 
47 void DependentPatchSize::ioParam_nfp(enum ParamsIOFlag ioFlag) {
48  if (ioFlag == PARAMS_IO_READ) {
49  parent->parameters()->handleUnnecessaryParameter(name, "nfp");
50  }
51  // During the communication phase, nfp will be copied from originalConn
52 }
53 
54 Response::Status
55 DependentPatchSize::communicateInitInfo(std::shared_ptr<CommunicateInitInfoMessage const> message) {
56  auto hierarchy = message->mHierarchy;
57 
58  char const *originalConnName = getOriginalConnName(hierarchy);
59  pvAssert(originalConnName);
60 
61  auto *originalPatchSize = getOriginalPatchSize(hierarchy, originalConnName);
62  pvAssert(originalPatchSize);
63 
64  if (!originalPatchSize->getInitInfoCommunicatedFlag()) {
65  if (parent->getCommunicator()->globalCommRank() == 0) {
66  InfoLog().printf(
67  "%s must wait until original connection \"%s\" has finished its communicateInitInfo "
68  "stage.\n",
69  getDescription_c(),
70  originalConnName);
71  }
72  return Response::POSTPONE;
73  }
74 
75  setPatchSize(originalPatchSize);
76  parent->parameters()->handleUnnecessaryParameter(name, "nxp", mPatchSizeX);
77  parent->parameters()->handleUnnecessaryParameter(name, "nyp", mPatchSizeY);
78  parent->parameters()->handleUnnecessaryParameter(name, "nfp", mPatchSizeF);
79 
80  auto status = PatchSize::communicateInitInfo(message);
81  return status;
82 }
83 
84 void DependentPatchSize::setPatchSize(PatchSize *originalPatchSize) {
85  mPatchSizeX = originalPatchSize->getPatchSizeX();
86  mPatchSizeY = originalPatchSize->getPatchSizeY();
87  mPatchSizeF = originalPatchSize->getPatchSizeF();
88 }
89 
90 char const *
91 DependentPatchSize::getOriginalConnName(std::map<std::string, Observer *> const hierarchy) const {
92  OriginalConnNameParam *originalConnNameParam =
93  mapLookupByType<OriginalConnNameParam>(hierarchy, getDescription());
94  FatalIf(
95  originalConnNameParam == nullptr,
96  "%s requires an OriginalConnNameParam component.\n",
97  getDescription_c());
98  char const *originalConnName = originalConnNameParam->getOriginalConnName();
99  return originalConnName;
100 }
101 
102 PatchSize *DependentPatchSize::getOriginalPatchSize(
103  std::map<std::string, Observer *> const hierarchy,
104  char const *originalConnName) const {
105  ObjectMapComponent *objectMapComponent =
106  mapLookupByType<ObjectMapComponent>(hierarchy, getDescription());
107  pvAssert(objectMapComponent);
108  BaseConnection *originalConn =
109  objectMapComponent->lookup<BaseConnection>(std::string(originalConnName));
110  if (originalConn == nullptr) {
111  if (parent->getCommunicator()->globalCommRank() == 0) {
112  ErrorLog().printf(
113  "%s: originalConnName \"%s\" does not correspond to a BaseConnection in the "
114  "column.\n",
115  getDescription_c(),
116  originalConnName);
117  }
118  MPI_Barrier(parent->getCommunicator()->globalCommunicator());
119  exit(PV_FAILURE);
120  }
121 
122  auto *originalPatchSize = originalConn->getComponentByType<PatchSize>();
123  FatalIf(
124  originalPatchSize == nullptr,
125  "%s original connection \"%s\" does not have an PatchSize.\n",
126  getDescription_c(),
127  originalConnName);
128  return originalPatchSize;
129 }
130 
131 } // namespace PV
virtual void ioParam_nxp(enum ParamsIOFlag ioFlag) override
nxp: DependentPatchSize does not read the nxp parameter, but copies it from the original connection...
virtual Response::Status communicateInitInfo(std::shared_ptr< CommunicateInitInfoMessage const > message) override
If nfp was set to a negative number in params, set it here to the postsynaptic layer&#39;s nf...
int ioParamsFillGroup(enum ParamsIOFlag ioFlag) override
Definition: PatchSize.cpp:24
virtual int ioParamsFillGroup(enum ParamsIOFlag ioFlag) override
virtual void ioParam_nfp(enum ParamsIOFlag ioFlag) override
nfp: DependentPatchSize does not read the nfp parameter, but copies it from the original connection...
virtual void ioParam_nyp(enum ParamsIOFlag ioFlag) override
nyp: DependentPatchSize does not read the nyp parameter, but copies it from the original connection...
virtual Response::Status communicateInitInfo(std::shared_ptr< CommunicateInitInfoMessage const > message) override
If nfp was set to a negative number in params, set it here to the postsynaptic layer&#39;s nf...
Definition: PatchSize.cpp:49