source: trunk/sys/libupc/include/upc_timers.h @ 278

Last change on this file since 278 was 1, checked in by alain, 8 years ago

First import

File size: 2.3 KB
Line 
1/* http://threads.hpcl.gwu.edu/sites/npb-upc/wiki */
2
3#ifndef UPC_TIMERS_H
4#define UPC_TIMERS_H
5
6#include <sys/time.h>
7#include <stdio.h>
8#include <unistd.h>
9
10#define NB_TIMERS 64
11
12static double UPC_Wtime();
13
14double upc_timers_start[NB_TIMERS] = {0};
15double upc_timers_elapsed[NB_TIMERS] = {0};
16int upc_timers_status[NB_TIMERS] = {0};
17#define MYTHREAD 0
18#define UPC_TIMER_STOPPED 0
19#define UPC_TIMER_RUNNING 1
20
21#ifdef UPC_TIMERS_DISABLE
22#define TIMER_START(n) { }while(0)
23#define TIMER_STOP(n) { }while(0)
24#else
25#define TIMER_START(n) timer_start(n)
26#define TIMER_STOP(n) timer_stop(n)
27#endif
28
29#ifdef _ALMOS_
30static double UPC_Wtime(){
31        return (double) clock();
32}
33#else  /* _ALMOS_ */
34#ifdef USE_MONOTONIC_CLOCK
35#include <time.h>
36static double UPC_Wtime(){
37    struct timespec sampletime;
38    double time;
39
40    // TODO: if you have Linux ver >= 2.6.28, you can use CLOCK_MONOTONIC_RAW
41    if( clock_gettime( CLOCK_MONOTONIC, &sampletime) != 0 )
42        fprintf(stderr, "Warning: Unable to read clock.\n");
43
44    time = sampletime.tv_sec + (sampletime.tv_nsec / 1000000000.);
45
46    return time;
47}
48#else  /* USE_MONOTONIC_CLOCK */
49static double UPC_Wtime(){
50    struct timeval sampletime;
51    double time;
52
53    gettimeofday( &sampletime, NULL);
54    time = sampletime.tv_sec + (sampletime.tv_usec / 1000000.);
55
56    return time;
57}
58#endif /* USE_MONOTONIC_CLOCK */
59#endif /* _ALMOS_ */
60
61double elapsed_time(){
62    return UPC_Wtime();
63}
64
65void timer_clear(int n){
66    if( upc_timers_status[n] == UPC_TIMER_RUNNING )
67        fprintf(stderr, "%d: Clearing a running timer(%d)\n", MYTHREAD, n);
68
69    upc_timers_elapsed[n] = 0.0;
70}
71
72void timer_start(int n){
73    if( upc_timers_status[n] == UPC_TIMER_RUNNING )
74        fprintf(stderr, "%d: Starting a running timer(%d)\n", MYTHREAD, n);
75
76    upc_timers_start[n] = UPC_Wtime();
77    upc_timers_status[n] = UPC_TIMER_RUNNING;
78}
79
80void timer_stop(int n){
81    double t;
82
83    if( upc_timers_status[n] != UPC_TIMER_RUNNING )
84        fprintf(stderr, "%d: Stopping a non running timer(%d)\n", MYTHREAD, n);
85
86    t = UPC_Wtime()- upc_timers_start[n];
87    upc_timers_elapsed[n] += t;
88    upc_timers_status[n] = UPC_TIMER_STOPPED;
89}
90
91double timer_read(int n){
92    if( upc_timers_status[n] == UPC_TIMER_RUNNING )
93        fprintf(stderr, "%d: Reading a running timer(%d)\n", MYTHREAD, n);
94
95    return upc_timers_elapsed[n];
96}
97
98#endif
99
Note: See TracBrowser for help on using the repository browser.