|
MoochoPack: Miscellaneous Utilities for MOOCHO
Version of the Day
|
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 // Redistribution and use in source and binary forms, with or without 00011 // modification, are permitted provided that the following conditions are 00012 // met: 00013 // 00014 // 1. Redistributions of source code must retain the above copyright 00015 // notice, this list of conditions and the following disclaimer. 00016 // 00017 // 2. Redistributions in binary form must reproduce the above copyright 00018 // notice, this list of conditions and the following disclaimer in the 00019 // documentation and/or other materials provided with the distribution. 00020 // 00021 // 3. Neither the name of the Corporation nor the names of the 00022 // contributors may be used to endorse or promote products derived from 00023 // this software without specific prior written permission. 00024 // 00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY 00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE 00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00036 // 00037 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov) 00038 // 00039 // *********************************************************************** 00040 // @HEADER 00041 00042 #include <ostream> 00043 #include <map> 00044 #include <vector> 00045 #include <string> 00046 #include <algorithm> 00047 #include <iomanip> 00048 00049 #include "ProfileHackPack_profile_hack.hpp" 00050 00051 namespace { 00052 00053 // 00054 class TimingEntry { 00055 public: 00056 TimingEntry() : num_calls(0), total_time_secs(0) {} 00057 size_t num_calls; 00058 double total_time_secs; // in seconds 00059 }; // end struct TimingEntry 00060 00061 // 00062 typedef std::map<std::string,TimingEntry> func_timing_map_t; 00063 00064 // 00065 func_timing_map_t func_timing_map; 00066 00067 // 00068 class SortByTimingDescending { 00069 public: 00070 bool operator()( const func_timing_map_t::value_type& x 00071 , const func_timing_map_t::value_type& y ) const 00072 { 00073 return x.second.total_time_secs > y.second.total_time_secs; 00074 } 00075 }; // end class AbsMultVal 00076 00077 // 00078 template< class T > 00079 inline 00080 T my_max( const T& v1, const T& v2 ) { return v1 > v2 ? v1 : v2; } 00081 00082 } // end namespace 00083 00084 void ProfileHackPack::set_time( const char func_name[], double time_secs ) 00085 { 00086 TimingEntry &entry = func_timing_map[func_name]; 00087 entry.num_calls++; 00088 entry.total_time_secs += time_secs; 00089 } 00090 00091 void ProfileHackPack::print_timings( std::ostream& out ) 00092 { 00093 using std::setw; 00094 using std::right; 00095 using std::left; 00096 00097 // Sort the entries by the function times in descending order 00098 typedef std::vector<std::pair<std::string,TimingEntry> > list_sorted_t; 00099 list_sorted_t list_sorted(func_timing_map.size()); 00100 { 00101 func_timing_map_t::const_iterator itr_from = func_timing_map.begin(); 00102 list_sorted_t::iterator itr_to = list_sorted.begin(); 00103 for( ; itr_from != func_timing_map.end(); ++itr_from, ++itr_to ) { 00104 itr_to->first = itr_from->first; 00105 itr_to->second = itr_from->second; 00106 } 00107 } 00108 std::copy( func_timing_map.begin(), func_timing_map.end(), list_sorted.begin() ); 00109 std::sort( list_sorted.begin(), list_sorted.end(), SortByTimingDescending() ); 00110 // Get the maximum function name size 00111 int max_func_name_len = 25; 00112 {for( list_sorted_t::const_iterator itr = list_sorted.begin(); itr != list_sorted.end(); ++itr ) 00113 max_func_name_len = my_max( int(max_func_name_len), int(itr->first.size()) );} 00114 // Print out the function times 00115 const int 00116 name_w = max_func_name_len+2, 00117 dbl_w = 22, 00118 int_w = 10; 00119 const char 00120 name_ul[] = "-------------------------", 00121 dbl_ul[] = "--------------------", 00122 int_ul[] = "--------"; 00123 00124 out << "\nPoor man\'s profile times:\n\n"; 00125 out << left << setw(name_w) << "function name" 00126 << right << setw(dbl_w) << "self+childern(sec)" 00127 << right << setw(int_w) << "# calls" 00128 << right << setw(dbl_w) << "av cpu/call(sec)" 00129 << std::endl 00130 << left << setw(name_w) << name_ul 00131 << right << setw(dbl_w) << dbl_ul 00132 << right << setw(int_w) << int_ul 00133 << right << setw(dbl_w) << dbl_ul 00134 << std::endl; 00135 {for( list_sorted_t::const_iterator itr = list_sorted.begin(); itr != list_sorted.end(); ++itr ) { 00136 out << left << setw(name_w) << itr->first 00137 << right << setw(dbl_w) << itr->second.total_time_secs 00138 << right << setw(int_w) << itr->second.num_calls 00139 << right << setw(dbl_w) << (itr->second.total_time_secs / itr->second.num_calls) 00140 << std::endl; 00141 }} 00142 }
1.7.6.1