MFEM v4.10.0
Finite element discretization library
Loading...
Searching...
No Matches
hip.hpp
Go to the documentation of this file.
1// Copyright (c) 2010-2026, Lawrence Livermore National Security, LLC. Produced
2// at the Lawrence Livermore National Laboratory. All Rights reserved. See files
3// LICENSE and NOTICE for details. LLNL-CODE-806117.
4//
5// This file is part of the MFEM library. For more information and source code
6// availability visit https://mfem.org.
7//
8// MFEM is free software; you can redistribute it and/or modify it under the
9// terms of the BSD-3 license. We welcome feedback and contributions, see file
10// CONTRIBUTING.md for details.
11
12#ifndef MFEM_HIP_HPP
13#define MFEM_HIP_HPP
14
15#include "../config/config.hpp"
16#include "error.hpp"
17
18// HIP block size used by MFEM.
19#define MFEM_HIP_BLOCKS 256
20
21#if defined(MFEM_USE_HIP)
22#define MFEM_USE_CUDA_OR_HIP
23#define MFEM_DEVICE_SYNC MFEM_GPU_CHECK(hipDeviceSynchronize())
24#define MFEM_STREAM_SYNC MFEM_GPU_CHECK(hipStreamSynchronize(0))
25// Define a HIP error check macro, MFEM_GPU_CHECK(x), where x returns/is of
26// type 'hipError_t'. This macro evaluates 'x' and raises an error if the
27// result is not hipSuccess.
28#define MFEM_GPU_CHECK(x) \
29 do { \
30 hipError_t mfem_err_internal_var_name = (x); \
31 if (mfem_err_internal_var_name != hipSuccess) { \
32 ::mfem::mfem_hip_error(mfem_err_internal_var_name, #x, _MFEM_FUNC_NAME, \
33 __FILE__, __LINE__); \
34 } \
35 } while (0)
36
37// Macros defined only when compiling with HIP language
38#if defined(__HIP__)
39#define MFEM_USE_CUDA_OR_HIP_LANG
40#define MFEM_DEVICE __device__
41#define MFEM_HOST __host__
42#define MFEM_LAMBDA __host__ __device__
43#define MFEM_LAUNCH_BOUNDS __launch_bounds__
44// #define MFEM_HOST_DEVICE __host__ __device__ // defined in config/config.hpp
45
46// Define the MFEM inner threading macros
47#if defined(__HIP_DEVICE_COMPILE__)
48#define MFEM_SHARED __shared__
49#define MFEM_SYNC_THREAD __syncthreads()
50#define MFEM_BLOCK_ID(k) hipBlockIdx_ ##k
51#define MFEM_THREAD_ID(k) hipThreadIdx_ ##k
52#define MFEM_THREAD_SIZE(k) hipBlockDim_ ##k
53#define MFEM_FOREACH_THREAD(i,k,N) \
54 for(int i=hipThreadIdx_ ##k; i<N; i+=hipBlockDim_ ##k)
55#define MFEM_FOREACH_THREAD_DIRECT(i,k,N) \
56 if(const int i=hipThreadIdx_ ##k; i<N)
57// Assigns a thread block shaped (SX,SY,SZ) contiguous in x.
58// Example (3,2,1) block:
59// 0 (0,0), 1 (1,0), 2 (2,0)
60// 3 (1,0), 4 (1,1), 5 (2,1)
61#define MFEM_FOREACH_THREAD_DIRECT_3D(ix, iy, iz, k, SX, SY, SZ) \
62 if (int ix = hipThreadIdx_##k % (SX), iy = hipThreadIdx_##k / (SX), \
63 iz = iy / (SY); \
64 (iy %= (SY)), (hipThreadIdx_##k < (SX) * (SY) * (SZ)))
65// Assigns a thread block shaped (OX,OY,OZ) to work on items (SX,SY,SZ),
66// contiguous in x. This intentionally offsets threads within the block to avoid
67// shared memory bank conflicts.
68// Example (3,2,1) block assigned to work on (2,2,1) items:
69// 0 (0,0), 1 (1,0), 2 (N/A)
70// 3 (1,0), 4 (1,1), 5 (N/A)
71#define MFEM_FOREACH_THREAD_DIRECT_3D_OFFSET(ix, iy, iz, k, SX, SY, SZ, OX, \
72 OY, OZ) \
73 if (int ix = hipThreadIdx_##k % (OX), iy = hipThreadIdx_##k / (OX), \
74 iz = iy / (OY); \
75 (ix < (SX)) && ((iy %= (OY)) < (SY)) && (iz < (SZ)))
76#endif // defined(__HIP_DEVICE_COMPILE__)
77#endif // defined(__HIP__)
78#endif // defined(MFEM_USE_HIP)
79
80namespace mfem
81{
82
83#ifdef MFEM_USE_HIP
84// Function used by the macro MFEM_GPU_CHECK.
85void mfem_hip_error(hipError_t err, const char *expr, const char *func,
86 const char *file, int line);
87#endif
88
89/// Allocates device memory
90void* HipMemAlloc(void **d_ptr, size_t bytes);
91
92/// Allocates managed device memory
93void* HipMallocManaged(void **d_ptr, size_t bytes);
94
95/// Allocates page-locked (pinned) host memory
96void* HipMemAllocHostPinned(void **ptr, size_t bytes);
97
98/// Frees device memory
99void* HipMemFree(void *d_ptr);
100
101/// Frees page-locked (pinned) host memory and returns destination ptr.
102void* HipMemFreeHostPinned(void *ptr);
103
104/// Copies memory from Host to Device
105void* HipMemcpyHtoD(void *d_dst, const void *h_src, size_t bytes);
106
107/// Copies memory from Host to Device
108void* HipMemcpyHtoDAsync(void *d_dst, const void *h_src, size_t bytes);
109
110/// Copies memory from Device to Device
111void* HipMemcpyDtoD(void *d_dst, const void *d_src, size_t bytes);
112
113/// Copies memory from Device to Device
114void* HipMemcpyDtoDAsync(void *d_dst, const void *d_src, size_t bytes);
115
116/// Copies memory from Device to Host
117void* HipMemcpyDtoH(void *h_dst, const void *d_src, size_t bytes);
118
119/// Copies memory from Device to Host
120void* HipMemcpyDtoHAsync(void *h_dst, const void *d_src, size_t bytes);
121
122/// Check the error code returned by hipGetLastError(), aborting on error.
123void HipCheckLastError();
124
125/// Get the number of HIP devices
127
128} // namespace mfem
129
130#endif // MFEM_HIP_HPP
void * HipMemcpyDtoD(void *dst, const void *src, size_t bytes)
Copies memory from Device to Device.
Definition hip.cpp:132
void * HipMemcpyDtoDAsync(void *dst, const void *src, size_t bytes)
Copies memory from Device to Device.
Definition hip.cpp:147
void * HipMemAllocHostPinned(void **ptr, size_t bytes)
Allocates page-locked (pinned) host memory.
Definition hip.cpp:64
void * HipMemcpyHtoDAsync(void *dst, const void *src, size_t bytes)
Copies memory from Host to Device.
Definition hip.cpp:124
void HipCheckLastError()
Check the error code returned by hipGetLastError(), aborting on error.
Definition hip.cpp:178
void * HipMemcpyHtoD(void *dst, const void *src, size_t bytes)
Copies memory from Host to Device.
Definition hip.cpp:109
OutStream err(std::cerr)
Global stream used by the library for standard error output. Initially it uses the same std::streambu...
Definition globals.hpp:71
void * HipMemFree(void *dptr)
Frees device memory.
Definition hip.cpp:79
int HipGetDeviceCount()
Get the number of HIP devices.
Definition hip.cpp:185
void mfem_hip_error(hipError_t error, const char *expr, const char *func, const char *file, int line)
Definition hip.cpp:23
void * HipMemcpyDtoH(void *dst, const void *src, size_t bytes)
Copies memory from Device to Host.
Definition hip.cpp:155
void * HipMemFreeHostPinned(void *ptr)
Frees page-locked (pinned) host memory and returns destination ptr.
Definition hip.cpp:94
void * HipMallocManaged(void **dptr, size_t bytes)
Allocates managed device memory.
Definition hip.cpp:49
void * HipMemcpyDtoHAsync(void *dst, const void *src, size_t bytes)
Copies memory from Device to Host.
Definition hip.cpp:170
void * HipMemAlloc(void **dptr, size_t bytes)
Allocates device memory.
Definition hip.cpp:34