Alien  1.3.0
Developer documentation
Loading...
Searching...
No Matches
SolverStater.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2024 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* alienc (C) 2000-2024 */
9/* */
10/* Interface C for alien */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13#include "SolverStater.h"
14
15#ifdef WIN32
16#include <windows.h>
17#define ARCANE_TIMER_USE_CLOCK
18#else
19#include <errno.h>
20#include <sys/time.h>
21#endif
22
23#include <arccore/base/FatalErrorException.h>
24#include <arccore/base/TraceInfo.h>
25#include <ctime>
26
27/*---------------------------------------------------------------------------*/
28
29namespace Alien
30{
31
32using namespace Arccore;
33/*---------------------------------------------------------------------------*/
34
35#ifdef ARCANE_TIMER_USE_CLOCK
36static clock_t current_clock_value = 0;
37#endif
38
39/*---------------------------------------------------------------------------*/
40/*---------------------------------------------------------------------------*/
41
42Real BaseSolverStater::_getVirtualTime()
43{
44 // From Arcane 1.16.3 to work with historical timers and Windows
45#ifdef ARCANE_TIMER_USE_CLOCK
46 clock_t cv = ::clock();
47 Real diffv = static_cast<Real>(cv - current_clock_value);
48 return diffv / CLOCKS_PER_SEC;
49#else
50 struct itimerval time_val;
51 int r = ::getitimer(ITIMER_VIRTUAL, &time_val);
52 if (r != 0)
53 _errorInTimer("getitimer()", r);
54 Real v = static_cast<Real>(time_val.it_value.tv_sec) * 1. + static_cast<Real>(time_val.it_value.tv_usec) * 1e-6;
55 return (5000000. - v);
56#endif
57}
58
59/*---------------------------------------------------------------------------*/
60
61Real BaseSolverStater::_getRealTime()
62{
63 // From Arcane 1.16.3 to work with old timers and Windows.
64#ifdef WIN32
65 SYSTEMTIME t;
66 GetSystemTime(&t);
67 Real hour = t.wHour * 3600.0;
68 Real minute = t.wMinute * 60.0;
69 Real second = t.wSecond;
70 Real milli_second = t.wMilliseconds * 1e-3;
71 return (hour + minute + second + milli_second);
72#else
73 struct timeval tp;
74 int r = gettimeofday(&tp, 0);
75 if (r != 0)
76 _errorInTimer("gettimeofday()", r);
77 Real tvalue =
78 (static_cast<Real>(tp.tv_sec) * 1. + static_cast<Real>(tp.tv_usec) * 1.e-6);
79 return tvalue;
80#endif
81}
82
83/*---------------------------------------------------------------------------*/
84
85void BaseSolverStater::_errorInTimer(const String& msg, int retcode)
86{
87 throw FatalErrorException(
88 A_FUNCINFO, String::format("{0} return code: {1} errno: {2}", msg, retcode, errno));
89}
90
91/*---------------------------------------------------------------------------*/
92
93void BaseSolverStater::_startTimer()
94{
95 ALIEN_ASSERT((m_state == eNone), ("Unexpected SolverStater state %d", m_state));
96 m_real_time = _getRealTime();
97 m_cpu_time = _getVirtualTime();
98}
99
100/*---------------------------------------------------------------------------*/
101
102void BaseSolverStater::_stopTimer()
103{
104 ALIEN_ASSERT((m_state != eNone), ("Unexpected SolverStater state %d", m_state));
105 m_real_time = _getRealTime() - m_real_time;
106 m_cpu_time = _getVirtualTime() - m_cpu_time;
107}
108
109/*---------------------------------------------------------------------------*/
110
111} // namespace Alien
112
113/*---------------------------------------------------------------------------*/
Real m_cpu_time
'cpu' time for the lastest start or stop
Real m_real_time
'wall clock' time for the lastest start or stop
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Definition BackEnd.h:17