1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
//===----------------------------------------------------------------------===//
//
// Part of libcu++, the C++ Standard Library for your entire system,
// under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES.
//
//===----------------------------------------------------------------------===//
#ifndef _CUDA_STD_CTIME
#define _CUDA_STD_CTIME
#include <cuda/std/detail/__config>
#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC)
# pragma GCC system_header
#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG)
# pragma clang system_header
#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC)
# pragma system_header
#endif // no system header
#if _CCCL_CUDA_COMPILATION()
# include <cuda/__ptx/instructions/get_sreg.h>
#endif // _CCCL_CUDA_COMPILATION()
#if !_CCCL_COMPILER(NVRTC)
# include <time.h>
#else
# define TIME_UTC 1
using time_t = long long int;
struct timespec
{
::time_t tv_sec;
long tv_nsec;
};
#endif // _CCCL_COMPILER(NVRTC)
#include <cuda/std/__cccl/prologue.h>
_LIBCUDACXX_BEGIN_NAMESPACE_STD
using ::clock_t;
using ::size_t;
using ::time_t;
using ::timespec;
// glibc defines clock and other functions as macros, we need to prevent our symbols being redefined by them
// clock
#if defined(clock)
# pragma push_macro("clock")
# undef clock
# define _LIBCUDACXX_POP_CLOCK_MACRO
#endif // clock
[[nodiscard]] _CCCL_API inline clock_t clock() noexcept
#ifdef _LIBCUDACXX_POP_CLOCK_MACRO
# pragma pop_macro("clock")
# undef _LIBCUDACXX_POP_CLOCK_MACRO
#endif // _LIBCUDACXX_POP_CLOCK_MACRO
{
NV_IF_ELSE_TARGET(NV_IS_HOST, (return ::clock();), (return static_cast<clock_t>(_CUDA_VPTX::get_sreg_clock64());))
}
// difftime
#if defined(difftime)
# pragma push_macro("difftime")
# undef difftime
# define _LIBCUDACXX_POP_DIFFTIME_MACRO
#endif // difftime
[[nodiscard]] _CCCL_API constexpr double difftime(time_t __end, time_t __start) noexcept
#ifdef _LIBCUDACXX_POP_DIFFTIME_MACRO
# pragma pop_macro("difftime")
# undef _LIBCUDACXX_POP_DIFFTIME_MACRO
#endif // _LIBCUDACXX_POP_DIFFTIME_MACRO
{
return static_cast<double>(__end - __start);
}
// time
#if _CCCL_CUDA_COMPILATION()
[[nodiscard]] _CCCL_HIDE_FROM_ABI _CCCL_DEVICE time_t __cccl_time_impl_device(time_t* __v) noexcept
{
const auto __t = static_cast<clock_t>(_CUDA_VPTX::get_sreg_globaltimer() / 1'000'000'000);
if (__v != nullptr)
{
*__v = __t;
}
return __t;
}
#endif // _CCCL_CUDA_COMPILATION()
#if defined(time)
# pragma push_macro("time")
# undef time
# define _LIBCUDACXX_POP_TIME_MACRO
#endif // time
_CCCL_API inline time_t time(time_t* __v) noexcept
#ifdef _LIBCUDACXX_POP_TIME_MACRO
# pragma pop_macro("time")
# undef _LIBCUDACXX_POP_TIME_MACRO
#endif // _LIBCUDACXX_POP_TIME_MACRO
{
NV_IF_ELSE_TARGET(NV_IS_HOST, (return ::time(__v);), (return _CUDA_VSTD::__cccl_time_impl_device(__v);))
}
#if !defined(__ANDROID_API__) || __ANDROID_API__ >= 29
// timespec_get
# if _CCCL_CUDA_COMPILATION()
[[nodiscard]] _CCCL_HIDE_FROM_ABI _CCCL_DEVICE int __cccl_timespec_get_impl_device(timespec* __ts, int __base) noexcept
{
if (__base != TIME_UTC)
{
return 0;
}
const auto __t = _CUDA_VPTX::get_sreg_globaltimer();
__ts->tv_sec = static_cast<time_t>(__t / 1'000'000'000);
__ts->tv_nsec = static_cast<long>(__t % 1'000'000'000);
return __base;
}
# endif // _CCCL_CUDA_COMPILATION()
# if defined(timespec_get)
# pragma push_macro("timespec_get")
# undef timespec_get
# define _LIBCUDACXX_POP_TIMESPEC_GET_MACRO
# endif // timespec_get
template <int = 0>
[[nodiscard]] _CCCL_API inline int timespec_get(timespec* __ts, int __base) noexcept
# ifdef _LIBCUDACXX_POP_TIMESPEC_GET_MACRO
# pragma pop_macro("timespec_get")
# undef _LIBCUDACXX_POP_TIMESPEC_GET_MACRO
# endif // _LIBCUDACXX_POP_TIMESPEC_GET_MACRO
{
NV_IF_ELSE_TARGET(NV_IS_HOST,
(return ::timespec_get(__ts, __base);),
(return _CUDA_VSTD::__cccl_timespec_get_impl_device(__ts, __base);))
}
#endif // !defined(__ANDROID_API__) || __ANDROID_API__ >= 29
_LIBCUDACXX_END_NAMESPACE_STD
#include <cuda/std/__cccl/epilogue.h>
#endif // _CUDA_STD_CTIME
|