11 #ifndef HYPERLAYER_HPP_ 12 #define HYPERLAYER_HPP_ 14 #include "checkpointing/CheckpointableFileStream.hpp" 15 #include "columns/Communicator.hpp" 16 #include "columns/DataStore.hpp" 17 #include "columns/HyPerCol.hpp" 18 #include "columns/Publisher.hpp" 19 #include "columns/Random.hpp" 20 #include "include/pv_common.h" 21 #include "include/pv_types.h" 22 #include "initv/BaseInitV.hpp" 23 #include "io/fileio.hpp" 24 #include "layers/BaseLayer.hpp" 25 #include "layers/PVLayerCube.hpp" 26 #include "probes/LayerProbe.hpp" 27 #include "utils/Timer.hpp" 31 #include <layers/updateStateFunctions.h> 34 #include <layers/updateStateFunctions.h> 37 #ifdef PV_USE_OPENMP_THREADS 39 #endif // PV_USE_OPENMP_THREADS 42 #include <arch/cuda/CudaBuffer.hpp> 43 #include <arch/cuda/CudaKernel.hpp> 44 #include <arch/cuda/CudaTimer.hpp> 50 #define HYPERLAYER_FEEDBACK_DELAY 1 51 #define HYPERLAYER_FEEDFORWARD_DELAY 0 58 typedef enum TriggerBehaviorTypeEnum {
62 } TriggerBehaviorType;
73 virtual void ioParam_dataType(
enum ParamsIOFlag ioFlag);
95 virtual void ioParam_nf(
enum ParamsIOFlag ioFlag);
204 int initialize_base();
210 virtual int initClayer();
212 virtual int allocateClayerBuffers();
213 int setLayerLoc(
PVLayerLoc *layerLoc,
float nxScale,
float nyScale,
int nf,
int numBatches);
214 virtual void allocateBuffers();
215 virtual void allocateGSyn();
223 template <
typename T>
224 void allocateBuffer(T **buf,
int bufsize,
const char *bufname);
236 virtual void allocateV();
237 virtual void allocateActivity();
238 virtual void allocatePrevActivity();
240 void checkpointPvpActivityFloat(
242 char const *bufferName,
246 void checkpointRandState(
248 char const *bufferName,
252 virtual void initializeV();
253 virtual void initializeActivity();
254 virtual Response::Status readStateFromCheckpoint(
Checkpointer *checkpointer)
override;
255 virtual void readActivityFromCheckpoint(
Checkpointer *checkpointer);
256 virtual void readVFromCheckpoint(
Checkpointer *checkpointer);
257 virtual void readDelaysFromCheckpoint(
Checkpointer *checkpointer);
259 virtual Response::Status copyInitialStateToGPU()
override;
260 #endif // PV_USE_CUDA 266 void updateNBands(
int numCalls);
268 virtual Response::Status processCheckpointRead()
override;
270 void calcNumExtended();
275 virtual bool needReset(
double timed,
double dt);
290 template <
typename T>
291 int freeBuffer(T **buf);
316 float *getActivity() {
317 return clayer->activity->data;
319 virtual double getTimeScale(
int batchIdx) {
return -1.0; };
320 virtual bool activityIsSpiking() {
return false; }
321 PVDataType getDataType() {
return dataType; }
322 virtual Response::Status respond(std::shared_ptr<BaseMessage const> message)
override;
337 void synchronizeMarginWidth(
HyPerLayer *layer);
346 virtual int recvAllSynapticInput();
349 Response::Status callUpdateState(
double simTime,
double dt);
362 virtual bool needUpdate(
double simTime,
double dt);
376 Response::Status respondLayerSetMaxPhase(std::shared_ptr<LayerSetMaxPhaseMessage const> message);
377 Response::Status respondLayerWriteParams(std::shared_ptr<LayerWriteParamsMessage const> message);
379 respondLayerProbeWriteParams(std::shared_ptr<LayerProbeWriteParamsMessage const> message);
381 respondLayerClearProgressFlags(std::shared_ptr<LayerClearProgressFlagsMessage const> message);
383 respondLayerRecvSynapticInput(std::shared_ptr<LayerRecvSynapticInputMessage const> message);
384 Response::Status respondLayerUpdateState(std::shared_ptr<LayerUpdateStateMessage const> message);
386 Response::Status respondLayerCopyFromGpu(std::shared_ptr<LayerCopyFromGpuMessage const> message);
387 #endif // PV_USE_CUDA 389 respondLayerAdvanceDataStore(std::shared_ptr<LayerAdvanceDataStoreMessage const> message);
390 Response::Status respondLayerPublish(std::shared_ptr<LayerPublishMessage const> message);
392 respondLayerCheckNotANumber(std::shared_ptr<LayerCheckNotANumberMessage const> message);
395 Response::Status respondLayerOutputState(std::shared_ptr<LayerOutputStateMessage const> message);
396 virtual int publish(
Communicator *comm,
double simTime);
397 virtual int resetGSynBuffers(
double timef,
double dt);
404 virtual void updateAllActiveIndices();
405 void updateActiveIndices();
406 int resetBuffer(
float *buf,
int numItems);
411 virtual Response::Status outputState(
double timef);
412 virtual int writeActivity(
double timed);
413 virtual int writeActivitySparse(
double timed);
416 Response::Status outputProbeParams();
424 void clearProgressFlags();
426 int getNumProbes() {
return numProbes; }
427 LayerProbe *getProbe(
int n) {
return (n >= 0 && n < numProbes) ? probes[n] : NULL; }
448 int getNumNeurons() {
return clayer->numNeurons; }
449 int getNumExtended() {
return clayer->numExtended; }
450 int getNumNeuronsAllBatches() {
return clayer->numNeuronsAllBatches; }
451 int getNumExtendedAllBatches() {
return clayer->numExtendedAllBatches; }
453 int getNumGlobalNeurons() {
455 return loc->nxGlobal * loc->nyGlobal * loc->nf;
457 int getNumGlobalExtended() {
459 return (loc->nxGlobal + loc->halo.lt + loc->halo.rt)
460 * (loc->nyGlobal + loc->halo.dn + loc->halo.up) * loc->nf;
462 int getNumDelayLevels() {
return numDelayLevels; }
464 int increaseDelayLevels(
int neededDelay);
465 virtual int requireMarginWidth(
int marginWidthNeeded,
int *marginWidthResult,
char axis);
466 virtual int requireChannel(
int channelNeeded,
int *numChannelsResult);
468 PVLayer *getCLayer() {
return clayer; }
469 float *getV() {
return clayer->V; }
470 int getNumChannels() {
return numChannels; }
471 float *getChannel(ChannelType ch) {
472 return (ch < this->numChannels && ch >= 0) ? GSyn[ch] : NULL;
474 virtual float getChannelTimeConst(
enum ChannelType channel_type) {
return 0.0f; }
475 int getXScale() {
return clayer->xScale; }
476 int getYScale() {
return clayer->yScale; }
478 bool useMirrorBCs() {
return this->mirrorBCflag; }
479 float getValueBC() {
return this->valueBC; }
481 bool getSparseFlag() {
return this->sparseLayer; }
483 int getPhase() {
return this->phase; }
488 PVLayerLoc const *getLayerLoc()
const {
return &(clayer->loc); }
489 bool isExtended() {
return true; }
491 double getLastUpdateTime() {
return mLastUpdateTime; }
494 Publisher *getPublisher() {
return publisher; }
497 virtual Response::Status
498 communicateInitInfo(std::shared_ptr<CommunicateInitInfoMessage const> message)
override;
499 virtual Response::Status allocateDataStructures()
override;
500 virtual Response::Status setMaxPhase(
int *maxPhase);
501 virtual Response::Status registerData(
Checkpointer *checkpointer)
override;
502 virtual Response::Status initializeState()
override;
508 virtual int runUpdateKernel();
509 virtual Response::Status updateStateGpu(
double timef,
double dt);
511 virtual Response::Status updateState(
double timef,
double dt);
512 virtual int setActivity();
515 bool mNeedToPublish =
true;
521 float nxScale, nyScale;
523 int xmargin, ymargin;
525 bool initializeFromCheckpointFlag =
true;
541 double initialWriteTime;
548 int writeActivityCalls;
550 int writeActivitySparseCalls;
556 unsigned int rngSeedBase;
560 char *initVTypeString =
nullptr;
564 int numSynchronizedMarginWidthLayers;
572 char *triggerLayerName;
574 char *triggerBehavior;
576 TriggerBehaviorType triggerBehaviorType;
577 char *triggerResetLayerName;
579 double triggerOffset;
585 char *dataTypeString;
588 double mLastUpdateTime;
589 double mLastTriggerTime;
592 std::vector<BaseConnection *> recvConns;
594 bool mHasReceived =
false;
595 bool mHasUpdated =
false;
600 virtual void syncGpu();
601 virtual double addGpuTimers();
603 void copyAllGSynToDevice();
604 void copyAllGSynFromDevice();
605 void copyAllVFromDevice();
606 void copyAllActivityFromDevice();
607 PVCuda::CudaBuffer *getDeviceV() {
return d_V; }
608 PVCuda::CudaBuffer *getDeviceGSyn() {
return d_GSyn; }
611 PVCuda::CudaBuffer *getCudnnGSyn() {
return cudnn_GSyn; }
612 #endif // PV_USE_CUDNN 613 PVCuda::CudaBuffer *getDeviceActivity() {
return d_Activity; }
615 PVCuda::CudaBuffer *getDeviceDatastore() {
return d_Datastore; }
617 PVCuda::CudaBuffer *getDeviceActiveIndices() {
return d_ActiveIndices; }
619 PVCuda::CudaBuffer *getDeviceNumActive() {
return d_numActive; }
622 PVCuda::CudaBuffer *getCudnnDatastore() {
return cudnn_Datastore; }
623 #endif // PV_USE_CUDNN 625 void setAllocDeviceV() { allocDeviceV =
true; }
626 void setAllocDeviceGSyn() { allocDeviceGSyn =
true; }
628 void setAllocDeviceActivity() { allocDeviceActivity =
true; }
630 void setAllocDeviceDatastore() { allocDeviceDatastore =
true; }
632 void setAllocDeviceActiveIndices() { allocDeviceActiveIndices =
true; }
634 bool getUpdatedDeviceActivityFlag() {
return updatedDeviceActivity; }
636 void setUpdatedDeviceActivityFlag(
bool in) { updatedDeviceActivity = in; }
638 bool getUpdatedDeviceDatastoreFlag() {
return updatedDeviceDatastore; }
640 void setUpdatedDeviceDatastoreFlag(
bool in) { updatedDeviceDatastore = in; }
642 bool getUpdatedDeviceGSynFlag() {
return updatedDeviceGSyn; }
644 void setUpdatedDeviceGSynFlag(
bool in) { updatedDeviceGSyn = in; }
647 virtual int allocateUpdateKernel();
653 bool allocDeviceGSyn;
654 bool allocDeviceActivity;
655 bool allocDeviceDatastore;
656 bool allocDeviceActiveIndices;
657 bool updatedDeviceActivity;
658 bool updatedDeviceDatastore;
659 bool updatedDeviceGSyn;
663 PVCuda::CudaBuffer *d_V;
664 PVCuda::CudaBuffer *d_GSyn;
665 PVCuda::CudaBuffer *d_Activity;
666 PVCuda::CudaBuffer *d_Datastore;
667 PVCuda::CudaBuffer *d_numActive;
668 PVCuda::CudaBuffer *d_ActiveIndices;
669 PVCuda::CudaKernel *krUpdate;
671 PVCuda::CudaBuffer *cudnn_GSyn;
672 PVCuda::CudaBuffer *cudnn_Datastore;
673 #endif // PV_USE_CUDNN 674 #endif // PV_USE_CUDA 678 Timer *recvsyn_timer;
679 Timer *publish_timer;
680 Timer *timescale_timer;
684 PVCuda::CudaTimer *gpu_recvsyn_timer;
685 PVCuda::CudaTimer *gpu_update_timer;
virtual int ioParamsFillGroup(enum ParamsIOFlag ioFlag) override
virtual void ioParam_triggerOffset(enum ParamsIOFlag ioFlag)
triggerOffset: If triggerLayer is set, triggers <triggerOffset> timesteps before target trigger ...
virtual void resetStateOnTrigger()
virtual void ioParam_triggerBehavior(enum ParamsIOFlag ioFlag)
triggerBehavior: If triggerLayerName is set, this parameter specifies how the trigger is handled...
virtual int allocateDeviceBuffers()
virtual void ioParam_writeStep(enum ParamsIOFlag ioFlag)
writeStep: Specifies how often to output a pvp file for this layer
virtual void ioParam_phase(enum ParamsIOFlag ioFlag)
phase: Defines the ordering in which each layer is updated
void allocateRestrictedBuffer(float **buf, const char *bufname)
virtual void ioParam_initializeFromCheckpointFlag(enum ParamsIOFlag ioFlag)
initializeFromCheckpointFlag: If set to true, initialize using checkpoint direcgtory set in HyPerCol...
bool isExchangeFinished(int delay=0)
virtual void ioParam_sparseLayer(enum ParamsIOFlag ioFlag)
sparseLayer: Specifies if the layer should be considered sparese for optimization and output ...
virtual void ioParam_nf(enum ParamsIOFlag ioFlag)
nf: Defines how many features this layer has
virtual void ioParam_triggerLayerName(enum ParamsIOFlag ioFlag)
triggerLayerName: Specifies the name of the layer that this layer triggers off of. If set to NULL or the empty string, the layer does not trigger but updates its state on every timestep.
virtual double getDeltaUpdateTime()
int initialize(const char *name, HyPerCol *hc)
virtual void ioParam_valueBC(enum ParamsIOFlag ioFlag)
valueBC: If mirrorBC is set to true, Uses the specified value for the margin area ...
void allocateExtendedBuffer(float **buf, const char *bufname)
virtual bool needUpdate(double simTime, double dt)
virtual void ioParam_mirrorBCflag(enum ParamsIOFlag ioFlag)
mirrorBCflag: If set to true, the margin will mirror the data
virtual void ioParam_triggerResetLayerName(enum ParamsIOFlag ioFlag)
triggerResetLayerName: If triggerLayerName is set, this parameter specifies the layer to use for upda...
void addRecvConn(BaseConnection *conn)
virtual void ioParam_InitVType(enum ParamsIOFlag ioFlag)
initVType: Specifies how to initialize the V buffer.
int freeExtendedBuffer(float **buf)
int freeRestrictedBuffer(float **buf)
virtual void ioParam_triggerFlag(enum ParamsIOFlag ioFlag)
triggerFlag: (Deprecated) Specifies if this layer is being triggered
virtual double getDeltaTriggerTime()
const float * getLayerData(int delay=0)
virtual void ioParam_writeSparseValues(enum ParamsIOFlag ioFlag)
writeSparseValues: No longer used.
virtual void ioParam_updateGpu(enum ParamsIOFlag ioFlag)
updateGpu: When compiled using CUDA or OpenCL GPU acceleration, this flag tells whether this layer's ...
virtual void ioParam_nxScale(enum ParamsIOFlag ioFlag)
nxScale: Defines the relationship between the x column size and the layer size.
virtual bool needReset(double timed, double dt)
virtual void ioParam_nyScale(enum ParamsIOFlag ioFlag)
nyScale: Defines the relationship between the y column size and the layer size.
virtual void ioParam_initialWriteTime(enum ParamsIOFlag ioFlag)
initialWriteTime: Specifies the first timestep to start outputing pvp files