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 "libmesh/restore_warnings.h"
33# define LIBMESH_DEVICE_INLINE KOKKOS_INLINE_FUNCTION
34
35// Backend-neutral device-code detection for Kokkos .K translation units.
36// This lets error/exception plumbing share a single predicate instead of
37// hardcoding per-backend checks in multiple headers.
38# if defined(__CUDA_ARCH__) || defined(__HIP_DEVICE_COMPILE__) || defined(__SYCL_DEVICE_ONLY__)
39# define LIBMESH_IN_DEVICE_CODE 1
40# else
41# define LIBMESH_IN_DEVICE_CODE 0
42# endif
43
44// Device-safe assert: uses printf (supported on CUDA/HIP) and
45// Kokkos::abort() for backend-portable device termination.
46// Defined here (not in libmesh_common.h) because Kokkos headers
47// are only available in .K translation units.
48# ifndef NDEBUG
49# define LIBMESH_DEVICE_ASSERT(asserted) \
50 do { if (!(asserted)) { \
51 printf("libMesh assert failed: %s, file %s, line %d\n", \
52 #asserted, __FILE__, __LINE__); \
53 ::Kokkos::abort("libmesh_assert failed"); \
54 } } while (0)
55# else
56# define LIBMESH_DEVICE_ASSERT(asserted) ((void) 0)
57# endif
58
59# define LIBMESH_DEVICE_ERROR_MSG(msg) \
60 do { \
61 printf("libMesh error: %s, file %s, line %d\n", \
62 msg, __FILE__, __LINE__); \
63 ::Kokkos::abort(msg); \
64 } while (0)
65
66# define LIBMESH_DEVICE_ERROR_MSG_IF(cond, msg) \
67 do { if (cond) { LIBMESH_DEVICE_ERROR_MSG(msg); } } while (0)
68
69#else
70# define LIBMESH_DEVICE_INLINE inline
71# define LIBMESH_IN_DEVICE_CODE 0
72#endif
73
74#endif // LIBMESH_LIBMESH_DEVICE_H