PetaVision  Alpha
BaseObject.hpp
1 /*
2  * BaseObject.hpp
3  *
4  * This is the base class for HyPerCol, layers, connections, probes, and
5  * anything else that the Factory object needs to know about.
6  *
7  * All objects in the BaseObject hierarchy should have an associated
8  * instantiating function, with the prototype
9  * BaseObject * createNameOfClass(char const * name, HyPerCol * initData);
10  *
11  * Each class's instantiating function should create an object of that class,
12  * with the arguments specifying the object's name and any necessary
13  * initializing data (for most classes, this is the parent HyPerCol.
14  * For HyPerCol, it is the PVInit object). This way, the class can be
15  * registered with the Factory object by calling
16  * Factory::registerKeyword() with a pointer to the class's instantiating
17  * method.
18  *
19  * Created on: Jan 20, 2016
20  * Author: pschultz
21  */
22 
23 #ifndef BASEOBJECT_HPP_
24 #define BASEOBJECT_HPP_
25 
26 #include "checkpointing/Checkpointer.hpp"
27 #include "checkpointing/CheckpointerDataInterface.hpp"
28 #include "columns/Messages.hpp"
29 #include "include/pv_common.h"
30 #include "observerpattern/Observer.hpp"
31 #include "utils/PVAlloc.hpp"
32 #include "utils/PVAssert.hpp"
33 #include "utils/PVLog.hpp"
34 #include <memory>
35 
36 #ifdef PV_USE_CUDA
37 #include "arch/cuda/CudaDevice.hpp"
38 #endif // PV_USE_CUDA
39 
40 namespace PV {
41 
42 class HyPerCol;
43 
45  public:
46  inline char const *getName() const { return name; }
47  // No getParent method because we are refactoring away from having objects
48  // having access to their containing HyPerCol.
49 
50  inline std::string const &getObjectType() const { return mObjectType; }
51 
55  char const *lookupKeyword() const;
56 
62  void readParams() { ioParams(PARAMS_IO_READ, false, false); }
63 
69  void writeParams() { ioParams(PARAMS_IO_WRITE, true, true); }
70 
83  void ioParams(enum ParamsIOFlag ioFlag, bool printHeader, bool printFooter);
84 
85  virtual Response::Status respond(std::shared_ptr<BaseMessage const> message) override;
86  // TODO: should return enum with values corresponding to PV_SUCCESS, PV_FAILURE, PV_POSTPONE
87 
88  virtual ~BaseObject();
89 
95  bool getInitInfoCommunicatedFlag() const { return mInitInfoCommunicatedFlag; }
96 
102  bool getDataStructuresAllocatedFlag() const { return mDataStructuresAllocatedFlag; }
103 
108  bool getInitialValuesSetFlag() const { return mInitialValuesSetFlag; }
109 
110 #ifdef PV_USE_CUDA
111 
116  bool isUsingGPU() const { return mUsingGPUFlag; }
117 #endif // PV_USE_CUDA
118 
119  protected:
120  BaseObject();
121  int initialize(char const *name, HyPerCol *hc);
122  int setName(char const *name);
123  int setParent(HyPerCol *hc);
124  virtual void setObjectType();
125  void setDescription();
126 
139  virtual int ioParamsFillGroup(enum ParamsIOFlag ioFlag) { return PV_SUCCESS; }
140 
141  Response::Status
142  respondCommunicateInitInfo(std::shared_ptr<CommunicateInitInfoMessage const> message);
143 #ifdef PV_USE_CUDA
144  Response::Status respondSetCudaDevice(std::shared_ptr<SetCudaDeviceMessage const> message);
145 #endif // PV_USE_CUDA
146  Response::Status respondAllocateData(std::shared_ptr<AllocateDataMessage const> message);
147  Response::Status respondInitializeState(std::shared_ptr<InitializeStateMessage const> message);
148  Response::Status
149  respondCopyInitialStateToGPU(std::shared_ptr<CopyInitialStateToGPUMessage const> message);
150  Response::Status respondCleanup(std::shared_ptr<CleanupMessage const> message);
151 
152  virtual Response::Status
153  communicateInitInfo(std::shared_ptr<CommunicateInitInfoMessage const> message) {
154  return Response::SUCCESS;
155  }
156 #ifdef PV_USE_CUDA
157  virtual Response::Status setCudaDevice(std::shared_ptr<SetCudaDeviceMessage const> message);
158 #endif // PV_USE_CUDA
159  virtual Response::Status allocateDataStructures() { return Response::NO_ACTION; }
160  virtual Response::Status initializeState() { return Response::NO_ACTION; }
161  virtual Response::Status readStateFromCheckpoint(Checkpointer *checkpointer) override {
162  return Response::NO_ACTION;
163  }
164  virtual Response::Status copyInitialStateToGPU() { return Response::SUCCESS; }
165  virtual Response::Status cleanup() { return Response::NO_ACTION; }
166 
170  void setInitInfoCommunicatedFlag() { mInitInfoCommunicatedFlag = true; }
171 
175  void setDataStructuresAllocatedFlag() { mDataStructuresAllocatedFlag = true; }
176 
180  void setInitialValuesSetFlag() { mInitialValuesSetFlag = true; }
181 
182  // Data members
183  protected:
184  char *name = nullptr;
185  std::string mObjectType;
186  // TODO: eliminate HyPerCol argument to constructor in favor of PVParams argument
187  HyPerCol *parent = nullptr;
188  bool mInitInfoCommunicatedFlag = false;
189  bool mDataStructuresAllocatedFlag = false;
190  bool mInitialValuesSetFlag = false;
191 #ifdef PV_USE_CUDA
192  bool mUsingGPUFlag = false;
193  PVCuda::CudaDevice *mCudaDevice = nullptr;
194 #endif // PV_USE_CUDA
195 
196  private:
197  int initialize_base();
198 }; // class BaseObject
199 
200 } // namespace PV
201 
202 #endif /* BASEOBJECT_HPP_ */
bool isUsingGPU() const
Definition: BaseObject.hpp:116
bool getInitialValuesSetFlag() const
Definition: BaseObject.hpp:108
bool getDataStructuresAllocatedFlag() const
Definition: BaseObject.hpp:102
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 writeParams()
Definition: BaseObject.hpp:69
void ioParams(enum ParamsIOFlag ioFlag, bool printHeader, bool printFooter)
Definition: BaseObject.cpp:74
void setInitialValuesSetFlag()
Definition: BaseObject.hpp:180
void readParams()
Definition: BaseObject.hpp:62
bool getInitInfoCommunicatedFlag() const
Definition: BaseObject.hpp:95