MFEM  v4.3.0
Finite element discretization library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
hip.cpp
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 #include "backends.hpp"
13 #include "globals.hpp"
14 
15 namespace mfem
16 {
17 
18 // Internal debug option, useful for tracking HIP allocations, deallocations
19 // and transfers.
20 // #define MFEM_TRACK_HIP_MEM
21 
22 #ifdef MFEM_USE_HIP
23 void mfem_hip_error(hipError_t err, const char *expr, const char *func,
24  const char *file, int line)
25 {
26  mfem::err << "\n\nHIP error: (" << expr << ") failed with error:\n --> "
27  << hipGetErrorString(err)
28  << "\n ... in function: " << func
29  << "\n ... in file: " << file << ':' << line << '\n';
30  mfem_error();
31 }
32 #endif
33 
34 void* HipMemAlloc(void** dptr, size_t bytes)
35 {
36 #ifdef MFEM_USE_HIP
37 #ifdef MFEM_TRACK_HIP_MEM
38  mfem::out << "HipMemAlloc(): allocating " << bytes << " bytes ... "
39  << std::flush;
40 #endif
41  MFEM_GPU_CHECK(hipMalloc(dptr, bytes));
42 #ifdef MFEM_TRACK_HIP_MEM
43  mfem::out << "done: " << *dptr << std::endl;
44 #endif
45 #endif
46  return *dptr;
47 }
48 
49 void* HipMallocManaged(void** dptr, size_t bytes)
50 {
51 #ifdef MFEM_USE_HIP
52 #ifdef MFEM_TRACK_HIP_MEM
53  mfem::out << "HipMallocManaged(): allocating " << bytes << " bytes ... "
54  << std::flush;
55 #endif
56  MFEM_GPU_CHECK(hipMallocManaged(dptr, bytes));
57 #ifdef MFEM_TRACK_HIP_MEM
58  mfem::out << "done: " << *dptr << std::endl;
59 #endif
60 #endif
61  return *dptr;
62 }
63 
64 void* HipMemAllocHostPinned(void** ptr, size_t bytes)
65 {
66 #ifdef MFEM_USE_HIP
67 #ifdef MFEM_TRACK_HIP_MEM
68  mfem::out << "HipMemAllocHostPinned(): allocating " << bytes << " bytes ... "
69  << std::flush;
70 #endif
71  MFEM_GPU_CHECK(hipHostMalloc(ptr, bytes, hipHostMallocDefault));
72 #ifdef MFEM_TRACK_HIP_MEM
73  mfem::out << "done: " << *ptr << std::endl;
74 #endif
75 #endif
76  return *ptr;
77 }
78 
79 void* HipMemFree(void *dptr)
80 {
81 #ifdef MFEM_USE_HIP
82 #ifdef MFEM_TRACK_HIP_MEM
83  mfem::out << "HipMemFree(): deallocating memory @ " << dptr << " ... "
84  << std::flush;
85 #endif
86  MFEM_GPU_CHECK(hipFree(dptr));
87 #ifdef MFEM_TRACK_HIP_MEM
88  mfem::out << "done." << std::endl;
89 #endif
90 #endif
91  return dptr;
92 }
93 
94 void* HipMemFreeHostPinned(void *ptr)
95 {
96 #ifdef MFEM_USE_HIP
97 #ifdef MFEM_TRACK_HIP_MEM
98  mfem::out << "HipMemFreeHostPinned(): deallocating memory @ " << ptr << " ... "
99  << std::flush;
100 #endif
101  MFEM_GPU_CHECK(hipHostFree(ptr));
102 #ifdef MFEM_TRACK_HIP_MEM
103  mfem::out << "done." << std::endl;
104 #endif
105 #endif
106  return ptr;
107 }
108 
109 void* HipMemcpyHtoD(void* dst, const void* src, size_t bytes)
110 {
111 #ifdef MFEM_USE_HIP
112 #ifdef MFEM_TRACK_HIP_MEM
113  mfem::out << "HipMemcpyHtoD(): copying " << bytes << " bytes from "
114  << src << " to " << dst << " ... " << std::flush;
115 #endif
116  MFEM_GPU_CHECK(hipMemcpy(dst, src, bytes, hipMemcpyHostToDevice));
117 #ifdef MFEM_TRACK_HIP_MEM
118  mfem::out << "done." << std::endl;
119 #endif
120 #endif
121  return dst;
122 }
123 
124 void* HipMemcpyHtoDAsync(void* dst, const void* src, size_t bytes)
125 {
126 #ifdef MFEM_USE_HIP
127  MFEM_GPU_CHECK(hipMemcpyAsync(dst, src, bytes, hipMemcpyHostToDevice));
128 #endif
129  return dst;
130 }
131 
132 void* HipMemcpyDtoD(void *dst, const void *src, size_t bytes)
133 {
134 #ifdef MFEM_USE_HIP
135 #ifdef MFEM_TRACK_HIP_MEM
136  mfem::out << "HipMemcpyDtoD(): copying " << bytes << " bytes from "
137  << src << " to " << dst << " ... " << std::flush;
138 #endif
139  MFEM_GPU_CHECK(hipMemcpy(dst, src, bytes, hipMemcpyDeviceToDevice));
140 #ifdef MFEM_TRACK_HIP_MEM
141  mfem::out << "done." << std::endl;
142 #endif
143 #endif
144  return dst;
145 }
146 
147 void* HipMemcpyDtoDAsync(void* dst, const void *src, size_t bytes)
148 {
149 #ifdef MFEM_USE_HIP
150  MFEM_GPU_CHECK(hipMemcpyAsync(dst, src, bytes, hipMemcpyDeviceToDevice));
151 #endif
152  return dst;
153 }
154 
155 void* HipMemcpyDtoH(void *dst, const void *src, size_t bytes)
156 {
157 #ifdef MFEM_USE_HIP
158 #ifdef MFEM_TRACK_HIP_MEM
159  mfem::out << "HipMemcpyDtoH(): copying " << bytes << " bytes from "
160  << src << " to " << dst << " ... " << std::flush;
161 #endif
162  MFEM_GPU_CHECK(hipMemcpy(dst, src, bytes, hipMemcpyDeviceToHost));
163 #ifdef MFEM_TRACK_HIP_MEM
164  mfem::out << "done." << std::endl;
165 #endif
166 #endif
167  return dst;
168 }
169 
170 void* HipMemcpyDtoHAsync(void *dst, const void *src, size_t bytes)
171 {
172 #ifdef MFEM_USE_HIP
173  MFEM_GPU_CHECK(hipMemcpyAsync(dst, src, bytes, hipMemcpyDeviceToHost));
174 #endif
175  return dst;
176 }
177 
179 {
180 #ifdef MFEM_USE_HIP
181  MFEM_GPU_CHECK(hipGetLastError());
182 #endif
183 }
184 
186 {
187  int num_gpus = -1;
188 #ifdef MFEM_USE_HIP
189  MFEM_GPU_CHECK(hipGetDeviceCount(&num_gpus));
190 #endif
191  return num_gpus;
192 }
193 
194 } // namespace mfem
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 mfem_error(const char *msg)
Function called when an error is encountered. Used by the macros MFEM_ABORT, MFEM_ASSERT, MFEM_VERIFY.
Definition: error.cpp:153
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
OutStream out(std::cout)
Global stream used by the library for standard output. Initially it uses the same std::streambuf as s...
Definition: globals.hpp:66