Page MenuHomec4science

lm_timer.cc
No OneTemporary

File Metadata

Created
Tue, Jul 9, 17:48

lm_timer.cc

/**
* @file lm_timer.cc
*
* @author Guillaume Anciaux <guillaume.anciaux@epfl.ch>
*
* @date Mon Sep 08 23:40:22 2014
*
* @brief Timer system for LibMultiScale
*
* @section LICENSE
*
* Copyright (©) 2010-2011 EPFL (Ecole Polytechnique Fédérale de Lausanne)
* Laboratory (LSMS - Laboratoire de Simulation en Mécanique des Solides)
*
* LibMultiScale is free software: you can redistribute it and/or modify it
* under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* LibMultiScale is distributed in the hope that it will be useful, but
* WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with LibMultiScale. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "lm_common.hh"
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
/* -------------------------------------------------------------------------- */
__BEGIN_LIBMULTISCALE__
#ifdef LIBMULTISCALE_TIMER
/* -------------------------------------------------------------------------- */
std::map<std::string, struct timespec> mesT;
std::map<std::string, UInt> nmes;
std::map<std::string, struct timespec> tstart;
std::map<std::string, struct timespec> tstop;
UInt header = false;
/* -------------------------------------------------------------------------- */
void dumpTimes(UInt proc_number) {
std::stringstream fname;
fname << "perf-" << proc_number;
std::ofstream out(fname.str().c_str());
out << std::setw(30) << "Operation" << std::setw(5) << " " << std::setw(20)
<< "Mean time(s)" << std::setw(5) << " " << std::setw(20)
<< "Total time(s)" << std::setw(5) << " " << std::setw(20)
<< "Call number" << std::endl;
std::multimap<Real, std::string> inv;
{
std::map<std::string, struct timespec>::iterator it = mesT.begin();
std::map<std::string, struct timespec>::iterator end = mesT.end();
for (; it != end; ++it) {
Real t = (*it).second.tv_sec + (*it).second.tv_nsec * 1e-9;
inv.insert(std::pair<Real, std::string>(t, (*it).first));
}
}
std::multimap<Real, std::string>::reverse_iterator it = inv.rbegin();
std::multimap<Real, std::string>::reverse_iterator end = inv.rend();
for (; it != end; ++it) {
std::string &key = (*it).second;
Real t = (*it).first;
UInt n = nmes[key];
Real t_moy = t / n;
out << std::scientific << std::setprecision(10) << std::setw(30) << key
<< std::setw(5) << " " << std::setw(20) << t_moy << std::setw(5) << " "
<< std::setw(20) << t << std::setw(5) << " " << std::setw(20) << n
<< std::endl;
}
}
/* -------------------------------------------------------------------------- */
/* add by jcho */
void dumpTimesInColumn(UInt proc_number, UInt step) {
std::multimap<Real, std::string> inv;
{
std::map<std::string, struct timespec>::iterator it = mesT.begin();
std::map<std::string, struct timespec>::iterator end = mesT.end();
for (; it != end; ++it) {
Real t = (*it).second.tv_sec + (*it).second.tv_nsec * 1e-9;
inv.insert(std::pair<Real, std::string>(t, (*it).first));
}
}
std::multimap<Real, std::string>::reverse_iterator it = inv.rbegin();
std::multimap<Real, std::string>::reverse_iterator end = inv.rend();
std::stringstream fname;
fname << "times-" << proc_number;
if (!header) {
header = true;
std::ofstream myfile;
myfile.open(fname.str().c_str());
myfile << "step" << std::setw(30);
for (; it != end; ++it) {
std::string &key = (*it).second;
myfile << key << std::setw(30);
}
myfile << std::endl;
myfile.close();
} else {
// analysis header
std::ifstream myfile0(fname.str().c_str());
std::string head;
std::getline(myfile0, head);
std::vector<std::string> keys;
std::istringstream stm(head);
std::string token;
while (stm >> token)
keys.push_back(token);
UInt data_length = keys.size();
// append data
UInt count = 0;
std::ofstream myfile;
myfile.open(fname.str().c_str(), std::ios::app);
myfile << step << std::setw(30);
for (UInt i = 1; i < data_length; ++i) {
it = inv.rbegin();
end = inv.rend();
for (; it != end; ++it) {
std::string &key = (*it).second;
Real t = (*it).first;
if (key == keys[i]) {
myfile << std::scientific << std::setprecision(10) << t
<< std::setw(30);
count += 1;
break;
}
}
}
if (count != data_length - 1) {
LM_FATAL("non matched number of data " << count << " and "
<< data_length - 1);
}
myfile << std::endl;
myfile.close();
}
}
#else
void dumpTimes(UInt) {}
void dumpTimesInColumn(UInt, UInt) {}
#endif
/* -------------------------------------------------------------------------- */
__END_LIBMULTISCALE__
/* -------------------------------------------------------------------------- */

Event Timeline