diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 4cbe800..e2c01d4 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -119,8 +119,8 @@ ENDIF() #################################### SET(UTILITIES Utilities/RandomGenerator.cpp - Utilities/OhmmsInform.cpp - Utilities/OhmmsInfo.cpp + Utilities/InfoStream.cpp + Utilities/OutputManager.cpp Utilities/NewTimer.cpp io/hdf_archive.cpp ${GITREV_TMP} diff --git a/src/Configuration.h b/src/Configuration.h index 44aaa47..764fc28 100644 --- a/src/Configuration.h +++ b/src/Configuration.h @@ -25,7 +25,7 @@ #include #include #include -#include +#include #include #define byRows 999 @@ -120,21 +120,6 @@ namespace qmcplusplus typedef SMSparseMatrix SPComplexSMSpMat; */ -inline std::ostream &app_log() { return OhmmsInfo::Log->getStream(); } - -inline std::ostream &app_error() -{ - OhmmsInfo::Log->getStream() << "ERROR "; - return OhmmsInfo::Error->getStream(); -} - -inline std::ostream &app_warning() -{ - OhmmsInfo::Log->getStream() << "WARNING "; - return OhmmsInfo::Warn->getStream(); -} - -inline std::ostream &app_debug() { return OhmmsInfo::Debug->getStream(); } } namespace std{ diff --git a/src/Utilities/InfoStream.cpp b/src/Utilities/InfoStream.cpp new file mode 100644 index 0000000..1872863 --- /dev/null +++ b/src/Utilities/InfoStream.cpp @@ -0,0 +1,56 @@ +////////////////////////////////////////////////////////////////////////////////////// +// This file is distributed under the University of Illinois/NCSA Open Source License. +// See LICENSE file in top directory for details. +// +// Copyright (c) 2017 Jeongnim Kim and QMCPACK developers. +// +// File developed by: Mark Dewing, mdewing@anl.gov, Argonne National Laboratory +// +// File created by: Mark Dewing, mdewing@anl.gov, Argonne National Laboratory +////////////////////////////////////////////////////////////////////////////////////// + + +#include +#include + +InfoStream::~InfoStream() +{ + if (currStream != nullStream) { + delete nullStream; + } + if (ownStream && currStream) { + delete(currStream); + } +} + +void InfoStream::pause() { + if (currStream != nullStream) + { + prevStream = currStream; + currStream = nullStream; + } +} + +void InfoStream::resume() { + if (prevStream) { + currStream = prevStream; + prevStream = NULL; + } +} + +void InfoStream::shutOff() { + prevStream = NULL; + currStream = nullStream; +} + +void InfoStream::redirectToFile(const std::string &fname) { + currStream = new std::ofstream(fname); + ownStream = true; +} + +void InfoStream::redirectToSameStream(InfoStream &info) +{ + currStream = &info.getStream(); + ownStream = false; +} + diff --git a/src/Utilities/InfoStream.h b/src/Utilities/InfoStream.h new file mode 100644 index 0000000..d843ed2 --- /dev/null +++ b/src/Utilities/InfoStream.h @@ -0,0 +1,95 @@ +////////////////////////////////////////////////////////////////////////////////////// +// This file is distributed under the University of Illinois/NCSA Open Source License. +// See LICENSE file in top directory for details. +// +// Copyright (c) 2017 Jeongnim Kim and QMCPACK developers. +// +// File developed by: Mark Dewing, mdewing@anl.gov, Argonne National Laboratory +// +// File created by: Mark Dewing, mdewing@anl.gov, Argonne National Laboratory +////////////////////////////////////////////////////////////////////////////////////// + + +/** @file InfoStream.h + * @brief Declaration of InfoStream class. + */ + +#ifndef INFOSTREAM_H +#define INFOSTREAM_H + +#include +#include +#include +#include + +/** + * Interface to output streams. Can redirect output to stdout/stderr, a file, or a null stream. + */ + +class InfoStream +{ +public: + InfoStream(std::ostream *output_stream): prevStream(NULL), nullStream(new std::ostream(NULL)), + ownStream(false) { + currStream = output_stream; + } + + InfoStream(InfoStream &in): prevStream(NULL), nullStream(new std::ostream(NULL)), + ownStream(false) { + redirectToSameStream(in); + } + + ~InfoStream(); + + std::ostream &getStream(const std::string &tag = "") { + return *currStream; + } + + void setStream(std::ostream *output_stream) { + currStream = output_stream; + } + + + void flush() { + currStream->flush(); + } + + /// Stop output (redirect to a null stream) + void pause(); + + /// Continue output on the stream used before pausing + void resume(); + + /// Open a file and output to that file + void redirectToFile(const std::string &fname); + + /// Copy a stream + void redirectToSameStream(InfoStream &info); + + /// Permanently turn off the stream + void shutOff(); + +private: + + // Keep track of whether we should delete the stream or not + bool ownStream; + + std::ostream *currStream; + + // save stream during pause + std::ostream *prevStream; + + // Created at construction. Used during pause + std::ostream *nullStream; +}; + +template +inline +InfoStream& operator<<(InfoStream& o, const T& val) +{ + o.getStream() << val; + return o; +} + + +#endif diff --git a/src/Utilities/OhmmsInfo.cpp b/src/Utilities/OhmmsInfo.cpp deleted file mode 100644 index aa16a11..0000000 --- a/src/Utilities/OhmmsInfo.cpp +++ /dev/null @@ -1,113 +0,0 @@ -//////////////////////////////////////////////////////////////////////////////// -// This file is distributed under the University of Illinois/NCSA Open Source -// License. See LICENSE file in top directory for details. -// -// Copyright (c) 2016 Jeongnim Kim and QMCPACK developers. -// -// File developed by: -// Jeongnim Kim, jeongnim.kim@gmail.com, -// University of Illinois at Urbana-Champaign -// Jeremy McMinnis, jmcminis@gmail.com, -// University of Illinois at Urbana-Champaign -// Mark A. Berrill, berrillma@ornl.gov, -// Oak Ridge National Laboratory -// -// File created by: -// Jeongnim Kim, jeongnim.kim@gmail.com, -// University of Illinois at Urbana-Champaign -//////////////////////////////////////////////////////////////////////////////// - -/** @file OhmmsInfo.cpp - * @brief Definition of OhmmsInfo class. - */ -#include "config.h" -#include -#include -#include "Utilities/OhmmsInfo.h" - -bool OhmmsInfo::Writeable = false; -OhmmsInform *OhmmsInfo::Debug = 0; -OhmmsInform *OhmmsInfo::Warn = 0; -OhmmsInform *OhmmsInfo::Error = 0; -OhmmsInform *OhmmsInfo::Log = 0; - -OhmmsInfo::OhmmsInfo(int argc, char **argv, int master) -{ - if (argc) - { - initialize(argv[0], master); - } - else - { - initialize("ohmms", master); - } -} - -OhmmsInfo::~OhmmsInfo() {} - -OhmmsInfo::OhmmsInfo(const std::string &fin_name, int rank, int gid, - int num_groups) -{ - Writeable = (rank == 0); - if (Log == 0) // check if this is the first time - { - Warn = new OhmmsInform("WARNING", false, Writeable); - Error = new OhmmsInform("ERROR", false, Writeable); - Log = new OhmmsInform(" ", false, Writeable); - } - if (Writeable && num_groups > 1) - { - char fn[128]; - sprintf(fn, "%s.g%03d.qmc", fin_name.c_str(), gid); - Log->set(fn); - Warn->set(*Log, "WARNING"); - Error->set(*Log, "ERROR"); - } -#if defined(PRINT_DEBUG) - if (Debug == 0) - { - char fn[128]; - sprintf(fn, "%s.p%03d.debug", fin_name.c_str(), rank); - Debug = new OhmmsInform("DEBUG", false, true); - Debug->set(fn); - Debug->getStream().setf(std::ios::scientific, std::ios::floatfield); - Debug->getStream().precision(6); - } -#endif -} - -void OhmmsInfo::initialize(const char *froot, int master) -{ - // initialize the estimator to record data - Warn = new OhmmsInform("WARNING", false, Writeable); - Error = new OhmmsInform("ERROR", false, Writeable); - Log = new OhmmsInform(" ", false, Writeable); - Error->setStdError(); -#ifdef PRINT_DEBUG - Debug = new OhmmsInform("DEBUG", false, Writeable); -#endif - Log->getStream().setf(std::ios::scientific, std::ios::floatfield); - Log->getStream().precision(6); - Log->getStream() << "\n"; -} - -void OhmmsInfo::die(const char *msg) -{ - Warn->getStream() << msg << std::endl; - Warn->getStream() << "Stop the execution." << std::endl; - exit(1); -} - -/** flush the buffer - */ -void OhmmsInfo::flush() -{ - // if(!Writeable) { - Log->getStream().flush(); - Error->getStream().flush(); - Warn->getStream().flush(); -#ifdef PRINT_DEBUG - Debug->getStream().flush(); -#endif - //} -} diff --git a/src/Utilities/OhmmsInfo.h b/src/Utilities/OhmmsInfo.h deleted file mode 100644 index 88756df..0000000 --- a/src/Utilities/OhmmsInfo.h +++ /dev/null @@ -1,98 +0,0 @@ -//////////////////////////////////////////////////////////////////////////////// -// This file is distributed under the University of Illinois/NCSA Open Source -// License. See LICENSE file in top directory for details. -// -// Copyright (c) 2016 Jeongnim Kim and QMCPACK developers. -// -// File developed by: -// Jeongnim Kim, jeongnim.kim@gmail.com, -// University of Illinois at Urbana-Champaign -// Jeremy McMinnis, jmcminis@gmail.com, -// University of Illinois at Urbana-Champaign -// Mark A. Berrill, berrillma@ornl.gov, -// Oak Ridge National Laboratory -// -// File created by: -// Jeongnim Kim, jeongnim.kim@gmail.com, -// University of Illinois at Urbana-Champaign -//////////////////////////////////////////////////////////////////////////////// - -/** @file OhmmsInfo.h - * @brief Declaration of OhmmsInfo class. - */ -#ifndef OHMMS_OHMMSINFO_H -#define OHMMS_OHMMSINFO_H - -#include "Utilities/OhmmsInform.h" -/** Control object for run-time information - * - * Similar class to PoomaInfo of Pooma with very limited functions - */ -class OhmmsInfo -{ - -public: - static bool Writeable; - static OhmmsInform *Debug; - static OhmmsInform *Warn; - static OhmmsInform *Error; - static OhmmsInform *Log; - - static void initialize(const char *froot, int master); - static void die(const char *); - static void flush(); - - /** constructor using command-line arguments - * @param argc number of arguments - * @param argv arguments - * @param master selects an std::ostream that does writing - */ - OhmmsInfo(int argc, char **argv, int master = -1); - /** constructor - * @param fin_name input file name - * @param rank node rank - * @param group groupd rank - * @param multi_run true, open one std::ostream per group - */ - OhmmsInfo(const std::string &fin_name, int rank = 0, int gid = 0, - int num_groups = 1); - ~OhmmsInfo(); - -private: - OhmmsInfo() {} -}; - -/**run-time messages - * - LOGMGS log message - * - ERRORMSG error message - * - WARNMSG warning message - * - DEBUGMSG debug message - */ -#ifdef DONOTUSEOHMMSINFO -#define LOGMSG(msg) -#define ERRORMSG(msg) -#define WARNMSG(msg) -#define DEBUGMSG(msg) -#define XMLReport(msg) -#else -#define LOGMSG(msg) \ - { \ - OhmmsInfo::Log->getStream() << msg << std::endl; \ - } -#define ERRORMSG(msg) \ - { \ - OhmmsInfo::Error->getStream() << "ERROR " << msg << std::endl; \ - } -#define WARNMSG(msg) \ - { \ - OhmmsInfo::Warn->getStream() << "WARN " << msg << std::endl; \ - } -#define XMLReport(msg) - -#ifdef PRINT_DEBUG -#define DEBUGMSG(msg) OhmmsInfo::Debug->getStream() << msg << std::endl -#else -#define DEBUGMSG(msg) -#endif -#endif -#endif // OHMMS_OHMMSINFO_H diff --git a/src/Utilities/OhmmsInform.cpp b/src/Utilities/OhmmsInform.cpp deleted file mode 100644 index 30e5217..0000000 --- a/src/Utilities/OhmmsInform.cpp +++ /dev/null @@ -1,141 +0,0 @@ -//////////////////////////////////////////////////////////////////////////////// -// This file is distributed under the University of Illinois/NCSA Open Source -// License. See LICENSE file in top directory for details. -// -// Copyright (c) 2016 Jeongnim Kim and QMCPACK developers. -// -// File developed by: -// Jeongnim Kim, jeongnim.kim@gmail.com, -// University of Illinois at Urbana-Champaign -// Jeremy McMinnis, jmcminis@gmail.com, -// University of Illinois at Urbana-Champaign -// Ye Luo, yeluo@anl.gov, -// Argonne National Laboratory -// Mark A. Berrill, berrillma@ornl.gov, -// Oak Ridge National Laboratory -// -// File created by: -// Jeongnim Kim, jeongnim.kim@gmail.com, -// University of Illinois at Urbana-Champaign -//////////////////////////////////////////////////////////////////////////////// - -/** @file OhmmsInform.cpp - * @brief Definition of OhmmsInform class. - */ -#include "Utilities/OhmmsInform.h" - -#include -#include - -OhmmsInform::OhmmsInform(bool allcanwrite, bool writenode) - : bgStream(0), myPrompt("qmc>") -{ - if (allcanwrite || writenode) - { - myStream = &std::cout; - OwnStream = false; - } - else - { - myStream = new std::ostringstream(); - OwnStream = true; - } - Blanks = 0; -} - -OhmmsInform::OhmmsInform(const char *prompt, bool allcanwrite, bool writenode) - : myStream(0), bgStream(0), myPrompt(prompt) -{ - if (allcanwrite || writenode) - { - myStream = &std::cout; - OwnStream = false; - } - else - { - myStream = new std::ostringstream(); - OwnStream = true; - } - Blanks = 0; -} - -OhmmsInform::OhmmsInform(const char *prompt, const char *fname, int appmode) - : OwnStream(true), bgStream(0), myPrompt(prompt) -{ - // file mode - if (appmode == OVERWRITE) - myStream = new std::ofstream(fname); - else - myStream = new std::ofstream(fname, std::ios::app); - Blanks = 0; -} - -OhmmsInform::OhmmsInform(const char *prompt, std::ostream &o) - : OwnStream(false), myStream(&o), myPrompt(prompt) -{ - Blanks = 0; -} - -void OhmmsInform::set(const char *fname, int appmode) -{ - if (OwnStream && myStream) delete myStream; - OwnStream = true; - if (appmode == OVERWRITE) - myStream = new std::ofstream(fname); - else - myStream = new std::ofstream(fname, std::ios::app); -} - -void OhmmsInform::set(OhmmsInform &o) -{ - if (OwnStream) - { - if (myStream) delete myStream; - } - myStream = o.myStream; - myPrompt = o.myPrompt; - OwnStream = false; -} - -void OhmmsInform::set(OhmmsInform &o, const std::string &s) -{ - if (OwnStream) - { - if (myStream) delete myStream; - } - myStream = o.myStream; - myPrompt = s; - OwnStream = false; -} -void OhmmsInform::setPrompt(const std::string &s) { myPrompt = s; } - -void OhmmsInform::setStdError() -{ - if (OwnStream) - { - if (myStream) delete myStream; - } - myStream = &std::cerr; - OwnStream = false; -} - -void OhmmsInform::turnoff() -{ - bgStream = myStream; - myStream = new std::ostream(0); -} - -void OhmmsInform::reset() -{ - if (bgStream) - { - delete myStream; - myStream = bgStream; - bgStream = 0; - } -} - -OhmmsInform::~OhmmsInform() -{ - if (OwnStream) delete myStream; -} diff --git a/src/Utilities/OhmmsInform.h b/src/Utilities/OhmmsInform.h deleted file mode 100644 index 732ddff..0000000 --- a/src/Utilities/OhmmsInform.h +++ /dev/null @@ -1,109 +0,0 @@ -//////////////////////////////////////////////////////////////////////////////// -// This file is distributed under the University of Illinois/NCSA Open Source -// License. See LICENSE file in top directory for details. -// -// Copyright (c) 2016 Jeongnim Kim and QMCPACK developers. -// -// File developed by: -// Jeongnim Kim, jeongnim.kim@gmail.com, -// University of Illinois at Urbana-Champaign -// Jeremy McMinnis, jmcminis@gmail.com, -// University of Illinois at Urbana-Champaign -// Mark A. Berrill, berrillma@ornl.gov, -// Oak Ridge National Laboratory -// -// File created by: -// Jeongnim Kim, jeongnim.kim@gmail.com, -// University of Illinois at Urbana-Champaign -//////////////////////////////////////////////////////////////////////////////// - -/** @file OhmmsInform.h - * @brief Declaration of OhmmsInform class. - */ -#ifndef OHMMSINFORM_H -#define OHMMSINFORM_H - -#include -#include -#include -#if (__GNUC__ == 2) -#include -#else -#include -#endif -#include - -/** Wrapper of std::ostream to provide uniform environments for run-time report - * - * Each object can choose a prompt, a mode (only master processor or all the - * processors), - * and an std::ostream. - */ -class OhmmsInform -{ - -public: - enum - { - OVERWRITE, - APPEND - }; - OhmmsInform(bool allcanwrite = true, bool writenode = true); - OhmmsInform(const char *prompt, bool allcanwrite = true, - bool writenode = true); - OhmmsInform(const char *prompt, const char *fname, int appmode = OVERWRITE); - OhmmsInform(const char *prompt, std::ostream &); - - ~OhmmsInform(); - - void set(const char *fname, int appmode = OVERWRITE); - void set(OhmmsInform &); - void set(OhmmsInform &, const std::string &s); - void setPrompt(const std::string &s); - inline void flush() { myStream->flush(); } - - inline std::ostream &getStream() { return *myStream; } - - /// switch to std::cerr - void setStdError(); - /// incremenet indentation - inline void pushd() { Blanks += 2; } - /// decrease indentation - inline void popd() - { - if (Blanks) Blanks -= 2; - } - - // inline bool open() const { return CanWrite;} - - /** temporarily turn off the stream - */ - void turnoff(); - - /** reset the stream to the original - */ - void reset(); - -private: - bool OwnStream; - int Blanks; - std::ostream *myStream; - std::ostream *bgStream; - std::string myPrompt; -}; - -// templated version of operator<< for Inform objects -template inline OhmmsInform &operator<<(OhmmsInform &o, const T &val) -{ - o.getStream() << val; - return o; -} - -// specialized function for sending strings to Inform object -inline OhmmsInform &operator<<(OhmmsInform &o, const std::string &s) -{ - o.getStream() << s.c_str(); - return o; -} - -#endif // OHMMSINFO_H diff --git a/src/Utilities/OutputManager.cpp b/src/Utilities/OutputManager.cpp new file mode 100644 index 0000000..6d2bc83 --- /dev/null +++ b/src/Utilities/OutputManager.cpp @@ -0,0 +1,68 @@ +////////////////////////////////////////////////////////////////////////////////////// +// This file is distributed under the University of Illinois/NCSA Open Source License. +// See LICENSE file in top directory for details. +// +// Copyright (c) 2017 Jeongnim Kim and QMCPACK developers. +// +// File developed by: Mark Dewing, mdewing@anl.gov Argonne National Laboratory +// +// File created by: Mark Dewing, mdewing@anl.gov Argonne National Laboratory +////////////////////////////////////////////////////////////////////////////////////// + + + +#include + + +InfoStream infoSummary(&std::cout); +InfoStream infoLog(&std::cout); +InfoStream infoError(&std::cerr); +InfoStream infoDebug(&std::cout); + +OutputManagerClass outputManager(Verbosity::LOW); + + +void +OutputManagerClass::setVerbosity(Verbosity level) +{ + global_verbosity_level = level; + if (isActive(Verbosity::DEBUG)) { + infoSummary.resume(); + infoLog.resume(); + infoDebug.resume(); + } else if (isActive(Verbosity::HIGH)) { + infoSummary.resume(); + infoLog.resume(); + infoDebug.pause(); + } else if (isActive(Verbosity::LOW)) { + infoSummary.resume(); + infoLog.pause(); + infoDebug.pause(); + } +} + +bool OutputManagerClass::isActive(Verbosity level) +{ + return level <= global_verbosity_level; +} + +void OutputManagerClass::pause() +{ + infoSummary.pause(); + infoLog.pause(); +} + +void OutputManagerClass::resume() +{ + infoSummary.resume(); + infoLog.resume(); +} + +void OutputManagerClass::shutOff() +{ + infoSummary.shutOff(); + infoLog.shutOff(); + infoError.shutOff(); + infoDebug.shutOff(); +} + diff --git a/src/Utilities/OutputManager.h b/src/Utilities/OutputManager.h new file mode 100644 index 0000000..1867ded --- /dev/null +++ b/src/Utilities/OutputManager.h @@ -0,0 +1,111 @@ +////////////////////////////////////////////////////////////////////////////////////// +// This file is distributed under the University of Illinois/NCSA Open Source License. +// See LICENSE file in top directory for details. +// +// Copyright (c) 2017 Jeongnim Kim and QMCPACK developers. +// +// File developed by: Mark Dewing, mdewing@anl.gov Argonne National Laboratory +// +// File created by: Mark Dewing, mdewing@anl.gov Argonne National Laboratory +////////////////////////////////////////////////////////////////////////////////////// + + +/** @file OutputManager.h + * @brief Declaration of OutputManager class. + */ +#ifndef OUTPUTMANAGER_H +#define OUTPUTMANAGER_H + +#include + + +enum class Verbosity {LOW, HIGH, DEBUG}; +enum class LogType {SUMMARY, APP, ERROR, DEBUG}; + +extern InfoStream infoSummary; +extern InfoStream infoLog; +extern InfoStream infoError; +extern InfoStream infoDebug; + +class OutputManagerClass +{ + Verbosity global_verbosity_level; + +public: + OutputManagerClass(Verbosity level=Verbosity::LOW) { setVerbosity(level); } + + void setVerbosity(Verbosity level); + + bool isActive(Verbosity level); + + bool isDebugActive() + { + return isActive(Verbosity::DEBUG); + } + + bool isHighActive() + { + return isActive(Verbosity::HIGH); + } + + std::ostream& getStream(LogType log) { + switch (log) { + case LogType::SUMMARY: + return infoSummary.getStream(); + case LogType::APP: + return infoLog.getStream(); + case LogType::ERROR: + return infoError.getStream(); + case LogType::DEBUG: + return infoDebug.getStream(); + } + } + + /// Pause the summary and log streams + void pause(); + + /// Resume the summary and log streams + void resume(); + + /// Permanently shut off all streams + void shutOff(); +}; + +extern OutputManagerClass outputManager; + +namespace qmcplusplus { + +inline std::ostream& app_summary() +{ + return outputManager.getStream(LogType::SUMMARY); +} + +inline std::ostream& app_log() +{ + return outputManager.getStream(LogType::APP); +} + +inline std::ostream& app_error() +{ + outputManager.getStream(LogType::ERROR) << "ERROR "; + return outputManager.getStream(LogType::ERROR); +} + +inline std::ostream& app_warning() +{ + outputManager.getStream(LogType::ERROR) << "WARNING "; + return outputManager.getStream(LogType::ERROR); +} + +inline std::ostream& app_debug_stream() +{ + return outputManager.getStream(LogType::DEBUG); +} + +// From https://stackoverflow.com/questions/11826554/standard-no-op-output-stream +// If debugging is not active, this skips evaluation of the arguments +#define app_debug if (!outputManager.isDebugActive()) {} else app_debug_stream + +}; + +#endif diff --git a/src/miniapps/miniafqmc_base.cpp b/src/miniapps/miniafqmc_base.cpp index c4ec0ee..4b46242 100644 --- a/src/miniapps/miniafqmc_base.cpp +++ b/src/miniapps/miniafqmc_base.cpp @@ -145,6 +145,10 @@ int main(int argc, char **argv) } } + if (verbose) { + outputManager.setVerbosity(Verbosity::HIGH); + } + Random.init(0, 1, iseed); int ip = 0; PrimeNumberSet myPrimes;