mirror of
https://github.com/triqs/dft_tools
synced 2024-10-31 11:13:46 +01:00
[exceptions] Add mpi node number and C++ trace to C++ exceptions.
- Trace : to be decided if we keep it -> or put it in the python object.
This commit is contained in:
parent
16b5e78e8e
commit
0c34877b9b
@ -19,55 +19,11 @@
|
||||
*
|
||||
******************************************************************************/
|
||||
#pragma once
|
||||
#include <triqs/utility/c14.hpp>
|
||||
#include <mpi.h>
|
||||
|
||||
namespace boost { // forward declare in case we do not include boost.
|
||||
namespace mpi {
|
||||
class communicator;
|
||||
}
|
||||
}
|
||||
#include "./communicator.hpp"
|
||||
|
||||
namespace triqs {
|
||||
namespace mpi {
|
||||
|
||||
/// Environment
|
||||
struct environment {
|
||||
// MPICH does not allow Init without argc, argv, so we do not allow default constructors
|
||||
// for portability, cf #133
|
||||
environment(int argc, char *argv[]) { MPI_Init(&argc, &argv); }
|
||||
~environment() { MPI_Finalize(); }
|
||||
};
|
||||
|
||||
/// The communicator. Todo : add more constructors.
|
||||
class communicator {
|
||||
MPI_Comm _com = MPI_COMM_WORLD;
|
||||
|
||||
public:
|
||||
communicator() = default;
|
||||
|
||||
MPI_Comm get() const { return _com; }
|
||||
|
||||
inline communicator(boost::mpi::communicator);
|
||||
|
||||
/// Cast to the boost mpi communicator
|
||||
inline operator boost::mpi::communicator () const;
|
||||
|
||||
int rank() const {
|
||||
int num;
|
||||
MPI_Comm_rank(_com, &num);
|
||||
return num;
|
||||
}
|
||||
|
||||
int size() const {
|
||||
int num;
|
||||
MPI_Comm_size(_com, &num);
|
||||
return num;
|
||||
}
|
||||
|
||||
void barrier() const { MPI_Barrier(_com); }
|
||||
};
|
||||
|
||||
/// a tag for each operation
|
||||
namespace tag {
|
||||
struct broadcast {};
|
||||
|
78
triqs/mpi/communicator.hpp
Normal file
78
triqs/mpi/communicator.hpp
Normal file
@ -0,0 +1,78 @@
|
||||
/*******************************************************************************
|
||||
*
|
||||
* TRIQS: a Toolbox for Research in Interacting Quantum Systems
|
||||
*
|
||||
* Copyright (C) 2014 by O. Parcollet
|
||||
*
|
||||
* TRIQS 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.
|
||||
*
|
||||
* TRIQS 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
|
||||
* TRIQS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
******************************************************************************/
|
||||
#pragma once
|
||||
#include <triqs/utility/c14.hpp>
|
||||
#include <mpi.h>
|
||||
|
||||
namespace boost { // forward declare in case we do not include boost.
|
||||
namespace mpi {
|
||||
class communicator;
|
||||
}
|
||||
}
|
||||
|
||||
namespace triqs {
|
||||
namespace mpi {
|
||||
|
||||
/// Environment
|
||||
struct environment {
|
||||
// MPICH does not allow Init without argc, argv, so we do not allow default constructors
|
||||
// for portability, cf #133
|
||||
environment(int argc, char *argv[]) { MPI_Init(&argc, &argv); }
|
||||
~environment() { MPI_Finalize(); }
|
||||
};
|
||||
|
||||
//
|
||||
inline bool is_initialized() noexcept {
|
||||
int flag;
|
||||
MPI_Initialized(&flag);
|
||||
return flag;
|
||||
}
|
||||
|
||||
/// The communicator. Todo : add more constructors.
|
||||
class communicator {
|
||||
MPI_Comm _com = MPI_COMM_WORLD;
|
||||
|
||||
public:
|
||||
communicator() = default;
|
||||
|
||||
MPI_Comm get() const { return _com; }
|
||||
|
||||
inline communicator(boost::mpi::communicator);
|
||||
|
||||
/// Cast to the boost mpi communicator
|
||||
inline operator boost::mpi::communicator() const;
|
||||
|
||||
int rank() const {
|
||||
int num;
|
||||
MPI_Comm_rank(_com, &num);
|
||||
return num;
|
||||
}
|
||||
|
||||
int size() const {
|
||||
int num;
|
||||
MPI_Comm_size(_com, &num);
|
||||
return num;
|
||||
}
|
||||
|
||||
void barrier() const { MPI_Barrier(_com); }
|
||||
};
|
||||
}
|
||||
}
|
@ -19,10 +19,9 @@
|
||||
* TRIQS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
******************************************************************************/
|
||||
#pragma once
|
||||
|
||||
#ifndef TRIQS_EXCEPTIONS_H
|
||||
#define TRIQS_EXCEPTIONS_H
|
||||
|
||||
#include "../mpi/communicator.hpp"
|
||||
#include "./stack_trace.hpp"
|
||||
#include <exception>
|
||||
#include <string>
|
||||
@ -37,7 +36,13 @@ namespace triqs {
|
||||
virtual ~exception() throw() {}
|
||||
template<typename T> exception & operator <<( T const & x) { std::stringstream f; f<<acc<<x; acc = f.str(); return *this;}
|
||||
exception & operator <<( const char * mess ) { (*this) << std::string(mess); return *this;}// to limit code size
|
||||
virtual const char* what() const throw() { return acc.c_str();}
|
||||
virtual const char* what() const throw() {
|
||||
std::stringstream out;
|
||||
out << acc << "\n Error occurred on node ";
|
||||
if (mpi::is_initialized()) out << mpi::communicator().rank()<<"\n";
|
||||
out << " C++ trace is : " << trace();
|
||||
return out.str().c_str();
|
||||
}
|
||||
virtual const char* trace() const throw() { return _trace.c_str();}
|
||||
};
|
||||
|
||||
@ -60,5 +65,4 @@ namespace triqs {
|
||||
#define TRIQS_RUNTIME_ERROR TRIQS_ERROR(triqs::runtime_error,"runtime error")
|
||||
#define TRIQS_KEYBOARD_INTERRUPT TRIQS_ERROR(triqs::keyboard_interrupt,"Ctrl-C")
|
||||
|
||||
#endif
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user