Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support use in CUDA device-side code #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Support use in CUDA device-side code #20

wants to merge 1 commit into from

Conversation

eyalroz
Copy link

@eyalroz eyalroz commented Sep 12, 2019

  • Now using a TCB_CONSTEXPR macro instead of constexpr, to hook our extra decorations
  • Decorating TCB_CONSTEXPR functions with __host__ __device__ when compiling CUDA code.

Notes:

  1. No direct decoration necessary, since spans are fully-constexpr.
  2. No decorations for functions involving exceptions, as they're host-only.
  3. Not well-tested at the moment.

Copy link
Owner

@tcbrindle tcbrindle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please prefix macros with TCB_SPAN_ rather than just TCB_, i.e. the new ones should be TCB_SPAN_CUDA_HOST_AND_DEVICE and TCB_SPAN_CONSTEXPR.

Other than that this looks good to me.

* Now using a `TCB_CONSTEXPR` macro instead of `constexpr`, to hook our extra decorations
* Decorating `TCB_CONSTEXPR` functions with `__host__ __device__` when compiling CUDA code.
@eyalroz
Copy link
Author

eyalroz commented Sep 12, 2019

@tcbrindle : How about now?

Also, even if you ok this - don't merge yet.

@sethrj
Copy link

sethrj commented Mar 6, 2020

@eyalroz I had to make some changes to your tentative span to get it to compile with nvcc (some errors, some warnings).

diff --git a/span.h b/span.h
index 380402b..41a20da 100644
--- a/span.h
+++ b/span.h
@@ -52,6 +52,11 @@
 #define TCB_SPAN_CUDA_HOST_AND_DEVICE
 #endif
 
+#ifdef __CUDA_ARCH__
+#define TCB_SPAN_NO_EXCEPTIONS
+#define TCB_SPAN_NO_CONTRACT_CHECKING
+#endif
+
 #ifndef TCB_SPAN_NO_EXCEPTIONS
 #include <cstdio>
 #include <stdexcept>
@@ -169,7 +174,8 @@ using byte = unsigned char;
 #define TCB_SPAN_NODISCARD
 #endif
 
-TCB_SPAN_INLINE_VAR constexpr std::size_t dynamic_extent = -1;
+TCB_SPAN_INLINE_VAR constexpr std::size_t dynamic_extent =
+    static_cast<std::size_t>(-1);
 
 template <typename ElementType, std::size_t Extent = dynamic_extent>
 class span;
@@ -178,7 +184,7 @@ namespace detail {
 
 template <typename E, std::size_t S>
 struct span_storage {
-  TCB_SPAN_CONSTEXPR span_storage() noexcept = default;
+  constexpr span_storage() noexcept = default;
 
   TCB_SPAN_CONSTEXPR span_storage(E* ptr, std::size_t /*unused*/) noexcept
       : ptr(ptr) {}
@@ -189,7 +195,7 @@ struct span_storage {
 
 template <typename E>
 struct span_storage<E, dynamic_extent> {
-  TCB_SPAN_CONSTEXPR span_storage() noexcept = default;
+  constexpr span_storage() noexcept = default;
 
   TCB_SPAN_CONSTEXPR span_storage(E* ptr, std::size_t size) noexcept
       : ptr(ptr), size(size) {}
@@ -385,7 +391,7 @@ class span {
   TCB_SPAN_CONSTEXPR span(const Container& cont)
       : storage_(detail::data(cont), detail::size(cont)) {}
 
-  TCB_SPAN_CONSTEXPR span(const span& other) noexcept = default;
+  constexpr span(const span& other) noexcept = default;
 
   template <typename OtherElementType, std::size_t OtherExtent,
             typename std::enable_if<
@@ -399,8 +405,7 @@ class span {
 
   ~span() noexcept = default;
 
-  TCB_SPAN_CONSTEXPR_ASSIGN span& operator=(const span& other) noexcept =
-      default;
+  constexpr span& operator=(const span& other) noexcept = default;
 
   // [span.sub], span subviews
   template <std::size_t Count>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants