From 4f2e87cf4f78072494849bc2c73ba9412028a5d5 Mon Sep 17 00:00:00 2001 From: Antonin Portelli Date: Wed, 18 Nov 2015 19:46:08 +0000 Subject: [PATCH 1/4] Hdf5File: random generator state I/O fix --- lib/Hdf5File.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/Hdf5File.cpp b/lib/Hdf5File.cpp index a2c7845..ca2a079 100644 --- a/lib/Hdf5File.cpp +++ b/lib/Hdf5File.cpp @@ -124,8 +124,8 @@ void Hdf5File::save(const RandGenState &state, const string &name) group = h5File_->createGroup(name.c_str() + nameOffset(name)); attr = group.createAttribute("type", PredType::NATIVE_SHORT, attrSpace); attr.write(PredType::NATIVE_SHORT, &rgStateType); - dataset = group.createDataSet("data", PredType::NATIVE_DOUBLE, dataSpace); - dataset.write(state.data(), PredType::NATIVE_DOUBLE); + dataset = group.createDataSet("data", PredType::NATIVE_INT, dataSpace); + dataset.write(state.data(), PredType::NATIVE_INT); } // read first name //////////////////////////////////////////////////////////// @@ -262,7 +262,7 @@ void Hdf5File::load(RandGenState &state, const DataSet &d) dataspace.getSimpleExtentDims(dim); if (dim[0] != RLXG_STATE_SIZE) { - // error here + LATAN_ERROR(Io, "random generator state has a wrong length"); } d.read(state.data(), PredType::NATIVE_INT); } From a4999a53fd12d51174edb3e9e2cfc5f3d5381318 Mon Sep 17 00:00:00 2001 From: Antonin Portelli Date: Wed, 18 Nov 2015 19:46:52 +0000 Subject: [PATCH 2/4] IO: code improvement --- lib/Io.cpp | 17 +++-------------- lib/Io.hpp | 39 +++++++++------------------------------ 2 files changed, 12 insertions(+), 44 deletions(-) diff --git a/lib/Io.cpp b/lib/Io.cpp index f96601a..0f2bb69 100644 --- a/lib/Io.cpp +++ b/lib/Io.cpp @@ -25,20 +25,9 @@ using namespace Latan; string Io::getFirstName(const string &fileName) { - string ext = extension(fileName); - - if (ext == "h5") - { - return getFirstName(fileName); - } - else if ((ext == "dat")||(ext == "sample")||(ext == "seed")) - { - return getFirstName(fileName); - } - else - { - LATAN_ERROR(Io, "unknown file extension '" + ext + "'"); - } + std::unique_ptr file = open(fileName); + + return file->getFirstName(); } unique_ptr Io::open(const std::string &fileName, const unsigned int mode) diff --git a/lib/Io.hpp b/lib/Io.hpp index 8d5e461..20999d8 100644 --- a/lib/Io.hpp +++ b/lib/Io.hpp @@ -48,7 +48,7 @@ public: static std::string getFirstName(const std::string &fileName); static std::string getFirstName(const std::string &fileName); static std::unique_ptr open(const std::string &fileName, - const unsigned int mode = File::Mode::write); + const unsigned int mode = File::Mode::read); }; // template implementation ///////////////////////////////////////////////////// @@ -63,27 +63,16 @@ IoT Io::load(const std::string &fileName, const std::string &name) template IoT Io::load(const std::string &fileName, const std::string &name) { - std::string ext = extension(fileName); - - if (ext == "h5") - { - return load(fileName, name); - } - else if ((ext == "dat")||(ext == "sample")||(ext == "seed")) - { - return load(fileName, name); - } - else - { - LATAN_ERROR(Io, "unknown file extension '" + ext + "'"); - } + std::unique_ptr file = open(fileName); + + return file->read(name); } template void Io::save(const IoT &data, const std::string &fileName, const unsigned int mode, const std::string &name) { - FileType file(fileName, mode); + FileType file(fileName, mode); std::string realName = (name.empty()) ? fileName : name; file.save(data, realName); @@ -93,20 +82,10 @@ template void Io::save(const IoT &data, const std::string &fileName, const unsigned int mode, const std::string &name) { - std::string ext = extension(fileName); - - if (ext == "h5") - { - save(data, fileName, mode, name); - } - else if ((ext == "dat")||(ext == "sample")||(ext == "seed")) - { - save(data, fileName, mode, name); - } - else - { - LATAN_ERROR(Io, "unknown file extension '" + ext + "'"); - } + std::unique_ptr file = open(fileName, mode); + std::string realName = (name.empty()) ? fileName : name; + + file->save(data, realName); } template From 7ade4be54be1f0479454466eab7c3442bec6c31c Mon Sep 17 00:00:00 2001 From: Antonin Portelli Date: Wed, 18 Nov 2015 19:47:29 +0000 Subject: [PATCH 3/4] resampler: option to choose output format --- utils/resample.cpp | 40 ++++++++++++++-------------------------- 1 file changed, 14 insertions(+), 26 deletions(-) diff --git a/utils/resample.cpp b/utils/resample.cpp index 7311de9..213fa39 100644 --- a/utils/resample.cpp +++ b/utils/resample.cpp @@ -34,7 +34,7 @@ using namespace Latan; static void usage(const string &cmdName) { cerr << "usage: " << cmdName; - cerr << " [-n -b -r -o ]"; + cerr << " [-n -b -r -o -f {h5|sample}]"; cerr << " "; cerr << endl; exit(EXIT_FAILURE); @@ -45,11 +45,12 @@ int main(int argc, char *argv[]) // argument parsing //////////////////////////////////////////////////////// int c; string manFileName, nameFileName, stateFileName, cmdName, outDirName = "."; + string ext = "h5"; Index binSize = 1, nSample = DEF_NSAMPLE; opterr = 0; cmdName = basename(argv[0]); - while ((c = getopt(argc, argv, "b:n:r:o:")) != -1) + while ((c = getopt(argc, argv, "b:n:r:o:f:")) != -1) { switch (c) { @@ -65,6 +66,9 @@ int main(int argc, char *argv[]) case 'r': stateFileName = optarg; break; + case 'f': + ext = optarg; + break; case '?': cerr << "error parsing option -" << char(optopt) << endl; usage(cmdName); @@ -92,11 +96,12 @@ int main(int argc, char *argv[]) cout << "================================================" << endl; cout << "Data resampler" << endl; cout << "------------------------------------------------" << endl; - cout << " #file= " << dataFileName.size() << endl; - cout << " #name= " << name.size() << endl; - cout << " bin size= " << binSize << endl; - cout << " #sample= " << nSample << endl; - cout << "output dir: " << outDirName << endl; + cout << " #file= " << dataFileName.size() << endl; + cout << " #name= " << name.size() << endl; + cout << " bin size= " << binSize << endl; + cout << " #sample= " << nSample << endl; + cout << " output dir: " << outDirName << endl; + cout << "output format: " << ext << endl; cout << "------------------------------------------------" << endl; // data loading //////////////////////////////////////////////////////////// @@ -109,30 +114,13 @@ int main(int argc, char *argv[]) } for (unsigned int i = 0; i < dataFileName.size(); ++i) { - std::unique_ptr dataFile; + std::unique_ptr dataFile = Io::open(dataFileName[i]); - if (extension(dataFileName[i]) == "dat") - { - dataFile.reset(new AsciiFile); - } - else if (extension(dataFileName[i]) == "h5") - { - dataFile.reset(new Hdf5File); - } - else - { - cerr << "error: '" << dataFileName[i]; - cerr << "' has an unknown extension" << endl; - - return EXIT_FAILURE; - } cout << '\r' << ProgressBar(i + 1, dataFileName.size()); - dataFile->open(dataFileName[i], AsciiFile::Mode::read); for (const string &n: name) { data[n][i] = dataFile->read(n); } - dataFile->close(); } cout << endl; @@ -148,7 +136,7 @@ int main(int argc, char *argv[]) } for (unsigned int i = 0; i < name.size(); ++i) { - const string outFileName = name[i] + "_" + manFileName + ".sample"; + const string outFileName = name[i] + "_" + manFileName + "." + ext; cout << '\r' << ProgressBar(i + 1, name.size()); data[name[i]].bin(binSize); From 0b7a839880108f45b8d544debea593695d345773 Mon Sep 17 00:00:00 2001 From: Antonin Portelli Date: Wed, 18 Nov 2015 19:48:10 +0000 Subject: [PATCH 4/4] tool to create & save a random generator state --- utils/Makefile.am | 5 +++++ utils/create_rg_state.cpp | 44 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 utils/create_rg_state.cpp diff --git a/utils/Makefile.am b/utils/Makefile.am index 7c846d7..75b2a77 100644 --- a/utils/Makefile.am +++ b/utils/Makefile.am @@ -15,11 +15,16 @@ endif endif bin_PROGRAMS = \ + latan_create_rg_state \ latan_make_fake_sample\ latan_sample_combine \ latan_sample_read \ latan_resample +latan_create_rg_state_SOURCES = create_rg_state.cpp +latan_create_rg_state_CXXFLAGS = $(COM_CXXFLAGS) +latan_create_rg_state_LDFLAGS = -L../lib/.libs -lLatAnalyze + latan_make_fake_sample_SOURCES = make_fake_sample.cpp latan_make_fake_sample_CXXFLAGS = $(COM_CXXFLAGS) latan_make_fake_sample_LDFLAGS = -L../lib/.libs -lLatAnalyze diff --git a/utils/create_rg_state.cpp b/utils/create_rg_state.cpp new file mode 100644 index 0000000..9ce3c82 --- /dev/null +++ b/utils/create_rg_state.cpp @@ -0,0 +1,44 @@ +/* + * create_rg_state.cpp, part of LatAnalyze 3 + * + * Copyright (C) 2013 - 2015 Antonin Portelli + * + * LatAnalyze 3 is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * LatAnalyze 3 is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with LatAnalyze 3. If not, see . + */ + +#include +#include +#include + +using namespace Latan; +using namespace std; + +int main(int argc, char *argv[]) +{ + string outFilename; + + if (argc != 2) + { + cerr << "usage: " << argv[0] << " " << endl; + + return EXIT_FAILURE; + } + outFilename = argv[1]; + + RandGen gen; + + Io::save(gen.getState(), outFilename); + + return EXIT_SUCCESS; +}