timer.hpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright (C) 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 timer.hpp
  6. *
  7. * This header provides the @c timer class, which provides access to
  8. * the MPI timers.
  9. */
  10. #ifndef BOOST_MPI_TIMER_HPP
  11. #define BOOST_MPI_TIMER_HPP
  12. #include <boost/mpi/config.hpp>
  13. #include <boost/limits.hpp>
  14. namespace boost { namespace mpi {
  15. /** @brief A simple timer that provides access to the MPI timing
  16. * facilities.
  17. *
  18. * The @c timer class is a simple wrapper around the MPI timing
  19. * facilities that mimics the interface of the Boost Timer library.
  20. */
  21. class BOOST_MPI_DECL timer {
  22. public:
  23. /** Initializes the timer
  24. *
  25. * @post @c elapsed() == 0
  26. */
  27. timer();
  28. /** Restart the timer.
  29. *
  30. * @post @c elapsed() == 0
  31. */
  32. void restart();
  33. /** Return the amount of time that has elapsed since the last
  34. * construction or reset, in seconds.
  35. */
  36. double elapsed() const;
  37. /** Return an estimate of the maximum possible value of
  38. * elapsed(). Note that this routine may return too high a value on
  39. * some systems.
  40. */
  41. double elapsed_max() const;
  42. /** Returns the minimum non-zero value that @c elapsed() may
  43. * return. This is the resolution of the timer.
  44. */
  45. double elapsed_min() const;
  46. /** Determines whether the elapsed time values are global times or
  47. local processor times. */
  48. static bool time_is_global();
  49. private:
  50. double start_time;
  51. }; // timer
  52. inline timer::timer()
  53. {
  54. restart();
  55. }
  56. inline void timer::restart()
  57. {
  58. start_time = MPI_Wtime();
  59. }
  60. inline double timer::elapsed() const
  61. {
  62. return MPI_Wtime() - start_time;
  63. }
  64. inline double timer::elapsed_max() const
  65. {
  66. return (std::numeric_limits<double>::max)();
  67. }
  68. inline double timer::elapsed_min() const
  69. {
  70. return MPI_Wtick();
  71. }
  72. } } // end namespace boost::mpi
  73. #endif // BOOST_MPI_TIMER_HPP