libMesh
Loading...
Searching...
No Matches
libmesh_device.h
Go to the documentation of this file.
1// The libMesh Finite Element Library.
2// Copyright (C) 2002-2026 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3
4// This library is free software; you can redistribute it and/or
5// modify it under the terms of the GNU Lesser General Public
6// License as published by the Free Software Foundation; either
7// version 2.1 of the License, or (at your option) any later version.
8
9// This library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12// Lesser General Public License for more details.
13
14// You should have received a copy of the GNU Lesser General Public
15// License along with this library; if not, write to the Free Software
16// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
18#ifndef LIBMESH_LIBMESH_DEVICE_H
19#define LIBMESH_LIBMESH_DEVICE_H
20
21// Defines LIBMESH_DEVICE_INLINE, mirroring MetaPhysicL's METAPHYSICL_INLINE
22// pattern (metaphysicl_device.h / METAPHYSICL_KOKKOS_COMPILATION).
23//
24// When compiling a .K translation unit (ACSM_KOKKOS_COMPILATION is defined
25// by kokkos.mk), this expands to KOKKOS_INLINE_FUNCTION so that annotated
26// methods are callable from both host and device code. In all other
27// translation units it expands to plain `inline`.
28#ifdef ACSM_KOKKOS_COMPILATION
29# include "libmesh/ignore_warnings.h"
30# include <Kokkos_Macros.hpp>
31# include <Kokkos_Abort.hpp>
32# include <Kokkos_MathematicalFunctions.hpp>
33# include "libmesh/restore_warnings.h"
34# define LIBMESH_DEVICE_INLINE KOKKOS_INLINE_FUNCTION
35
36// Backend-neutral device-code detection for Kokkos .K translation units.
37// This lets error/exception plumbing share a single predicate instead of
38// hardcoding per-backend checks in multiple headers.
39# if defined(__CUDA_ARCH__) || defined(__HIP_DEVICE_COMPILE__) || defined(__SYCL_DEVICE_ONLY__)
40# define LIBMESH_IN_DEVICE_CODE 1
41# else
42# define LIBMESH_IN_DEVICE_CODE 0
43# endif
44
45// Two-level stringification, so that a macro argument such as __LINE__
46// is expanded before it is turned into a string literal.
47# define LIBMESH_STRINGIFY_IMPL(x) #x
48# define LIBMESH_STRINGIFY(x) LIBMESH_STRINGIFY_IMPL(x)
49
50// Device-safe assert and error. Kokkos::abort() prints its message and
51// terminates portably on every backend, so the whole message, location
52// included, is assembled as a single string literal at compile time;
53// this avoids a separate formatted printf call in device code.
54// Defined here (not in libmesh_common.h) because Kokkos headers are
55// only available in .K translation units.
56# ifndef NDEBUG
57# define LIBMESH_DEVICE_ASSERT(asserted) \
58 do { if (!(asserted)) { \
59 ::Kokkos::abort("libMesh assert failed: " #asserted \
60 ", file " __FILE__ \
61 ", line " LIBMESH_STRINGIFY(__LINE__)); \
62 } } while (0)
63# else
64# define LIBMESH_DEVICE_ASSERT(asserted) ((void) 0)
65# endif
66
67// The message is stringified rather than evaluated: libMesh error
68// messages are often stream expressions (e.g. "text" << value), which
69// cannot be formatted in device code, and host-only inline functions
70// using them are still compiled in the device pass of a .K file.
71# define LIBMESH_DEVICE_ERROR_MSG(msg) \
72 ::Kokkos::abort("libMesh error: " #msg \
73 ", file " __FILE__ \
74 ", line " LIBMESH_STRINGIFY(__LINE__))
75
76# define LIBMESH_DEVICE_ERROR_MSG_IF(cond, msg) \
77 do { if (cond) { LIBMESH_DEVICE_ERROR_MSG(msg); } } while (0)
78
79#else
80# define LIBMESH_DEVICE_INLINE inline
81# define LIBMESH_IN_DEVICE_CODE 0
82
83namespace std {}
84#endif
85
86namespace libMesh
87{
88// Namespace providing the math functions (sqrt, abs, ...) for
89// LIBMESH_DEVICE_INLINE functions: std:: functions are not callable from
90// device code, while their Kokkos counterparts are callable from both host
91// and device. Use as `using portable_math::sqrt; sqrt(x);` so that
92// argument-dependent lookup still finds overloads for non-arithmetic types
93// such as MetaPhysicL::DualNumber.
94#ifdef ACSM_KOKKOS_COMPILATION
95namespace portable_math = ::Kokkos;
96#else
97namespace portable_math = std;
98#endif
99}
100
101#endif // LIBMESH_LIBMESH_DEVICE_H
The libMesh namespace provides an interface to certain functionality in the library.