Skip to content
This repository has been archived by the owner on Mar 21, 2024. It is now read-only.

Commit

Permalink
Support cuda::std::chrono with nvc++ (#394)
Browse files Browse the repository at this point in the history
* Support cuda::std::chrono with nvc++

Replaces a use of __CUDA_ARCH__ in cuda::std::chrono with NV_IF_TARGET.

* Use `NV_DISPATCH_TARGET` to select between host and device

* Add tests for the cuda extensions

---------

Co-authored-by: Michael Schellenberger Costa <[email protected]>
  • Loading branch information
gonzalobg and miscco authored Mar 16, 2023
1 parent d2acf5c commit b866f22
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 3 deletions.
31 changes: 31 additions & 0 deletions .upstream-tests/test/cuda/chrono/from_time_t.pass.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, 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) 2023 NVIDIA CORPORATION & AFFILIATES.
//
//===----------------------------------------------------------------------===//

// <cuda/std/chrono>

// system_clock

// static time_point from_time_t(time_t t);

#include <nv/target>

#include <cuda/std/chrono>

#include "test_macros.h"

int main(int, char**)
{
NV_IF_TARGET(
NV_IS_HOST, (
typedef ::std::chrono::system_clock C;
C::time_point t1 = C::from_time_t(C::to_time_t(C::now()));
unused(t1);
));
return 0;
}
31 changes: 31 additions & 0 deletions .upstream-tests/test/cuda/chrono/to_time_t.pass.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, 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) 2023 NVIDIA CORPORATION & AFFILIATES.
//
//===----------------------------------------------------------------------===//

// <cuda/std/chrono>

// system_clock

// time_t to_time_t(const time_point& t);

#include <nv/target>

#include <cuda/std/chrono>

#include "test_macros.h"

int main(int, char**)
{
NV_IF_TARGET(
NV_IS_HOST, (
typedef ::std::chrono::system_clock C;
cuda::std::time_t t1 = C::to_time_t(C::now());
unused(t1);
));
return 0;
}
10 changes: 7 additions & 3 deletions include/cuda/std/detail/libcxx/include/__cuda/chrono.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#error "<__cuda/chrono> should only be included in from <cuda/std/chrono>"
#endif // __cuda_std__

#include <nv/target>

#if defined(_LIBCUDACXX_USE_PRAGMA_GCC_SYSTEM_HEADER)
#pragma GCC system_header
#endif
Expand All @@ -26,17 +28,19 @@ namespace chrono {
inline _LIBCUDACXX_INLINE_VISIBILITY
system_clock::time_point system_clock::now() _NOEXCEPT
{
#ifdef __CUDA_ARCH__
NV_DISPATCH_TARGET(
NV_IS_DEVICE, (
uint64_t __time;
asm volatile("mov.u64 %0, %%globaltimer;":"=l"(__time)::);
return time_point(duration_cast<duration>(nanoseconds(__time)));
#else
),
NV_IS_HOST, (
return time_point(duration_cast<duration>(nanoseconds(
::std::chrono::duration_cast<::std::chrono::nanoseconds>(
::std::chrono::system_clock::now().time_since_epoch()
).count()
)));
#endif
));
}

inline _LIBCUDACXX_INLINE_VISIBILITY
Expand Down

0 comments on commit b866f22

Please sign in to comment.