3 // Copyright (C) 2007 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the terms
7 // of the GNU General Public License as published by the Free Software
8 // Foundation; either version 2, or (at your option) any later
11 // This library is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this library; see the file COPYING. If not, write to
18 // the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
19 // MA 02111-1307, USA.
21 // As a special exception, you may use this file as part of a free
22 // software library without restriction. Specifically, if other files
23 // instantiate templates or use macros or inline functions from this
24 // file, or you compile this file and link it with other files to
25 // produce an executable, this file does not by itself cause the
26 // resulting executable to be covered by the GNU General Public
27 // License. This exception does not however invalidate any other
28 // reasons why the executable file might be covered by the GNU General
31 /** @file parallel/timing.h
32 * @brief Provides a simple tool to do performance debugging, also in
34 * This file is a GNU parallel extension to the Standard C++ Library.
37 // Written by Johannes Singler.
39 #ifndef _GLIBCXX_PARALLEL_TIMING_H
40 #define _GLIBCXX_PARALLEL_TIMING_H 1
45 #include <parallel/tags.h>
47 namespace __gnu_parallel
49 // XXX integrate with existing performance testing infrastructure.
50 /** @brief Type of of point in time, used for the Timing classes. */
51 typedef double point_in_time;
53 template<typename tag, typename must_be_int = int>
56 /** @brief A class that provides simple run time measurements, also
58 * @param tag If parallel_tag, then the measurements are actually done.
59 * Otherwise, no code at all is emitted by the compiler. */
60 template<typename must_be_int>
61 class Timing<parallel_tag, must_be_int>
64 static const int max_points_in_time = 100;
65 point_in_time points_in_time[max_points_in_time];
66 point_in_time active, last_start;
69 const char* tags[max_points_in_time];
85 /** @brief Take a running time measurement.
86 * @param tag Optional description that will be output again with
88 * It should describe the operation before the tic(). To time a
89 * series of @c n operations, there should be @c n+1 calls to
90 * tic(), and one call to print(). */
92 tic(const char* tag = NULL)
94 points_in_time[pos] = omp_get_wtime();
99 /** @brief Start the running time measurement.
101 * Should be paired with stop(). */
105 _GLIBCXX_PARALLEL_ASSERT(last_start == -1.0);
106 last_start = omp_get_wtime();
109 /** @brief Stop the running time measurement.
111 * Should be paired with start(). */
115 _GLIBCXX_PARALLEL_ASSERT(last_start != -1.0);
116 active += (omp_get_wtime() - last_start);
120 /** @brief Reset running time accumulation. */
128 /** @brief Accumulate the time between all pairs of start() and
134 /** @brief Total time between first and last tic() */
137 { return (points_in_time[pos - 1] - points_in_time[0]) * 1000.0; }
140 /** @brief Construct string to print out, presenting the timings. */
144 // Avoid stream library here, to avoid cyclic dependencies in
149 str = new char[pos * 200];
153 sprintf(str, "t %2d T[ms]", omp_get_thread_num());
156 for (int i = 0; i < pos; )
158 point_in_time last = points_in_time[i];
163 sprintf(tmp, "%2d: ", i - 1);
165 sprintf(tmp, "%20s: ", tags[i]);
168 sprintf(tmp, "%7.2f ", (points_in_time[i] - last) * 1000.0);
177 /** @brief Print the running times between the tic()s. */
186 printf("%s\n", c_str());
190 /** @brief A class that provides simple run time measurements, also
192 * @param tag If parallel_tag, then the measurements are actually done,
193 * otherwise, no code at all is emitted by the compiler.
195 template<typename must_be_int>
196 class Timing<sequential_tag, must_be_int>
199 static const char* empty_string;
202 inline void tic(const char* /*tag*/ = NULL) { }
203 inline void start() { }
204 inline void stop() { }
205 inline void reset() { }
206 inline point_in_time active_time() { return -1.0; }
207 inline point_in_time total_time() { return -1.0; }
208 inline const char* c_str() { return empty_string; }
209 inline void print() { }
212 template<typename must_be_int>
213 const char* Timing<sequential_tag, must_be_int>::empty_string = "";