PetaVision  Alpha
BaseDelivery.cpp
1 /*
2  * BaseDelivery.cpp
3  *
4  * Created on: Nov 17, 2017
5  * Author: Pete Schultz
6  */
7 
8 #include "BaseDelivery.hpp"
9 #include "columns/HyPerCol.hpp"
10 #include "layers/HyPerLayer.hpp"
11 #include "utils/MapLookupByType.hpp"
12 
13 namespace PV {
14 
15 BaseDelivery::BaseDelivery(char const *name, HyPerCol *hc) { initialize(name, hc); }
16 
17 int BaseDelivery::initialize(char const *name, HyPerCol *hc) {
18  return BaseObject::initialize(name, hc);
19 }
20 
21 void BaseDelivery::setObjectType() { mObjectType = "BaseDelivery"; }
22 
23 int BaseDelivery::ioParamsFillGroup(enum ParamsIOFlag ioFlag) {
24  ioParam_channelCode(ioFlag);
25  ioParam_receiveGpu(ioFlag);
26  return PV_SUCCESS;
27 }
28 
29 void BaseDelivery::ioParam_channelCode(enum ParamsIOFlag ioFlag) {
30  if (ioFlag == PARAMS_IO_READ) {
31  int ch = 0;
32  this->parent->parameters()->ioParamValueRequired(ioFlag, this->getName(), "channelCode", &ch);
33  switch (ch) {
34  case CHANNEL_EXC: mChannelCode = CHANNEL_EXC; break;
35  case CHANNEL_INH: mChannelCode = CHANNEL_INH; break;
36  case CHANNEL_INHB: mChannelCode = CHANNEL_INHB; break;
37  case CHANNEL_GAP: mChannelCode = CHANNEL_GAP; break;
38  case CHANNEL_NORM: mChannelCode = CHANNEL_NORM; break;
39  case CHANNEL_NOUPDATE: mChannelCode = CHANNEL_NOUPDATE; break;
40  default:
41  if (parent->getCommunicator()->globalCommRank() == 0) {
42  ErrorLog().printf(
43  "%s: channelCode %d is not a valid channel.\n", this->getDescription_c(), ch);
44  }
45  MPI_Barrier(this->parent->getCommunicator()->globalCommunicator());
46  exit(EXIT_FAILURE);
47  break;
48  }
49  }
50  else if (ioFlag == PARAMS_IO_WRITE) {
51  int ch = (int)mChannelCode;
52  parent->parameters()->ioParamValueRequired(ioFlag, this->getName(), "channelCode", &ch);
53  }
54  else {
55  assert(0); // All possibilities of ioFlag are covered above.
56  }
57 }
58 
59 void BaseDelivery::ioParam_receiveGpu(enum ParamsIOFlag ioFlag) {
60 #ifdef PV_USE_CUDA
61  parent->parameters()->ioParamValue(
62  ioFlag,
63  name,
64  "receiveGpu",
65  &mReceiveGpu,
66  mReceiveGpu /*default*/,
67  true /*warn if absent*/);
68 #else
69  parent->parameters()->ioParamValue(
70  ioFlag,
71  name,
72  "receiveGpu",
73  &mReceiveGpu,
74  mReceiveGpu /*default*/,
75  false /*warn if absent*/);
76  if (parent->getCommunicator()->globalCommRank() == 0) {
77  FatalIf(
78  mReceiveGpu,
79  "%s: receiveGpu is set to true in params, but PetaVision was compiled without GPU "
80  "acceleration.\n",
81  getDescription_c());
82  }
83 #endif // PV_USE_CUDA
84 }
85 
86 Response::Status
87 BaseDelivery::communicateInitInfo(std::shared_ptr<CommunicateInitInfoMessage const> message) {
88  if (mConnectionData == nullptr) {
89  mConnectionData = mapLookupByType<ConnectionData>(message->mHierarchy, getDescription());
90  FatalIf(
91  mConnectionData == nullptr,
92  "%s requires a ConnectionData component.\n",
93  getDescription_c());
94  }
95  pvAssert(mConnectionData);
96  if (!mConnectionData->getInitInfoCommunicatedFlag()) {
97  if (parent->getCommunicator()->globalCommRank() == 0) {
98  InfoLog().printf(
99  "%s must wait until the ConnectionData component has finished its "
100  "communicateInitInfo stage.\n",
101  getDescription_c());
102  }
103  return Response::POSTPONE;
104  }
105 
106  mPreLayer = mConnectionData->getPre();
107  mPostLayer = mConnectionData->getPost();
108  pvAssert(mPreLayer != nullptr and mPostLayer != nullptr);
109 
110  int numChannelsCheck = 0;
111  int channelAsInt = (int)getChannelCode();
112  if (channelAsInt >= 0) {
113  int status = getPostLayer()->requireChannel(channelAsInt, &numChannelsCheck);
114  if (status != PV_SUCCESS) {
115  if (parent->getCommunicator()->globalCommRank() == 0) {
116  ErrorLog().printf(
117  "%s: postsynaptic layer \"%s\" failed to add channel %d\n",
118  getDescription_c(),
119  getPostLayer()->getName(),
120  channelAsInt);
121  }
122  MPI_Barrier(parent->getCommunicator()->globalCommunicator());
123  exit(EXIT_FAILURE);
124  }
125  }
126 #ifdef PV_USE_CUDA
127  mUsingGPUFlag = mReceiveGpu;
128 #endif // PV_USE_CUDA
129 
130  return Response::SUCCESS;
131 }
132 
133 } // namespace PV
HyPerLayer * getPre()
virtual void ioParam_receiveGpu(enum ParamsIOFlag ioFlag)
receiveGpu: If PetaVision was compiled with GPU acceleration and this flag is set to true...
HyPerLayer * getPost()
int ioParamsFillGroup(enum ParamsIOFlag ioFlag) override
virtual void ioParam_channelCode(enum ParamsIOFlag ioFlag)
channelCode: Specifies which channel in the post layer this connection is attached to ...
bool getInitInfoCommunicatedFlag() const
Definition: BaseObject.hpp:95