]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Avoid __CUDA_ARCH__ in source code
authorDaniel Arndt <arndtd@ornl.gov>
Wed, 1 Feb 2023 22:02:19 +0000 (17:02 -0500)
committerDaniel Arndt <arndtd@ornl.gov>
Wed, 1 Feb 2023 23:34:02 +0000 (23:34 +0000)
include/deal.II/base/exceptions.h
include/deal.II/base/point.h
include/deal.II/base/tensor.h

index b0648add4ad39d0011b680b714f91657633621e3..c0ab82d72c93f2f959c9d3a6ef57617a579fe7c1 100644 (file)
@@ -28,6 +28,7 @@
 #  include <cusparse.h>
 #endif
 
+#include <Kokkos_Core.hpp>
 
 DEAL_II_NAMESPACE_OPEN
 
@@ -1505,39 +1506,91 @@ namespace deal_II_exceptions
  * @ingroup Exceptions
  */
 #ifdef DEBUG
-#  ifdef DEAL_II_HAVE_BUILTIN_EXPECT
-#    define Assert(cond, exc)                                            \
-      {                                                                  \
-        if (__builtin_expect(!(cond), false))                            \
-          ::dealii::deal_II_exceptions::internals::issue_error_noreturn( \
-            ::dealii::deal_II_exceptions::internals::ExceptionHandling:: \
-              abort_or_throw_on_exception,                               \
-            __FILE__,                                                    \
-            __LINE__,                                                    \
-            __PRETTY_FUNCTION__,                                         \
-            #cond,                                                       \
-            #exc,                                                        \
-            exc);                                                        \
-      }
-#  else /*ifdef DEAL_II_HAVE_BUILTIN_EXPECT*/
-#    define Assert(cond, exc)                                            \
-      {                                                                  \
-        if (!(cond))                                                     \
-          ::dealii::deal_II_exceptions::internals::issue_error_noreturn( \
-            ::dealii::deal_II_exceptions::internals::ExceptionHandling:: \
-              abort_or_throw_on_exception,                               \
-            __FILE__,                                                    \
-            __LINE__,                                                    \
-            __PRETTY_FUNCTION__,                                         \
-            #cond,                                                       \
-            #exc,                                                        \
-            exc);                                                        \
-      }
-#  endif /*ifdef DEAL_II_HAVE_BUILTIN_EXPECT*/
-#else
+#  ifdef KOKKOS_VERSION >= 30600
+#    ifdef DEAL_II_HAVE_BUILTIN_EXPECT
+#      define Assert(cond, exc)                                              \
+        {                                                                    \
+          KOKKOS_IF_ON_HOST(({                                               \
+            if (__builtin_expect(!(cond), false))                            \
+              ::dealii::deal_II_exceptions::internals::issue_error_noreturn( \
+                ::dealii::deal_II_exceptions::internals::ExceptionHandling:: \
+                  abort_or_throw_on_exception,                               \
+                __FILE__,                                                    \
+                __LINE__,                                                    \
+                __PRETTY_FUNCTION__,                                         \
+                #cond,                                                       \
+                #exc,                                                        \
+                exc);                                                        \
+          }))                                                                \
+          KOKKOS_IF_ON_DEVICE(({                                             \
+            if (!(cond))                                                     \
+              Kokkos::abort(#cond);                                          \
+          }))                                                                \
+        }
+#    else /*ifdef DEAL_II_HAVE_BUILTIN_EXPECT*/
+#      define Assert(cond, exc)                                              \
+        {                                                                    \
+          KOKKOS_IF_ON_HOST(({                                               \
+            if (!(cond))                                                     \
+              ::dealii::deal_II_exceptions::internals::issue_error_noreturn( \
+                ::dealii::deal_II_exceptions::internals::ExceptionHandling:: \
+                  abort_or_throw_on_exception,                               \
+                __FILE__,                                                    \
+                __LINE__,                                                    \
+                __PRETTY_FUNCTION__,                                         \
+                #cond,                                                       \
+                #exc,                                                        \
+                exc);                                                        \
+          }))                                                                \
+          KOKKOS_IF_ON_DEVICE(({                                             \
+            if (!(cond))                                                     \
+              Kokkos::abort(#cond);                                          \
+          }))                                                                \
+        }
+#    endif /*ifdef DEAL_II_HAVE_BUILTIN_EXPECT*/
+#  else    /*ifdef KOKKOS_VERSION >= 30600*/
+#    ifdef KOKKOS_ACTIVE_EXECUTION_MEMORY_SPACE_HOST
+#      ifdef DEAL_II_HAVE_BUILTIN_EXPECT
+#        define Assert(cond, exc)                                            \
+          {                                                                  \
+            if (__builtin_expect(!(cond), false))                            \
+              ::dealii::deal_II_exceptions::internals::issue_error_noreturn( \
+                ::dealii::deal_II_exceptions::internals::ExceptionHandling:: \
+                  abort_or_throw_on_exception,                               \
+                __FILE__,                                                    \
+                __LINE__,                                                    \
+                __PRETTY_FUNCTION__,                                         \
+                #cond,                                                       \
+                #exc,                                                        \
+                exc);                                                        \
+          }
+#      else /*ifdef DEAL_II_HAVE_BUILTIN_EXPECT*/
+#        define Assert(cond, exc)                                            \
+          {                                                                  \
+            if (!(cond))                                                     \
+              ::dealii::deal_II_exceptions::internals::issue_error_noreturn( \
+                ::dealii::deal_II_exceptions::internals::ExceptionHandling:: \
+                  abort_or_throw_on_exception,                               \
+                __FILE__,                                                    \
+                __LINE__,                                                    \
+                __PRETTY_FUNCTION__,                                         \
+                #cond,                                                       \
+                #exc,                                                        \
+                exc);                                                        \
+          }
+#      endif /*ifdef DEAL_II_HAVE_BUILTIN_EXPECT*/
+#    else    /*#ifdef KOKKOS_ACTIVE_EXECUTION_MEMORY_SPACE_HOST*/
+#      define Assert(cond, exc)   \
+        {                         \
+          if (!(cond))            \
+            Kokkos::abort(#cond); \
+        }
+#    endif /*ifdef KOKKOS_ACTIVE_EXECUTION_MEMORY_SPACE_HOST*/
+#  endif   /*KOKKOS_ACTIVE_EXECUTION_MEMORY_SPACE_HOST*/
+#else      /*ifdef DEBUG*/
 #  define Assert(cond, exc) \
     {}
-#endif
+#endif /*ifdef DEBUG*/
 
 
 
index ce28ad3c41059a9e41b09e1f99e46347873279c1..858428fce658366f077537b2b8851fedfeb63e09 100644 (file)
@@ -363,14 +363,12 @@ template <int dim, typename Number>
 inline DEAL_II_HOST_DEVICE
 Point<dim, Number>::Point(const Number x)
 {
-#  ifndef __CUDA_ARCH__
   Assert(dim == 1,
          ExcMessage(
            "You can only initialize Point<1> objects using the constructor "
            "that takes only one argument. Point<dim> objects with dim!=1 "
            "require initialization with the constructor that takes 'dim' "
            "arguments."));
-#  endif
 
   // we can only get here if we pass the assertion. use the switch anyway so
   // as to avoid compiler warnings about uninitialized elements or writing
@@ -391,14 +389,12 @@ template <int dim, typename Number>
 inline DEAL_II_HOST_DEVICE
 Point<dim, Number>::Point(const Number x, const Number y)
 {
-#  ifndef __CUDA_ARCH__
   Assert(dim == 2,
          ExcMessage(
            "You can only initialize Point<2> objects using the constructor "
            "that takes two arguments. Point<dim> objects with dim!=2 "
            "require initialization with the constructor that takes 'dim' "
            "arguments."));
-#  endif
 
   // we can only get here if we pass the assertion. use the indirection anyway
   // so as to avoid compiler warnings about uninitialized elements or writing
@@ -414,14 +410,12 @@ template <int dim, typename Number>
 inline DEAL_II_HOST_DEVICE
 Point<dim, Number>::Point(const Number x, const Number y, const Number z)
 {
-#  ifndef __CUDA_ARCH__
   Assert(dim == 3,
          ExcMessage(
            "You can only initialize Point<3> objects using the constructor "
            "that takes three arguments. Point<dim> objects with dim!=3 "
            "require initialization with the constructor that takes 'dim' "
            "arguments."));
-#  endif
 
   // we can only get here if we pass the assertion. use the indirection anyway
   // so as to avoid compiler warnings about uninitialized elements or writing
@@ -470,9 +464,7 @@ template <int dim, typename Number>
 inline DEAL_II_HOST_DEVICE Number
 Point<dim, Number>::operator()(const unsigned int index) const
 {
-#  ifndef __CUDA_ARCH__
   AssertIndexRange(static_cast<int>(index), dim);
-#  endif
   return this->values[index];
 }
 
@@ -482,9 +474,7 @@ template <int dim, typename Number>
 inline DEAL_II_HOST_DEVICE Number &
 Point<dim, Number>::operator()(const unsigned int index)
 {
-#  ifndef __CUDA_ARCH__
   AssertIndexRange(static_cast<int>(index), dim);
-#  endif
   return this->values[index];
 }
 
index 55db04f176d385d8fff5e29561000aaff4e5904a..4bc8797b080bdad28751063623f87f552a8facec 100644 (file)
@@ -1039,11 +1039,8 @@ template <int dim, typename Number>
 constexpr DEAL_II_HOST_DEVICE_ALWAYS_INLINE
 Tensor<0, dim, Number>::operator Number &()
 {
-  // We cannot use Assert inside a CUDA kernel
-#  ifndef __CUDA_ARCH__
   Assert(dim != 0,
          ExcMessage("Cannot access an object of type Tensor<0,0,Number>"));
-#  endif
   return value;
 }
 
@@ -1052,11 +1049,8 @@ template <int dim, typename Number>
 constexpr inline DEAL_II_ALWAYS_INLINE
   DEAL_II_HOST_DEVICE Tensor<0, dim, Number>::operator const Number &() const
 {
-  // We cannot use Assert inside a CUDA kernel
-#  ifndef __CUDA_ARCH__
   Assert(dim != 0,
          ExcMessage("Cannot access an object of type Tensor<0,0,Number>"));
-#  endif
   return value;
 }
 
@@ -1162,15 +1156,29 @@ namespace internal
       val *= s;
     }
 
-#  ifdef __CUDA_ARCH__
     template <typename Number, typename OtherNumber>
     constexpr DEAL_II_HOST_DEVICE_ALWAYS_INLINE void
-    multiply_assign_scalar(std::complex<Number> &, const OtherNumber &)
+    multiply_assign_scalar(std::complex<Number> &val, const OtherNumber &s)
     {
-      printf("This function is not implemented for std::complex<Number>!\n");
-      assert(false);
-    }
+#  ifdef KOKKOS_VERSION >= 30600
+      KOKKOS_IF_ON_HOST((val *= s;))
+      KOKKOS_IF_ON_DEVICE(({
+        (void)val;
+        (void)s;
+        Kokkos::abort(
+          "This function is not implemented for std::complex<Number>!\n");
+      }))
+#  else
+#    ifdef KOKKOS_ACTIVE_EXECUTION_MEMORY_SPACE_HOST
+      val *= s;
+#    else
+      (void)val;
+      (void)s;
+      Kokkos::abort(
+        "This function is not implemented for std::complex<Number>!\n");
+#    endif
 #  endif
+    }
   } // namespace ComplexWorkaround
 } // namespace internal
 
@@ -1219,11 +1227,8 @@ constexpr DEAL_II_HOST_DEVICE inline DEAL_II_ALWAYS_INLINE
   typename Tensor<0, dim, Number>::real_type
   Tensor<0, dim, Number>::norm_square() const
 {
-  // We cannot use Assert inside a CUDA kernel
-#  ifndef __CUDA_ARCH__
   Assert(dim != 0,
          ExcMessage("Cannot access an object of type Tensor<0,0,Number>"));
-#  endif
   return numbers::NumberTraits<Number>::abs_square(value);
 }
 
@@ -1321,10 +1326,7 @@ constexpr DEAL_II_HOST_DEVICE_ALWAYS_INLINE
 Tensor<rank_, dim, Number>::Tensor(
   const ArrayView<ElementType, MemorySpace> &initializer)
 {
-  // We cannot use Assert in a CUDA kernel
-#  ifndef __CUDA_ARCH__
   AssertDimension(initializer.size(), n_independent_components);
-#  endif
 
   for (unsigned int i = 0; i < n_independent_components; ++i)
     (*this)[unrolled_to_component_indices(i)] = initializer[i];
@@ -1391,10 +1393,7 @@ namespace internal
               const unsigned int i,
               std::integral_constant<int, dim>)
     {
-      // We cannot use Assert in a CUDA kernel
-#  ifndef __CUDA_ARCH__
       AssertIndexRange(i, dim);
-#  endif
       return values[i];
     }
 
@@ -1404,13 +1403,10 @@ namespace internal
               const unsigned int,
               std::integral_constant<int, 0>)
     {
-      // We cannot use Assert in a CUDA kernel
-#  ifndef __CUDA_ARCH__
       Assert(
         false,
         ExcMessage(
           "Cannot access elements of an object of type Tensor<rank,0,Number>."));
-#  endif
       return *dummy;
     }
   } // namespace TensorSubscriptor

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.