Enable parallel tests.
[hoomd-blue.git] / libhoomd / utils / ClockSource.h
blob842087fbe9b9d9438ebd3f1fd80a934f9ed20eeb
1 /*
2 Highly Optimized Object-oriented Many-particle Dynamics -- Blue Edition
3 (HOOMD-blue) Open Source Software License Copyright 2009-2014 The Regents of
4 the University of Michigan All rights reserved.
6 HOOMD-blue may contain modifications ("Contributions") provided, and to which
7 copyright is held, by various Contributors who have granted The Regents of the
8 University of Michigan the right to modify and/or distribute such Contributions.
10 You may redistribute, use, and create derivate works of HOOMD-blue, in source
11 and binary forms, provided you abide by the following conditions:
13 * Redistributions of source code must retain the above copyright notice, this
14 list of conditions, and the following disclaimer both in the code and
15 prominently in any materials provided with the distribution.
17 * Redistributions in binary form must reproduce the above copyright notice, this
18 list of conditions, and the following disclaimer in the documentation and/or
19 other materials provided with the distribution.
21 * All publications and presentations based on HOOMD-blue, including any reports
22 or published results obtained, in whole or in part, with HOOMD-blue, will
23 acknowledge its use according to the terms posted at the time of submission on:
24 http://codeblue.umich.edu/hoomd-blue/citations.html
26 * Any electronic documents citing HOOMD-Blue will link to the HOOMD-Blue website:
27 http://codeblue.umich.edu/hoomd-blue/
29 * Apart from the above required attributions, neither the name of the copyright
30 holder nor the names of HOOMD-blue's contributors may be used to endorse or
31 promote products derived from this software without specific prior written
32 permission.
34 Disclaimer
36 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS ``AS IS'' AND
37 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
38 WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND/OR ANY
39 WARRANTIES THAT THIS SOFTWARE IS FREE OF INFRINGEMENT ARE DISCLAIMED.
41 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
42 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
43 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
44 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
45 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
46 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
47 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50 // Maintainer: joaander
52 /*! \file ClockSource.h
53 \brief Declares the ClockSource class
56 #ifdef NVCC
57 #error This header cannot be compiled by nvcc
58 #endif
60 #ifndef __CLOCK_SOURCE_H__
61 #define __CLOCK_SOURCE_H__
63 // The clock code uses 64 bit integers for big numbers of nanoseconds. Include the
64 // cross-platform int64_t types from boost
65 #include <boost/cstdint.hpp>
66 using boost::int64_t;
67 using boost::uint64_t;
69 #include <string>
71 #include <iostream>
73 // If this is being compiled on a windows platform, we need to define a surrogate
74 // gettimeofday
75 #ifdef WIN32
76 #include <windows.h>
77 #define EPOCHFILETIME (116444736000000000i64)
79 //! Emulation class for timezone on windows
80 /*! \ingroup utils
82 struct timezone
84 int tz_minuteswest; //!< no docs
85 int tz_dsttime; //!< no docs
88 //! Emulation for gettimeofday in windows
89 /*! \param tv timeval to return current time in
90 \param tz unused
91 \ingroup utils
93 __inline int gettimeofday(struct timeval *tv, struct timezone *tz)
95 FILETIME ft;
96 LARGE_INTEGER li;
97 __int64 t;
98 static int tzflag;
100 if (tv)
102 GetSystemTimeAsFileTime(&ft);
103 li.LowPart = ft.dwLowDateTime;
104 li.HighPart = ft.dwHighDateTime;
105 t = li.QuadPart; /* In 100-nanosecond intervals */
106 t -= EPOCHFILETIME; /* Offset to the Epoch time */
107 t /= 10; /* In microseconds */
108 tv->tv_sec = (long)(t / 1000000);
109 tv->tv_usec = (long)(t % 1000000);
112 // lets just ignore the whole timezone thing
113 /*if (tz)
115 if (!tzflag)
117 _tzset();
118 tzflag++;
120 tz->tz_minuteswest = _timezone / 60;
121 tz->tz_dsttime = _daylight;
124 return 0;
127 #else
128 // else, we are on a unix-ish platform and need to include a few files to get
129 // gettimeofday
130 #include <sys/time.h>
131 #include <unistd.h>
134 // unix-ish systems also need a Sleep function
135 //! Sleep for for a time
136 /*! \param msec Number of milliseconds to sleep for
137 \ingroup utils
139 inline void Sleep(int msec)
141 usleep(msec*1000);
143 #endif
145 //! Source of time measurements
146 /*! Access the operating system's timer and reports a time since construction in nanoseconds.
147 The resolution of the timer is system dependant, though typically around 10 microseconds
148 or better. Critical accessor methods are inlined for low overhead
149 \ingroup utils
151 class ClockSource
153 public:
154 //! Construct a ClockSource
155 ClockSource();
156 //! Get the current time in nanoseconds
157 int64_t getTime() const;
159 //! Formats a given time value to HH:MM:SS
160 static std::string formatHMS(int64_t t);
161 private:
162 int64_t m_start_time; //!< Stores a base time to reference from
165 //! Exports the ClockSource class to python
166 void export_ClockSource();
168 inline int64_t ClockSource::getTime() const
170 timeval t;
171 gettimeofday(&t, NULL);
173 int64_t nsec = int64_t(t.tv_sec) * int64_t(1000000000) + int64_t(t.tv_usec)*int64_t(1000);
174 return nsec - m_start_time;
177 #endif