From f6c9f761a98688d0055e15f96b52504b28786861 Mon Sep 17 00:00:00 2001 From: Carlchristian Eckert Date: Mon, 8 Jun 2015 15:10:38 +0200 Subject: [PATCH 1/2] changed all filesystem code to boost::filesystem - closes #40 --- CMakeLists.txt | 2 +- include/parser.hpp | 93 +++++++++++++++------- include/progressbar.hpp | 4 +- include/write_matlab_output.hpp | 4 +- include/write_to_file.hpp | 6 +- include/write_to_vtk.hpp | 8 +- src/main.cu | 24 +++--- src/parser.cu | 80 ++++++++++--------- src/{progressbar.cu => progressbar.cc} | 15 ++-- src/write_matlab_output.cu | 32 +++++--- src/{write_to_file.cu => write_to_file.cc} | 28 ++++--- src/{write_to_vtk.cu => write_to_vtk.cc} | 43 +++++----- 12 files changed, 206 insertions(+), 133 deletions(-) rename src/{progressbar.cu => progressbar.cc} (95%) rename src/{write_to_file.cu => write_to_file.cc} (71%) rename src/{write_to_vtk.cu => write_to_vtk.cc} (88%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2d68e69..f66f553 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -64,7 +64,7 @@ set(CUDA_NVCC_LINKER_FLAGS ${CUDA_NVCC_FLAGS}) ############################################################################### # Boost ############################################################################### -find_package(Boost 1.48.0 REQUIRED COMPONENTS program_options filesystem) +find_package(Boost 1.48.0 COMPONENTS system program_options filesystem REQUIRED) include_directories(SYSTEM ${Boost_INCLUDE_DIRS}) set(LIBS ${LIBS} ${Boost_LIBRARIES}) diff --git a/include/parser.hpp b/include/parser.hpp index f499321..07fe758 100644 --- a/include/parser.hpp +++ b/include/parser.hpp @@ -1,5 +1,5 @@ /** - * Copyright 2013 Erik Zenker, Carlchristian Eckert, Marius Melzer + * Copyright 2015 Erik Zenker, Carlchristian Eckert, Marius Melzer * * This file is part of HASEonGPU * @@ -29,15 +29,16 @@ #pragma once #include /* string */ -#include -#include /* ifstream */ -#include /* atof */ -#include +#include #include #include #include +#include /* fs::path */ +#include /* fs::fstream */ +namespace fs = boost::filesystem; + enum DeviceMode { NO_DEVICE_MODE, GPU_DEVICE_MODE, CPU_DEVICE_MODE}; enum ParallelMode { NO_PARALLEL_MODE, THREADED_PARALLEL_MODE, MPI_PARALLEL_MODE }; @@ -49,25 +50,24 @@ enum ParallelMode { NO_PARALLEL_MODE, THREADED_PARALLEL_MODE, MPI_PARALLEL_MODE * int, float, double). * * @param filename file to parse - * @param vector contains the parsed values + * @param vector contains the parsed values * * @return 1 if parsing was succesful (file can be opened) * @return 0 otherwise **/ template -int fileToVector(const std::string filename, std::vector *v){ +int fileToVector(const fs::path filename, std::vector *v){ std::string line; - std::ifstream fileStream; + fs::ifstream fileStream; T value = 0.0; - fileStream.open(filename.c_str()); + fileStream.open(filename); if(fileStream.is_open()){ while(fileStream.good()){ - std::getline(fileStream, line); - value = (T) atof(line.c_str()); + fileStream >> value; if(isNaN(value)){ - dout(V_ERROR) << "NAN in input data: " << filename << std::endl; - exit(1); + dout(V_ERROR) << "NAN in input data: " << filename << std::endl; + exit(1); } v->push_back(value); } @@ -81,28 +81,46 @@ int fileToVector(const std::string filename, std::vector *v){ v->pop_back(); fileStream.close(); return 0; - + } + + +/** + * @brief overload to allow distinct directory and filename + * + * @param directory directory where file is located + * @param filename file to parse + * @param vector contains the parsed values + * + * @return 1 if parsing was succesful (file can be opened) + * @return 0 otherwise + **/ +template +int fileToVector(const fs::path directory, const fs::path filename, std::vector *v){ + fs::path tempPath(directory); + return(fileToVector(tempPath /= filename, v)); +} + + /** * @brief Parses just one line(value) of a given file(filename). * The value should be a number (short, unsigned, * int, float, double). * * @param filename file to parse - * @param value is the value which was parsed + * @param value is the value which was parsed * * @return 1 if parsing was succesful (file can be opened) * @return 0 otherwise **/ template -int fileToValue(const std::string filename, T &value){ +int fileToValue(const fs::path filename, T &value){ std::string line; - std::ifstream fileStream; + fs::ifstream fileStream; - fileStream.open(filename.c_str()); + fileStream.open(filename); if(fileStream.is_open()){ - std::getline(fileStream, line); - value = (T) atof(line.c_str()); + fileStream >> value; } else{ dout(V_ERROR) << "Can't open file " << filename << std::endl; @@ -111,7 +129,24 @@ int fileToValue(const std::string filename, T &value){ } fileStream.close(); return 0; - + +} + + +/** + * @brief overload to allow distinct directory and filename + * + * @param directory directory where file is located + * @param filename file to parse + * @param value is the value which was parsed + * + * @return 1 if parsing was succesful (file can be opened) + * @return 0 otherwise + **/ +template +int fileToValue(const fs::path directory, const fs::path filename, T &value){ + fs::path tempPath(directory); + return(fileToValue(tempPath /= filename, value)); } @@ -120,7 +155,7 @@ void parseCommandLine( char** argv, unsigned *raysPerSample, unsigned *maxRaysPerSample, - std::string *inputPath, + fs::path *inputPath, bool *writeVtk, DeviceMode *deviceMode, ParallelMode *parallelMode, @@ -129,7 +164,7 @@ void parseCommandLine( int *minSample_i, int *maxSample_i, unsigned *maxRepetitions, - std::string *outputPath, + fs::path *outputPath, double *mseThreshold, unsigned *lambdaResolution ); @@ -137,9 +172,9 @@ void parseCommandLine( void printCommandLine( unsigned raysPerSample, unsigned maxRaysPerSample, - std::string inputPath, + fs::path inputPath, bool writeVtk, - std::string compareLocation, + fs::path compareLocation, const DeviceMode deviceMode, const ParallelMode parallelMode, bool useReflections, @@ -147,7 +182,7 @@ void printCommandLine( int minSample_i, int maxSample_i, unsigned maxRepetitions, - std::string outputPath, + fs::path outputPath, double mseThreshold ); @@ -155,7 +190,7 @@ int checkParameterValidity( const int argc, const unsigned raysPerSample, unsigned *maxRaysPerSample, - const std::string inputPath, + const fs::path inputPath, const unsigned deviceCount, const DeviceMode deviceMode, const ParallelMode parallelMode, @@ -163,7 +198,7 @@ int checkParameterValidity( const int minSample_i, const int maxSample_i, const unsigned maxRepetitions, - const std::string outputPath, + const fs::path outputPath, double *mseThreshold ); @@ -173,7 +208,7 @@ void checkSampleRange( const unsigned numberOfSamples ); -std::vector parseMesh(std::string rootPath, +std::vector parseMesh(fs::path rootPath, std::vector devices, unsigned maxGpus); diff --git a/include/progressbar.hpp b/include/progressbar.hpp index 5068b67..fe9013f 100644 --- a/include/progressbar.hpp +++ b/include/progressbar.hpp @@ -32,6 +32,8 @@ #pragma once #include +#include /* boost::filesystem::path */ + /** * @brief writes the progress of an operation to dout(V_PROGRESS) (see logging.h), * updating the progress every time the function is called. @@ -75,4 +77,4 @@ void fancyProgressBar(const unsigned current,const unsigned nTotal); * @param path the name of the file to write * */ -void fileProgressBar(const unsigned nTotal, const std::string path); +void fileProgressBar(const unsigned nTotal, const boost::filesystem::path path); diff --git a/include/write_matlab_output.hpp b/include/write_matlab_output.hpp index da9ac56..582c874 100644 --- a/include/write_matlab_output.hpp +++ b/include/write_matlab_output.hpp @@ -23,6 +23,8 @@ #include #include +#include /* boost::filesystem::path */ + /** * @brief creates textfiles containing all the results as a 2D matrix * (needs to be reshaped in matlab, see calcPhiASE.m) @@ -41,7 +43,7 @@ * @license GPLv3 */ void writeMatlabOutput( - const std::string experimentPath, + const boost::filesystem::path experimentPath, const std::vector ase, const std::vector N_rays, const std::vector mse_values, diff --git a/include/write_to_file.hpp b/include/write_to_file.hpp index 2ee58fc..d2ddbc7 100644 --- a/include/write_to_file.hpp +++ b/include/write_to_file.hpp @@ -29,6 +29,8 @@ #include #include +#include /* boost::filesystem::path */ + /** * @brief writes a value to a file, where the filename is appended with 2 longish indices * @@ -41,7 +43,7 @@ */ int writeValueToFile( const float value, - const std::string path, + const boost::filesystem::path path, const std::string indexName1, const int index1, const std::string indexName2, @@ -56,4 +58,4 @@ int writeValueToFile( * @param pFilename the name of the output file * */ -void writeVectorToFile(std::vector v, std::string pFilename); +void writeVectorToFile(std::vector v, boost::filesystem::path filename); diff --git a/include/write_to_vtk.hpp b/include/write_to_vtk.hpp index 98c1a55..90dd6b6 100644 --- a/include/write_to_vtk.hpp +++ b/include/write_to_vtk.hpp @@ -29,6 +29,8 @@ #include #include +#include /* boost::filesystem::path */ + #include /** @@ -48,7 +50,7 @@ */ int writePointsToVtk(const Mesh& mesh, const std::vector ase, - const std::string filename, + const boost::filesystem::path filename, const unsigned minRaysPerSample, const unsigned maxRaysPerSample, const float mseThreshold, @@ -72,7 +74,7 @@ int writePointsToVtk(const Mesh& mesh, */ int writePrismToVtk(const Mesh& mesh, const std::vector prismData, - const std::string filename, + const boost::filesystem::path filename, const unsigned minRaysPerSample, const unsigned maxRaysPerSample, const float mseThreshold, @@ -87,7 +89,7 @@ int writePrismToVtk(const Mesh& mesh, * @return a vector containing the difference between "compare" and "data" * */ -std::vector compareVtk(std::vector compare, std::string filename); +std::vector compareVtk(std::vector compare, boost::filesystem::path filename); diff --git a/src/main.cu b/src/main.cu index 613e8fe..695630c 100644 --- a/src/main.cu +++ b/src/main.cu @@ -37,6 +37,10 @@ #include /* accumulate*/ #include +// Boost stuff +#include /* fs::path */ +namespace fs = boost::filesystem; + // User header files #include #include @@ -93,8 +97,8 @@ int main(int argc, char **argv){ time_t starttime = time(0); unsigned usedGpus = 0; - std::string inputPath; - std::string outputPath; + fs::path inputPath; + fs::path outputPath; double mseThreshold = 0; // Wavelength data @@ -121,10 +125,10 @@ int main(int argc, char **argv){ dout(V_INFO) << "parameter validity was checked!" << std::endl; // Parse wavelengths from files - if(fileToVector(inputPath + "sigmaA.txt", &sigmaA)) return 1; - if(fileToVector(inputPath + "sigmaE.txt", &sigmaE)) return 1; - if(fileToVector(inputPath + "lambdaA.txt", &lambdaA)) return 1; - if(fileToVector(inputPath + "lambdaE.txt", &lambdaE)) return 1; + if(fileToVector(inputPath, "sigmaA.txt", &sigmaA)) return 1; + if(fileToVector(inputPath, "sigmaE.txt", &sigmaE)) return 1; + if(fileToVector(inputPath, "lambdaA.txt", &lambdaA)) return 1; + if(fileToVector(inputPath, "lambdaE.txt", &lambdaE)) return 1; lambdaResolution = std::max(lambdaResolution, (unsigned) lambdaA.size()); lambdaResolution = std::max(lambdaResolution, (unsigned) lambdaE.size()); @@ -274,10 +278,10 @@ int main(int argc, char **argv){ std::vector tmpPhiAse(phiAse.begin(), phiAse.end()); std::vector tmpTotalRays(totalRays.begin(), totalRays.end()); - writePointsToVtk(meshs[0], dndtAse, outputPath + "vtk/dndt", minRaysPerSample, maxRaysPerSample, mseThreshold, useReflections, runtime); - writePointsToVtk(meshs[0], tmpPhiAse, outputPath + "vtk/phiase", minRaysPerSample, maxRaysPerSample, mseThreshold, useReflections, runtime); - writePointsToVtk(meshs[0], mse, outputPath + "vtk/mse", minRaysPerSample, maxRaysPerSample, mseThreshold, useReflections, runtime); - writePointsToVtk(meshs[0], tmpTotalRays, outputPath + "vtk/total_rays", minRaysPerSample, maxRaysPerSample, mseThreshold, useReflections, runtime); + writePointsToVtk(meshs[0], dndtAse, outputPath /= "vtk/dndt", minRaysPerSample, maxRaysPerSample, mseThreshold, useReflections, runtime); + writePointsToVtk(meshs[0], tmpPhiAse, outputPath /= "vtk/phiase", minRaysPerSample, maxRaysPerSample, mseThreshold, useReflections, runtime); + writePointsToVtk(meshs[0], mse, outputPath /= "vtk/mse", minRaysPerSample, maxRaysPerSample, mseThreshold, useReflections, runtime); + writePointsToVtk(meshs[0], tmpTotalRays, outputPath /= "vtk/total_rays", minRaysPerSample, maxRaysPerSample, mseThreshold, useReflections, runtime); } // Print statistics diff --git a/src/parser.cu b/src/parser.cu index 8ae51de..8a10e55 100644 --- a/src/parser.cu +++ b/src/parser.cu @@ -35,13 +35,17 @@ #include #include +#include /* fs::path */ +namespace fs = boost::filesystem; + + void parseCommandLine( const int argc, char** argv, unsigned *raysPerSample, unsigned *maxRaysPerSample, - std::string *inputPath, + fs::path *inputPath, bool *writeVtk, DeviceMode *deviceMode, ParallelMode *parallelMode, @@ -50,7 +54,7 @@ void parseCommandLine( int *minSample_i, int *maxSample_i, unsigned *maxRepetitions, - std::string *outputPath, + fs::path *outputPath, double *mseThreshold, unsigned *lambdaResolution ) { @@ -82,9 +86,9 @@ void parseCommandLine( "The minimal number of rays to use for each sample point") ( "max-rays", po::value (maxRaysPerSample)->default_value(100000), "The maximal number of rays to use for each sample point") - ( "input-path,i", po::value (inputPath)->default_value("input/"), + ( "input-path,i", po::value (inputPath)->default_value(fs::path("input/")), "The path to a folder that contains the input files") - ( "output-path,o", po::value (outputPath)->default_value("output/"), + ( "output-path,o", po::value (outputPath)->default_value(fs::path("output/")), "The path to a folder that contains the output files") ( "ngpus,g", po::value (maxgpus)->default_value(1), "The maximum number of GPUs to b used on a single node. Should be left unchanged for --parallel-mode=mpi") @@ -125,8 +129,8 @@ void parseCommandLine( *deviceMode = NO_DEVICE_MODE; // append trailing folder separator, if necessary - if(inputPath->at(inputPath->size()-1) != '/') inputPath->append("/"); - if(outputPath->at(outputPath->size()-1) != '/') outputPath->append("/"); + *outputPath /= ""; + *inputPath /= ""; @@ -137,9 +141,9 @@ void parseCommandLine( void printCommandLine( unsigned raysPerSample, unsigned maxRaysPerSample, - std::string inputPath, + const fs::path inputPath, bool writeVtk, - std::string compareLocation, + const fs::path compareLocation, const DeviceMode dMode, const ParallelMode pMode, bool useReflections, @@ -147,7 +151,7 @@ void printCommandLine( int minSample_i, int maxSample_i, unsigned maxRepetitions, - std::string outputPath, + const fs::path outputPath, double mseThreshold){ dout(V_INFO) << "raysPerSample: " << raysPerSample << std::endl; @@ -168,7 +172,7 @@ int checkParameterValidity( const int argc, const unsigned raysPerSample, unsigned *maxRaysPerSample, - const std::string inputPath, + const fs::path inputPath, const unsigned deviceCount, const DeviceMode deviceMode, const ParallelMode parallelMode, @@ -176,7 +180,7 @@ int checkParameterValidity( const int minSampleRange, const int maxSampleRange, const unsigned maxRepetitions, - const std::string outputPath, + const fs::path outputPath, double *mseThreshold ) { @@ -199,12 +203,12 @@ int checkParameterValidity( return 1; } - if (inputPath.size() == 0) { + if (!exists(inputPath) || !is_directory(inputPath) || is_empty(inputPath)) { dout(V_ERROR) << "Please specify the experiment's location with --input-path=PATH_TO_EXPERIMENT" << std::endl; return 1; } - if (outputPath.size() == 0) { + if (!exists(outputPath) || !is_directory(outputPath)) { dout(V_ERROR) << "Please specify the output location with --output-path=PATH_TO_EXPERIMENT" << std::endl; return 1; } @@ -370,10 +374,11 @@ Mesh createMesh(const std::vector &triangleIndices, } + /** * */ -std::vector parseMesh(std::string rootPath, +std::vector parseMesh(const fs::path rootPath, std::vector devices, unsigned maxGpus) { @@ -406,29 +411,30 @@ std::vector parseMesh(std::string rootPath, double claddingAbsorption = 0; // Parse experimentdata from files - if(fileToVector(rootPath + "triangleNormalPoint.txt", &triangleNormalPoint)) return meshs; - if(fileToVector(rootPath + "betaVolume.txt", &betaVolume)) return meshs; - if(fileToVector(rootPath + "forbiddenEdge.txt", &forbiddenEdge)) return meshs; - if(fileToVector(rootPath + "triangleNeighbors.txt", &triangleNeighbors)) return meshs; - if(fileToVector(rootPath + "triangleNormalsX.txt", &triangleNormalsX)) return meshs; - if(fileToVector(rootPath + "triangleNormalsY.txt", &triangleNormalsY)) return meshs; - if(fileToVector(rootPath + "triangleCenterX.txt", &triangleCenterX)) return meshs; - if(fileToVector(rootPath + "triangleCenterY.txt", &triangleCenterY)) return meshs; - if(fileToVector(rootPath + "points.txt", &points)) return meshs; - if(fileToVector(rootPath + "trianglePointIndices.txt", &trianglePointIndices)) return meshs; - if(fileToVector(rootPath + "triangleSurfaces.txt", &triangleSurfaces)) return meshs; - if(fileToValue(rootPath + "numberOfPoints.txt", numberOfPoints)) return meshs; - if(fileToValue(rootPath + "numberOfTriangles.txt", numberOfTriangles)) return meshs; - if(fileToValue(rootPath + "numberOfLevels.txt", numberOfLevels)) return meshs; - if(fileToValue(rootPath + "thickness.txt", thickness)) return meshs; - if(fileToValue(rootPath + "nTot.txt", nTot)) return meshs; - if(fileToValue(rootPath + "crystalTFluo.txt", crystalTFluo)) return meshs; - if(fileToValue(rootPath + "claddingNumber.txt", claddingNumber)) return meshs; - if(fileToValue(rootPath + "claddingAbsorption.txt", claddingAbsorption)) return meshs; - if(fileToVector(rootPath + "betaCells.txt", &betaCells)) return meshs; - if(fileToVector(rootPath + "claddingCellTypes.txt", &claddingCellTypes)) return meshs; - if(fileToVector(rootPath + "refractiveIndices.txt", &refractiveIndices)) return meshs; - if(fileToVector(rootPath + "reflectivities.txt", &reflectivities)) return meshs; + //fs::path triangleNormalPointPath(rootPath); + if(fileToVector(rootPath, "triangleNormalPoint.txt", &triangleNormalPoint)) return meshs; + if(fileToVector(rootPath, "betaVolume.txt", &betaVolume)) return meshs; + if(fileToVector(rootPath, "forbiddenEdge.txt", &forbiddenEdge)) return meshs; + if(fileToVector(rootPath, "triangleNeighbors.txt", &triangleNeighbors)) return meshs; + if(fileToVector(rootPath, "triangleNormalsX.txt", &triangleNormalsX)) return meshs; + if(fileToVector(rootPath, "triangleNormalsY.txt", &triangleNormalsY)) return meshs; + if(fileToVector(rootPath, "triangleCenterX.txt", &triangleCenterX)) return meshs; + if(fileToVector(rootPath, "triangleCenterY.txt", &triangleCenterY)) return meshs; + if(fileToVector(rootPath, "points.txt", &points)) return meshs; + if(fileToVector(rootPath, "trianglePointIndices.txt", &trianglePointIndices)) return meshs; + if(fileToVector(rootPath, "triangleSurfaces.txt", &triangleSurfaces)) return meshs; + if(fileToValue(rootPath, "numberOfPoints.txt", numberOfPoints)) return meshs; + if(fileToValue(rootPath, "numberOfTriangles.txt", numberOfTriangles)) return meshs; + if(fileToValue(rootPath, "numberOfLevels.txt", numberOfLevels)) return meshs; + if(fileToValue(rootPath, "thickness.txt", thickness)) return meshs; + if(fileToValue(rootPath, "nTot.txt", nTot)) return meshs; + if(fileToValue(rootPath, "crystalTFluo.txt", crystalTFluo)) return meshs; + if(fileToValue(rootPath, "claddingNumber.txt", claddingNumber)) return meshs; + if(fileToValue(rootPath, "claddingAbsorption.txt", claddingAbsorption)) return meshs; + if(fileToVector(rootPath, "betaCells.txt", &betaCells)) return meshs; + if(fileToVector(rootPath, "claddingCellTypes.txt", &claddingCellTypes)) return meshs; + if(fileToVector(rootPath, "refractiveIndices.txt", &refractiveIndices)) return meshs; + if(fileToVector(rootPath, "reflectivities.txt", &reflectivities)) return meshs; // assert input sizes assert(numberOfPoints == (points.size() / 2)); diff --git a/src/progressbar.cu b/src/progressbar.cc similarity index 95% rename from src/progressbar.cu rename to src/progressbar.cc index a2eebdf..aa092f5 100644 --- a/src/progressbar.cu +++ b/src/progressbar.cc @@ -25,9 +25,14 @@ #include #include +#include /* fs::path */ +#include /* fs::fstream */ + #include #include +namespace fs = boost::filesystem; + /** * @brief prints a line of ascii-art in the style of a sine-wave * @@ -103,7 +108,7 @@ void fancyProgressBar(const unsigned nTotal){ //find the starting time of the whole progress if(part==0){ gettimeofday(&startTime,NULL); } - maxNTotal = max(maxNTotal, nTotal); + maxNTotal = std::max(maxNTotal, nTotal); static const unsigned fillwidthPart = unsigned(1+log10(maxNTotal)); static unsigned tic = 0; timeval now; @@ -142,7 +147,7 @@ void fancyProgressBar(const unsigned current, const unsigned nTotal){ // get the starting time on the very first call if(part==0){ gettimeofday(&startTime,NULL); } - maxNTotal = max(maxNTotal, nTotal); + maxNTotal = std::max(maxNTotal, nTotal); static const unsigned fillwidthPart = unsigned(1+log10(maxNTotal)); static unsigned tic = 0; timeval now; @@ -173,10 +178,10 @@ void fancyProgressBar(const unsigned current, const unsigned nTotal){ } -void fileProgressBar(unsigned nTotal, std::string path){ +void fileProgressBar(unsigned nTotal, const fs::path path){ const int length = 50; - std::ofstream filestream; + fs::ofstream filestream; static timeval startTime; static unsigned part = 0; //if this is the first call, set the start time @@ -193,7 +198,7 @@ void fileProgressBar(unsigned nTotal, std::string path){ ++tic; if(!filestream.is_open()){ - filestream.open(path.c_str(),std::ofstream::trunc); + filestream.open(path,std::ofstream::trunc); } const float percentage = float(part) / float(nTotal); diff --git a/src/write_matlab_output.cu b/src/write_matlab_output.cu index 31515af..94734a2 100644 --- a/src/write_matlab_output.cu +++ b/src/write_matlab_output.cu @@ -19,12 +19,15 @@ */ -#include #include -#include +#include /* std::setprecision(), std::fixed() */ #include +#include /* fs::path */ +#include /* fs::fstream */ +namespace fs = boost::filesystem; + /** * @brief write input data in a 3D-matrix which can be parsed by matlab with reshape * (see calcPhiASE.m) @@ -40,7 +43,7 @@ template void write3dMatrix( const std::vector& data, - std::ofstream &file, + fs::ofstream &file, const unsigned rowCount, const unsigned columnCount, const unsigned pageCount @@ -62,27 +65,30 @@ void write3dMatrix( void writeMatlabOutput( - const std::string experimentPath, + const fs::path experimentPath, const std::vector ase, const std::vector N_rays, const std::vector expectedValues, const unsigned numberOfSamples, const unsigned numberOfLevels){ - std::ofstream aseFile; - std::ofstream raysFile; - std::ofstream expectedValuesFile; + fs::ofstream aseFile; + fs::ofstream raysFile; + fs::ofstream expectedValuesFile; const unsigned samplesPerLevel = numberOfSamples/numberOfLevels; - aseFile.open((experimentPath + "phi_ASE.txt").c_str()); - write3dMatrix(ase,aseFile,samplesPerLevel,numberOfLevels,1); + fs::path asePath(experimentPath); + aseFile.open(asePath /= "phi_ASE.txt"); + write3dMatrix(ase, aseFile, samplesPerLevel, numberOfLevels, 1); aseFile.close(); - raysFile.open((experimentPath + "N_rays.txt").c_str()); - write3dMatrix(N_rays,raysFile,samplesPerLevel,numberOfLevels,1); + fs::path raysPath(experimentPath); + raysFile.open(raysPath /= "N_rays.txt"); + write3dMatrix(N_rays, raysFile, samplesPerLevel, numberOfLevels, 1); raysFile.close(); - expectedValuesFile.open((experimentPath + "mse_values.txt").c_str()); - write3dMatrix(expectedValues,expectedValuesFile,samplesPerLevel,numberOfLevels,1); + fs::path msePath(experimentPath); + expectedValuesFile.open(msePath /= "mse_values.txt"); + write3dMatrix(expectedValues, expectedValuesFile, samplesPerLevel, numberOfLevels, 1); expectedValuesFile.close(); } diff --git a/src/write_to_file.cu b/src/write_to_file.cc similarity index 71% rename from src/write_to_file.cu rename to src/write_to_file.cc index eaca001..711b89d 100644 --- a/src/write_to_file.cu +++ b/src/write_to_file.cc @@ -19,15 +19,19 @@ */ -#include /* ofstream */ -#include -#include +#include /* std::stringstream */ +#include /* std::to_string */ +#include /* setw(), setfill */ + +#include /* fs::path */ +#include /* fs::fstream */ +namespace fs = boost::filesystem; #include int writeValueToFile( const float value, - const std::string path, + const fs::path path, const std::string indexName1, const int index1, const std::string indexName2, @@ -38,8 +42,8 @@ int writeValueToFile( stringstream filenameStream; filenameStream << path << indexName1 << "_" << setfill('0') << setw(3) << index1 << "_" << indexName2 << setfill('0') << setw(6) << index2; - ofstream oFile; - oFile.open(filenameStream.str().c_str()); + fs::ofstream oFile; + oFile.open(filenameStream.str()); oFile << value << endl; oFile.close(); @@ -47,20 +51,22 @@ int writeValueToFile( } -void writeVectorToFile(std::vector v, std::string pFilename){ +void writeVectorToFile(std::vector v, fs::path filename){ // Add time to filename time_t currentTime; time(¤tTime); - std::stringstream filenameStream; - filenameStream << pFilename << "_" << (int) currentTime << ".dat"; + filename += "_"; + filename += std::to_string((int) currentTime); + filename += ".dat"; // Init filestream - std::ofstream file; - file.open(filenameStream.str().c_str()); + fs::ofstream file; + file.open(filename); // Write vector data for(std::vector::iterator it = v.begin(); it != v.end(); ++it){ file << *it << std::endl; } + file.close(); } diff --git a/src/write_to_vtk.cu b/src/write_to_vtk.cc similarity index 88% rename from src/write_to_vtk.cu rename to src/write_to_vtk.cc index 3ffe27a..b8f5200 100644 --- a/src/write_to_vtk.cu +++ b/src/write_to_vtk.cc @@ -20,12 +20,14 @@ -#include /* ofstream */ #include /* vector */ -#include /* std::setprecision() */ -#include +#include /* std::fixed, std::setprecision() */ +#include /* std::to_string() */ #include /* time, time_t */ -#include /* std::stringstream */ + +#include /* fs::path */ +#include /* fs::ofstream, fs::ifstream */ +namespace fs = boost::filesystem; #include #include @@ -39,7 +41,7 @@ */ int writeToVtk(const Mesh& mesh, const std::vector data, - const std::string pfilename, + fs::path filename, const unsigned raysPerSample, const unsigned maxRaysPerSample, const float expectationThreshold, @@ -59,23 +61,24 @@ int writeToVtk(const Mesh& mesh, // Construct experiment information unsigned r = useReflections ? mesh.getMaxReflections() : 0; - std::stringstream experimentStream; - experimentStream << "RAYS=" << raysPerSample << " MAXRAYS=" << maxRaysPerSample << " REFLECTIONS=" << r << " EXPECTATION=" << expectationThreshold << " RUNTIME=" << runtime; // Add time to filename time_t currentTime; time(¤tTime); - std::stringstream filenameStream; - filenameStream << pfilename << "_" << (int) currentTime << ".vtk"; + filename += ("_"); + filename += std::to_string((int) currentTime); + filename += (".vtk"); - dout(V_INFO) << "Write experiment data to vtk-file " << filenameStream.str() << std::endl; + dout(V_INFO) << "Write experiment data to vtk-file " << filename << std::endl; - std::ofstream vtkFile; - vtkFile.open(filenameStream.str().c_str()); + fs::ofstream vtkFile; + vtkFile.open(filename); // Write header of vtk file vtkFile << "# vtk DataFile Version 2.0" << std::endl; - vtkFile << experimentStream.str() << std::endl; + vtkFile << "RAYS=" << raysPerSample << " MAXRAYS=" << maxRaysPerSample << + " REFLECTIONS=" << r << " EXPECTATION=" << expectationThreshold << + " RUNTIME=" << runtime << std::endl; vtkFile << "ASCII" << std::endl; // Write point data @@ -127,7 +130,7 @@ int writeToVtk(const Mesh& mesh, int writePrismToVtk( const Mesh& mesh, const std::vector prismData, - const std::string pfilename, + const fs::path filename, const unsigned raysPerSample, const unsigned maxRaysPerSample, const float expectationThreshold, @@ -138,7 +141,7 @@ int writePrismToVtk( return writeToVtk( mesh, prismData, - pfilename, + filename, raysPerSample, maxRaysPerSample, expectationThreshold, @@ -151,7 +154,7 @@ int writePrismToVtk( int writePointsToVtk( const Mesh& mesh, const std::vector prismData, - const std::string pfilename, + const fs::path filename, const unsigned raysPerSample, const unsigned maxRaysPerSample, const float expectationThreshold, @@ -162,7 +165,7 @@ int writePointsToVtk( return writeToVtk( mesh, prismData, - pfilename, + filename, raysPerSample, maxRaysPerSample, expectationThreshold, @@ -173,8 +176,8 @@ int writePointsToVtk( -std::vector compareVtk(std::vector compare, std::string filename){ - std::ifstream filestream; +std::vector compareVtk(std::vector compare, fs::path filename){ + fs::ifstream filestream; std::string line; bool foundLine = false; double value = 0; @@ -196,7 +199,7 @@ std::vector compareVtk(std::vector compare, std::string filename aseTotal += compare.at(i); } - filestream.open(filename.c_str(), std::ifstream::in); + filestream.open(filename, fs::ifstream::in); if(filestream.is_open()){ while(filestream.good()){ From 11a13fa3582bb916ee59acdd9aa54d0c8d9ba61e Mon Sep 17 00:00:00 2001 From: Carlchristian Eckert Date: Mon, 8 Jun 2015 21:01:59 +0200 Subject: [PATCH 2/2] some const issues and license date --- include/parser.hpp | 2 +- include/progressbar.hpp | 2 +- include/write_matlab_output.hpp | 2 +- include/write_to_file.hpp | 2 +- include/write_to_vtk.hpp | 4 ++-- src/main.cu | 2 +- src/parser.cu | 2 +- src/progressbar.cc | 2 +- src/write_matlab_output.cu | 2 +- src/write_to_file.cc | 2 +- src/write_to_vtk.cc | 4 ++-- 11 files changed, 13 insertions(+), 13 deletions(-) diff --git a/include/parser.hpp b/include/parser.hpp index 07fe758..402052b 100644 --- a/include/parser.hpp +++ b/include/parser.hpp @@ -208,7 +208,7 @@ void checkSampleRange( const unsigned numberOfSamples ); -std::vector parseMesh(fs::path rootPath, +std::vector parseMesh(const fs::path rootPath, std::vector devices, unsigned maxGpus); diff --git a/include/progressbar.hpp b/include/progressbar.hpp index fe9013f..d290b80 100644 --- a/include/progressbar.hpp +++ b/include/progressbar.hpp @@ -1,5 +1,5 @@ /** - * Copyright 2013 Erik Zenker, Carlchristian Eckert, Marius Melzer + * Copyright 2015 Erik Zenker, Carlchristian Eckert, Marius Melzer * * This file is part of HASEonGPU * diff --git a/include/write_matlab_output.hpp b/include/write_matlab_output.hpp index 582c874..1d683ac 100644 --- a/include/write_matlab_output.hpp +++ b/include/write_matlab_output.hpp @@ -1,5 +1,5 @@ /** - * Copyright 2013 Erik Zenker, Carlchristian Eckert, Marius Melzer + * Copyright 2015 Erik Zenker, Carlchristian Eckert, Marius Melzer * * This file is part of HASEonGPU * diff --git a/include/write_to_file.hpp b/include/write_to_file.hpp index d2ddbc7..1896bbd 100644 --- a/include/write_to_file.hpp +++ b/include/write_to_file.hpp @@ -1,5 +1,5 @@ /** - * Copyright 2013 Erik Zenker, Carlchristian Eckert, Marius Melzer + * Copyright 2015 Erik Zenker, Carlchristian Eckert, Marius Melzer * * This file is part of HASEonGPU * diff --git a/include/write_to_vtk.hpp b/include/write_to_vtk.hpp index 90dd6b6..2db00bf 100644 --- a/include/write_to_vtk.hpp +++ b/include/write_to_vtk.hpp @@ -1,5 +1,5 @@ /** - * Copyright 2013 Erik Zenker, Carlchristian Eckert, Marius Melzer + * Copyright 2015 Erik Zenker, Carlchristian Eckert, Marius Melzer * * This file is part of HASEonGPU * @@ -89,7 +89,7 @@ int writePrismToVtk(const Mesh& mesh, * @return a vector containing the difference between "compare" and "data" * */ -std::vector compareVtk(std::vector compare, boost::filesystem::path filename); +std::vector compareVtk(std::vector compare, const boost::filesystem::path filename); diff --git a/src/main.cu b/src/main.cu index 695630c..8d53a28 100644 --- a/src/main.cu +++ b/src/main.cu @@ -1,5 +1,5 @@ /** - * Copyright 2013 Erik Zenker, Carlchristian Eckert, Marius Melzer + * Copyright 2015 Erik Zenker, Carlchristian Eckert, Marius Melzer * * This file is part of HASEonGPU * diff --git a/src/parser.cu b/src/parser.cu index 8a10e55..0a1db2f 100644 --- a/src/parser.cu +++ b/src/parser.cu @@ -1,5 +1,5 @@ /** - * Copyright 2013 Erik Zenker, Carlchristian Eckert, Marius Melzer + * Copyright 2015 Erik Zenker, Carlchristian Eckert, Marius Melzer * * This file is part of HASEonGPU * diff --git a/src/progressbar.cc b/src/progressbar.cc index aa092f5..c43c285 100644 --- a/src/progressbar.cc +++ b/src/progressbar.cc @@ -1,5 +1,5 @@ /** - * Copyright 2013 Erik Zenker, Carlchristian Eckert, Marius Melzer + * Copyright 2015 Erik Zenker, Carlchristian Eckert, Marius Melzer * * This file is part of HASEonGPU * diff --git a/src/write_matlab_output.cu b/src/write_matlab_output.cu index 94734a2..98154d8 100644 --- a/src/write_matlab_output.cu +++ b/src/write_matlab_output.cu @@ -1,5 +1,5 @@ /** - * Copyright 2013 Erik Zenker, Carlchristian Eckert, Marius Melzer + * Copyright 2015 Erik Zenker, Carlchristian Eckert, Marius Melzer * * This file is part of HASEonGPU * diff --git a/src/write_to_file.cc b/src/write_to_file.cc index 711b89d..8efce80 100644 --- a/src/write_to_file.cc +++ b/src/write_to_file.cc @@ -1,5 +1,5 @@ /** - * Copyright 2013 Erik Zenker, Carlchristian Eckert, Marius Melzer + * Copyright 2015 Erik Zenker, Carlchristian Eckert, Marius Melzer * * This file is part of HASEonGPU * diff --git a/src/write_to_vtk.cc b/src/write_to_vtk.cc index b8f5200..10d49ee 100644 --- a/src/write_to_vtk.cc +++ b/src/write_to_vtk.cc @@ -1,5 +1,5 @@ /** - * Copyright 2013 Erik Zenker, Carlchristian Eckert, Marius Melzer + * Copyright 2015 Erik Zenker, Carlchristian Eckert, Marius Melzer * * This file is part of HASEonGPU * @@ -176,7 +176,7 @@ int writePointsToVtk( -std::vector compareVtk(std::vector compare, fs::path filename){ +std::vector compareVtk(std::vector compare, const fs::path filename){ fs::ifstream filestream; std::string line; bool foundLine = false;