walltime.h
Go to the documentation of this file.
1 /*
2  * Copyright 2018 The urg_stamped Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef SCIP2_WALLTIME_H
18 #define SCIP2_WALLTIME_H
19 
20 #include <scip2/logger.h>
21 
22 namespace scip2
23 {
24 template <int DEVICE_TIMESTAMP_BITS>
25 class Walltime
26 {
27 protected:
31 
32  constexpr static uint32_t middle_bits_ = (1 << DEVICE_TIMESTAMP_BITS) / 2;
33 
34 public:
35  uint64_t update(const uint32_t& time_device)
36  {
37  if (!initialized_)
38  {
39  time_device_prev_ = time_device;
40  initialized_ = true;
41  }
42 
43  if (detectDeviceTimeUnderflow(time_device))
44  {
45  if (walltime_device_base_ >= (1 << DEVICE_TIMESTAMP_BITS))
46  {
47  time_device_prev_ = (1 << DEVICE_TIMESTAMP_BITS) - time_device;
48  return walltime_device_base_ - time_device_prev_;
49  }
50  logger::warn() << "Device time jumped. prev: " << time_device_prev_
51  << ", current: " << time_device << std::endl;
52  }
53 
54  if (time_device < middle_bits_ &&
55  middle_bits_ < time_device_prev_)
56  {
57  walltime_device_base_ += 1 << DEVICE_TIMESTAMP_BITS;
58  }
59 
60  time_device_prev_ = time_device;
61 
62  return walltime_device_base_ + time_device;
63  }
64 
65  bool detectDeviceTimeUnderflow(const uint32_t& time_device) const
66  {
67  return (time_device_prev_ < middle_bits_ &&
68  middle_bits_ < time_device &&
69  time_device - time_device_prev_ > middle_bits_);
70  }
71 
73  : initialized_(false)
74  , time_device_prev_(0)
75  , walltime_device_base_(0)
76  {
77  }
78 };
79 } // namespace scip2
80 
81 #endif // SCIP2_WALLTIME_H
bool detectDeviceTimeUnderflow(const uint32_t &time_device) const
Definition: walltime.h:65
uint64_t update(const uint32_t &time_device)
Definition: walltime.h:35
uint64_t walltime_device_base_
Definition: walltime.h:30
uint32_t time_device_prev_
Definition: walltime.h:29
std::ostream & warn()
Definition: logger.cpp:101
static constexpr uint32_t middle_bits_
Definition: walltime.h:32
bool initialized_
Definition: walltime.h:28


urg_stamped
Author(s): Atsushi Watanabe
autogenerated on Tue May 11 2021 02:14:05