Commit 2bfa5c5c authored by kraus's avatar kraus
Browse files

Resolve "Sampler cleanup with KEEP option not working when directory is present"

parent 24046b7f
//
// Copyright (c) 2008 - 2020, Paul Scherrer Institut, Villigen PSI, Switzerland
//
// All rights reserved
//
// This file is part of OPAL.
//
// OPAL 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.
//
// You should have received a copy of the GNU General Public License
// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
//
#ifndef GSLERRORHANDLING_H
#define GSLERRORHANDLING_H
#include "Utilities/OpalException.h"
inline void handleGSLErrors(const char *reason,
const char *file,
int /*line*/,
int /*gsl_errno*/) {
throw OpalException(file, reason);
}
#endif
\ No newline at end of file
...@@ -46,6 +46,7 @@ ...@@ -46,6 +46,7 @@
#include "Utility/IpplException.h" #include "Utility/IpplException.h"
#include "Utility/IpplInfo.h" #include "Utility/IpplInfo.h"
#include "Utility/IpplTimings.h" #include "Utility/IpplTimings.h"
#include "GSLErrorHandling.h"
#include <gsl/gsl_errno.h> #include <gsl/gsl_errno.h>
...@@ -61,13 +62,6 @@ Ippl *ippl; ...@@ -61,13 +62,6 @@ Ippl *ippl;
Inform *gmsg; Inform *gmsg;
namespace { namespace {
void errorHandlerGSL(const char *reason,
const char *file,
int /*line*/,
int /*gsl_errno*/) {
throw OpalException(file, reason);
}
void printStdoutHeader() { void printStdoutHeader() {
OPALTimer::Timer simtimer; OPALTimer::Timer simtimer;
std::string dateStr(simtimer.date()); std::string dateStr(simtimer.date());
...@@ -146,7 +140,7 @@ int main(int argc, char *argv[]) { ...@@ -146,7 +140,7 @@ int main(int argc, char *argv[]) {
H5SetVerbosityLevel(1); //65535); H5SetVerbosityLevel(1); //65535);
gsl_set_error_handler(&errorHandlerGSL); gsl_set_error_handler(&handleGSLErrors);
static IpplTimings::TimerRef mainTimer = IpplTimings::getTimer("mainTimer"); static IpplTimings::TimerRef mainTimer = IpplTimings::getTimer("mainTimer");
IpplTimings::startTimer(mainTimer); IpplTimings::startTimer(mainTimer);
......
...@@ -376,9 +376,8 @@ void OpalSimulation::run() { ...@@ -376,9 +376,8 @@ void OpalSimulation::run() {
//FIXME: this seems to crash OPAL in some cases //FIXME: this seems to crash OPAL in some cases
//redirectOutToFile(); //redirectOutToFile();
#ifdef SUPRESS_OUTPUT #ifdef SUPRESS_OUTPUT
//XXX: hack to disable output to stdout and stderr //XXX: hack to disable output to stdout
std::cout.setstate(std::ios::failbit); std::cout.setstate(std::ios::failbit);
// std::cerr.setstate(std::ios::failbit);
#endif #endif
// now we can run the simulation // now we can run the simulation
run_opal(arg, inputFileName.str(), restartStep, Options::infoLevel, Options::warnLevel, comm_); run_opal(arg, inputFileName.str(), restartStep, Options::infoLevel, Options::warnLevel, comm_);
...@@ -386,7 +385,6 @@ void OpalSimulation::run() { ...@@ -386,7 +385,6 @@ void OpalSimulation::run() {
//restoreOut(); //restoreOut();
#ifdef SUPRESS_OUTPUT #ifdef SUPRESS_OUTPUT
std::cout.clear(); std::cout.clear();
std::cerr.clear();
#endif #endif
} catch(OpalException *ex) { } catch(OpalException *ex) {
...@@ -394,13 +392,12 @@ void OpalSimulation::run() { ...@@ -394,13 +392,12 @@ void OpalSimulation::run() {
//restoreOut(); //restoreOut();
#ifdef SUPRESS_OUTPUT #ifdef SUPRESS_OUTPUT
std::cout.clear(); std::cout.clear();
std::cerr.clear();
#endif #endif
std::cout << "Opal exception during simulation run: \n" std::cerr << "Opal exception during simulation run: \n"
<< ex->where() << "\n" << ex->where() << "\n"
<< ex->what() << std::endl; << ex->what() << std::endl;
std::cout << "Continuing 2, disregarding this simulation.." std::cerr << "Continuing, disregarding this simulation.."
<< std::endl; << std::endl;
} catch(ClassicException *ex) { } catch(ClassicException *ex) {
...@@ -408,14 +405,26 @@ void OpalSimulation::run() { ...@@ -408,14 +405,26 @@ void OpalSimulation::run() {
//restoreOut(); //restoreOut();
#ifdef SUPRESS_OUTPUT #ifdef SUPRESS_OUTPUT
std::cout.clear(); std::cout.clear();
std::cerr.clear();
#endif #endif
std::cout << "Classic exception during simulation run: \n" std::cerr << "Classic exception during simulation run: \n"
<< ex->where() << "\n" << ex->where() << "\n"
<< ex->what() << std::endl; << ex->what() << std::endl;
std::cout << "Continuing 3, disregarding this simulation.." std::cerr << "Continuing, disregarding this simulation.."
<< std::endl; << std::endl;
} catch(std::exception &ex) {
#ifdef SUPRESS_OUTPUT
std::cout.clear();
#endif
std::cerr << "Exception occured during simulation run: \n"
<< ex.what() << std::endl
<< "Continuing, disregarding this simulation.." << std::endl;
} catch(...) {
#ifdef SUPRESS_OUTPUT
std::cout.clear();
#endif
std::cerr << "Unknown exception occured during simulation run.\n"
<< "Continuing, disregarding this simulation.." << std::endl;
} }
...@@ -424,7 +433,7 @@ void OpalSimulation::run() { ...@@ -424,7 +433,7 @@ void OpalSimulation::run() {
delete[] inputfile; delete[] inputfile;
err = chdir(pwd_.c_str()); err = chdir(pwd_.c_str());
if (err != 0) { if (err != 0) {
std::cout << "Cannot chdir to " std::cerr << "Cannot chdir to "
<< pwd_ << std::endl; << pwd_ << std::endl;
} }
} }
...@@ -640,10 +649,13 @@ void OpalSimulation::cleanUp(const std::vector<std::string>& keep) { ...@@ -640,10 +649,13 @@ void OpalSimulation::cleanUp(const std::vector<std::string>& keep) {
try { try {
int my_rank = 0; int my_rank = 0;
MPI_Comm_rank(comm_, &my_rank); MPI_Comm_rank(comm_, &my_rank);
if (my_rank == 0) { if (my_rank != 0) {
fs::path p(simulationDirName_.c_str()); return;
}
fs::path p(simulationDirName_.c_str());
{
fs::directory_iterator it{p}; fs::directory_iterator it{p};
while ( it != fs::directory_iterator{} ) { while (it != fs::directory_iterator{}) {
std::string extension = Util::toUpper(fs::extension(it->path().filename())); std::string extension = Util::toUpper(fs::extension(it->path().filename()));
// remove . // remove .
...@@ -651,7 +663,16 @@ void OpalSimulation::cleanUp(const std::vector<std::string>& keep) { ...@@ -651,7 +663,16 @@ void OpalSimulation::cleanUp(const std::vector<std::string>& keep) {
auto result = std::find(keep.begin(), keep.end(), extension); auto result = std::find(keep.begin(), keep.end(), extension);
if ( result == keep.end() ) { if ( result == keep.end() && ! fs::is_directory(it->path())) {
fs::remove(it->path());
}
++it;
}
}
{
fs::directory_iterator it{p};
while (it != fs::directory_iterator{}) {
if (fs::is_directory(it->path()) && fs::is_empty(it->path())) {
fs::remove(it->path()); fs::remove(it->path());
} }
++it; ++it;
......
...@@ -20,6 +20,10 @@ extern Inform *gmsg; ...@@ -20,6 +20,10 @@ extern Inform *gmsg;
#include "Message/Communicate.h" #include "Message/Communicate.h"
#include "GSLErrorHandling.h"
#include <gsl/gsl_errno.h>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <limits> #include <limits>
...@@ -45,6 +49,8 @@ int run_opal(char */*args*/[], std::string inputfile, int restartStep, ...@@ -45,6 +49,8 @@ int run_opal(char */*args*/[], std::string inputfile, int restartStep,
amrex::Initialize(comm); amrex::Initialize(comm);
#endif #endif
gsl_set_error_handler(&handleGSLErrors);
OpalData *opal = OpalData::getInstance(); OpalData *opal = OpalData::getInstance();
Configure::configure(); Configure::configure();
...@@ -98,4 +104,4 @@ int run_opal(char */*args*/[], std::string inputfile, int restartStep, ...@@ -98,4 +104,4 @@ int run_opal(char */*args*/[], std::string inputfile, int restartStep,
output.close(); output.close();
return 0; return 0;
} }
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment