PetaVision  Alpha
HyPerCol.hpp
1 /*
2  * HyPerCol.hpp
3  *
4  * Created on: Jul 30, 2008
5  * Author: Craig Rasmussen
6  */
7 
8 #ifndef HYPERCOL_HPP_
9 #define HYPERCOL_HPP_
10 
11 #include "checkpointing/Checkpointer.hpp"
12 #include "columns/BaseObject.hpp"
13 #include "columns/Communicator.hpp"
14 #include "columns/Messages.hpp"
15 #include "columns/PV_Init.hpp"
16 #include "include/pv_types.h"
17 #include "io/PVParams.hpp"
18 #include "observerpattern/Observer.hpp"
19 #include "observerpattern/ObserverTable.hpp"
20 #include "observerpattern/Subject.hpp"
21 #include "utils/Clock.hpp"
22 #include "utils/Timer.hpp"
23 #include <fstream>
24 #include <memory>
25 #include <sstream>
26 #include <string>
27 #include <sys/stat.h>
28 #include <time.h>
29 #include <typeinfo>
30 #include <vector>
31 
32 #ifdef PV_USE_CUDA
33 #include <arch/cuda/CudaDevice.hpp>
34 #endif
35 
36 namespace PV {
37 
38 class PV_Init;
39 class PVParams;
40 
41 class HyPerCol : public Subject, public Observer {
42 
43  private:
53  virtual void ioParam_stopTime(enum ParamsIOFlag ioFlag);
54 
59  virtual void ioParam_dt(enum ParamsIOFlag ioFlag);
60 
65  virtual void ioParam_progressInterval(enum ParamsIOFlag ioFlag);
66 
72  virtual void ioParam_writeProgressToErr(enum ParamsIOFlag ioFlag);
73 
79  virtual void ioParam_printParamsFilename(enum ParamsIOFlag ioFlag);
80 
85  virtual void ioParam_randomSeed(enum ParamsIOFlag ioFlag);
86 
90  virtual void ioParam_nx(enum ParamsIOFlag ioFlag);
91 
95  virtual void ioParam_ny(enum ParamsIOFlag ioFlag);
96 
100  virtual void ioParam_nBatch(enum ParamsIOFlag ioFlag);
101 
106  virtual void ioParam_errorOnNotANumber(enum ParamsIOFlag ioFlag);
107 
108  public:
109  HyPerCol(PV_Init *initObj);
110  virtual ~HyPerCol();
111 
112  // Public functions
113 
114  virtual Response::Status respond(std::shared_ptr<BaseMessage const> message) override;
115 
122  Observer *getObjectFromName(std::string const &objectName) const;
123 
131  Observer *getNextObject(Observer const *currentObject) const;
132 
133  void advanceTimeLoop(Clock &runClock, int const runClockStartingStep);
134  int advanceTime(double time);
135  void nonblockingLayerUpdate(std::shared_ptr<LayerUpdateStateMessage const> updateMessage);
136  void nonblockingLayerUpdate(
137  std::shared_ptr<LayerRecvSynapticInputMessage const> recvMessage,
138  std::shared_ptr<LayerUpdateStateMessage const> updateMessage);
139  int processParams(char const *path);
140  int ioParamsFinishGroup(enum ParamsIOFlag);
141  int ioParamsStartGroup(enum ParamsIOFlag ioFlag, const char *group_name);
142 
152  void allocateColumn();
153  int run() { return run(mStopTime, mDeltaTime); }
154  int run(double stopTime, double dt);
155 
156  // Getters and setters
157 
158  bool getVerifyWrites() { return mCheckpointer->doesVerifyWrites(); }
159  bool getCheckpointWriteFlag() const { return mCheckpointer->getCheckpointWriteFlag(); }
160  char const *getLastCheckpointDir() const { return mCheckpointer->getLastCheckpointDir(); }
161  bool getWriteTimescales() const { return mWriteTimescales; }
162  const char *getName() { return mName; }
163  const char *getOutputPath() { return mCheckpointer->getOutputPath().c_str(); }
164  const char *getPrintParamsFilename() const { return mPrintParamsFilename; }
165  double getDeltaTime() const { return mDeltaTime; }
166  double simulationTime() const { return mSimTime; }
167  double getStopTime() const { return mStopTime; }
168  int globalRank() { return mCommunicator->globalCommRank(); }
169  int columnId() { return mCommunicator->commRank(); }
170  int getNxGlobal() { return mNumXGlobal; }
171  int getNyGlobal() { return mNumYGlobal; }
172  int getNBatch() { return mNumBatch; }
173  int getNBatchGlobal() { return mNumBatchGlobal; }
174  int getNumThreads() const { return mNumThreads; }
175  int numberOfBorderRegions() const { return MAX_NEIGHBORS; }
176  int numberOfColumns() { return mCommunicator->commSize(); }
177  int numberOfGlobalColumns() { return mCommunicator->globalCommSize(); }
178  int commColumn() { return mCommunicator->commColumn(); }
179  int commRow() { return mCommunicator->commRow(); }
180  int commBatch() { return mCommunicator->commBatch(); }
181  int numCommColumns() { return mCommunicator->numCommColumns(); }
182  int numCommRows() { return mCommunicator->numCommRows(); }
183  int numCommBatches() { return mCommunicator->numCommBatches(); }
184  Communicator *getCommunicator() const { return mCommunicator; }
185  PV_Init *getPV_InitObj() const { return mPVInitObj; }
186  FileStream *getPrintParamsStream() const { return mPrintParamsStream; }
187  PVParams *parameters() const { return mParams; }
188  long int getFinalStep() const { return mFinalStep; }
189  unsigned int getRandomSeed() { return mRandomSeed; }
190  unsigned int seedRandomFromWallClock();
191 
192  // A hack to allow test_cocirc, test_gauss2d, and test_post_weights to send a
193  // CommunicateInitInfoMessage.
194  std::map<std::string, Observer *> *copyObjectMap() {
195  auto objectMap = new std::map<std::string, Observer *>;
196  *objectMap = mObjectHierarchy.getObjectMap();
197  return objectMap;
198  }
199 
200  private:
201  int getAutoGPUDevice();
202 
203 #ifdef PV_USE_CUDA
204  public:
205  PVCuda::CudaDevice *getDevice() { return mCudaDevice; }
206 #endif
207 
208  // Private functions
209 
210  private:
211  void setDescription();
212  int initialize_base();
213  int initialize(PV_Init *initObj);
214  void ioParams(enum ParamsIOFlag ioFlag);
215  int ioParamsFillGroup(enum ParamsIOFlag ioFlag);
216  void addObject(BaseObject *obj);
217  int checkDirExists(const char *dirname, struct stat *pathstat);
218  inline void notifyLoop(std::vector<std::shared_ptr<BaseMessage const>> messages) {
219  bool printFlag = getCommunicator()->globalCommRank() == 0;
220  Subject::notifyLoop(mObjectHierarchy, messages, printFlag, description);
221  }
222  inline void notifyLoop(std::shared_ptr<BaseMessage const> message) {
223  notifyLoop(std::vector<std::shared_ptr<BaseMessage const>>{message});
224  }
225  Response::Status
226  respondPrepareCheckpointWrite(std::shared_ptr<PrepareCheckpointWriteMessage const> message);
227 #ifdef PV_USE_CUDA
228  void initializeCUDA(std::string const &in_device);
229  int finalizeCUDA();
230 #endif // PV_USE_CUDA
231  void outputParams(char const *path);
232  void outputParamsHeadComments(FileStream *fileStream, char const *commentToken);
240  int setNumThreads(bool printMessagesFlag);
241 
242  // Private variables
243 
244  private:
245  ObserverTable mObjectHierarchy;
246  bool mErrorOnNotANumber; // If true, check each layer's activity buffer for
247  // not-a-numbers and
248  // exit with an error if any appear
249  bool mCheckpointReadFlag; // whether to load from a checkpoint directory
250  bool mReadyFlag; // Initially false; set to true when communicateInitInfo,
251  // allocateDataStructures, and initializeState stages are completed
252  bool mParamsProcessedFlag; // Initially false; set to true when processParams
253  // is called.
254  bool mWriteTimeScaleFieldnames; // determines whether fieldnames are written to
255  // HyPerCol_timescales file
256  bool mWriteProgressToErr; // Whether to write progress step to standard error
257  // (True) or standard
258  // output (False) (default is output)
259  bool mOwnsCommunicator; // True if icComm was created by initialize, false if
260  // passed in the
261  // constructor
262  bool mWriteTimescales;
263  char *mName;
264  char *mPrintParamsFilename; // filename for outputting the mParams, including
265  // defaults and
266  // excluding unread mParams
267  double mSimTime;
268  double mStopTime; // time to stop time
269  double mDeltaTime; // time step interval
270  double mProgressInterval; // Output progress after mSimTime increases by this
271  // amount.
272  double mNextProgressTime; // Next time to output a progress message
273  int mNumPhases;
274  int mNumXGlobal;
275  int mNumYGlobal;
276  int mNumBatch;
277  int mNumBatchGlobal;
278  int mOrigStdOut;
279  int mOrigStdErr;
280  int mNumThreads;
281  int *mLayerStatus;
282  int *mConnectionStatus;
283  Communicator *mCommunicator; // manages communication between HyPerColumns};
284 
285  Checkpointer *mCheckpointer = nullptr; // manages checkpointing and outputState output
286  long int mCurrentStep;
287  long int mFinalStep;
288  PV_Init *mPVInitObj;
289  FileStream *mPrintParamsStream; // file pointer associated with mPrintParamsFilename
290  FileStream *mLuaPrintParamsStream; // file pointer associated with the output lua file
291  PVParams *mParams; // manages input parameters
292  size_t mLayerArraySize;
293  size_t mConnectionArraySize;
294  std::ofstream mTimeScaleStream;
295  Timer *mRunTimer;
296  std::vector<Timer *> mPhaseRecvTimers; // Timer ** mPhaseRecvTimers;
297  unsigned int mRandomSeed;
298 #ifdef PV_USE_CUDA
299  PVCuda::CudaDevice *mCudaDevice; // object for running kernels on OpenCL device
300 #endif
301 
302 }; // class HyPerCol
303 
304 // July 7, 2017: Functionality of createHyPerCol() moved into HyPerCol::initialize()
305 
306 } // namespace PV
307 
308 #endif /* HYPERCOL_HPP_ */
void allocateColumn()
Definition: HyPerCol.cpp:361
Observer * getNextObject(Observer const *currentObject) const
Definition: HyPerCol.cpp:1259
void notifyLoop(ObserverTable const &table, std::vector< std::shared_ptr< BaseMessage const >> messages, bool printFlag, std::string const &description)
Definition: Subject.cpp:57
virtual void ioParam_writeProgressToErr(enum ParamsIOFlag ioFlag)
writeProgressToErr: Whether to print timestep progress to the error stream instead of the output stre...
Definition: HyPerCol.cpp:292
virtual void ioParam_randomSeed(enum ParamsIOFlag ioFlag)
randomSeed: The seed for the random number generator for reproducability
Definition: HyPerCol.cpp:309
virtual void ioParam_dt(enum ParamsIOFlag ioFlag)
dt: The default delta time to use.
Definition: HyPerCol.cpp:279
virtual void ioParam_stopTime(enum ParamsIOFlag ioFlag)
mStopTime: The set stopping time for the run
Definition: HyPerCol.cpp:283
int setNumThreads(bool printMessagesFlag)
Definition: HyPerCol.cpp:516
virtual void ioParam_errorOnNotANumber(enum ParamsIOFlag ioFlag)
errorOnNotANumber: Specifies if the run should check on each timestep for nans in activity...
Definition: HyPerCol.cpp:356
virtual void ioParam_ny(enum ParamsIOFlag ioFlag)
ny: Specifies the size of the column
Definition: HyPerCol.cpp:339
virtual void ioParam_nBatch(enum ParamsIOFlag ioFlag)
ny: Specifies the batch size of the column
Definition: HyPerCol.cpp:343
virtual void ioParam_nx(enum ParamsIOFlag ioFlag)
nx: Specifies the size of the column
Definition: HyPerCol.cpp:335
virtual void ioParam_progressInterval(enum ParamsIOFlag ioFlag)
mProgressInterval: Specifies how often a progress report prints out
Definition: HyPerCol.cpp:287
Observer * getObjectFromName(std::string const &objectName) const
Definition: HyPerCol.cpp:1253
virtual void ioParam_printParamsFilename(enum ParamsIOFlag ioFlag)
mPrintParamsFilename: Specifies the output mParams filename.
Definition: HyPerCol.cpp:297