ARGOBOTS
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 int ABTI_timer_create(ABTI_timer **pp_newtimer);
9 
24 double ABT_get_wtime(void)
25 {
26  return ABTI_get_wtime();
27 }
28 
43 {
44  int abt_errno = ABT_SUCCESS;
45  ABTI_timer *p_newtimer;
46 
47  abt_errno = ABTI_timer_create(&p_newtimer);
48  ABTI_CHECK_ERROR(abt_errno);
49 
50  *newtimer = ABTI_timer_get_handle(p_newtimer);
51 
52 fn_exit:
53  return abt_errno;
54 
55 fn_fail:
56  *newtimer = ABT_TIMER_NULL;
57  HANDLE_ERROR_FUNC_WITH_CODE(abt_errno);
58  goto fn_exit;
59 }
60 
76 int ABT_timer_dup(ABT_timer timer, ABT_timer *newtimer)
77 {
78  int abt_errno = ABT_SUCCESS;
79  ABTI_timer *p_timer = ABTI_timer_get_ptr(timer);
80  ABTI_CHECK_NULL_TIMER_PTR(p_timer);
81 
82  ABTI_timer *p_newtimer;
83 
84  abt_errno = ABTI_timer_create(&p_newtimer);
85  ABTI_CHECK_ERROR(abt_errno);
86 
87  memcpy(p_newtimer, p_timer, sizeof(ABTI_timer));
88 
89  *newtimer = ABTI_timer_get_handle(p_newtimer);
90 
91 fn_exit:
92  return abt_errno;
93 
94 fn_fail:
95  HANDLE_ERROR_FUNC_WITH_CODE(abt_errno);
96  goto fn_exit;
97 }
98 
114 {
115  int abt_errno = ABT_SUCCESS;
116  ABTI_timer *p_timer = ABTI_timer_get_ptr(*timer);
117  ABTI_CHECK_NULL_TIMER_PTR(p_timer);
118 
119  /* We use libc malloc/free for ABT_timer because ABTU_malloc/free might
120  * need the initialization of Argobots if they are not the same as libc
121  * malloc/free. This is to allow ABT_timer to be used irrespective of
122  * Argobots initialization. */
123  free(p_timer);
124  *timer = ABT_TIMER_NULL;
125 
126 fn_exit:
127  return abt_errno;
128 
129 fn_fail:
130  HANDLE_ERROR_FUNC_WITH_CODE(abt_errno);
131  goto fn_exit;
132 }
133 
147 {
148  int abt_errno = ABT_SUCCESS;
149  ABTI_timer *p_timer = ABTI_timer_get_ptr(timer);
150  ABTI_CHECK_NULL_TIMER_PTR(p_timer);
151 
152  ABTD_time_get(&p_timer->start);
153 
154 fn_exit:
155  return abt_errno;
156 
157 fn_fail:
158  HANDLE_ERROR_FUNC_WITH_CODE(abt_errno);
159  goto fn_exit;
160 }
161 
175 {
176  int abt_errno = ABT_SUCCESS;
177  ABTI_timer *p_timer = ABTI_timer_get_ptr(timer);
178  ABTI_CHECK_NULL_TIMER_PTR(p_timer);
179 
180  ABTD_time_get(&p_timer->end);
181 
182 fn_exit:
183  return abt_errno;
184 
185 fn_fail:
186  HANDLE_ERROR_FUNC_WITH_CODE(abt_errno);
187  goto fn_exit;
188 }
189 
204 int ABT_timer_read(ABT_timer timer, double *secs)
205 {
206  int abt_errno = ABT_SUCCESS;
207  ABTI_timer *p_timer = ABTI_timer_get_ptr(timer);
208  ABTI_CHECK_NULL_TIMER_PTR(p_timer);
209 
210  double start, end;
211 
212  start = ABTD_time_read_sec(&p_timer->start);
213  end = ABTD_time_read_sec(&p_timer->end);
214 
215  *secs = end - start;
216 
217 fn_exit:
218  return abt_errno;
219 
220 fn_fail:
221  HANDLE_ERROR_FUNC_WITH_CODE(abt_errno);
222  goto fn_exit;
223 }
224 
240 int ABT_timer_stop_and_read(ABT_timer timer, double *secs)
241 {
242  int abt_errno = ABT_SUCCESS;
243  ABTI_timer *p_timer = ABTI_timer_get_ptr(timer);
244  ABTI_CHECK_NULL_TIMER_PTR(p_timer);
245 
246  double start, end;
247 
248  ABTD_time_get(&p_timer->end);
249  start = ABTD_time_read_sec(&p_timer->start);
250  end = ABTD_time_read_sec(&p_timer->end);
251 
252  *secs = end - start;
253 
254 fn_exit:
255  return abt_errno;
256 
257 fn_fail:
258  HANDLE_ERROR_FUNC_WITH_CODE(abt_errno);
259  goto fn_exit;
260 }
261 
277 int ABT_timer_stop_and_add(ABT_timer timer, double *secs)
278 {
279  int abt_errno = ABT_SUCCESS;
280  ABTI_timer *p_timer = ABTI_timer_get_ptr(timer);
281  ABTI_CHECK_NULL_TIMER_PTR(p_timer);
282 
283  double start, end;
284 
285  ABTD_time_get(&p_timer->end);
286  start = ABTD_time_read_sec(&p_timer->start);
287  end = ABTD_time_read_sec(&p_timer->end);
288 
289  *secs += (end - start);
290 
291 fn_exit:
292  return abt_errno;
293 
294 fn_fail:
295  HANDLE_ERROR_FUNC_WITH_CODE(abt_errno);
296  goto fn_exit;
297 }
298 
312 int ABT_timer_get_overhead(double *overhead)
313 {
314  int abt_errno = ABT_SUCCESS;
315  ABT_timer h_timer;
316  int i;
317  const int iter = 5000;
318  double secs, sum = 0.0;
319 
320  abt_errno = ABT_timer_create(&h_timer);
321  ABTI_CHECK_ERROR(abt_errno);
322 
323  for (i = 0; i < iter; i++) {
324  ABT_timer_start(h_timer);
325  ABT_timer_stop(h_timer);
326  ABT_timer_read(h_timer, &secs);
327  sum += secs;
328  }
329 
330  abt_errno = ABT_timer_free(&h_timer);
331  ABTI_CHECK_ERROR(abt_errno);
332 
333  *overhead = sum / iter;
334 
335 fn_exit:
336  return abt_errno;
337 
338 fn_fail:
339  HANDLE_ERROR_FUNC_WITH_CODE(abt_errno);
340  goto fn_exit;
341 }
342 
343 /*****************************************************************************/
344 /* Internal functions */
345 /*****************************************************************************/
346 
347 int ABTI_timer_create(ABTI_timer **pp_newtimer)
348 {
349  int abt_errno = ABT_SUCCESS;
350 
351  /* We use libc malloc/free for ABT_timer because ABTU_malloc/free might
352  * need the initialization of Argobots if they are not the same as libc
353  * malloc/free. This is to allow ABT_timer to be used irrespective of
354  * Argobots initialization. */
355  ABTI_timer *p_newtimer = (ABTI_timer *)malloc(sizeof(ABTI_timer));
356  ABTI_CHECK_TRUE(p_newtimer != NULL, ABT_ERR_MEM);
357 
358  *pp_newtimer = p_newtimer;
359 
360 fn_exit:
361  return abt_errno;
362 
363 fn_fail:
364  HANDLE_ERROR_FUNC_WITH_CODE(abt_errno);
365  goto fn_exit;
366 }
int ABT_timer_stop(ABT_timer timer)
Stop the timer.
Definition: timer.c:174
int ABT_timer_stop_and_add(ABT_timer timer, double *secs)
Stop the timer and add the elapsed time of the timer.
Definition: timer.c:277
int ABT_timer_dup(ABT_timer timer, ABT_timer *newtimer)
Duplicate the timer.
Definition: timer.c:76
#define ABT_TIMER_NULL
Definition: abt.h:355
int ABT_timer_read(ABT_timer timer, double *secs)
Read the elapsed time of the timer.
Definition: timer.c:204
int ABT_timer_get_overhead(double *overhead)
Obtain the overhead time of using ABT_timer.
Definition: timer.c:312
#define HANDLE_ERROR_FUNC_WITH_CODE(n)
Definition: abti_error.h:241
int ABT_timer_create(ABT_timer *newtimer)
Create a new timer.
Definition: timer.c:42
int ABT_timer_free(ABT_timer *timer)
Free the timer object.
Definition: timer.c:113
#define ABT_ERR_MEM
Definition: abt.h:66
#define ABT_SUCCESS
Definition: abt.h:64
int ABT_timer_stop_and_read(ABT_timer timer, double *secs)
Stop the timer and read the elapsed time of the timer.
Definition: timer.c:240
int ABT_timer_start(ABT_timer timer)
Start the timer.
Definition: timer.c:146
double ABT_get_wtime(void)
Get elapsed wall clock time.
Definition: timer.c:24
struct ABT_timer_opaque * ABT_timer
Definition: abt.h:307