ARGOBOTS  1.1
timer.c
Go to the documentation of this file.
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  * See COPYRIGHT in top-level directory.
4  */
5 
6 #include "abti.h"
7 
8 ABTU_ret_err static int timer_alloc(ABTI_timer **pp_newtimer);
9 
28 double ABT_get_wtime(void)
29 {
30  return ABTI_get_wtime();
31 }
32 
61 {
62 #ifndef ABT_CONFIG_ENABLE_VER_20_API
63  *newtimer = ABT_TIMER_NULL;
64 #endif
65  ABTI_timer *p_newtimer;
66  int abt_errno = timer_alloc(&p_newtimer);
67  ABTI_CHECK_ERROR(abt_errno);
68 
69  *newtimer = ABTI_timer_get_handle(p_newtimer);
70  return ABT_SUCCESS;
71 }
72 
102 int ABT_timer_dup(ABT_timer timer, ABT_timer *newtimer)
103 {
104 #ifndef ABT_CONFIG_ENABLE_VER_20_API
105  *newtimer = ABT_TIMER_NULL;
106 #endif
107  ABTI_timer *p_timer = ABTI_timer_get_ptr(timer);
108  ABTI_CHECK_NULL_TIMER_PTR(p_timer);
109 
110  ABTI_timer *p_newtimer;
111  int abt_errno = timer_alloc(&p_newtimer);
112  ABTI_CHECK_ERROR(abt_errno);
113 
114  memcpy(p_newtimer, p_timer, sizeof(ABTI_timer));
115  *newtimer = ABTI_timer_get_handle(p_newtimer);
116  return ABT_SUCCESS;
117 }
118 
141 {
142  ABTI_timer *p_timer = ABTI_timer_get_ptr(*timer);
143  ABTI_CHECK_NULL_TIMER_PTR(p_timer);
144 
145  /* We use libc malloc/free for ABT_timer because ABTU_malloc/free might
146  * need the initialization of Argobots if they are not the same as libc
147  * malloc/free. This is to allow ABT_timer to be used irrespective of
148  * Argobots initialization. */
149  free(p_timer);
150  *timer = ABT_TIMER_NULL;
151  return ABT_SUCCESS;
152 }
153 
175 {
176  ABTI_timer *p_timer = ABTI_timer_get_ptr(timer);
177  ABTI_CHECK_NULL_TIMER_PTR(p_timer);
178 
179  ABTD_time_get(&p_timer->start);
180  return ABT_SUCCESS;
181 }
182 
204 {
205  ABTI_timer *p_timer = ABTI_timer_get_ptr(timer);
206  ABTI_CHECK_NULL_TIMER_PTR(p_timer);
207 
208  ABTD_time_get(&p_timer->end);
209  return ABT_SUCCESS;
210 }
211 
237 int ABT_timer_read(ABT_timer timer, double *secs)
238 {
239  ABTI_timer *p_timer = ABTI_timer_get_ptr(timer);
240  ABTI_CHECK_NULL_TIMER_PTR(p_timer);
241 
242  double start = ABTD_time_read_sec(&p_timer->start);
243  double end = ABTD_time_read_sec(&p_timer->end);
244 
245  *secs = end - start;
246  return ABT_SUCCESS;
247 }
248 
275 int ABT_timer_stop_and_read(ABT_timer timer, double *secs)
276 {
277  ABTI_timer *p_timer = ABTI_timer_get_ptr(timer);
278  ABTI_CHECK_NULL_TIMER_PTR(p_timer);
279 
280  ABTD_time_get(&p_timer->end);
281  double start = ABTD_time_read_sec(&p_timer->start);
282  double end = ABTD_time_read_sec(&p_timer->end);
283 
284  *secs = end - start;
285  return ABT_SUCCESS;
286 }
287 
314 int ABT_timer_stop_and_add(ABT_timer timer, double *secs)
315 {
316  ABTI_timer *p_timer = ABTI_timer_get_ptr(timer);
317  ABTI_CHECK_NULL_TIMER_PTR(p_timer);
318 
319  ABTD_time_get(&p_timer->end);
320  double start = ABTD_time_read_sec(&p_timer->start);
321  double end = ABTD_time_read_sec(&p_timer->end);
322 
323  *secs += (end - start);
324  return ABT_SUCCESS;
325 }
326 
355 int ABT_timer_get_overhead(double *overhead)
356 {
357  int abt_errno;
358  ABT_timer h_timer;
359  int i;
360  const int iter = 5000;
361  double secs, sum = 0.0;
362 
363  abt_errno = ABT_timer_create(&h_timer);
364  ABTI_CHECK_ERROR(abt_errno);
365 
366  for (i = 0; i < iter; i++) {
367  ABT_timer_start(h_timer);
368  ABT_timer_stop(h_timer);
369  ABT_timer_read(h_timer, &secs);
370  sum += secs;
371  }
372 
373  abt_errno = ABT_timer_free(&h_timer);
374  ABTI_CHECK_ERROR(abt_errno);
375 
376  *overhead = sum / iter;
377  return ABT_SUCCESS;
378 }
379 
380 /*****************************************************************************/
381 /* Internal static functions */
382 /*****************************************************************************/
383 
384 ABTU_ret_err static int timer_alloc(ABTI_timer **pp_newtimer)
385 {
386  /* We use libc malloc/free for ABT_timer because ABTU_malloc/free might
387  * need the initialization of Argobots if they are not the same as libc
388  * malloc/free. This is to allow ABT_timer to be used irrespective of
389  * Argobots initialization. */
390  ABTI_timer *p_newtimer = (ABTI_timer *)malloc(sizeof(ABTI_timer));
391  ABTI_CHECK_TRUE(p_newtimer != NULL, ABT_ERR_MEM);
392 
393  *pp_newtimer = p_newtimer;
394  return ABT_SUCCESS;
395 }
ABT_get_wtime
double ABT_get_wtime(void)
Get elapsed wall clock time.
Definition: timer.c:28
ABTI_timer_get_ptr
static ABTI_timer * ABTI_timer_get_ptr(ABT_timer timer)
Definition: abti_timer.h:18
ABT_timer_create
int ABT_timer_create(ABT_timer *newtimer)
Create a new timer.
Definition: timer.c:60
ABT_timer_free
int ABT_timer_free(ABT_timer *timer)
Free a timer.
Definition: timer.c:140
ABTI_timer_get_handle
static ABT_timer ABTI_timer_get_handle(ABTI_timer *p_timer)
Definition: abti_timer.h:33
ABTI_CHECK_ERROR
#define ABTI_CHECK_ERROR(abt_errno)
Definition: abti_error.h:120
ABTI_timer::end
ABTD_time end
Definition: abti.h:485
ABT_timer_get_overhead
int ABT_timer_get_overhead(double *overhead)
Obtain an overhead time of using ABT_timer.
Definition: timer.c:355
ABT_timer_dup
int ABT_timer_dup(ABT_timer timer, ABT_timer *newtimer)
Duplicate a timer.
Definition: timer.c:102
ABT_timer_start
int ABT_timer_start(ABT_timer timer)
Start a timer.
Definition: timer.c:174
abti.h
ABTI_timer
Definition: abti.h:483
ABTI_timer::start
ABTD_time start
Definition: abti.h:484
ABT_ERR_MEM
#define ABT_ERR_MEM
Error code: Memory allocation failure.
Definition: abt.h:104
ABT_TIMER_NULL
#define ABT_TIMER_NULL
Definition: abt.h:1073
ABT_timer_stop
int ABT_timer_stop(ABT_timer timer)
Stop a timer.
Definition: timer.c:203
ABT_SUCCESS
#define ABT_SUCCESS
Error code: the routine returns successfully.
Definition: abt.h:92
ABTU_ret_err
#define ABTU_ret_err
Definition: abtu.h:146
ABT_timer
struct ABT_timer_opaque * ABT_timer
Timer handle type.
Definition: abt.h:994
ABTD_time_read_sec
double ABTD_time_read_sec(ABTD_time *p_time)
Definition: abtd_time.c:35
ABTI_CHECK_NULL_TIMER_PTR
#define ABTI_CHECK_NULL_TIMER_PTR(p)
Definition: abti_error.h:312
ABTD_time_get
void ABTD_time_get(ABTD_time *p_time)
Definition: abtd_time.c:21
ABT_timer_stop_and_add
int ABT_timer_stop_and_add(ABT_timer timer, double *secs)
Stop a timer and add an elapsed time of a timer.
Definition: timer.c:314
ABTI_CHECK_TRUE
#define ABTI_CHECK_TRUE(cond, abt_errno)
Definition: abti_error.h:130
ABT_timer_read
int ABT_timer_read(ABT_timer timer, double *secs)
Read the elapsed time of the timer.
Definition: timer.c:237
timer_alloc
static ABTU_ret_err int timer_alloc(ABTI_timer **pp_newtimer)
Definition: timer.c:384
ABTI_get_wtime
static double ABTI_get_wtime(void)
Definition: abti_timer.h:11
ABT_timer_stop_and_read
int ABT_timer_stop_and_read(ABT_timer timer, double *secs)
Stop a timer and read an elapsed time of a timer.
Definition: timer.c:275