SST/macro
timestamp.h
Go to the documentation of this file.
1 /*
2  * This file is part of SST/macroscale:
3  * The macroscale architecture simulator from the SST suite.
4  * Copyright (c) 2009 Sandia Corporation.
5  * This software is distributed under the BSD License.
6  * Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
7  * the U.S. Government retains certain rights in this software.
8  * For more information, see the LICENSE file in the top
9  * SST/macroscale directory.
10  */
11 
12 #ifndef SSTMAC_COMMON_TIMESTAMP_H_INCLUDED
13 #define SSTMAC_COMMON_TIMESTAMP_H_INCLUDED
14 
15 #include <iosfwd>
16 #include <stdint.h>
17 #include <iostream>
19 
20 namespace sstmac {
21 
22 /**
23  * A basic container for time (subject to future transplant).
24  * Stores time as an integral number of picoseconds (tentatively).
25  * With 1 psec resolution, a 64 bit int can hold roughly +/- 106 days.
26  *
27  * Intended to be reasonably compatible with ns3::HighPrecision time.
28  */
29 class timestamp
30 {
31  public:
32  /// The type that holds a timestamp.
33  typedef int64_t tick_t;
34 
35  private:
36  /// Picoseconds between clock ticks.
37 
38  static timestamp
39  scaled_time(int64_t value, int64_t scaling, const char *caller,
40  const char *units);
41 
42  /// The current time value of this container in ticks.
43  tick_t ticks_;
44 
45  public:
46  static void
47  init_stamps(tick_t tick_spacing);
48 
49  typedef enum { exact } timestamp_param_type_t;
50 
51  /// Round the given time value (in seconds) to the nearest
52  /// representable internal time.
53  /// \throw sprockit::time_error if this time is outside the range that can
54  /// can be represented with this time container
55  timestamp(double t);
56 
57  explicit
58  timestamp(tick_t ticks, timestamp_param_type_t ty);
59 
60  explicit
61  timestamp();
62 
63  static tick_t zero;
64 
65  /// Convert a tick type to int64_t.
66  int64_t
67  ticks_int64() const;
68 
69  /// Return the current time in seconds.
70  double
71  sec() const;
72 
73  /// Return the current time in milliseconds.
74  double
75  msec() const;
76 
77  /// Return the current time in microseconds.
78  double
79  usec() const;
80 
81  /// Return the current time in nanoseconds.
82  double
83  nsec() const;
84 
85  /// Return the current time in picoseconds.
86  double
87  psec() const;
88 
89  void correct_round_off(const timestamp& now);
90 
91  public:
92  /// Get the number of ticks.
93  inline tick_t
94  ticks() const {
95  return ticks_;
96  }
97 
98  static double
100 
101  /// Get the tick interval in picoseconds.
102  static tick_t
103  tick_interval();
104 
105  static int64_t
107 
108  /// Get the tick interval in std::string form (for example, "1ps").
109  static const std::string &
111 
112  /// Get the number of ticks per second (1e12/tick_interval()).
113  static tick_t
114  frequency();
115 
116  /// Get the largest time value possible (in seconds).
117  static double
118  max_time();
119 
120  /// Get the smallest (most negative) time value possible (in seconds).
121  static double
122  min_time();
123 
124  /// Get a time value corresponding exactly to the given number of
125  /// picoseconds.
126  /// \throw sprockit::time_error if this time cannot be exactly represented.
127  static timestamp
128  exact_psec(int64_t psec);
129 
130  /// Get a time value corresponding exactly to the given number of
131  /// nanoseconds.
132  /// \throw sprockit::time_error if this time cannot be exactly represented.
133  static timestamp
134  exact_nsec(int64_t nsec);
135 
136  /// Get a time value corresponding exactly to the given number of
137  /// microseconds.
138  /// \throw sprockit::time_error if this time cannot be exactly represented.
139  static timestamp
140  exact_usec(int64_t usec);
141 
142  /// Get a time value corresponding exactly to the given number of
143  /// milliseconds.
144  /// \throw sprockit::time_error if this time cannot be exactly represented.
145  static timestamp
146  exact_msec(int64_t msec);
147 
148  /// Get a time value corresponding exactly to the given number of
149  /// seconds.
150  /// \throw sprockit::time_error if this time cannot be exactly represented.
151  static timestamp
152  exact_sec(int64_t sec);
153 
154  /// Get a time value with exactly the given number of ticks.
155  /// This is a template function to ensure that we do proper range checking
156  /// on input values.
157  template<typename T>
158  static timestamp
159  exact_ticks(T val) {
160  timestamp ts(0);
161  ts.ticks_ = tick_t(val);
162  return ts;
163  }
164 
165  /// Fast and exact comparison operations.
166  inline bool
167  operator==(const timestamp &other) const {
168  return (ticks_ == other.ticks_);
169  }
170  inline bool
171  operator!=(const timestamp &other) const {
172  return (ticks_ != other.ticks_);
173  }
174  inline bool
175  operator<(const timestamp &other) const {
176  return (ticks_ < other.ticks_);
177  }
178  inline bool
179  operator<=(const timestamp &other) const {
180  return (ticks_ <= other.ticks_);
181  }
182  inline bool
183  operator>(const timestamp &other) const {
184  return (ticks_ > other.ticks_);
185  }
186  inline bool
187  operator>=(const timestamp &other) const {
188  return (ticks_ >= other.ticks_);
189  }
190  timestamp&
191  operator+=(const timestamp &other);
192  timestamp&
193  operator-=(const timestamp &other);
194  timestamp&
195  operator*=(double scale);
196  timestamp&
197  operator/=(double scale);
198 };
199 
200 timestamp
201 operator+(const timestamp &a, const timestamp &b);
202 timestamp
203 operator-(const timestamp &a, const timestamp &b);
204 timestamp
205 operator*(const timestamp &t, double scaling);
206 timestamp
207 operator*(double scaling, const timestamp &t);
208 timestamp
209 operator/(const timestamp &t, double scaling);
210 
211 std::ostream&
212 operator<<(std::ostream &os, const timestamp &t);
213 
214 std::string
216 
217 
218 } // end of namespace sstmac
219 
221 template <>
222 class serialize<sstmac::timestamp>
223 {
224  public:
225  void
226  operator()(sstmac::timestamp& t, serializer& ser){
227  ser.primitive(t);
228  }
229 };
231 
232 #endif
233 
tick_t ticks_
The current time value of this container in ticks.
Definition: timestamp.h:43
static tick_t tick_interval()
Get the tick interval in picoseconds.
timestamp & operator+=(const timestamp &other)
static double min_time()
Get the smallest (most negative) time value possible (in seconds).
timestamp operator*(const timestamp &t, double scaling)
static const std::string & tick_interval_string()
Get the tick interval in std::string form (for example, "1ps").
double sec() const
Return the current time in seconds.
static double tick_interval_sec()
void correct_round_off(const timestamp &now)
std::string to_printf_type(timestamp t)
timestamp & operator-=(const timestamp &other)
bool operator<(const timestamp &other) const
Definition: timestamp.h:175
double nsec() const
Return the current time in nanoseconds.
std::ostream & operator<<(std::ostream &os, const event_loc_id &loc)
bool operator<=(const timestamp &other) const
Definition: timestamp.h:179
bool operator!=(const timestamp &other) const
Definition: timestamp.h:171
tick_t ticks() const
Get the number of ticks.
Definition: timestamp.h:94
int64_t ticks_int64() const
Convert a tick type to int64_t.
static tick_t frequency()
Get the number of ticks per second (1e12/tick_interval()).
timestamp operator/(const timestamp &t, double scaling)
double usec() const
Return the current time in microseconds.
timestamp & operator*=(double scale)
static timestamp exact_nsec(int64_t nsec)
Get a time value corresponding exactly to the given number of nanoseconds.
A basic container for time (subject to future transplant).
Definition: timestamp.h:29
static double max_time()
Get the largest time value possible (in seconds).
SUMI = Simulator unified messagine interface It is also the name for a solid ink in Japanese - i...
int64_t tick_t
The type that holds a timestamp.
Definition: timestamp.h:33
timestamp operator-(const timestamp &a, const timestamp &b)
static timestamp exact_sec(int64_t sec)
Get a time value corresponding exactly to the given number of seconds.
void operator()(sstmac::timestamp &t, serializer &ser)
Definition: timestamp.h:226
static timestamp scaled_time(int64_t value, int64_t scaling, const char *caller, const char *units)
Picoseconds between clock ticks.
double msec() const
Return the current time in milliseconds.
#define START_SERIALIZATION_NAMESPACE
Definition: serializable.h:36
static void init_stamps(tick_t tick_spacing)
static tick_t zero
Definition: timestamp.h:63
static int64_t tick_interval_int64()
timestamp operator+(const timestamp &a, const timestamp &b)
bool operator>(const timestamp &other) const
Definition: timestamp.h:183
#define END_SERIALIZATION_NAMESPACE
Definition: serializable.h:37
static timestamp exact_ticks(T val)
Get a time value with exactly the given number of ticks.
Definition: timestamp.h:159
static timestamp exact_msec(int64_t msec)
Get a time value corresponding exactly to the given number of milliseconds.
bool operator==(const timestamp &other) const
Fast and exact comparison operations.
Definition: timestamp.h:167
static timestamp exact_usec(int64_t usec)
Get a time value corresponding exactly to the given number of microseconds.
static timestamp exact_psec(int64_t psec)
Get a time value corresponding exactly to the given number of picoseconds.
double psec() const
Return the current time in picoseconds.
bool operator>=(const timestamp &other) const
Definition: timestamp.h:187
timestamp & operator/=(double scale)