ProfileHackPack_profile_hack.cpp

Go to the documentation of this file.
00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 // Moocho: Multi-functional Object-Oriented arCHitecture for Optimization
00005 //                  Copyright (2003) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 // 
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov) 
00025 // 
00026 // ***********************************************************************
00027 // @HEADER
00028 
00029 #include <ostream>
00030 #include <map>
00031 #include <vector>
00032 #include <string>
00033 #include <algorithm>
00034 #include <iomanip>
00035 
00036 #include "ProfileHackPack_profile_hack.hpp"
00037 
00038 namespace {
00039 
00040 //
00041 class TimingEntry {
00042 public:
00043   TimingEntry() : num_calls(0), total_time_secs(0) {}
00044   size_t         num_calls;
00045   double         total_time_secs;   // in seconds
00046 }; // end struct TimingEntry
00047 
00048 //
00049 typedef std::map<std::string,TimingEntry>  func_timing_map_t;
00050 
00051 //
00052 func_timing_map_t   func_timing_map;
00053 
00054 //
00055 class SortByTimingDescending {
00056 public:
00057   bool operator()( const func_timing_map_t::value_type& x
00058     , const func_timing_map_t::value_type& y ) const
00059   {
00060     return x.second.total_time_secs > y.second.total_time_secs;
00061   }
00062 };  // end class AbsMultVal
00063 
00064 //
00065 template< class T >
00066 inline
00067 T my_max( const T& v1, const T& v2 ) { return v1 > v2 ? v1 : v2; }
00068 
00069 } // end namespace 
00070 
00071 void ProfileHackPack::set_time( const char func_name[], double time_secs )
00072 {
00073   TimingEntry &entry = func_timing_map[func_name];
00074   entry.num_calls++;
00075   entry.total_time_secs += time_secs;
00076 }
00077 
00078 void ProfileHackPack::print_timings( std::ostream& out )
00079 {
00080   using std::setw;
00081   using std::right;
00082   using std::left;
00083 
00084   // Sort the entries by the function times in descending order
00085   typedef std::vector<std::pair<std::string,TimingEntry> > list_sorted_t;
00086   list_sorted_t  list_sorted(func_timing_map.size());
00087   {
00088     func_timing_map_t::const_iterator itr_from = func_timing_map.begin();
00089     list_sorted_t::iterator           itr_to   = list_sorted.begin();
00090     for( ; itr_from != func_timing_map.end(); ++itr_from, ++itr_to ) {
00091       itr_to->first = itr_from->first;
00092       itr_to->second = itr_from->second;
00093     }
00094   }
00095   std::copy( func_timing_map.begin(), func_timing_map.end(), list_sorted.begin() );
00096   std::sort( list_sorted.begin(), list_sorted.end(), SortByTimingDescending() );
00097   // Get the maximum function name size
00098   int max_func_name_len = 25;
00099   {for( list_sorted_t::const_iterator itr = list_sorted.begin(); itr != list_sorted.end(); ++itr )
00100     max_func_name_len = my_max( int(max_func_name_len), int(itr->first.size()) );}
00101   // Print out the function times
00102   const int
00103     name_w = max_func_name_len+2,
00104     dbl_w  = 22,
00105     int_w  = 10;
00106   const char
00107     name_ul[] = "-------------------------",
00108     dbl_ul[]  = "--------------------",
00109     int_ul[]  = "--------";
00110 
00111   out << "\nPoor man\'s profile times:\n\n";
00112   out << left  << setw(name_w) << "function name"
00113     << right << setw(dbl_w)  << "self+childern(sec)"
00114     << right << setw(int_w)  << "# calls"
00115     << right << setw(dbl_w)  << "av cpu/call(sec)"
00116     << std::endl
00117     << left  << setw(name_w) << name_ul
00118     << right << setw(dbl_w)  << dbl_ul
00119     << right << setw(int_w)  << int_ul
00120     << right << setw(dbl_w)  << dbl_ul
00121     << std::endl;
00122   {for( list_sorted_t::const_iterator itr = list_sorted.begin(); itr != list_sorted.end(); ++itr ) {
00123   out << left  << setw(name_w) << itr->first
00124     << right << setw(dbl_w)  << itr->second.total_time_secs
00125     << right << setw(int_w)  << itr->second.num_calls
00126     << right << setw(dbl_w)  << (itr->second.total_time_secs / itr->second.num_calls)
00127     << std::endl;
00128   }}
00129 }

Generated on Tue Oct 20 12:51:49 2009 for MOOCHO (Single Doxygen Collection) by doxygen 1.4.7