imate
C++/CUDA Reference
Loading...
Searching...
No Matches
highres_time_stamp.h File Reference
#include <stdint.h>
Include dependency graph for highres_time_stamp.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

uint64_t get_highres_time_stamp (void)
 

Function Documentation

◆ get_highres_time_stamp()

uint64_t get_highres_time_stamp ( void  )

This function returns a high-resolution clock counter to be used to seed a random generating function (see split_mix_64.cpp). The purpose of high-resolution counter is that if the random-generating function is called subsequently in very short intervals (about nano-second), the seed value should be distinct.

In UNIX, such distinct seed values can be generated by std::clock() function which gives the processor counter. This function has enough resolution to generate distinct values if called subsequently. In MacOS, this function is available only after version 10.12.

In Windows, however, std::clock() is not a process counter, rather, it measures the wall clock with resoluton of one second! Thus, this function is not suitable at all on Windows. Instead, the QueryPerformanceCounter is used.

In Case if machine is neither POSIX compilant nor Windows, this function falls back to std::time and std::clock.

Note that this function works without C++11.

An alternative approach is to use rdtsc(), but its use is discouraged, since it is far less portable and many processors (like ARM64 archetecture) do not support it. Unlike rdtsc, this function works on both X86_64 and ARM64.

Definition at line 88 of file highres_time_stamp.cpp.

89{
90 // Zero means no proper function found for this OS and processor to
91 // support high-precision time counting.
92 uint64_t time_stamp = 0;
93
94 #if defined(USE_CLOCK_GETTIME)
95
96 // Using POSIX clock_gettime
97 struct timespec clock;
98 if (clock_gettime(CLOCK_REALTIME, &clock) == -1)
99 {
100 // One means there is an error, but the program continues without
101 // raising an error.
102 time_stamp = 1;
103 }
104
105 time_stamp = clock.tv_sec * 1e9 + clock.tv_nsec;
106
107 #elif defined(USE_QUERY_PERFORMANCE_COUNTER)
108
109 // Using Windows API for query performance counter
110 LARGE_INTEGER ticks;
111 if (!QueryPerformanceCounter(&ticks))
112 {
113 time_stamp = 1;
114 }
115 else
116 {
117 time_stamp = static_cast<uint64_t>(ticks.QuadPart);
118 }
119
120 #else
121
122 // Use std::time and std::clock.
123 // Note that std::time has low resolution second accuracy. To improve
124 // it, we add std::clock, which has high-resolution (in UNIX only).
125 // In UNIX, std::clock is the process time with high resolution. In
126 // Windows, note well that std::clock is the wall time with "second"
127 // resolution, and should be avoided.
128 time_stamp = static_cast<uint64_t>(std::time(0)) +
129 static_cast<uint64_t>(std::clock());
130
131 #endif
132
133 return time_stamp;
134}

Referenced by SplitMix64::SplitMix64().

Here is the caller graph for this function: