MFEM  v4.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, Lawrence Livermore National Security, LLC. Produced at
2 // the Lawrence Livermore National Laboratory. LLNL-CODE-443211. All Rights
3 // reserved. See file COPYRIGHT for details.
4 //
5 // This file is part of the MFEM library. For more information and source code
6 // availability see http://mfem.org.
7 //
8 // MFEM is free software; you can redistribute it and/or modify it under the
9 // terms of the GNU Lesser General Public License (as published by the Free
10 // Software Foundation) version 2.1 dated February 1999.
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_CUDA_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* CuMemFree(void *dptr)
50 {
51 #ifdef MFEM_USE_CUDA
52 #ifdef MFEM_TRACK_CUDA_MEM
53  mfem::out << "CuMemFree(): deallocating memory @ " << dptr << " ... "
54  << std::flush;
55 #endif
56  MFEM_CUDA_CHECK(cudaFree(dptr));
57 #ifdef MFEM_TRACK_CUDA_MEM
58  mfem::out << "done." << std::endl;
59 #endif
60 #endif
61  return dptr;
62 }
63 
64 void* CuMemcpyHtoD(void* dst, const void* src, size_t bytes)
65 {
66 #ifdef MFEM_USE_CUDA
67 #ifdef MFEM_TRACK_CUDA_MEM
68  mfem::out << "CuMemcpyHtoD(): copying " << bytes << " bytes from "
69  << src << " to " << dst << " ... " << std::flush;
70 #endif
71  MFEM_CUDA_CHECK(cudaMemcpy(dst, src, bytes, cudaMemcpyHostToDevice));
72 #ifdef MFEM_TRACK_CUDA_MEM
73  mfem::out << "done." << std::endl;
74 #endif
75 #endif
76  return dst;
77 }
78 
79 void* CuMemcpyHtoDAsync(void* dst, const void* src, size_t bytes)
80 {
81 #ifdef MFEM_USE_CUDA
82  MFEM_CUDA_CHECK(cudaMemcpyAsync(dst, src, bytes, cudaMemcpyHostToDevice));
83 #endif
84  return dst;
85 }
86 
87 void* CuMemcpyDtoD(void *dst, const void *src, size_t bytes)
88 {
89 #ifdef MFEM_USE_CUDA
90 #ifdef MFEM_TRACK_CUDA_MEM
91  mfem::out << "CuMemcpyDtoD(): copying " << bytes << " bytes from "
92  << src << " to " << dst << " ... " << std::flush;
93 #endif
94  MFEM_CUDA_CHECK(cudaMemcpy(dst, src, bytes, cudaMemcpyDeviceToDevice));
95 #ifdef MFEM_TRACK_CUDA_MEM
96  mfem::out << "done." << std::endl;
97 #endif
98 #endif
99  return dst;
100 }
101 
102 void* CuMemcpyDtoDAsync(void* dst, const void *src, size_t bytes)
103 {
104 #ifdef MFEM_USE_CUDA
105  MFEM_CUDA_CHECK(cudaMemcpyAsync(dst, src, bytes, cudaMemcpyDeviceToDevice));
106 #endif
107  return dst;
108 }
109 
110 void* CuMemcpyDtoH(void *dst, const void *src, size_t bytes)
111 {
112 #ifdef MFEM_USE_CUDA
113 #ifdef MFEM_TRACK_CUDA_MEM
114  mfem::out << "CuMemcpyDtoH(): copying " << bytes << " bytes from "
115  << src << " to " << dst << " ... " << std::flush;
116 #endif
117  MFEM_CUDA_CHECK(cudaMemcpy(dst, src, bytes, cudaMemcpyDeviceToHost));
118 #ifdef MFEM_TRACK_CUDA_MEM
119  mfem::out << "done." << std::endl;
120 #endif
121 #endif
122  return dst;
123 }
124 
125 void* CuMemcpyDtoHAsync(void *dst, const void *src, size_t bytes)
126 {
127 #ifdef MFEM_USE_CUDA
128  MFEM_CUDA_CHECK(cudaMemcpyAsync(dst, src, bytes, cudaMemcpyDeviceToHost));
129 #endif
130  return dst;
131 }
132 
133 } // namespace mfem
void * CuMemcpyHtoD(void *dst, const void *src, size_t bytes)
Copies memory from Host to Device.
Definition: cuda.cpp:64
void * CuMemFree(void *dptr)
Frees 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:146
void * CuMemcpyDtoD(void *dst, const void *src, size_t bytes)
Copies memory from Device to Device.
Definition: cuda.cpp:87
void * CuMemcpyDtoDAsync(void *dst, const void *src, size_t bytes)
Copies memory from Device to Device.
Definition: cuda.cpp:102
OutStream err(std::cerr)
Global stream used by the library for standard error output. Initially it uses the same std::streambu...
Definition: globals.hpp:69
void * CuMemcpyDtoHAsync(void *dst, const void *src, size_t bytes)
Copies memory from Device to Host.
Definition: cuda.cpp:125
void * CuMemcpyHtoDAsync(void *dst, const void *src, size_t bytes)
Copies memory from Host to Device.
Definition: cuda.cpp:79
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:64
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:110