Skip to content

Commit

Permalink
Merge pull request ComputationalRadiationPhysics#60 from slizzered/is…
Browse files Browse the repository at this point in the history
…sue40-boost_filesystem

Nice work! ...it was a bad idea to use plain strings for filenames and paths.
  • Loading branch information
erikzenker committed Jun 8, 2015
2 parents e4fd3dc + 11a13fa commit 9e9e59a
Show file tree
Hide file tree
Showing 12 changed files with 216 additions and 143 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})

Expand Down
93 changes: 64 additions & 29 deletions include/parser.hpp
Original file line number Diff line number Diff line change
@@ -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
*
Expand Down Expand Up @@ -29,15 +29,16 @@

#pragma once
#include <string> /* string */
#include <iostream>
#include <fstream> /* ifstream */
#include <cstdlib> /* atof */
#include <vector>
#include <vector>

#include <logging.hpp>
#include <mesh.hpp>
#include <nan_fix.hpp>

#include <boost/filesystem.hpp> /* fs::path */
#include <boost/filesystem/fstream.hpp> /* 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 };

Expand All @@ -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 <class T>
int fileToVector(const std::string filename, std::vector<T> *v){
int fileToVector(const fs::path filename, std::vector<T> *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);
}
Expand All @@ -81,28 +81,46 @@ int fileToVector(const std::string filename, std::vector<T> *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 <class T>
int fileToVector(const fs::path directory, const fs::path filename, std::vector<T> *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 <class T>
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;
Expand All @@ -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 <class T>
int fileToValue(const fs::path directory, const fs::path filename, T &value){
fs::path tempPath(directory);
return(fileToValue(tempPath /= filename, value));
}


Expand All @@ -120,7 +155,7 @@ void parseCommandLine(
char** argv,
unsigned *raysPerSample,
unsigned *maxRaysPerSample,
std::string *inputPath,
fs::path *inputPath,
bool *writeVtk,
DeviceMode *deviceMode,
ParallelMode *parallelMode,
Expand All @@ -129,41 +164,41 @@ void parseCommandLine(
int *minSample_i,
int *maxSample_i,
unsigned *maxRepetitions,
std::string *outputPath,
fs::path *outputPath,
double *mseThreshold,
unsigned *lambdaResolution
);

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,
unsigned maxgpus,
int minSample_i,
int maxSample_i,
unsigned maxRepetitions,
std::string outputPath,
fs::path outputPath,
double mseThreshold
);

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,
unsigned *maxgpus,
const int minSample_i,
const int maxSample_i,
const unsigned maxRepetitions,
const std::string outputPath,
const fs::path outputPath,
double *mseThreshold
);

Expand All @@ -173,7 +208,7 @@ void checkSampleRange(
const unsigned numberOfSamples
);

std::vector<Mesh> parseMesh(std::string rootPath,
std::vector<Mesh> parseMesh(const fs::path rootPath,
std::vector<unsigned> devices,
unsigned maxGpus);

Expand Down
6 changes: 4 additions & 2 deletions include/progressbar.hpp
Original file line number Diff line number Diff line change
@@ -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
*
Expand Down Expand Up @@ -32,6 +32,8 @@
#pragma once
#include <string>

#include <boost/filesystem.hpp> /* 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.
Expand Down Expand Up @@ -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);
6 changes: 4 additions & 2 deletions include/write_matlab_output.hpp
Original file line number Diff line number Diff line change
@@ -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
*
Expand All @@ -23,6 +23,8 @@
#include <vector>
#include <string>

#include <boost/filesystem.hpp> /* boost::filesystem::path */

/**
* @brief creates textfiles containing all the results as a 2D matrix
* (needs to be reshaped in matlab, see calcPhiASE.m)
Expand All @@ -41,7 +43,7 @@
* @license GPLv3
*/
void writeMatlabOutput(
const std::string experimentPath,
const boost::filesystem::path experimentPath,
const std::vector<float> ase,
const std::vector<unsigned> N_rays,
const std::vector<double> mse_values,
Expand Down
8 changes: 5 additions & 3 deletions include/write_to_file.hpp
Original file line number Diff line number Diff line change
@@ -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
*
Expand Down Expand Up @@ -29,6 +29,8 @@
#include <string>
#include <vector>

#include <boost/filesystem.hpp> /* boost::filesystem::path */

/**
* @brief writes a value to a file, where the filename is appended with 2 longish indices
*
Expand All @@ -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,
Expand All @@ -56,4 +58,4 @@ int writeValueToFile(
* @param pFilename the name of the output file
*
*/
void writeVectorToFile(std::vector<double> v, std::string pFilename);
void writeVectorToFile(std::vector<double> v, boost::filesystem::path filename);
10 changes: 6 additions & 4 deletions include/write_to_vtk.hpp
Original file line number Diff line number Diff line change
@@ -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
*
Expand Down Expand Up @@ -29,6 +29,8 @@
#include <vector>
#include <string>

#include <boost/filesystem.hpp> /* boost::filesystem::path */

#include <mesh.hpp>

/**
Expand All @@ -48,7 +50,7 @@
*/
int writePointsToVtk(const Mesh& mesh,
const std::vector<double> ase,
const std::string filename,
const boost::filesystem::path filename,
const unsigned minRaysPerSample,
const unsigned maxRaysPerSample,
const float mseThreshold,
Expand All @@ -72,7 +74,7 @@ int writePointsToVtk(const Mesh& mesh,
*/
int writePrismToVtk(const Mesh& mesh,
const std::vector<double> prismData,
const std::string filename,
const boost::filesystem::path filename,
const unsigned minRaysPerSample,
const unsigned maxRaysPerSample,
const float mseThreshold,
Expand All @@ -87,7 +89,7 @@ int writePrismToVtk(const Mesh& mesh,
* @return a vector containing the difference between "compare" and "data"
*
*/
std::vector<double> compareVtk(std::vector<double> compare, std::string filename);
std::vector<double> compareVtk(std::vector<double> compare, const boost::filesystem::path filename);



26 changes: 15 additions & 11 deletions src/main.cu
Original file line number Diff line number Diff line change
@@ -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
*
Expand Down Expand Up @@ -37,6 +37,10 @@
#include <numeric> /* accumulate*/
#include <stdexcept>

// Boost stuff
#include <boost/filesystem.hpp> /* fs::path */
namespace fs = boost::filesystem;

// User header files
#include <calc_phi_ase.hpp>
#include <calc_phi_ase_threaded.hpp>
Expand Down Expand Up @@ -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
Expand All @@ -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());

Expand Down Expand Up @@ -274,10 +278,10 @@ int main(int argc, char **argv){
std::vector<double> tmpPhiAse(phiAse.begin(), phiAse.end());
std::vector<double> 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
Expand Down
Loading

0 comments on commit 9e9e59a

Please sign in to comment.