File: magma_timer.h

package info (click to toggle)
magma 2.9.0%2Bds-2
  • links: PTS, VCS
  • area: contrib
  • in suites: trixie
  • size: 83,212 kB
  • sloc: cpp: 709,115; fortran: 121,916; ansic: 32,343; python: 25,603; f90: 15,208; makefile: 942; xml: 253; csh: 232; sh: 203; perl: 104
file content (247 lines) | stat: -rw-r--r-- 6,878 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
    -- MAGMA (version 2.9.0) --
       Univ. of Tennessee, Knoxville
       Univ. of California, Berkeley
       Univ. of Colorado, Denver
       @date January 2025
       
       @author Mark Gates
*/

#ifndef MAGMA_TIMER_H
#define MAGMA_TIMER_H

#include <stdio.h>

#include "magma_v2.h"

typedef double    magma_timer_t;
typedef long long magma_flops_t;

#if defined(ENABLE_TIMER)
    #include <stdio.h>
    #include <stdarg.h>
    
    #if defined(HAVE_PAPI)
        #include <papi.h>
        extern int gPAPI_flops_set;  // defined in testing/magma_util.cpp
    #endif
#endif

// If we're not using GNU C, elide __attribute__
#ifndef __GNUC__
  #define  __attribute__(x)  /*NOTHING*/
#endif

/***************************************************************************//**
    @param[out]
    t       On output, set to current time.
    
    If ENABLE_TIMER is not defined, does nothing.
    
    @ingroup magma_timer
*******************************************************************************/
static inline void timer_start( magma_timer_t &t )
{
    #if defined(ENABLE_TIMER)
    t = magma_wtime();
    #endif
}


/***************************************************************************//**
    @param[out]
    t       On output, set to current time.
    
    @param[in]
    queue  Queue to sync with, before getting time.
    
    If ENABLE_TIMER is not defined, does nothing.
    
    @ingroup magma_timer
*******************************************************************************/
static inline void timer_sync_start( magma_timer_t &t, magma_queue_t queue )
{
    #if defined(ENABLE_TIMER)
    magma_queue_sync( queue );
    t = magma_wtime();
    #endif
}


/***************************************************************************//**
    @param[in,out]
    t       On input, time when timer_start() was called.
            On output, set to (current time - start time).

    @return t, to sum up times:
    
        magma_timer_t time, time_sum=0;
        for( ... ) {
            timer_start( time );
            ...do timed operations...
            time_sum += timer_stop( time );
        
            ...do other operations...
        }
    
    If ENABLE_TIMER is not defined, returns 0.
    
    @ingroup magma_timer
*******************************************************************************/
static inline magma_timer_t timer_stop( magma_timer_t &t )
{
    #if defined(ENABLE_TIMER)
    t = magma_wtime() - t;
    return t;
    #else
    return 0;
    #endif
}


/***************************************************************************//**
    @param[in,out]
    t       On input, time when timer_start() was called.
            On output, set to (current time - start time).
    
    @param[in]
    queue  Queue to sync with, before getting time.

    @return t, to sum up times:
    
        magma_timer_t time, time_sum=0;
        for( ... ) {
            timer_start( time );
            ...do timed operations...
            time_sum += timer_stop( time );
        
            ...do other operations...
        }
    
    If ENABLE_TIMER is not defined, returns 0.
    
    @ingroup magma_timer
*******************************************************************************/
static inline magma_timer_t timer_sync_stop( magma_timer_t &t, magma_queue_t queue )
{
    #if defined(ENABLE_TIMER)
    magma_queue_sync( queue );
    t = magma_wtime() - t;
    return t;
    #else
    return 0;
    #endif
}


/***************************************************************************//**
    @param[out]
    flops   On output, set to current flop counter.
    
    Requires global gPAPI_flops_set to be setup by testing/magma_util.cpp
    Note that newer CPUs may not support flop counts; see
    https://icl.cs.utk.edu/projects/papi/wiki/PAPITopics:SandyFlops
    
    If ENABLE_TIMER and HAVE_PAPI are not both defined, does nothing.
    
    @ingroup magma_timer
*******************************************************************************/
static inline void flops_start( magma_flops_t &flops )
{
    #if defined(ENABLE_TIMER) && defined(HAVE_PAPI)
    PAPI_read( gPAPI_flops_set, &flops );
    #endif
}


/***************************************************************************//**
    @param[out]
    flops   On input, flop counter when flops_start() was called.
            On output, set to (current flop counter - start flop counter).
    
    @return flops, so you can sum up; see timer_stop().
    
    If ENABLE_TIMER and HAVE_PAPI are not both defined, returns 0.
    
    @ingroup magma_timer
*******************************************************************************/
static inline magma_flops_t flops_stop( magma_flops_t &flops )
{
    #if defined(ENABLE_TIMER) && defined(HAVE_PAPI)
    magma_flops_t end;
    PAPI_read( gPAPI_flops_set, &end );
    flops = end - flops;
    return flops;
    #else
    return 0;
    #endif
}


/***************************************************************************//**
    If ENABLE_TIMER is defined, same as printf;
    else does nothing (returns 0).
    
    @ingroup magma_timer
*******************************************************************************/
static inline int timer_printf( const char* format, ... )
    __attribute__((format(printf,1,2)));

static inline int timer_printf( const char* format, ... )
{
    int len = 0;
    #if defined(ENABLE_TIMER)
    va_list ap;
    va_start( ap, format );
    len = vprintf( format, ap );
    va_end( ap );
    #endif
    return len;
}


/***************************************************************************//**
    If ENABLE_TIMER is defined, same as fprintf;
    else does nothing (returns 0).
    
    @ingroup magma_timer
*******************************************************************************/
static inline int timer_fprintf( FILE* stream, const char* format, ... )
    __attribute__((format(printf,2,3)));

static inline int timer_fprintf( FILE* stream, const char* format, ... )
{
    int len = 0;
    #if defined(ENABLE_TIMER)
    va_list ap;
    va_start( ap, format );
    len = vfprintf( stream, format, ap );
    va_end( ap );
    #endif
    return len;
}


/***************************************************************************//**
    If ENABLE_TIMER is defined, same as snprintf;
    else does nothing (returns 0).
    
    @ingroup magma_timer
*******************************************************************************/
static inline int timer_snprintf( char* str, size_t size, const char* format, ... )
    __attribute__((format(printf,3,4)));

static inline int timer_snprintf( char* str, size_t size, const char* format, ... )
{
    int len = 0;
    #if defined(ENABLE_TIMER)
    va_list ap;
    va_start( ap, format );
    len = vsnprintf( str, size, format, ap );
    va_end( ap );
    #endif
    return len;
}

#endif        //  #ifndef MAGMA_TIMER_H