MFEM  v4.1.0
Finite element discretization library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
cuda.cpp
Go to the documentation of this file.
1 // Copyright (c) 2010-2020, 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 "cuda.hpp"
13 #include "globals.hpp"
14 
15 namespace mfem
16 {
17 
18 // Internal debug option, useful for tracking CUDA allocations, deallocations
19 // and transfers.
20 // #define MFEM_TRACK_CUDA_MEM
21 
22 #ifdef MFEM_USE_CUDA
23 void mfem_cuda_error(cudaError_t err, const char *expr, const char *func,
24  const char *file, int line)
25 {
26  mfem::err << "\n\nCUDA error: (" << expr << ") failed with error:\n --> "
27  << cudaGetErrorString(err)
28  << "\n ... in function: " << func
29  << "\n ... in file: " << file << ':' << line << '\n';
30  mfem_error();
31 }
32 #endif
33 
34 void* CuMemAlloc(void** dptr, size_t bytes)
35 {
36 #ifdef MFEM_USE_CUDA
37 #ifdef MFEM_TRACK_CUDA_MEM
38  mfem::out << "CuMemAlloc(): allocating " << bytes << " bytes ... "
39  << std::flush;
40 #endif
41  MFEM_GPU_CHECK(cudaMalloc(dptr, bytes));
42 #ifdef MFEM_TRACK_CUDA_MEM
43  mfem::out << "done: " << *dptr << std::endl;
44 #endif
45 #endif
46  return *dptr;
47 }
48 
49 void* CuMallocManaged(void** dptr, size_t bytes)
50 {
51 #ifdef MFEM_USE_CUDA
52 #ifdef MFEM_TRACK_CUDA_MEM
53  mfem::out << "CuMallocManaged(): allocating " << bytes << " bytes ... "
54  << std::flush;
55 #endif
56  MFEM_GPU_CHECK(cudaMallocManaged(dptr, bytes));
57 #ifdef MFEM_TRACK_CUDA_MEM
58  mfem::out << "done: " << *dptr << std::endl;
59 #endif
60 #endif
61  return *dptr;
62 }
63 
64 void* CuMemFree(void *dptr)
65 {
66 #ifdef MFEM_USE_CUDA
67 #ifdef MFEM_TRACK_CUDA_MEM
68  mfem::out << "CuMemFree(): deallocating memory @ " << dptr << " ... "
69  << std::flush;
70 #endif
71  MFEM_GPU_CHECK(cudaFree(dptr));
72 #ifdef MFEM_TRACK_CUDA_MEM
73  mfem::out << "done." << std::endl;
74 #endif
75 #endif
76  return dptr;
77 }
78 
79 void* CuMemcpyHtoD(void* dst, const void* src, size_t bytes)
80 {
81 #ifdef MFEM_USE_CUDA
82 #ifdef MFEM_TRACK_CUDA_MEM
83  mfem::out << "CuMemcpyHtoD(): copying " << bytes << " bytes from "
84  << src << " to " << dst << " ... " << std::flush;
85 #endif
86  MFEM_GPU_CHECK(cudaMemcpy(dst, src, bytes, cudaMemcpyHostToDevice));
87 #ifdef MFEM_TRACK_CUDA_MEM
88  mfem::out << "done." << std::endl;
89 #endif
90 #endif
91  return dst;
92 }
93 
94 void* CuMemcpyHtoDAsync(void* dst, const void* src, size_t bytes)
95 {
96 #ifdef MFEM_USE_CUDA
97  MFEM_GPU_CHECK(cudaMemcpyAsync(dst, src, bytes, cudaMemcpyHostToDevice));
98 #endif
99  return dst;
100 }
101 
102 void* CuMemcpyDtoD(void *dst, const void *src, size_t bytes)
103 {
104 #ifdef MFEM_USE_CUDA
105 #ifdef MFEM_TRACK_CUDA_MEM
106  mfem::out << "CuMemcpyDtoD(): copying " << bytes << " bytes from "
107  << src << " to " << dst << " ... " << std::flush;
108 #endif
109  MFEM_GPU_CHECK(cudaMemcpy(dst, src, bytes, cudaMemcpyDeviceToDevice));
110 #ifdef MFEM_TRACK_CUDA_MEM
111  mfem::out << "done." << std::endl;
112 #endif
113 #endif
114  return dst;
115 }
116 
117 void* CuMemcpyDtoDAsync(void* dst, const void *src, size_t bytes)
118 {
119 #ifdef MFEM_USE_CUDA
120  MFEM_GPU_CHECK(cudaMemcpyAsync(dst, src, bytes, cudaMemcpyDeviceToDevice));
121 #endif
122  return dst;
123 }
124 
125 void* CuMemcpyDtoH(void *dst, const void *src, size_t bytes)
126 {
127 #ifdef MFEM_USE_CUDA
128 #ifdef MFEM_TRACK_CUDA_MEM
129  mfem::out << "CuMemcpyDtoH(): copying " << bytes << " bytes from "
130  << src << " to " << dst << " ... " << std::flush;
131 #endif
132  MFEM_GPU_CHECK(cudaMemcpy(dst, src, bytes, cudaMemcpyDeviceToHost));
133 #ifdef MFEM_TRACK_CUDA_MEM
134  mfem::out << "done." << std::endl;
135 #endif
136 #endif
137  return dst;
138 }
139 
140 void* CuMemcpyDtoHAsync(void *dst, const void *src, size_t bytes)
141 {
142 #ifdef MFEM_USE_CUDA
143  MFEM_GPU_CHECK(cudaMemcpyAsync(dst, src, bytes, cudaMemcpyDeviceToHost));
144 #endif
145  return dst;
146 }
147 
149 {
150 #ifdef MFEM_USE_CUDA
151  MFEM_GPU_CHECK(cudaGetLastError());
152 #endif
153 }
154 
156 {
157  int num_gpus = -1;
158 #ifdef MFEM_USE_CUDA
159  MFEM_GPU_CHECK(cudaGetDeviceCount(&num_gpus));
160 #endif
161  return num_gpus;
162 }
163 
164 } // namespace mfem
void * CuMemcpyHtoD(void *dst, const void *src, size_t bytes)
Copies memory from Host to Device.
Definition: cuda.cpp:79
void * CuMemFree(void *dptr)
Frees device memory.
Definition: cuda.cpp:64
void CuCheckLastError()
Check the error code returned by cudaGetLastError(), aborting on error.
Definition: cuda.cpp:148
int CuGetDeviceCount()
Get the number of CUDA devices.
Definition: cuda.cpp:155
void * CuMallocManaged(void **dptr, size_t bytes)
Allocates managed device memory.
Definition: cuda.cpp:49
void mfem_cuda_error(cudaError_t err, const char *expr, const char *func, const char *file, int line)
Definition: cuda.cpp:23
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 * CuMemcpyDtoD(void *dst, const void *src, size_t bytes)
Copies memory from Device to Device.
Definition: cuda.cpp:102
void * CuMemcpyDtoDAsync(void *dst, const void *src, size_t bytes)
Copies memory from Device to Device.
Definition: cuda.cpp:117
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 * CuMemcpyDtoHAsync(void *dst, const void *src, size_t bytes)
Copies memory from Device to Host.
Definition: cuda.cpp:140
void * CuMemcpyHtoDAsync(void *dst, const void *src, size_t bytes)
Copies memory from Host to Device.
Definition: cuda.cpp:94
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
void * CuMemAlloc(void **dptr, size_t bytes)
Allocates device memory.
Definition: cuda.cpp:34
void * CuMemcpyDtoH(void *dst, const void *src, size_t bytes)
Copies memory from Device to Host.
Definition: cuda.cpp:125