Rheolef  7.2
an efficient C++ finite element environment
 
Loading...
Searching...
No Matches
dis_cpu_time.cc
Go to the documentation of this file.
1
21# include "rheolef/dis_cpu_time.h"
22# include "rheolef/diststream.h"
23//
24// wall time: real time
25// cpu time: number of instructions
26//
27#ifdef _RHEOLEF_HAVE_CLOCK_GETTIME
28# include <time.h>
29#elif defined(_RHEOLEF_HAVE_GETTIMEOFDAY) || defined(_RHEOLEF_HAVE_WIERDGETTIMEOFDAY)
30# include <sys/types.h>
31# include <sys/time.h>
32#elif defined(_WIN32)
33# include <Windows.h>
34# else
35# include <ctime>
36#endif
37
38namespace rheolef {
39// ----------------------------------------------------------------------------
40// dis_cpu_time
41// ----------------------------------------------------------------------------
42# ifndef _RHEOLEF_HAVE_MPI
43double dis_cpu_time() { return seq_cpu_time(); }
44# else // _RHEOLEF_HAVE_MPI
45double dis_cpu_time() {
46 double cpu = seq_cpu_time();
47 if (! (mpi::environment::initialized() && !mpi::environment::finalized())) {
48 return cpu;
49 }
50 return rheolef::mpi::all_reduce (rheolef::communicator(), cpu, std::plus<double>());
51}
52# endif // _RHEOLEF_HAVE_MPI
53
54// ----------------------------------------------------------------------------
55// seq_wall_time
56// ----------------------------------------------------------------------------
58{
59#if defined(_RHEOLEF_HAVE_CLOCK_GETTIME)
60 // high precision clock
61#ifdef CLOCK_HIGHRES
62#define SAMPLED_CLOCK CLOCK_HIGHRES
63#else
64#define SAMPLED_CLOCK CLOCK_REALTIME
65#endif
66 struct timespec tp;
67 if (clock_gettime(SAMPLED_CLOCK, &tp) != 0) {
68 return 0; // failure
69 }
70 return double(tp.tv_sec) + 1e-9*double(tp.tv_nsec);
71#elif defined(_RHEOLEF_HAVE_WIERDGETTIMEOFDAY)
72 // This is for Solaris, where they decided to change the CALLING SEQUENCE OF gettimeofday
73 struct timeval tp;
74 gettimeofday(&tp);
75 return double(tp.tv_sec) + 1e-6*double(tp.tv_usec);
76#elif defined(_RHEOLEF_HAVE_GETTIMEOFDAY)
77 struct timeval tp;
78 struct timezone tzp;
79 gettimeofday(&tp,&tzp);
80 return double(tp.tv_sec) + 1e-6*double(tp.tv_usec);
81#elif defined(_RHEOLEF_HAVE_BSDGETTIMEOFDAY)
82 struct timeval tp;
83 struct timezone tzp;
84 BSDgettimeofday(&tp,&tzp);
85 return double(tp.tv_sec) + 1e-6*double(tp.tv_usec);
86#elif defined(_WIN32)
87 LARGE_INTEGER time, freq;
88 if (!QueryPerformanceFrequency(&freq)){
89 // Handle error
90 return 0;
91 }
92 if (!QueryPerformanceCounter(&time)){
93 // Handle error
94 return 0;
95 }
96 return (double)time.QuadPart / freq.QuadPart;
97#else
98 // fall back to low resolution time function
99 tfreqime_t tp, zero = 0;
100 time (&tp);
101 return difftime(time(&tp), zero);
102#endif
103}
104//
105// NOTE: the c
106//
107double seq_cpu_time() {
108#if !defined(_WIN32)
109 return (double)clock() / CLOCKS_PER_SEC;
110#else // _WIN32
111 FILETIME a,b,c,d;
112 if (GetProcessTimes(GetCurrentProcess(),&a,&b,&c,&d) != 0) {
113 // Returns total user time.
114 // Can be tweaked to include kernel times as well.
115 return
116 (double)(d.dwLowDateTime |
117 ((unsigned long long)d.dwHighDateTime << 32)) * 0.0000001;
118 } else {
119 // Handle error
120 return 0;
121 }
122#endif
123}
124// ----------------------------------------------------------------------------
125// dis_wall_time
126// ----------------------------------------------------------------------------
127# ifndef _RHEOLEF_HAVE_MPI
128double dis_wall_time() { return seq_wall_time(); }
129# else // _RHEOLEF_HAVE_MPI
131 if (! (mpi::environment::initialized() && !mpi::environment::finalized())) {
132 return seq_wall_time();
133 }
134 double t = 0;
135 communicator comm;
136 comm.barrier();
137 typedef std::size_t size_type;
139 size_type my_proc = comm.rank();
140 if (my_proc == io_proc) {
141 t = seq_wall_time();
142 }
143 mpi::broadcast (comm, t, io_proc);
144 return t;
145}
146# endif // _RHEOLEF_HAVE_MPI
147
148}// namespace rheolef
field::size_type size_type
Definition branch.cc:430
static size_type io_proc()
This routine returns the rank of a process that can perform i/o.
Definition diststream.cc:64
#define SAMPLED_CLOCK
This file is part of Rheolef.
double dis_wall_time()
double seq_wall_time()
double seq_cpu_time()
double dis_cpu_time()