MFEM  v4.3.0
Finite element discretization library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
hip.hpp
Go to the documentation of this file.
1 // Copyright (c) 2010-2021, 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 #ifdef MFEM_USE_HIP
22 #define MFEM_DEVICE __device__
23 #define MFEM_LAMBDA __host__ __device__
24 #define MFEM_HOST_DEVICE __host__ __device__
25 #define MFEM_DEVICE_SYNC MFEM_GPU_CHECK(hipDeviceSynchronize())
26 #define MFEM_STREAM_SYNC MFEM_GPU_CHECK(hipStreamSynchronize(0))
27 // Define a HIP error check macro, MFEM_GPU_CHECK(x), where x returns/is of
28 // type 'hipError_t'. This macro evaluates 'x' and raises an error if the
29 // result is not hipSuccess.
30 #define MFEM_GPU_CHECK(x) \
31  do \
32  { \
33  hipError_t err = (x); \
34  if (err != hipSuccess) \
35  { \
36  mfem_hip_error(err, #x, _MFEM_FUNC_NAME, __FILE__, __LINE__); \
37  } \
38  } \
39  while (0)
40 #endif // MFEM_USE_HIP
41 
42 // Define the MFEM inner threading macros
43 #if defined(MFEM_USE_HIP) && defined(__HIP_DEVICE_COMPILE__)
44 #define MFEM_SHARED __shared__
45 #define MFEM_SYNC_THREAD __syncthreads()
46 #define MFEM_BLOCK_ID(k) hipBlockIdx_ ##k
47 #define MFEM_THREAD_ID(k) hipThreadIdx_ ##k
48 #define MFEM_THREAD_SIZE(k) hipBlockDim_ ##k
49 #define MFEM_FOREACH_THREAD(i,k,N) \
50  for(int i=hipThreadIdx_ ##k; i<N; i+=hipBlockDim_ ##k)
51 #endif
52 
53 namespace mfem
54 {
55 
56 #ifdef MFEM_USE_HIP
57 // Function used by the macro MFEM_GPU_CHECK.
58 void mfem_hip_error(hipError_t err, const char *expr, const char *func,
59  const char *file, int line);
60 #endif
61 
62 /// Allocates device memory
63 void* HipMemAlloc(void **d_ptr, size_t bytes);
64 
65 /// Allocates managed device memory
66 void* HipMallocManaged(void **d_ptr, size_t bytes);
67 
68 /// Allocates page-locked (pinned) host memory
69 void* HipMemAllocHostPinned(void **ptr, size_t bytes);
70 
71 /// Frees device memory
72 void* HipMemFree(void *d_ptr);
73 
74 /// Frees page-locked (pinned) host memory and returns destination ptr.
75 void* HipMemFreeHostPinned(void *ptr);
76 
77 /// Copies memory from Host to Device
78 void* HipMemcpyHtoD(void *d_dst, const void *h_src, size_t bytes);
79 
80 /// Copies memory from Host to Device
81 void* HipMemcpyHtoDAsync(void *d_dst, const void *h_src, size_t bytes);
82 
83 /// Copies memory from Device to Device
84 void* HipMemcpyDtoD(void *d_dst, const void *d_src, size_t bytes);
85 
86 /// Copies memory from Device to Device
87 void* HipMemcpyDtoDAsync(void *d_dst, const void *d_src, size_t bytes);
88 
89 /// Copies memory from Device to Host
90 void* HipMemcpyDtoH(void *h_dst, const void *d_src, size_t bytes);
91 
92 /// Copies memory from Device to Host
93 void* HipMemcpyDtoHAsync(void *h_dst, const void *d_src, size_t bytes);
94 
95 /// Check the error code returned by hipGetLastError(), aborting on error.
96 void HipCheckLastError();
97 
98 /// Get the number of HIP devices
99 int HipGetDeviceCount();
100 
101 } // namespace mfem
102 
103 #endif // MFEM_HIP_HPP
void * HipMallocManaged(void **dptr, size_t bytes)
Allocates managed device memory.
Definition: hip.cpp:49
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
int HipGetDeviceCount()
Get the number of HIP devices.
Definition: hip.cpp:185
void * HipMemcpyDtoDAsync(void *dst, const void *src, size_t bytes)
Copies memory from Device to Device.
Definition: hip.cpp:147
void * HipMemFreeHostPinned(void *ptr)
Frees page-locked (pinned) host memory and returns destination ptr.
Definition: hip.cpp:94
void * HipMemcpyDtoHAsync(void *dst, const void *src, size_t bytes)
Copies memory from Device to Host.
Definition: hip.cpp:170
void * HipMemAllocHostPinned(void **ptr, size_t bytes)
Allocates page-locked (pinned) host memory.
Definition: hip.cpp:64
void * HipMemFree(void *dptr)
Frees device memory.
Definition: hip.cpp:79
void mfem_hip_error(hipError_t err, 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 * HipMemAlloc(void **dptr, size_t bytes)
Allocates device memory.
Definition: hip.cpp:34
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 * HipMemcpyHtoD(void *dst, const void *src, size_t bytes)
Copies memory from Host to Device.
Definition: hip.cpp:109
void * HipMemcpyDtoD(void *dst, const void *src, size_t bytes)
Copies memory from Device to Device.
Definition: hip.cpp:132