ARGOBOTS  dce6e727ffc4ca5b3ffc04cb9517c6689be51ec5
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  ABTI_UB_ASSERT(newtimer);
63 
64 #ifndef ABT_CONFIG_ENABLE_VER_20_API
65  *newtimer = ABT_TIMER_NULL;
66 #endif
67  ABTI_timer *p_newtimer;
68  int abt_errno = timer_alloc(&p_newtimer);
69  ABTI_CHECK_ERROR(abt_errno);
70 
71  *newtimer = ABTI_timer_get_handle(p_newtimer);
72  return ABT_SUCCESS;
73 }
74 
104 int ABT_timer_dup(ABT_timer timer, ABT_timer *newtimer)
105 {
106  ABTI_UB_ASSERT(newtimer);
107 
108 #ifndef ABT_CONFIG_ENABLE_VER_20_API
109  *newtimer = ABT_TIMER_NULL;
110 #endif
111  ABTI_timer *p_timer = ABTI_timer_get_ptr(timer);
112  ABTI_CHECK_NULL_TIMER_PTR(p_timer);
113 
114  ABTI_timer *p_newtimer;
115  int abt_errno = timer_alloc(&p_newtimer);
116  ABTI_CHECK_ERROR(abt_errno);
117 
118  memcpy(p_newtimer, p_timer, sizeof(ABTI_timer));
119  *newtimer = ABTI_timer_get_handle(p_newtimer);
120  return ABT_SUCCESS;
121 }
122 
145 {
146  ABTI_UB_ASSERT(timer);
147 
148  ABTI_timer *p_timer = ABTI_timer_get_ptr(*timer);
149  ABTI_CHECK_NULL_TIMER_PTR(p_timer);
150 
151  /* We use libc malloc/free for ABT_timer because ABTU_malloc/free might
152  * need the initialization of Argobots if they are not the same as libc
153  * malloc/free. This is to allow ABT_timer to be used irrespective of
154  * Argobots initialization. */
155  free(p_timer);
156  *timer = ABT_TIMER_NULL;
157  return ABT_SUCCESS;
158 }
159 
181 {
182  ABTI_timer *p_timer = ABTI_timer_get_ptr(timer);
183  ABTI_CHECK_NULL_TIMER_PTR(p_timer);
184 
185  ABTD_time_get(&p_timer->start);
186  return ABT_SUCCESS;
187 }
188 
210 {
211  ABTI_timer *p_timer = ABTI_timer_get_ptr(timer);
212  ABTI_CHECK_NULL_TIMER_PTR(p_timer);
213 
214  ABTD_time_get(&p_timer->end);
215  return ABT_SUCCESS;
216 }
217 
243 int ABT_timer_read(ABT_timer timer, double *secs)
244 {
245  ABTI_UB_ASSERT(secs);
246 
247  ABTI_timer *p_timer = ABTI_timer_get_ptr(timer);
248  ABTI_CHECK_NULL_TIMER_PTR(p_timer);
249 
250  double start = ABTD_time_read_sec(&p_timer->start);
251  double end = ABTD_time_read_sec(&p_timer->end);
252 
253  *secs = end - start;
254  return ABT_SUCCESS;
255 }
256 
283 int ABT_timer_stop_and_read(ABT_timer timer, double *secs)
284 {
285  ABTI_UB_ASSERT(secs);
286 
287  ABTI_timer *p_timer = ABTI_timer_get_ptr(timer);
288  ABTI_CHECK_NULL_TIMER_PTR(p_timer);
289 
290  ABTD_time_get(&p_timer->end);
291  double start = ABTD_time_read_sec(&p_timer->start);
292  double end = ABTD_time_read_sec(&p_timer->end);
293 
294  *secs = end - start;
295  return ABT_SUCCESS;
296 }
297 
324 int ABT_timer_stop_and_add(ABT_timer timer, double *secs)
325 {
326  ABTI_UB_ASSERT(secs);
327 
328  ABTI_timer *p_timer = ABTI_timer_get_ptr(timer);
329  ABTI_CHECK_NULL_TIMER_PTR(p_timer);
330 
331  ABTD_time_get(&p_timer->end);
332  double start = ABTD_time_read_sec(&p_timer->start);
333  double end = ABTD_time_read_sec(&p_timer->end);
334 
335  *secs += (end - start);
336  return ABT_SUCCESS;
337 }
338 
367 int ABT_timer_get_overhead(double *overhead)
368 {
369  ABTI_UB_ASSERT(overhead);
370 
371  int abt_errno;
372  ABT_timer h_timer;
373  int i;
374  const int iter = 5000;
375  double secs, sum = 0.0;
376 
377  abt_errno = ABT_timer_create(&h_timer);
378  ABTI_CHECK_ERROR(abt_errno);
379 
380  for (i = 0; i < iter; i++) {
381  ABT_timer_start(h_timer);
382  ABT_timer_stop(h_timer);
383  ABT_timer_read(h_timer, &secs);
384  sum += secs;
385  }
386 
387  abt_errno = ABT_timer_free(&h_timer);
388  ABTI_CHECK_ERROR(abt_errno);
389 
390  *overhead = sum / iter;
391  return ABT_SUCCESS;
392 }
393 
394 /*****************************************************************************/
395 /* Internal static functions */
396 /*****************************************************************************/
397 
398 ABTU_ret_err static int timer_alloc(ABTI_timer **pp_newtimer)
399 {
400  /* We use libc malloc/free for ABT_timer because ABTU_malloc/free might
401  * need the initialization of Argobots if they are not the same as libc
402  * malloc/free. This is to allow ABT_timer to be used irrespective of
403  * Argobots initialization. */
404  ABTI_timer *p_newtimer = (ABTI_timer *)malloc(sizeof(ABTI_timer));
405  ABTI_CHECK_TRUE(p_newtimer != NULL, ABT_ERR_MEM);
406 
407  *pp_newtimer = p_newtimer;
408  return ABT_SUCCESS;
409 }
ABT_get_wtime
double ABT_get_wtime(void)
Get elapsed wall clock time.
Definition: timer.c:28
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:144
ABT_timer_get_overhead
int ABT_timer_get_overhead(double *overhead)
Obtain an overhead time of using ABT_timer.
Definition: timer.c:367
ABT_timer_dup
int ABT_timer_dup(ABT_timer timer, ABT_timer *newtimer)
Duplicate a timer.
Definition: timer.c:104
ABT_timer_start
int ABT_timer_start(ABT_timer timer)
Start a timer.
Definition: timer.c:180
abti.h
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:1116
ABT_timer_stop
int ABT_timer_stop(ABT_timer timer)
Stop a timer.
Definition: timer.c:209
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:155
ABT_timer
struct ABT_timer_opaque * ABT_timer
Timer handle type.
Definition: abt.h:1036
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:324
ABT_timer_read
int ABT_timer_read(ABT_timer timer, double *secs)
Read the elapsed time of the timer.
Definition: timer.c:243
timer_alloc
static ABTU_ret_err int timer_alloc(ABTI_timer **pp_newtimer)
Definition: timer.c:398
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:283