root/ompi/mpi/c/wtime.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. MPI_Wtime

   1 /*
   2  * Copyright (c) 2004-2007 The Trustees of Indiana University and Indiana
   3  *                         University Research and Technology
   4  *                         Corporation.  All rights reserved.
   5  * Copyright (c) 2004-2018 The University of Tennessee and The University
   6  *                         of Tennessee Research Foundation.  All rights
   7  *                         reserved.
   8  * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
   9  *                         University of Stuttgart.  All rights reserved.
  10  * Copyright (c) 2004-2005 The Regents of the University of California.
  11  *                         All rights reserved.
  12  * Copyright (c) 2006-2014 Cisco Systems, Inc.  All rights reserved.
  13  * Copyright (c) 2015      Research Organization for Information Science
  14  *                         and Technology (RIST). All rights reserved.
  15  * Copyright (c) 2017      IBM Corporation.  All rights reserved.
  16  * Copyright (c) 2017      Los Alamos National Security, LLC. All rights
  17  *                         reserved.
  18  * $COPYRIGHT$
  19  *
  20  * Additional copyrights may follow
  21  *
  22  * $HEADER$
  23  */
  24 #include "ompi_config.h"
  25 
  26 #ifdef HAVE_SYS_TIME_H
  27 #include <sys/time.h>
  28 #endif
  29 #include <stdio.h>
  30 #ifdef HAVE_TIME_H
  31 #include <time.h>
  32 #endif  /* HAVE_TIME_H */
  33 
  34 #include MCA_timer_IMPLEMENTATION_HEADER
  35 #include "ompi/mpi/c/bindings.h"
  36 #include "ompi/runtime/mpiruntime.h"
  37 #include "ompi/runtime/ompi_spc.h"
  38 
  39 #if OMPI_BUILD_MPI_PROFILING
  40 #if OPAL_HAVE_WEAK_SYMBOLS
  41 #pragma weak MPI_Wtime = PMPI_Wtime
  42 #endif
  43 #define MPI_Wtime PMPI_Wtime
  44 /**
  45  * Have a base time set on the first call to wtime, to improve the range
  46  * and accuracy of the user visible timer.
  47  * More info: https://github.com/mpi-forum/mpi-issues/issues/77#issuecomment-369663119
  48  */
  49 #if defined(__linux__) && OPAL_HAVE_CLOCK_GETTIME
  50 struct timespec ompi_wtime_time_origin = {.tv_sec = 0};
  51 #else
  52 struct timeval ompi_wtime_time_origin = {.tv_sec = 0};
  53 #endif
  54 #else  /* OMPI_BUILD_MPI_PROFILING */
  55 #if defined(__linux__) && OPAL_HAVE_CLOCK_GETTIME
  56 extern struct timespec ompi_wtime_time_origin;
  57 #else
  58 extern struct timeval ompi_wtime_time_origin;
  59 #endif
  60 #endif
  61 
  62 double MPI_Wtime(void)
  63 {
  64     double wtime;
  65 
  66     SPC_RECORD(OMPI_SPC_WTIME, 1);
  67 
  68     /*
  69      * See https://github.com/open-mpi/ompi/issues/3003 to find out
  70      * what's happening here.
  71      */
  72 #if 0
  73 #if OPAL_TIMER_CYCLE_NATIVE
  74     wtime = ((double) opal_timer_base_get_cycles()) / opal_timer_base_get_freq();
  75 #elif OPAL_TIMER_USEC_NATIVE
  76     wtime = ((double) opal_timer_base_get_usec()) / 1000000.0;
  77 #endif
  78 #else
  79 #if defined(__linux__) && OPAL_HAVE_CLOCK_GETTIME
  80     struct timespec tp;
  81     (void) clock_gettime(CLOCK_MONOTONIC, &tp);
  82     if( OPAL_UNLIKELY(0 == ompi_wtime_time_origin.tv_sec) ) {
  83         ompi_wtime_time_origin = tp;
  84     }
  85     wtime  = (double)(tp.tv_nsec - ompi_wtime_time_origin.tv_nsec)/1.0e+9;
  86     wtime += (tp.tv_sec - ompi_wtime_time_origin.tv_sec);
  87 #else
  88     /* Fall back to gettimeofday() if we have nothing else */
  89     struct timeval tv;
  90     gettimeofday(&tv, NULL);
  91     if( OPAL_UNLIKELY(0 == ompi_wtime_time_origin.tv_sec) ) {
  92         ompi_wtime_time_origin = tv;
  93     }
  94     wtime  = (double)(tv.tv_usec - ompi_wtime_time_origin.tv_usec) / 1.0e+6;
  95     wtime += (tv.tv_sec - ompi_wtime_time_origin.tv_sec);
  96 #endif
  97 #endif
  98 
  99     OPAL_CR_NOOP_PROGRESS();
 100 
 101     return wtime;
 102 }

/* [<][>][^][v][top][bottom][index][help] */