From f5ad4f3de8003843cfe01b27ccfa59ac1d51f04c Mon Sep 17 00:00:00 2001 From: Michael Marshall <43034299+mmphys@users.noreply.github.com> Date: Fri, 26 Jul 2019 19:46:55 +0100 Subject: [PATCH] Added the ability to write a version of the validated XML file excluding any of the module IDs supplied in a separate exclude file --- Hadrons/Application.cc | 21 +++++++---- Hadrons/Application.hpp | 3 +- Hadrons/Utilities/HadronsXmlValidate.cc | 47 ++++++++++++++++++++++--- 3 files changed, 60 insertions(+), 11 deletions(-) diff --git a/Hadrons/Application.cc b/Hadrons/Application.cc index e4264f84..9a2451a8 100644 --- a/Hadrons/Application.cc +++ b/Hadrons/Application.cc @@ -180,7 +180,7 @@ void Application::parseParameterFile(const std::string parameterFileName) pop(reader); } -void Application::saveParameterFile(const std::string parameterFileName, unsigned int prec) +void Application::saveParameterFile(const std::string ¶meterFileName, const std::vector &Except, unsigned int prec) { LOG(Message) << "Saving application to '" << parameterFileName << "'..." << std::endl; if (env().getGrid()->IsBoss()) @@ -194,18 +194,27 @@ void Application::saveParameterFile(const std::string parameterFileName, unsigne push(writer, "modules"); for (unsigned int i = 0; i < nMod; ++i) { - push(writer, "module"); id.name = vm().getModuleName(i); - id.type = vm().getModule(i)->getRegisteredName(); - write(writer, "id", id); - vm().getModule(i)->saveParameters(writer, "options"); - pop(writer); + if( std::find( Except.begin(), Except.end(), id.name ) == Except.end() ) + { + push(writer, "module"); + id.type = vm().getModule(i)->getRegisteredName(); + write(writer, "id", id); + vm().getModule(i)->saveParameters(writer, "options"); + pop(writer); + } } pop(writer); pop(writer); } } +void Application::saveParameterFile(const std::string ¶meterFileName, unsigned int prec) +{ + const std::vector Except; + saveParameterFile(parameterFileName, Except, prec); +} + // schedule computation //////////////////////////////////////////////////////// void Application::schedule(void) { diff --git a/Hadrons/Application.hpp b/Hadrons/Application.hpp index 36179c5f..38442ea3 100644 --- a/Hadrons/Application.hpp +++ b/Hadrons/Application.hpp @@ -81,7 +81,8 @@ public: void run(void); // XML parameter file I/O void parseParameterFile(const std::string parameterFileName); - void saveParameterFile(const std::string parameterFileName, unsigned int prec=15); + void saveParameterFile(const std::string ¶meterFileName, unsigned int prec=15); + void saveParameterFile(const std::string ¶meterFileName, const std::vector &Except, unsigned int prec=15); // schedule computation void schedule(void); void saveSchedule(const std::string filename); diff --git a/Hadrons/Utilities/HadronsXmlValidate.cc b/Hadrons/Utilities/HadronsXmlValidate.cc index 73cf3139..a6100f2b 100644 --- a/Hadrons/Utilities/HadronsXmlValidate.cc +++ b/Hadrons/Utilities/HadronsXmlValidate.cc @@ -27,24 +27,52 @@ See the full license in the file "LICENSE" in the top level distribution directo /* END LEGAL */ #include +#include +#include +#include using namespace Grid; using namespace QCD; using namespace Hadrons; +// Does the specified file exist? +bool FileExists(const std::string& Filename) +{ + struct stat buf; + return stat(Filename.c_str(), &buf) != -1; +} + +void Shorten( Application &app, const std::string &FileList, const std::string OutFileName ) +{ + std::vector Except; + std::ifstream list{ FileList }; + for( std::string s; std::getline(list, s); ) { + //const std::string::size_type l{ s.find_first_of( '.' ) }; + //if( l != std::string::npos ) + //s.resize( l ); + if( s.length() ) + Except.push_back( s ); + } + std::sort( Except.begin(), Except.end() ); + for( const std::string &s : Except ) + std::cout << s << std::endl; + app.saveParameterFile( OutFileName, Except ); +} + int main(int argc, char *argv[]) { // parse command line std::string parameterFileName; - if (argc != 2) + if (argc != 2 && argc != 4) { - std::cerr << "usage: " << argv[0] << " "; + std::cerr << "usage: " << argv[0] << " [filelist.txt output.xml]"; std::cerr << std::endl; std::exit(EXIT_FAILURE); } parameterFileName = argv[1]; - + if( argc == 4 ) + Grid_init(&argc, &argv); try { Application application(parameterFileName); @@ -54,11 +82,22 @@ int main(int argc, char *argv[]) vm.getModuleGraph(); LOG(Message) << "Application valid (check XML warnings though)" << std::endl; + if( argc == 4 ) { + const std::string FileList{ argv[3] }; + const std::string OutFileName{ argv[2] }; + if( !FileExists( FileList ) ) + std::cout << "File list \"" << FileList << "\" does not exist" << std::endl; + else { + Shorten( application, FileList, OutFileName ); + } + } } catch (const std::exception& e) { Exceptions::abort(e); } - + if( argc == 4 ) + Grid_finalize(); + return EXIT_SUCCESS; }