1 /*
2 
3    BLIS
4    An object-based framework for developing high-performance BLAS-like
5    libraries.
6 
7    Copyright (C) 2014, The University of Texas at Austin
8    Copyright (C) 2018 - 2019, Advanced Micro Devices, Inc.
9 
10    Redistribution and use in source and binary forms, with or without
11    modification, are permitted provided that the following conditions are
12    met:
13     - Redistributions of source code must retain the above copyright
14       notice, this list of conditions and the following disclaimer.
15     - Redistributions in binary form must reproduce the above copyright
16       notice, this list of conditions and the following disclaimer in the
17       documentation and/or other materials provided with the distribution.
18     - Neither the name(s) of the copyright holder(s) nor the names of its
19       contributors may be used to endorse or promote products derived
20       from this software without specific prior written permission.
21 
22    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25    A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26    HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28    LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 
34 */
35 
36 #include "blis.h"
37 
38 static double gtod_ref_time_sec = 0.0;
39 
bli_clock(void)40 double bli_clock( void )
41 {
42 	return bli_clock_helper();
43 }
44 
bli_clock_min_diff(double time_min,double time_start)45 double bli_clock_min_diff( double time_min, double time_start )
46 {
47 	double time_min_prev;
48 	double time_diff;
49 
50 	// Save the old value.
51 	time_min_prev = time_min;
52 
53 	time_diff = bli_clock() - time_start;
54 
55 	time_min = bli_fmin( time_min, time_diff );
56 
57 	// Assume that anything:
58 	// - under or equal to zero,
59 	// - under a nanosecond
60 	// is actually garbled due to the clocks being taken too closely together.
61 	if      ( time_min <= 0.0    ) time_min = time_min_prev;
62 	else if ( time_min <  1.0e-9 ) time_min = time_min_prev;
63 
64 	return time_min;
65 }
66 
67 #ifdef BLIS_DISABLE_SYSTEM
68 // --- Begin systemless definitions --------------------------------------------
69 
bli_clock_helper()70 double bli_clock_helper()
71 {
72     return 0.0;
73 }
74 
75 // --- End systemless definitions ----------------------------------------------
76 #else
77 // --- Begin system definitions ------------------------------------------------
78 
79 #if BLIS_OS_WINDOWS
80 // --- Begin Windows build definitions -----------------------------------------
81 
bli_clock_helper()82 double bli_clock_helper()
83 {
84     LARGE_INTEGER clock_freq = {0};
85     LARGE_INTEGER clock_val;
86     BOOL          r_val;
87 
88     r_val = QueryPerformanceFrequency( &clock_freq );
89 
90     if ( r_val == 0 )
91     {
92         bli_print_msg( "QueryPerformanceFrequency() failed", __FILE__, __LINE__ );
93         bli_abort();
94     }
95 
96     r_val = QueryPerformanceCounter( &clock_val );
97 
98     if ( r_val == 0 )
99     {
100         bli_print_msg( "QueryPerformanceCounter() failed", __FILE__, __LINE__ );
101         bli_abort();
102     }
103 
104     return ( ( double) clock_val.QuadPart / ( double) clock_freq.QuadPart );
105 }
106 
107 // --- End Windows build definitions -------------------------------------------
108 #elif BLIS_OS_OSX
109 // --- Begin OSX build definitions -------------------------------------------
110 
bli_clock_helper()111 double bli_clock_helper()
112 {
113     mach_timebase_info_data_t timebase;
114     mach_timebase_info( &timebase );
115 
116     uint64_t nsec = mach_absolute_time();
117 
118     double the_time = (double) nsec * 1.0e-9 * timebase.numer / timebase.denom;
119 
120     if ( gtod_ref_time_sec == 0.0 )
121         gtod_ref_time_sec = the_time;
122 
123     return the_time - gtod_ref_time_sec;
124 }
125 
126 // --- End OSX build definitions ---------------------------------------------
127 #else
128 // --- Begin Linux build definitions -------------------------------------------
129 
bli_clock_helper()130 double bli_clock_helper()
131 {
132     double the_time, norm_sec;
133     struct timespec ts;
134 
135     clock_gettime( CLOCK_MONOTONIC, &ts );
136 
137     if ( gtod_ref_time_sec == 0.0 )
138         gtod_ref_time_sec = ( double ) ts.tv_sec;
139 
140     norm_sec = ( double ) ts.tv_sec - gtod_ref_time_sec;
141 
142     the_time = norm_sec + ts.tv_nsec * 1.0e-9;
143 
144     return the_time;
145 }
146 
147 // --- End Linux build definitions ---------------------------------------------
148 #endif
149 
150 // --- End system definitions --------------------------------------------------
151 #endif
152 
153