Kokkos Core Kernels Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Kokkos_Abort.hpp
1 //@HEADER
2 // ************************************************************************
3 //
4 // Kokkos v. 4.0
5 // Copyright (2022) National Technology & Engineering
6 // Solutions of Sandia, LLC (NTESS).
7 //
8 // Under the terms of Contract DE-NA0003525 with NTESS,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions.
12 // See https://kokkos.org/LICENSE for license information.
13 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
14 //
15 //@HEADER
16 
17 #ifndef KOKKOS_ABORT_HPP
18 #define KOKKOS_ABORT_HPP
19 
20 #include <Kokkos_Macros.hpp>
21 #include <Kokkos_Printf.hpp>
22 #ifdef KOKKOS_ENABLE_CUDA
23 #include <Cuda/Kokkos_Cuda_abort.hpp>
24 #endif
25 #ifdef KOKKOS_ENABLE_HIP
26 #include <HIP/Kokkos_HIP_Abort.hpp>
27 #endif
28 #ifdef KOKKOS_ENABLE_SYCL
29 #include <SYCL/Kokkos_SYCL_Abort.hpp>
30 #endif
31 
32 namespace Kokkos {
33 namespace Impl {
34 
35 [[noreturn]] void host_abort(const char *const);
36 
37 #if defined(KOKKOS_ENABLE_CUDA) && defined(__CUDA_ARCH__)
38 
39 #if defined(KOKKOS_ENABLE_DEBUG_BOUNDS_CHECK)
40 // required to workaround failures in random number generator unit tests with
41 // pre-volta architectures
42 #define KOKKOS_IMPL_ABORT_NORETURN
43 #else
44 // cuda_abort aborts when building for other platforms than macOS
45 #define KOKKOS_IMPL_ABORT_NORETURN [[noreturn]]
46 #endif
47 
48 #elif defined(KOKKOS_COMPILER_NVHPC)
49 
50 #define KOKKOS_IMPL_ABORT_NORETURN
51 
52 #elif defined(KOKKOS_ENABLE_HIP) && defined(__HIP_DEVICE_COMPILE__)
53 // HIP aborts
54 #define KOKKOS_IMPL_ABORT_NORETURN [[noreturn]]
55 #elif defined(KOKKOS_ENABLE_SYCL) && defined(__SYCL_DEVICE_ONLY__)
56 // FIXME_SYCL SYCL doesn't abort
57 #define KOKKOS_IMPL_ABORT_NORETURN
58 #elif !defined(KOKKOS_ENABLE_OPENMPTARGET) && !defined(KOKKOS_ENABLE_OPENACC)
59 // Host aborts
60 #define KOKKOS_IMPL_ABORT_NORETURN [[noreturn]]
61 #else
62 // Everything else does not abort
63 #define KOKKOS_IMPL_ABORT_NORETURN
64 #endif
65 
66 // FIXME_SYCL
67 // Accomodate host pass for device functions that are not [[noreturn]]
68 #if defined(KOKKOS_ENABLE_SYCL) || \
69  (defined(KOKKOS_ENABLE_CUDA) && defined(KOKKOS_ENABLE_DEBUG_BOUNDS_CHECK))
70 #define KOKKOS_IMPL_ABORT_NORETURN_DEVICE
71 #else
72 #define KOKKOS_IMPL_ABORT_NORETURN_DEVICE KOKKOS_IMPL_ABORT_NORETURN
73 #endif
74 
75 #if defined(KOKKOS_ENABLE_CUDA) || defined(KOKKOS_ENABLE_HIP) || \
76  defined(KOKKOS_ENABLE_SYCL) || defined(KOKKOS_ENABLE_OPENMPTARGET) || \
77  defined(KOKKOS_ENABLE_OPENACC)
78 KOKKOS_IMPL_ABORT_NORETURN_DEVICE inline KOKKOS_IMPL_DEVICE_FUNCTION void
79 device_abort(const char *const msg) {
80 #if defined(KOKKOS_ENABLE_CUDA)
81  ::Kokkos::Impl::cuda_abort(msg);
82 #elif defined(KOKKOS_ENABLE_HIP)
83  ::Kokkos::Impl::hip_abort(msg);
84 #elif defined(KOKKOS_ENABLE_SYCL)
85  ::Kokkos::Impl::sycl_abort(msg);
86 #elif defined(KOKKOS_ENABLE_OPENMPTARGET) || defined(KOKKOS_ENABLE_OPENACC)
87  printf("%s", msg); // FIXME_OPENMPTARGET FIXME_OPENACC
88 #else
89 #error faulty logic
90 #endif
91 }
92 #endif
93 } // namespace Impl
94 
95 KOKKOS_IMPL_ABORT_NORETURN KOKKOS_INLINE_FUNCTION void abort(
96  const char *const message) {
97  KOKKOS_IF_ON_HOST(::Kokkos::Impl::host_abort(message);)
98  KOKKOS_IF_ON_DEVICE(::Kokkos::Impl::device_abort(message);)
99 }
100 
101 #undef KOKKOS_IMPL_ABORT_NORETURN
102 
103 } // namespace Kokkos
104 
105 #endif /* #ifndef KOKKOS_ABORT_HPP */