1
0
mirror of https://github.com/paboyle/Grid.git synced 2025-04-09 21:50:45 +01:00

Hadrons: error managed through expections

This commit is contained in:
Antonin Portelli 2017-12-12 14:04:28 +00:00
parent 64161a8743
commit 26d7b829a0
15 changed files with 174 additions and 43 deletions

View File

@ -123,11 +123,11 @@ void Application::parseParameterFile(const std::string parameterFileName)
setPar(par); setPar(par);
if (!push(reader, "modules")) if (!push(reader, "modules"))
{ {
HADRON_ERROR("Cannot open node 'modules' in parameter file '" + parameterFileName + "'"); HADRON_ERROR(Parsing, "Cannot open node 'modules' in parameter file '" + parameterFileName + "'");
} }
if (!push(reader, "module")) if (!push(reader, "module"))
{ {
HADRON_ERROR("Cannot open node 'modules/module' in parameter file '" + parameterFileName + "'"); HADRON_ERROR(Parsing, "Cannot open node 'modules/module' in parameter file '" + parameterFileName + "'");
} }
do do
{ {
@ -262,7 +262,7 @@ void Application::saveSchedule(const std::string filename)
if (!scheduled_) if (!scheduled_)
{ {
HADRON_ERROR("Computation not scheduled"); HADRON_ERROR(Definition, "Computation not scheduled");
} }
LOG(Message) << "Saving current schedule to '" << filename << "'..." LOG(Message) << "Saving current schedule to '" << filename << "'..."
<< std::endl; << std::endl;
@ -296,7 +296,7 @@ void Application::printSchedule(void)
{ {
if (!scheduled_) if (!scheduled_)
{ {
HADRON_ERROR("Computation not scheduled"); HADRON_ERROR(Definition, "Computation not scheduled");
} }
LOG(Message) << "Schedule (memory peak: " << MEM_MSG(memPeak_) << "):" LOG(Message) << "Schedule (memory peak: " << MEM_MSG(memPeak_) << "):"
<< std::endl; << std::endl;

View File

@ -35,6 +35,9 @@ using namespace Grid;
using namespace QCD; using namespace QCD;
using namespace Hadrons; using namespace Hadrons;
#define ERROR_NO_ADDRESS(address)\
HADRON_ERROR(Definition, "no object with address " + std::to_string(address));
/****************************************************************************** /******************************************************************************
* Environment implementation * * Environment implementation *
******************************************************************************/ ******************************************************************************/
@ -83,7 +86,7 @@ GridCartesian * Environment::getGrid(const unsigned int Ls) const
} }
catch(std::out_of_range &) catch(std::out_of_range &)
{ {
HADRON_ERROR("no grid with Ls= " << Ls); HADRON_ERROR(Definition, "no grid with Ls= " + std::to_string(Ls));
} }
} }
@ -102,7 +105,7 @@ GridRedBlackCartesian * Environment::getRbGrid(const unsigned int Ls) const
} }
catch(std::out_of_range &) catch(std::out_of_range &)
{ {
HADRON_ERROR("no red-black 5D grid with Ls= " << Ls); HADRON_ERROR(Definition, "no red-black 5D grid with Ls= " + std::to_string(Ls));
} }
} }
@ -152,7 +155,7 @@ void Environment::addObject(const std::string name, const int moduleAddress)
} }
else else
{ {
HADRON_ERROR("object '" + name + "' already exists"); HADRON_ERROR(Definition, "object '" + name + "' already exists");
} }
} }
@ -175,7 +178,7 @@ unsigned int Environment::getObjectAddress(const std::string name) const
} }
else else
{ {
HADRON_ERROR("no object with name '" + name + "'"); HADRON_ERROR(Definition, "no object with name '" + name + "'");
} }
} }
@ -187,7 +190,7 @@ std::string Environment::getObjectName(const unsigned int address) const
} }
else else
{ {
HADRON_ERROR("no object with address " + std::to_string(address)); ERROR_NO_ADDRESS(address);
} }
} }
@ -206,7 +209,7 @@ std::string Environment::getObjectType(const unsigned int address) const
} }
else else
{ {
HADRON_ERROR("no object with address " + std::to_string(address)); ERROR_NO_ADDRESS(address);
} }
} }
@ -223,7 +226,7 @@ Environment::Size Environment::getObjectSize(const unsigned int address) const
} }
else else
{ {
HADRON_ERROR("no object with address " + std::to_string(address)); ERROR_NO_ADDRESS(address);
} }
} }
@ -240,7 +243,7 @@ Environment::Storage Environment::getObjectStorage(const unsigned int address) c
} }
else else
{ {
HADRON_ERROR("no object with address " + std::to_string(address)); ERROR_NO_ADDRESS(address);
} }
} }
@ -257,7 +260,7 @@ int Environment::getObjectModule(const unsigned int address) const
} }
else else
{ {
HADRON_ERROR("no object with address " + std::to_string(address)); ERROR_NO_ADDRESS(address);
} }
} }
@ -274,7 +277,7 @@ unsigned int Environment::getObjectLs(const unsigned int address) const
} }
else else
{ {
HADRON_ERROR("no object with address " + std::to_string(address)); ERROR_NO_ADDRESS(address);
} }
} }

View File

@ -227,7 +227,7 @@ void Environment::createDerivedObject(const std::string name,
} }
else else
{ {
HADRON_ERROR("object '" + name + "' already allocated"); HADRON_ERROR(Definition, "object '" + name + "' already allocated");
} }
} }
@ -253,20 +253,20 @@ T * Environment::getObject(const unsigned int address) const
} }
else else
{ {
HADRON_ERROR("object with address " + std::to_string(address) + HADRON_ERROR(Definition, "object with address " + std::to_string(address) +
" does not have type '" + typeName(&typeid(T)) + " does not have type '" + typeName(&typeid(T)) +
"' (has type '" + getObjectType(address) + "')"); "' (has type '" + getObjectType(address) + "')");
} }
} }
else else
{ {
HADRON_ERROR("object with address " + std::to_string(address) + HADRON_ERROR(Definition, "object with address " + std::to_string(address) +
" is empty"); " is empty");
} }
} }
else else
{ {
HADRON_ERROR("no object with address " + std::to_string(address)); HADRON_ERROR(Definition, "no object with address " + std::to_string(address));
} }
} }
@ -292,7 +292,7 @@ bool Environment::isObjectOfType(const unsigned int address) const
} }
else else
{ {
HADRON_ERROR("no object with address " + std::to_string(address)); HADRON_ERROR(Definition, "no object with address " + std::to_string(address));
} }
} }

View File

@ -0,0 +1,57 @@
/*************************************************************************************
Grid physics library, www.github.com/paboyle/Grid
Source file: extras/Hadrons/Exceptions.cc
Copyright (C) 2017
Author: Antonin Portelli <antonin.portelli@me.com>
This program 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 2 of the License, or
(at your option) any later version.
This program 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 this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
See the full license in the file "LICENSE" in the top level distribution directory
*************************************************************************************/
/* END LEGAL */
#include <Grid/Hadrons/Exceptions.hpp>
#ifndef ERR_SUFF
#define ERR_SUFF " (" + loc + ")"
#endif
#define CONST_EXC(name, init) \
name::name(std::string msg, std::string loc)\
:init\
{}
using namespace Grid;
using namespace Hadrons;
using namespace Exceptions;
// logic errors
CONST_EXC(Logic, logic_error(msg + ERR_SUFF))
CONST_EXC(Definition, Logic("definition error: " + msg, loc))
CONST_EXC(Implementation, Logic("implementation error: " + msg, loc))
CONST_EXC(Range, Logic("range error: " + msg, loc))
CONST_EXC(Size, Logic("size error: " + msg, loc))
// runtime errors
CONST_EXC(Runtime, runtime_error(msg + ERR_SUFF))
CONST_EXC(Argument, Runtime("argument error: " + msg, loc))
CONST_EXC(Io, Runtime("IO error: " + msg, loc))
CONST_EXC(Memory, Runtime("memory error: " + msg, loc))
CONST_EXC(Parsing, Runtime("parsing error: " + msg, loc))
CONST_EXC(Program, Runtime("program error: " + msg, loc))
CONST_EXC(System, Runtime("system error: " + msg, loc))

View File

@ -0,0 +1,72 @@
/*************************************************************************************
Grid physics library, www.github.com/paboyle/Grid
Source file: extras/Hadrons/Exceptions.hpp
Copyright (C) 2017
Author: Antonin Portelli <antonin.portelli@me.com>
This program 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 2 of the License, or
(at your option) any later version.
This program 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 this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
See the full license in the file "LICENSE" in the top level distribution directory
*************************************************************************************/
/* END LEGAL */
#ifndef Hadrons_Exceptions_hpp_
#define Hadrons_Exceptions_hpp_
#include <stdexcept>
#ifndef Hadrons_Global_hpp_
#include <Grid/Hadrons/Global.hpp>
#endif
#define SRC_LOC std::string(__FUNCTION__) + " at " + std::string(__FILE__) + ":"\
+ std::to_string(__LINE__)
#define HADRON_ERROR(exc, msg)\
LOG(Error) << msg << std::endl;\
throw(Exceptions::exc(msg, SRC_LOC));
#define DECL_EXC(name, base) \
class name: public base\
{\
public:\
name(std::string msg, std::string loc);\
}
BEGIN_HADRONS_NAMESPACE
namespace Exceptions
{
// logic errors
DECL_EXC(Logic, std::logic_error);
DECL_EXC(Definition, Logic);
DECL_EXC(Implementation, Logic);
DECL_EXC(Range, Logic);
DECL_EXC(Size, Logic);
// runtime errors
DECL_EXC(Runtime, std::runtime_error);
DECL_EXC(Argument, Runtime);
DECL_EXC(Io, Runtime);
DECL_EXC(Memory, Runtime);
DECL_EXC(Parsing, Runtime);
DECL_EXC(Program, Runtime);
DECL_EXC(System, Runtime);
}
END_HADRONS_NAMESPACE
#endif // Hadrons_Exceptions_hpp_

View File

@ -95,7 +95,7 @@ std::unique_ptr<T> Factory<T>::create(const std::string type,
} }
catch (std::out_of_range &) catch (std::out_of_range &)
{ {
HADRON_ERROR("object of type '" + type + "' unknown"); HADRON_ERROR(Argument, "object of type '" + type + "' unknown");
} }
return func(name); return func(name);

View File

@ -100,11 +100,6 @@ public:
}; };
#define LOG(channel) std::cout << HadronsLog##channel #define LOG(channel) std::cout << HadronsLog##channel
#define HADRON_ERROR(msg)\
LOG(Error) << msg << " (" << __FUNCTION__ << " at " << __FILE__ << ":"\
<< __LINE__ << ")" << std::endl;\
abort();
#define DEBUG_VAR(var) LOG(Debug) << #var << "= " << (var) << std::endl; #define DEBUG_VAR(var) LOG(Debug) << #var << "= " << (var) << std::endl;
extern HadronsLogger HadronsLogError; extern HadronsLogger HadronsLogError;
@ -176,4 +171,6 @@ typedef XmlWriter CorrWriter;
END_HADRONS_NAMESPACE END_HADRONS_NAMESPACE
#include <Grid/Hadrons/Exceptions.hpp>
#endif // Hadrons_Global_hpp_ #endif // Hadrons_Global_hpp_

View File

@ -185,7 +185,7 @@ void Graph<T>::removeVertex(const T &value)
} }
else else
{ {
HADRON_ERROR("vertex " << value << " does not exists"); HADRON_ERROR(Range, "vertex does not exists");
} }
// remove all edges containing the vertex // remove all edges containing the vertex
@ -214,7 +214,7 @@ void Graph<T>::removeEdge(const Edge &e)
} }
else else
{ {
HADRON_ERROR("edge " << e << " does not exists"); HADRON_ERROR(Range, "edge does not exists");
} }
} }
@ -260,7 +260,7 @@ void Graph<T>::mark(const T &value, const bool doMark)
} }
else else
{ {
HADRON_ERROR("vertex " << value << " does not exists"); HADRON_ERROR(Range, "vertex does not exists");
} }
} }
@ -298,7 +298,7 @@ bool Graph<T>::isMarked(const T &value) const
} }
else else
{ {
HADRON_ERROR("vertex " << value << " does not exists"); HADRON_ERROR(Range, "vertex does not exists");
return false; return false;
} }
@ -544,7 +544,7 @@ std::vector<T> Graph<T>::topoSort(void)
{ {
if (tmpMarked.at(v)) if (tmpMarked.at(v))
{ {
HADRON_ERROR("cannot topologically sort a cyclic graph"); HADRON_ERROR(Range, "cannot topologically sort a cyclic graph");
} }
if (!isMarked(v)) if (!isMarked(v))
{ {
@ -603,7 +603,7 @@ std::vector<T> Graph<T>::topoSort(Gen &gen)
{ {
if (tmpMarked.at(v)) if (tmpMarked.at(v))
{ {
HADRON_ERROR("cannot topologically sort a cyclic graph"); HADRON_ERROR(Range, "cannot topologically sort a cyclic graph");
} }
if (!isMarked(v)) if (!isMarked(v))
{ {

View File

@ -7,6 +7,7 @@ libHadrons_a_SOURCES = \
$(modules_cc) \ $(modules_cc) \
Application.cc \ Application.cc \
Environment.cc \ Environment.cc \
Exceptions.cc \
Global.cc \ Global.cc \
Module.cc \ Module.cc \
VirtualMachine.cc VirtualMachine.cc
@ -15,6 +16,7 @@ nobase_libHadrons_a_HEADERS = \
$(modules_hpp) \ $(modules_hpp) \
Application.hpp \ Application.hpp \
Environment.hpp \ Environment.hpp \
Exceptions.hpp \
Factory.hpp \ Factory.hpp \
GeneticScheduler.hpp \ GeneticScheduler.hpp \
Global.hpp \ Global.hpp \

View File

@ -50,7 +50,7 @@ std::string ModuleBase::getName(void) const
// get factory registration name if available // get factory registration name if available
std::string ModuleBase::getRegisteredName(void) std::string ModuleBase::getRegisteredName(void)
{ {
HADRON_ERROR("module '" + getName() + "' has a type not registered" HADRON_ERROR(Definition, "module '" + getName() + "' has no registered type"
+ " in the factory"); + " in the factory");
} }

View File

@ -118,7 +118,7 @@ void TWardIdentity<FImpl>::setup(void)
Ls_ = env().getObjectLs(par().q); Ls_ = env().getObjectLs(par().q);
if (Ls_ != env().getObjectLs(par().action)) if (Ls_ != env().getObjectLs(par().action))
{ {
HADRON_ERROR("Ls mismatch between quark action and propagator"); HADRON_ERROR(Size, "Ls mismatch between quark action and propagator");
} }
} }

View File

@ -187,7 +187,7 @@ void TGaugeProp<FImpl>::execute(void)
{ {
if (Ls_ != env().getObjectLs(par().source)) if (Ls_ != env().getObjectLs(par().source))
{ {
HADRON_ERROR("Ls mismatch between quark action and source"); HADRON_ERROR(Size, "Ls mismatch between quark action and source");
} }
else else
{ {

View File

@ -122,7 +122,7 @@ void TTestSeqConserved<FImpl>::setup(void)
auto Ls = env().getObjectLs(par().q); auto Ls = env().getObjectLs(par().q);
if (Ls != env().getObjectLs(par().action)) if (Ls != env().getObjectLs(par().action))
{ {
HADRON_ERROR("Ls mismatch between quark action and propagator"); HADRON_ERROR(Size, "Ls mismatch between quark action and propagator");
} }
} }

View File

@ -115,7 +115,7 @@ void VirtualMachine::pushModule(VirtualMachine::ModPt &pt)
} }
else else
{ {
HADRON_ERROR("object '" + out HADRON_ERROR(Definition, "object '" + out
+ "' is already produced by module '" + "' is already produced by module '"
+ module_[env().getObjectModule(out)].name + module_[env().getObjectModule(out)].name
+ "' (while pushing module '" + name + "')"); + "' (while pushing module '" + name + "')");
@ -144,7 +144,7 @@ void VirtualMachine::pushModule(VirtualMachine::ModPt &pt)
} }
else else
{ {
HADRON_ERROR("module '" + name + "' already exists"); HADRON_ERROR(Definition, "module '" + name + "' already exists");
} }
} }
@ -171,7 +171,7 @@ ModuleBase * VirtualMachine::getModule(const unsigned int address) const
} }
else else
{ {
HADRON_ERROR("no module with address " + std::to_string(address)); HADRON_ERROR(Definition, "no module with address " + std::to_string(address));
} }
} }
@ -188,7 +188,7 @@ unsigned int VirtualMachine::getModuleAddress(const std::string name) const
} }
else else
{ {
HADRON_ERROR("no module with name '" + name + "'"); HADRON_ERROR(Definition, "no module with name '" + name + "'");
} }
} }
@ -200,7 +200,7 @@ std::string VirtualMachine::getModuleName(const unsigned int address) const
} }
else else
{ {
HADRON_ERROR("no module with address " + std::to_string(address)); HADRON_ERROR(Definition, "no module with address " + std::to_string(address));
} }
} }
@ -212,7 +212,7 @@ std::string VirtualMachine::getModuleType(const unsigned int address) const
} }
else else
{ {
HADRON_ERROR("no module with address " + std::to_string(address)); HADRON_ERROR(Definition, "no module with address " + std::to_string(address));
} }
} }
@ -273,7 +273,7 @@ Graph<unsigned int> VirtualMachine::makeModuleGraph(void) const
// { // {
// if (o.module < 0) // if (o.module < 0)
// { // {
// HADRON_ERROR("object '" + o.name + "' does not have a creator"); // HADRON_ERROR(Runtime, "object '" + o.name + "' does not have a creator");
// } // }
// } // }
// } // }

View File

@ -147,7 +147,7 @@ M * VirtualMachine::getModule(const unsigned int address) const
} }
else else
{ {
HADRON_ERROR("module '" + module_[address].name HADRON_ERROR(Definition, "module '" + module_[address].name
+ "' does not have type " + typeid(M).name() + "' does not have type " + typeid(M).name()
+ "(has type: " + getModuleType(address) + ")"); + "(has type: " + getModuleType(address) + ")");
} }