PetaVision  Alpha
BaseObject.cpp
1 /*
2  * BaseObject.cpp
3  *
4  * Created on: Jan 20, 2016
5  * Author: pschultz
6  */
7 
8 #include "BaseObject.hpp"
9 #include "columns/HyPerCol.hpp"
10 #include <cassert>
11 #include <cerrno>
12 #include <cstdio>
13 #include <cstdlib>
14 #include <cstring>
15 
16 namespace PV {
17 
18 BaseObject::BaseObject() {
19  initialize_base();
20  // Note that initialize() is not called in the constructor.
21  // Instead, derived classes should call BaseObject::initialize in their own
22  // constructor.
23 }
24 
25 int BaseObject::initialize_base() {
26  name = NULL;
27  parent = NULL;
28  return PV_SUCCESS;
29 }
30 
31 int BaseObject::initialize(const char *name, HyPerCol *hc) {
32  int status = setName(name);
33  if (status == PV_SUCCESS) {
34  status = setParent(hc);
35  }
36  if (status == PV_SUCCESS) {
37  setDescription();
38  }
39  return status;
40 }
41 
42 char const *BaseObject::lookupKeyword() const {
43  return parent->parameters()->groupKeywordFromName(getName());
44 }
45 
46 int BaseObject::setName(char const *name) {
47  pvAssert(this->name == NULL);
48  int status = PV_SUCCESS;
49  this->name = strdup(name);
50  if (this->name == NULL) {
51  ErrorLog().printf("could not set name \"%s\": %s\n", name, strerror(errno));
52  status = PV_FAILURE;
53  }
54  return status;
55 }
56 
57 int BaseObject::setParent(HyPerCol *hc) {
58  pvAssert(parent == NULL);
59  HyPerCol *parentCol = dynamic_cast<HyPerCol *>(hc);
60  int status = parentCol != NULL ? PV_SUCCESS : PV_FAILURE;
61  if (parentCol) {
62  parent = parentCol;
63  }
64  return status;
65 }
66 
67 void BaseObject::setDescription() {
68  setObjectType();
69  description = getObjectType() + " \"" + getName() + "\"";
70 }
71 
72 void BaseObject::setObjectType() { mObjectType = lookupKeyword(); }
73 
74 void BaseObject::ioParams(enum ParamsIOFlag ioFlag, bool printHeader, bool printFooter) {
75  if (printHeader) {
76  parent->ioParamsStartGroup(ioFlag, name);
77  }
78  ioParamsFillGroup(ioFlag);
79  if (printFooter) {
80  parent->ioParamsFinishGroup(ioFlag);
81  }
82 }
83 
84 Response::Status BaseObject::respond(std::shared_ptr<BaseMessage const> message) {
85  // TODO: convert PV_SUCCESS, PV_FAILURE, etc. to enum
86  Response::Status status = CheckpointerDataInterface::respond(message);
87  if (!Response::completed(status)) {
88  return status;
89  }
90  if (message == nullptr) {
91  return Response::NO_ACTION;
92  }
93  else if (
94  auto castMessage = std::dynamic_pointer_cast<CommunicateInitInfoMessage const>(message)) {
95  return respondCommunicateInitInfo(castMessage);
96  }
97 #ifdef PV_USE_CUDA
98  else if (auto castMessage = std::dynamic_pointer_cast<SetCudaDeviceMessage const>(message)) {
99  return respondSetCudaDevice(castMessage);
100  }
101 #endif // PV_USE_CUDA
102  else if (auto castMessage = std::dynamic_pointer_cast<AllocateDataMessage const>(message)) {
103  return respondAllocateData(castMessage);
104  }
105  else if (auto castMessage = std::dynamic_pointer_cast<InitializeStateMessage const>(message)) {
106  return respondInitializeState(castMessage);
107  }
108  else if (
109  auto castMessage =
110  std::dynamic_pointer_cast<CopyInitialStateToGPUMessage const>(message)) {
111  return respondCopyInitialStateToGPU(castMessage);
112  }
113  else if (auto castMessage = std::dynamic_pointer_cast<CleanupMessage const>(message)) {
114  return respondCleanup(castMessage);
115  }
116  else {
117  return Response::SUCCESS;
118  }
119 }
120 
121 Response::Status
122 BaseObject::respondCommunicateInitInfo(std::shared_ptr<CommunicateInitInfoMessage const> message) {
123  Response::Status status = Response::NO_ACTION;
125  return status;
126  }
127  status = communicateInitInfo(message);
128  if (Response::completed(status)) {
130  }
131  return status;
132 }
133 
134 #ifdef PV_USE_CUDA
135 Response::Status
136 BaseObject::respondSetCudaDevice(std::shared_ptr<SetCudaDeviceMessage const> message) {
137  return setCudaDevice(message);
138 }
139 #endif // PV_USE_CUDA
140 
141 Response::Status
142 BaseObject::respondAllocateData(std::shared_ptr<AllocateDataMessage const> message) {
143  Response::Status status = Response::NO_ACTION;
145  return status;
146  }
147  status = allocateDataStructures();
148  if (Response::completed(status)) {
150  }
151  return status;
152 }
153 
154 Response::Status
155 BaseObject::respondInitializeState(std::shared_ptr<InitializeStateMessage const> message) {
156  auto status = Response::NO_ACTION;
157  if (getInitialValuesSetFlag()) {
158  return Response::NO_ACTION;
159  }
160  status = initializeState();
161  if (Response::completed(status)) {
163  }
164  return status;
165 }
166 
167 Response::Status BaseObject::respondCopyInitialStateToGPU(
168  std::shared_ptr<CopyInitialStateToGPUMessage const> message) {
169  return copyInitialStateToGPU();
170 }
171 
172 Response::Status BaseObject::respondCleanup(std::shared_ptr<CleanupMessage const> message) {
173  return cleanup();
174 }
175 
176 #ifdef PV_USE_CUDA
177 Response::Status BaseObject::setCudaDevice(std::shared_ptr<SetCudaDeviceMessage const> message) {
178  if (mUsingGPUFlag) {
179  mCudaDevice = message->mCudaDevice;
180  FatalIf(
181  mCudaDevice == nullptr,
182  "%s received SetCudaDevice with null device pointer.\n",
183  getDescription_c());
184  }
185  return Response::SUCCESS;
186 }
187 #endif // PV_USE_CUDA
188 
189 BaseObject::~BaseObject() { free(name); }
190 
191 } /* namespace PV */
bool getInitialValuesSetFlag() const
Definition: BaseObject.hpp:108
bool getDataStructuresAllocatedFlag() const
Definition: BaseObject.hpp:102
static bool completed(Status &a)
Definition: Response.hpp:49
virtual int ioParamsFillGroup(enum ParamsIOFlag ioFlag)
Definition: BaseObject.hpp:139
char const * lookupKeyword() const
Definition: BaseObject.cpp:42
void setInitInfoCommunicatedFlag()
Definition: BaseObject.hpp:170
void setDataStructuresAllocatedFlag()
Definition: BaseObject.hpp:175
void ioParams(enum ParamsIOFlag ioFlag, bool printHeader, bool printFooter)
Definition: BaseObject.cpp:74
void setInitialValuesSetFlag()
Definition: BaseObject.hpp:180
bool getInitInfoCommunicatedFlag() const
Definition: BaseObject.hpp:95