exception.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright (C) 2005-2006 Douglas Gregor <doug.gregor -at- gmail.com>.
  2. // Use, modification and distribution is subject to the Boost Software
  3. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. /** @file exception.hpp
  6. *
  7. * This header provides exception classes that report MPI errors to
  8. * the user and macros that translate MPI error codes into Boost.MPI
  9. * exceptions.
  10. */
  11. #ifndef BOOST_MPI_EXCEPTION_HPP
  12. #define BOOST_MPI_EXCEPTION_HPP
  13. #include <boost/mpi/config.hpp>
  14. #include <exception>
  15. #include <cassert>
  16. #include <string>
  17. #include <boost/config.hpp>
  18. #include <boost/throw_exception.hpp>
  19. namespace boost { namespace mpi {
  20. /** @brief Catch-all exception class for MPI errors.
  21. *
  22. * Instances of this class will be thrown when an MPI error
  23. * occurs. MPI failures that trigger these exceptions may or may not
  24. * be recoverable, depending on the underlying MPI
  25. * implementation. Consult the documentation for your MPI
  26. * implementation to determine the effect of MPI errors.
  27. */
  28. class BOOST_MPI_DECL exception : public std::exception
  29. {
  30. public:
  31. /**
  32. * Build a new @c exception exception.
  33. *
  34. * @param routine The MPI routine in which the error
  35. * occurred. This should be a pointer to a string constant: it
  36. * will not be copied.
  37. *
  38. * @param result_code The result code returned from the MPI
  39. * routine that aborted with an error.
  40. */
  41. exception(const char* routine, int result_code);
  42. virtual ~exception() throw();
  43. /**
  44. * A description of the error that occurred.
  45. */
  46. virtual const char * what () const throw ()
  47. {
  48. return this->message.c_str();
  49. }
  50. /** Retrieve the name of the MPI routine that reported the error. */
  51. const char* routine() const { return routine_; }
  52. /**
  53. * @brief Retrieve the result code returned from the MPI routine
  54. * that reported the error.
  55. */
  56. int result_code() const { return result_code_; }
  57. /**
  58. * @brief Returns the MPI error class associated with the error that
  59. * triggered this exception.
  60. */
  61. int error_class() const
  62. {
  63. int result;
  64. MPI_Error_class(result_code_, &result);
  65. return result;
  66. }
  67. protected:
  68. /// The MPI routine that triggered the error
  69. const char* routine_;
  70. /// The failed result code reported by the MPI implementation.
  71. int result_code_;
  72. /// The formatted error message
  73. std::string message;
  74. };
  75. /**
  76. * Call the MPI routine MPIFunc with arguments Args (surrounded by
  77. * parentheses). If the result is not MPI_SUCCESS, use
  78. * boost::throw_exception to throw an exception or abort, depending on
  79. * BOOST_NO_EXCEPTIONS.
  80. */
  81. #define BOOST_MPI_CHECK_RESULT( MPIFunc, Args ) \
  82. { \
  83. int _check_result = MPIFunc Args; \
  84. assert(_check_result == MPI_SUCCESS); \
  85. if (_check_result != MPI_SUCCESS) \
  86. boost::throw_exception(boost::mpi::exception(#MPIFunc, \
  87. _check_result)); \
  88. }
  89. } } // end namespace boost::mpi
  90. #endif // BOOST_MPI_EXCEPTION_HPP