MOOCHO (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ProfileHackPack_profile_hack.cpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Moocho: Multi-functional Object-Oriented arCHitecture for Optimization
5 // Copyright (2003) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #include <ostream>
43 #include <map>
44 #include <vector>
45 #include <string>
46 #include <algorithm>
47 #include <iomanip>
48 
50 
51 namespace {
52 
53 //
54 class TimingEntry {
55 public:
56  TimingEntry() : num_calls(0), total_time_secs(0) {}
57  size_t num_calls;
58  double total_time_secs; // in seconds
59 }; // end struct TimingEntry
60 
61 //
62 typedef std::map<std::string,TimingEntry> func_timing_map_t;
63 
64 //
65 func_timing_map_t func_timing_map;
66 
67 //
68 class SortByTimingDescending {
69 public:
70  bool operator()( const func_timing_map_t::value_type& x
71  , const func_timing_map_t::value_type& y ) const
72  {
73  return x.second.total_time_secs > y.second.total_time_secs;
74  }
75 }; // end class AbsMultVal
76 
77 //
78 template< class T >
79 inline
80 T my_max( const T& v1, const T& v2 ) { return v1 > v2 ? v1 : v2; }
81 
82 } // end namespace
83 
84 void ProfileHackPack::set_time( const char func_name[], double time_secs )
85 {
86  TimingEntry &entry = func_timing_map[func_name];
87  entry.num_calls++;
88  entry.total_time_secs += time_secs;
89 }
90 
91 void ProfileHackPack::print_timings( std::ostream& out )
92 {
93  using std::setw;
94  using std::right;
95  using std::left;
96 
97  // Sort the entries by the function times in descending order
98  typedef std::vector<std::pair<std::string,TimingEntry> > list_sorted_t;
99  list_sorted_t list_sorted(func_timing_map.size());
100  {
101  func_timing_map_t::const_iterator itr_from = func_timing_map.begin();
102  list_sorted_t::iterator itr_to = list_sorted.begin();
103  for( ; itr_from != func_timing_map.end(); ++itr_from, ++itr_to ) {
104  itr_to->first = itr_from->first;
105  itr_to->second = itr_from->second;
106  }
107  }
108  std::copy( func_timing_map.begin(), func_timing_map.end(), list_sorted.begin() );
109  std::sort( list_sorted.begin(), list_sorted.end(), SortByTimingDescending() );
110  // Get the maximum function name size
111  int max_func_name_len = 25;
112  {for( list_sorted_t::const_iterator itr = list_sorted.begin(); itr != list_sorted.end(); ++itr )
113  max_func_name_len = my_max( int(max_func_name_len), int(itr->first.size()) );}
114  // Print out the function times
115  const int
116  name_w = max_func_name_len+2,
117  dbl_w = 22,
118  int_w = 10;
119  const char
120  name_ul[] = "-------------------------",
121  dbl_ul[] = "--------------------",
122  int_ul[] = "--------";
123 
124  out << "\nPoor man\'s profile times:\n\n";
125  out << left << setw(name_w) << "function name"
126  << right << setw(dbl_w) << "self+childern(sec)"
127  << right << setw(int_w) << "# calls"
128  << right << setw(dbl_w) << "av cpu/call(sec)"
129  << std::endl
130  << left << setw(name_w) << name_ul
131  << right << setw(dbl_w) << dbl_ul
132  << right << setw(int_w) << int_ul
133  << right << setw(dbl_w) << dbl_ul
134  << std::endl;
135  {for( list_sorted_t::const_iterator itr = list_sorted.begin(); itr != list_sorted.end(); ++itr ) {
136  out << left << setw(name_w) << itr->first
137  << right << setw(dbl_w) << itr->second.total_time_secs
138  << right << setw(int_w) << itr->second.num_calls
139  << right << setw(dbl_w) << (itr->second.total_time_secs / itr->second.num_calls)
140  << std::endl;
141  }}
142 }
void print_timings(std::ostream &out)
Print out the timing generated by calls to set_time().
void set_time(const char func_name[], double time_secs)
Set the name and time for a function or other section of code.
void copy(const f_int &N, const f_dbl_prec *X, const f_int &INCX, f_dbl_prec *Y, const f_int &INCY)
std::ostream * out
AbstractLinAlgPack::value_type value_type