int copyCorrectOutput(HyPerCol * hc, int argc, char * argv[]) { int status = PV_SUCCESS; std::string sourcePathString = hc->getOutputPath(); sourcePathString += "/" "a3_reconstruction.pvp"; const char * sourcePath = sourcePathString.c_str(); MoviePvp * correctLayer = dynamic_cast<MoviePvp *>(hc->getLayerFromName("correct")); assert(correctLayer); const char * destPath = correctLayer->getInputPath(); if (strcmp(&destPath[strlen(destPath)-4], ".pvp")!=0) { if (hc->columnId()==0) { fprintf(stderr, "%s --generate: This system test assumes that the layer \"correct\" is a Movie layer with imageListPath ending in \".pvp\".\n", argv[0]); } MPI_Barrier(hc->icCommunicator()->communicator()); exit(EXIT_FAILURE); } if (hc->columnId()==0) { PV_Stream * infile = PV_fopen(sourcePath, "r", false/*verifyWrites*/); assert(infile); PV_fseek(infile, 0L, SEEK_END); long int filelength = PV_ftell(infile); PV_fseek(infile, 0L, SEEK_SET); char * buf = (char *) malloc((size_t) filelength); size_t charsread = PV_fread(buf, sizeof(char), (size_t) filelength, infile); assert(charsread == (size_t) filelength); PV_fclose(infile); infile = NULL; PV_Stream * outfile = PV_fopen(destPath, "w", false/*verifyWrites*/); assert(outfile); size_t charswritten = PV_fwrite(buf, sizeof(char), (size_t) filelength, outfile); assert(charswritten == (size_t) filelength); PV_fclose(outfile); outfile = NULL; free(buf); buf = NULL; } return status; }
int TrainingLayer::readTrainingLabels(const char * filename, int ** trainingLabelsFromFile) { PV_Stream * instream = PV_fopen(filename, "r", false/*verifyWrites*/); if( instream == NULL ) { fprintf( stderr, "TrainingLayer error opening \"%s\": %s\n", filename, strerror(errno) ); *trainingLabelsFromFile = NULL; return 0; } int didReadLabel; int n = 0; int label; int * labels = NULL; int * oldlabels; do { didReadLabel = fscanf(instream->fp, "%d", &label); updatePV_StreamFilepos(instream); // to recalculate instream->filepos since it's not easy to tell how many characters were read switch( didReadLabel ) { case 0: PV_fseek( instream, 1L, SEEK_CUR ); break; case 1: n++; oldlabels = labels; labels = (int *) malloc((size_t) n * sizeof(int) ); assert(labels); for(int k=0; k<n-1; k++) labels[k] = oldlabels[k]; labels[n-1] = label; free(oldlabels); break; } } while( didReadLabel != EOF ); PV_fclose(instream); *trainingLabelsFromFile = labels; return n; }
//This function will reset the file position of the open file int Movie::getNumFrames(){ int count = 0; if(parent->columnId()==0){ int c; PV_fseek(filenamestream, 0L, SEEK_SET); while((c = fgetc(filenamestream->fp)) != EOF) { count++; ungetc(c, filenamestream->fp); //Here, we're using batch 0, but we're resetting the batch pos of it at the end advanceFileName(0); } PV_fseek(filenamestream, 0L, SEEK_SET); batchPos[0] = 0L; frameNumbers[0] = -1; } MPI_Bcast(&count, 1, MPI_INT, 0, parent->icCommunicator()->communicator()); return count; }
int MoviePvp::checkpointRead(const char * cpDir, double * timef){ int status = ImagePvp::checkpointRead(cpDir, timef); // should this be moved to readStateFromCheckpoint? if (writeFrameToTimestamp) { long timestampFilePos = 0L; parent->readScalarFromFile(cpDir, getName(), "TimestampState", ×tampFilePos, timestampFilePos); if (timestampFile) { assert(parent->columnId()==0); if (PV_fseek(timestampFile, timestampFilePos, SEEK_SET) != 0) { fprintf(stderr, "MovieLayer::checkpointRead error: unable to recover initial file position in timestamp file for layer %s: %s\n", name, strerror(errno)); exit(EXIT_FAILURE); } } } return status; }
//This function takes care of rewinding for frame files const char * Movie::advanceFileName(int batchIdx) { // IMPORTANT!! This function should only be called by getNextFileName(int), and only by the root process assert(parent->columnId()==0); //Restore position of batch Idx PV_fseek(filenamestream, batchPos[batchIdx], SEEK_SET); int c; size_t maxlen = PV_PATH_MAX; bool reset = false; // Ignore blank lines bool hasrewound = false; bool lineisblank = true; while(lineisblank) { // if at end of file (EOF), rewind if ((c = fgetc(filenamestream->fp)) == EOF) { PV_fseek(filenamestream, 0L, SEEK_SET); frameNumbers[0] = -1; fprintf(stderr, "Movie %s: EOF reached, rewinding file \"%s\"\n", name, inputPath); if (hasrewound) { fprintf(stderr, "Movie %s: filenamestream \"%s\" does not have any non-blank lines.\n", name, filenamestream->name); exit(EXIT_FAILURE); } hasrewound = true; reset = true; } else { ungetc(c, filenamestream->fp); } //Always do at least once int loopCount = 0; do{ char * path = fgets(inputfile, maxlen, filenamestream->fp); if (path != NULL) { filenamestream->filepos += strlen(path); path[PV_PATH_MAX-1] = '\0'; size_t len = strlen(path); if (len > 0) { if (path[len-1] == '\n') { path[len-1] = '\0'; len--; } } for (size_t n=0; n<len; n++) { if (!isblank(path[n])) { frameNumbers[batchIdx]++; lineisblank = false; break; } } loopCount++; } }while(resetToStartOnLoop && reset && loopCount < startFrameIndex[batchIdx]); assert(strlen(inputfile)>(size_t) 0); // assert(inputfile && strlen(inputfile)>(size_t) 0); // current version of clang generates a warning since inputfile is a member variable declared as an array and therefore always non-null. // Keeping the line in case inputfile is changed to be malloc'd instead of declared as an array. char * expandedpath = expandLeadingTilde(inputfile); if (strlen(expandedpath)>=PV_PATH_MAX) { fprintf(stderr, "Movie \"%s\": input line \"%s\" from imageListPath is too long.\n", name, expandedpath); exit(EXIT_FAILURE); } strncpy(inputfile, expandedpath, PV_PATH_MAX); free(expandedpath); } //Save batch position batchPos[batchIdx] = getPV_StreamFilepos(filenamestream); return inputfile; }