Merge pull request #45 from devinamatthews/high_prec_timers

Use clock_gettime(CLOCK_MONOTONIC) and mach_absolute_time instead of gettimeofday
This commit is contained in:
Field G. Van Zee
2016-03-04 17:26:58 -06:00
4 changed files with 96 additions and 46 deletions

View File

@@ -159,6 +159,16 @@ endif
#
# --- Append OS-specific libraries to LDFLAGS ----------------------------------
#
ifeq ($(OS_NAME),Linux)
LDFLAGS += -lrt
endif
#
# --- Main target variable definitions -----------------------------------------
#

View File

@@ -39,6 +39,9 @@ CONFIG_MK_INCLUDED := yes
# The name of the configuration sub-directory.
CONFIG_NAME := @config_name@
# The operating system name, which should be either 'Linux' or 'Darwin'.
OS_NAME := $(shell uname -s)
# The directory path to the top level of the source distribution.
DIST_PATH := @dist_path@

View File

@@ -65,60 +65,74 @@ double bli_clock_min_diff( double time_min, double time_start )
return time_min;
}
// --- Begin Linux build definitions -------------------------------------------
#ifndef BLIS_ENABLE_WINDOWS_BUILD
#if BLIS_OS_WINDOWS
// --- Begin Windows build definitions -----------------------------------------
double bli_clock_helper()
{
double the_time, norm_sec;
struct timeval tv;
LARGE_INTEGER clock_freq = {0};
LARGE_INTEGER clock_val;
BOOL r_val;
gettimeofday( &tv, NULL );
r_val = QueryPerformanceFrequency( &clock_freq );
if ( gtod_ref_time_sec == 0.0 )
gtod_ref_time_sec = ( double ) tv.tv_sec;
if ( r_val == 0 )
{
bli_print_msg( "QueryPerformanceFrequency() failed", __FILE__, __LINE__ );
bli_abort();
}
norm_sec = ( double ) tv.tv_sec - gtod_ref_time_sec;
r_val = QueryPerformanceCounter( &clock_val );
the_time = norm_sec + tv.tv_usec * 1.0e-6;
if ( r_val == 0 )
{
bli_print_msg( "QueryPerformanceCounter() failed", __FILE__, __LINE__ );
bli_abort();
}
return the_time;
return ( ( double) clock_val.QuadPart / ( double) clock_freq.QuadPart );
}
// --- End Windows build definitions -------------------------------------------
#elif BLIS_OS_OSX
// --- Begin OSX build definitions -------------------------------------------
double bli_clock_helper()
{
mach_timebase_info_data_t timebase;
mach_timebase_info( &timebase );
uint64_t nsec = mach_absolute_time();
double the_time = (double) nsec * 1.0e-9 * timebase.numer / timebase.denom;
if ( gtod_ref_time_sec == 0.0 )
gtod_ref_time_sec = the_time;
return the_time - gtod_ref_time_sec;
}
// --- End OSX build definitions ---------------------------------------------
#else
// --- Begin Linux build definitions -------------------------------------------
double bli_clock_helper()
{
double the_time, norm_sec;
struct timespec ts;
clock_gettime( CLOCK_MONOTONIC, &ts );
if ( gtod_ref_time_sec == 0.0 )
gtod_ref_time_sec = ( double ) ts.tv_sec;
norm_sec = ( double ) ts.tv_sec - gtod_ref_time_sec;
the_time = norm_sec + ts.tv_nsec * 1.0e-9;
return the_time;
}
// --- End Linux build definitions ---------------------------------------------
#else
// --- Begin Windows build definitions -----------------------------------------
#define WIN32_LEAN_AND_MEAN
#define VC_EXTRALEAN
#include <windows.h>
double bli_clock_helper()
{
LARGE_INTEGER clock_freq = {0};
LARGE_INTEGER clock_val;
BOOL r_val;
r_val = QueryPerformanceFrequency( &clock_freq );
if ( r_val == 0 )
{
bli_print_msg( "QueryPerformanceFrequency() failed", __FILE__, __LINE__ );
bli_abort();
}
r_val = QueryPerformanceCounter( &clock_val );
if ( r_val == 0 )
{
bli_print_msg( "QueryPerformanceCounter() failed", __FILE__, __LINE__ );
bli_abort();
}
return ( ( double) clock_val.QuadPart / ( double) clock_freq.QuadPart );
}
#endif
// --- End Windows build definitions -------------------------------------------

View File

@@ -40,9 +40,30 @@
#include <math.h>
#include <string.h>
#ifdef BLIS_ENABLE_WINDOWS_BUILD
#if defined(_WIN32) || defined(__CYGWIN__)
#define BLIS_OS_WINDOWS 1
#elif defined(__APPLE__) || defined(__MACH__)
#define BLIS_OS_OSX 1
#elif defined(__ANDROID__)
#define BLIS_OS_ANDROID 1
#elif defined(__linux__)
#define BLIS_OS_LINUX 1
#elif defined(__bgq__)
#define BLIS_OS_BGQ 1
#elif defined(__bg__)
#define BLIS_OS_BGP 1
#elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || \
defined(__bsdi__) || defined(__DragonFly__)
#define BLIS_OS_BSD 1
#else
#error "Cannot determine operating system"
#endif
#if BLIS_OS_WINDOWS
// Include Windows header file.
#define WIN32_LEAN_AND_MEAN
#define VC_EXTRALEAN
#include <windows.h>
// Undefine attribute specifiers in Windows.
@@ -54,8 +75,10 @@
#endif
// gettimeofday() needs this.
#ifdef BLIS_ENABLE_WINDOWS_BUILD
#if BLIS_OS_WINDOWS
#include <time.h>
#elif BLIS_OS_OSX
#include <mach/mach_time.h>
#else
#include <sys/time.h>
#include <time.h>