LCOV - code coverage report
Current view: top level - src/utility - LAGraph_WallClockTime.c (source / functions) Hit Total Coverage
Test: LAGraph code coverage report. Commit id: cc56ed4. Current time (UTC): 2024-08-30T17:14:30Z Lines: 6 6 100.0 %
Date: 2024-08-30 17:16:41 Functions: 1 1 100.0 %

          Line data    Source code
       1             : //------------------------------------------------------------------------------
       2             : // LAGraph_WallClockTime: return the current wall clock time
       3             : //------------------------------------------------------------------------------
       4             : 
       5             : // LAGraph, (c) 2019-2022 by The LAGraph Contributors, All Rights Reserved.
       6             : // SPDX-License-Identifier: BSD-2-Clause
       7             : //
       8             : // For additional details (including references to third party source code and
       9             : // other files) see the LICENSE file or contact permission@sei.cmu.edu. See
      10             : // Contributors.txt for a full list of contributors. Created, in part, with
      11             : // funding and support from the U.S. Government (see Acknowledgments.txt file).
      12             : // DM22-0790
      13             : 
      14             : // Contributed by Timothy A. Davis, Texas A&M University
      15             : 
      16             : //------------------------------------------------------------------------------
      17             : 
      18             : // Unlike all other LAGraph functions, this function does not return an error
      19             : // code as an int, nor does it have a char *msg parameter for error messages.
      20             : // It simply returns the current wall clock time, as a double value, indicating
      21             : // the amount of time passed in seconds since some fixed point in the past.
      22             : 
      23             : // Example usage:
      24             : 
      25             : /*
      26             :     double t1 = LAGraph_WallClockTime ( ) ;
      27             : 
      28             :     // ... do stuff
      29             :     double t2 = LAGraph_WallClockTime ( ) ;
      30             :     printf ("time to 'do stuff' : %g (seconds)\n', t2 - t1) ;
      31             :     // ... more stuff
      32             :     double t3 = LAGraph_WallClockTime ( ) ;
      33             :     printf ("time to 'do stuff' and 'more stuff': %g (seconds)\n', t3 - t1) ;
      34             : */
      35             : 
      36             : #include "LG_internal.h"
      37             : 
      38             : #if !defined ( _OPENMP )
      39             :     #include <time.h>
      40             :     #if defined ( __linux__ ) || defined ( __GNU__ )
      41             :         #include <sys/time.h>
      42             :     #endif
      43             :     #if defined ( __MACH__ ) && defined ( __APPLE__ )
      44             :         #include <mach/clock.h>
      45             :         #include <mach/mach.h>
      46             :     #endif
      47             : #endif
      48             : 
      49        3383 : double LAGraph_WallClockTime (void)
      50             : {
      51        3383 :     double t_wallclock = 0 ;
      52             : 
      53             :     #if defined ( _OPENMP )
      54             : 
      55             :         // OpenMP is available; use the OpenMP timer function
      56             :         t_wallclock = omp_get_wtime ( ) ;
      57             : 
      58             :     #elif defined ( __linux__ )
      59             : 
      60             :         // Linux has a very low resolution clock() function, so use the high
      61             :         // resolution clock_gettime instead.  May require -lrt
      62             :         struct timespec t ;
      63        3383 :         int e = clock_gettime (CLOCK_MONOTONIC, &t) ;
      64        3383 :         if (e == 0)
      65             :         {
      66        3383 :             t_wallclock = (double) t.tv_sec + 1e-9 * ((double) t.tv_nsec) ;
      67             :         }
      68             : 
      69             :     #elif defined ( __MACH__ )
      70             : 
      71             :         // Mac OSX
      72             :         clock_serv_t cclock ;
      73             :         mach_timespec_t t ;
      74             :         host_get_clock_service (mach_host_self ( ), SYSTEM_CLOCK, &cclock) ;
      75             :         clock_get_time (cclock, &t) ;
      76             :         mach_port_deallocate (mach_task_self ( ), cclock) ;
      77             :         t_wallclock = (double) t.tv_sec + 1e-9 * ((double) t.tv_nsec) ;
      78             : 
      79             :     #else
      80             : 
      81             :         // The ANSI C11 clock() function is used instead.  This gives the
      82             :         // processor time, not the wallclock time, and it might have low
      83             :         // resolution.  It returns the time since some unspecified fixed time
      84             :         // in the past, as a clock_t integer.  The clock ticks per second are
      85             :         // given by CLOCKS_PER_SEC.  In Mac OSX this is a very high resolution
      86             :         // clock, and clock ( ) is faster than clock_get_time (...) ;
      87             :         clock_t t = clock ( ) ;
      88             :         t_wallclock = ((double) t) / ((double) CLOCKS_PER_SEC) ;
      89             : 
      90             :     #endif
      91             : 
      92        3383 :     return (t_wallclock) ;
      93             : }

Generated by: LCOV version 1.14