PetaVision  Alpha
InitVFromFile.cpp
1 /*
2  * InitVFromFile.cpp
3  *
4  * Created on: Oct 26, 2016
5  * Author: pschultz
6  */
7 
8 #include "InitVFromFile.hpp"
9 #include "columns/HyPerCol.hpp"
10 #include "utils/BufferUtilsMPI.hpp"
11 
12 namespace PV {
13 InitVFromFile::InitVFromFile() { initialize_base(); }
14 
15 InitVFromFile::InitVFromFile(char const *name, HyPerCol *hc) {
16  initialize_base();
17  initialize(name, hc);
18 }
19 
20 InitVFromFile::~InitVFromFile() { free(mVfilename); }
21 
22 int InitVFromFile::initialize_base() { return PV_SUCCESS; }
23 
24 int InitVFromFile::initialize(char const *name, HyPerCol *hc) {
25  int status = BaseInitV::initialize(name, hc);
26  return status;
27 }
28 
29 int InitVFromFile::ioParamsFillGroup(enum ParamsIOFlag ioFlag) {
30  int status = BaseInitV::ioParamsFillGroup(ioFlag);
31  ioParam_Vfilename(ioFlag);
32  ioParam_frameNumber(ioFlag);
33  return status;
34 }
35 
36 void InitVFromFile::ioParam_Vfilename(enum ParamsIOFlag ioFlag) {
37  parent->parameters()->ioParamString(
38  ioFlag, name, "Vfilename", &mVfilename, nullptr, true /*warnIfAbsent*/);
39  if (mVfilename == nullptr) {
40  Fatal().printf(
41  "InitVFromFile::initialize, group \"%s\": for InitVFromFile, string parameter "
42  "\"Vfilename\" "
43  "must be defined. Exiting\n",
44  name);
45  }
46 }
47 
48 void InitVFromFile::ioParam_frameNumber(enum ParamsIOFlag ioFlag) {
49  parent->parameters()->ioParamValue(
50  ioFlag, name, "frameNumber", &mFrameNumber, mFrameNumber, true /*warnIfAbsent*/);
51 }
52 
53 void InitVFromFile::calcV(float *V, const PVLayerLoc *loc) {
54  char const *ext = strrchr(mVfilename, '.');
55  bool isPvpFile = (ext && strcmp(ext, ".pvp") == 0);
56  if (isPvpFile) {
57  FileStream fileStream(mVfilename, std::ios_base::in | std::ios_base::binary, false);
58  BufferUtils::ActivityHeader header = BufferUtils::readActivityHeader(fileStream);
59  int fileType = header.fileType;
60  if (header.fileType == PVP_NONSPIKING_ACT_FILE_TYPE) {
61  readDenseActivityPvp(V, loc, fileStream, header);
62  }
63  else { // TODO: Handle sparse activity pvp files.
64  if (getMPIBlock()->getRank() == 0) {
65  ErrorLog() << "InitVFromFile: filename \"" << mVfilename << "\" has fileType "
66  << header.fileType << ", which is not supported for InitVFromFile.\n";
67  }
68  MPI_Barrier(getMPIBlock()->getComm());
69  MPI_Finalize();
70  exit(EXIT_FAILURE);
71  }
72  }
73  else { // TODO: Treat as an image file
74  if (getMPIBlock()->getRank() == 0) {
75  ErrorLog().printf("InitVFromFile: file \"%s\" is not a pvp file.\n", this->mVfilename);
76  }
77  MPI_Barrier(getMPIBlock()->getComm());
78  exit(EXIT_FAILURE);
79  }
80 }
81 
82 void InitVFromFile::readDenseActivityPvp(
83  float *V,
84  PVLayerLoc const *loc,
85  FileStream &fileStream,
86  BufferUtils::ActivityHeader const &header) {
87  auto mpiBlock = getMPIBlock();
88  bool isRootProc = mpiBlock->getRank() == 0;
89  std::size_t frameSize = (std::size_t)header.recordSize * sizeof(float) + sizeof(double);
90  int numFrames = header.nBands;
91  int blockBatchDimension = mpiBlock->getBatchDimension();
92  for (int m = 0; m < blockBatchDimension; m++) {
93  for (int b = 0; b < loc->nbatch; b++) {
94  int globalBatchIndex = (mpiBlock->getStartBatch() + m) * loc->nbatch + b;
95  float *Vbatch = V + b * (loc->nx * loc->ny * loc->nf);
96  Buffer<float> pvpBuffer;
97  if (isRootProc) {
98  int frameIndex = (mFrameNumber + globalBatchIndex) % numFrames;
99  fileStream.setOutPos(sizeof(header) + frameIndex * sizeof(float) * frameSize, true);
100  int xStart = header.nx * mpiBlock->getStartColumn() / mpiBlock->getNumColumns();
101  int yStart = header.ny * mpiBlock->getStartRow() / mpiBlock->getNumRows();
102  pvpBuffer.resize(header.nx, header.ny, header.nf);
103  BufferUtils::readFrameWindow(fileStream, &pvpBuffer, header, xStart, yStart, 0);
104  }
105  else {
106  pvpBuffer.resize(loc->nx, loc->ny, loc->nf);
107  }
108  BufferUtils::scatter(mpiBlock, pvpBuffer, loc->nx, loc->ny, m, 0);
109  std::vector<float> bufferData = pvpBuffer.asVector();
110  std::memcpy(Vbatch, bufferData.data(), sizeof(float) * pvpBuffer.getTotalElements());
111  }
112  }
113 }
114 
115 } // end namespace PV
virtual void ioParam_frameNumber(enum ParamsIOFlag ioFlag)
frameNumber: selects which frame of the pvp file to use. The default value is zero. Note that this parameter is zero-indexed: for example, if a pvp file has five frames, the allowable values of this parameter are 0 through 4, inclusive. When batching, the batch element 0 loads the indicated frame, batch element 1 loads frameNumber + 1, etc. If the number of frames in the file is exhausted, the file wraps around to the beginning.
virtual int ioParamsFillGroup(enum ParamsIOFlag ioFlag) override
virtual void ioParam_Vfilename(enum ParamsIOFlag ioFlag)
VFilename: The path to the file with the initial values. Relative paths are relative to the working d...
virtual int ioParamsFillGroup(enum ParamsIOFlag ioFlag) override
Definition: BaseInitV.cpp:34