From: Arnaud Giersch Date: Sun, 12 Dec 2010 11:50:22 +0000 (+0100) Subject: Wip++... X-Git-Tag: v0.1~248 X-Git-Url: https://bilbo.iut-bm.univ-fcomte.fr/and/gitweb/loba.git/commitdiff_plain/f620be5869815a0dccd5b37089c2ee91399915e4?ds=inline Wip++... * add log_rate option * rework ESSE macro * rework timer.h --- diff --git a/Makefile b/Makefile index 8f484f1..61fab37 100644 --- a/Makefile +++ b/Makefile @@ -33,6 +33,7 @@ SETLOCALVERSION := ./setlocalversion SRC.loba := main.cpp \ communicator.cpp \ cost_func.cpp \ + misc.cpp \ neighbor.cpp \ options.cpp \ process.cpp \ diff --git a/misc.cpp b/misc.cpp new file mode 100644 index 0000000..06ec972 --- /dev/null +++ b/misc.cpp @@ -0,0 +1,6 @@ +#include "misc.h" + +namespace misc { + const char str_esse[] = "s"; + const char str_nil[] = ""; +} diff --git a/misc.h b/misc.h index 1decbef..4bae95a 100644 --- a/misc.h +++ b/misc.h @@ -10,7 +10,12 @@ #define LOG_ISENABLED(priority) \ (_XBT_LOG_ISENABLEDV((*_XBT_LOGV(default)), (priority))) -#define ESSE(n) ((n) > 1 ? "s" : "") +namespace misc { + extern const char str_esse[]; + extern const char str_nil[]; +} +/* Returns c-string "s" if n > 1, empty string "" otherwise. */ +#define ESSE(n) ((n) > 1 ? misc::str_esse : misc::str_nil) #endif // !MISC_H diff --git a/options.cpp b/options.cpp index 18d4613..a16023a 100644 --- a/options.cpp +++ b/options.cpp @@ -19,6 +19,8 @@ namespace opt { int help_requested = 0; bool version_requested = false; + unsigned log_rate = 1; + unsigned maxiter = 4; bool exit_on_close = false; @@ -46,7 +48,7 @@ int opt::parse_args(int* argc, char* argv[]) int c; opterr = 0; - while ((c = getopt(*argc, argv, "bc:ehi:V")) != -1) { + while ((c = getopt(*argc, argv, "bc:ehi:l:V")) != -1) { switch (c) { case 'b': opt::bookkeeping = true; @@ -63,6 +65,9 @@ int opt::parse_args(int* argc, char* argv[]) case 'i': std::istringstream(optarg) >> opt::maxiter; break; + case 'l': + std::istringstream(optarg) >> opt::log_rate; + break; case 'V': opt::version_requested = true; break; @@ -103,6 +108,7 @@ void opt::print() INFO0(",----[ Simulation parameters ]"); INFO1("| platform_file.......: \"%s\"", opt::platform_file); INFO1("| application_file....: \"%s\"", opt::application_file); + INFO1("| log rate............: %u", opt::log_rate); INFO1("| maxiter.............: %u", opt::maxiter); INFO1("| exit on close.......: %s", on_off(opt::exit_on_close)); INFO1("| bookkeeping.........: %s", on_off(opt::bookkeeping)); @@ -137,6 +143,9 @@ void opt::usage() std::clog << oo("-i", "value") << "maximum number of iterations, 0 for infinity (" << opt::maxiter << ")\n"; + std::clog << oo("-l", "value") + << "print current load every \"value\" iterations, 0 to disable (" + << opt::log_rate << ")\n"; #undef o #undef oo diff --git a/options.h b/options.h index 43286f3..8125edb 100644 --- a/options.h +++ b/options.h @@ -14,6 +14,8 @@ namespace opt { extern int help_requested; extern bool version_requested; + extern unsigned log_rate; + extern unsigned maxiter; extern bool exit_on_close; diff --git a/process.cpp b/process.cpp index d8ec500..bc71b9d 100644 --- a/process.cpp +++ b/process.cpp @@ -30,7 +30,6 @@ process::process(int argc, char* argv[]) e_xbt_log_priority_t logp = xbt_log_priority_verbose; if (!LOG_ISENABLED(logp)) return; - LOG1(logp, "My initial load is: %g", load); std::ostringstream oss; oss << neigh.size() << " neighbor"; if (!neigh.empty()) { @@ -48,16 +47,21 @@ int process::run() { bool one_more = true; unsigned iter = 0; + + INFO1("Initial load: %g", load); VERB0("Starting..."); while (one_more) { bool close_received; + ++iter; - if (opt::bookkeeping) - INFO3("(%u) current load: %g ; expected: %g", - iter, load, expected_load); - else - INFO2("(%u) current load: %g", - iter, load); + if (opt::log_rate && iter % opt::log_rate == 0) { + if (opt::bookkeeping) + INFO3("(%u) current load: %g ; expected: %g", + iter, load, expected_load); + else + INFO2("(%u) current load: %g", + iter, load); + } compute(); close_received = !receive(false); @@ -68,7 +72,6 @@ int process::run() */ comm.flush(false); - ++iter; if (opt::exit_on_close && close_received) one_more = false; @@ -85,6 +88,11 @@ int process::run() */ VERB0("Done."); + if (opt::bookkeeping) + INFO4("Final load after %d iteration%s: %g ; expected: %g", + iter, ESSE(iter), load, expected_load); + else + INFO3("Final load after %d iteration%s: %g", iter, ESSE(iter), load); return 0; } diff --git a/timer.h b/timer.h index dbf32e1..ccb115b 100644 --- a/timer.h +++ b/timer.h @@ -4,24 +4,31 @@ #include #include -#if NEED_TIMERCLEAR -inline -void timerclear(struct timeval& a) -{ - tv.sec = tv.usec = 0; -} +#ifdef _BSD_SOURCE +# define HAVE_TIMERADD +# define HAVE_TIMERSUB +# define HAVE_TIMERCLEAR +#else +# warning _BSD_SOURCE not defined +# undef HAVE_TIMERADD +# undef HAVE_TIMERSUB +# undef HAVE_TIMERCLEAR #endif inline struct timeval operator+(const struct timeval& a, const struct timeval& b) { struct timeval result; +#ifdef HAVE_TIMERADD + timeradd(&a, &b, &result); +#else result.tv_sec = a.tv_sec + b.tv_sec; result.tv_usec = a.tv_usec + b.tv_usec; if (result.tv_usec >= 1000000) { ++result.tv_sec; result.tv_usec -= 1000000; } +#endif return result; } @@ -29,63 +36,102 @@ inline struct timeval operator-(const struct timeval& a, const struct timeval& b) { struct timeval result; +#ifdef HAVE_TIMERSUB + timersub(&a, &b, &result); +#else result.tv_sec = a.tv_sec - b.tv_sec; result.tv_usec = a.tv_usec - b.tv_usec; if (result.tv_usec < 0) { - -- result.tv_sec; + --result.tv_sec; result.tv_usec += 1000000; } +#endif return result; } -double timertod(const struct timeval& a) -{ - return a.tv_sec + a.tv_usec / 1e6; -} - class timestamp { +public: + timestamp(); + ~timestamp(); + void reset(); + + void start(); + void stop(); + + struct timeval tv_duration() const; + double duration() const; + private: struct rusage before; struct rusage after; struct timeval difference; -public: - timestamp() - { - reset(); - } + static void tv_clear(struct timeval& a); + static double timertod(const struct timeval& a); +}; - void reset() - { - timerclear(&before.ru_utime); - timerclear(&before.ru_stime); - timerclear(&after.ru_utime); - timerclear(&after.ru_stime); - timerclear(&difference); - } +inline +timestamp::timestamp() +{ + reset(); +} - void start() - { - getrusage(RUSAGE_SELF, &before); - } +inline +timestamp::~timestamp() +{ +} - void stop() - { - getrusage(RUSAGE_SELF, &after); - difference = difference + ((after.ru_utime + after.ru_stime) - - (before.ru_utime + before.ru_stime)); - } +inline +void timestamp::reset() +{ + tv_clear(before.ru_utime); + tv_clear(before.ru_stime); + tv_clear(after.ru_utime); + tv_clear(after.ru_stime); + tv_clear(difference); +} - struct timeval tv_duration() const - { - return difference; - } +inline +void timestamp::start() +{ + getrusage(RUSAGE_SELF, &before); +} - double duration() const - { - return timertod(difference); - } -}; +inline +void timestamp::stop() +{ + getrusage(RUSAGE_SELF, &after); + difference = difference + ((after.ru_utime + after.ru_stime) - + (before.ru_utime + before.ru_stime)); +} + +inline +struct timeval timestamp::tv_duration() const +{ + return difference; +} + +inline +double timestamp::duration() const +{ + return timertod(difference); +} + +inline +void timestamp::tv_clear(struct timeval& a) +{ +#ifdef HAVE_TIMERCLEAR + timerclear(&a); +#else + tv.sec = tv.usec = 0; +#endif +} + +inline +double timestamp::timertod(const struct timeval& a) +{ + return a.tv_sec + a.tv_usec / 1e6; +} #endif // !TIMER_H