Serac  0.1
Serac is an implicit thermal strucural mechanics simulation code.
debug_print.hpp
1 // Copyright (c) Lawrence Livermore National Security, LLC and
2 // other Serac Project Developers. See the top-level LICENSE file for
3 // details.
4 //
5 // SPDX-License-Identifier: (BSD-3-Clause)
6 
7 #pragma once
8 
9 #include <fstream>
10 #include <iomanip>
11 #include <iostream>
12 #include <string>
13 #include <typeinfo>
14 #include <vector>
15 
16 #include "mfem.hpp"
17 #include "axom/core.hpp"
18 
19 #ifdef __GNUG__
20 #include <cxxabi.h>
21 #include <cstdlib>
22 #endif
23 
25 #include "serac/numerics/functional/element_restriction.hpp"
26 
27 namespace serac {
28 
35 template <typename T>
36 std::string typeString(T& var)
37 {
38  // Remove reference, but keep the const/volatile qualifiers.
39  const char* name = typeid(var).name();
40 #ifdef __GNUG__
41  int status = -4; // Arbitrary value to eliminate the compiler warning
42  char* demangled = abi::__cxa_demangle(name, nullptr, nullptr, &status);
43  std::string result((status == 0) ? demangled : name);
44  std::free(demangled);
45  if constexpr (std::is_const_v<T>) {
46  result = "const " + result;
47  }
48  return result;
49 #else
50  // Return name if compiler doesn't support GNU's extensions (most do)
51  return name;
52 #endif
53 }
54 
61 template <typename T>
62 void writeToFile(std::vector<T> v, std::string filename)
63 {
64  std::ofstream outfile(filename);
65  for (int i = 0; i < v.size(); i++) {
66  outfile << v[i] << std::endl;
67  }
68  outfile.close();
69 }
70 
76 void writeToFile(mfem::Vector v, std::string filename)
77 {
78  std::ofstream outfile(filename);
79  for (int i = 0; i < v.Size(); i++) {
80  outfile << v[i] << std::endl;
81  }
82  outfile.close();
83 }
84 
90 void writeToFile(mfem::SparseMatrix A, std::string filename)
91 {
92  std::ofstream outfile(filename);
93  A.PrintMM(outfile);
94  outfile.close();
95 }
96 
100 std::ostream& operator<<(std::ostream& out, DoF dof)
101 {
102  out << "{" << dof.index() << ", " << dof.sign() << ", " << dof.orientation() << "}";
103  return out;
104 }
105 
112 template <typename T>
113 void writeToFile(axom::Array<T, 2, serac::detail::host_memory_space> arr, std::string filename)
114 {
115  std::ofstream outfile(filename);
116 
117  for (axom::IndexType i = 0; i < arr.shape()[0]; i++) {
118  outfile << "{";
119  for (axom::IndexType j = 0; j < arr.shape()[1]; j++) {
120  outfile << arr(i, j);
121  if (j < arr.shape()[1] - 1) outfile << ", ";
122  }
123  outfile << "}\n";
124  }
125 
126  outfile.close();
127 }
128 
135 template <typename T>
136 void writeToFile(axom::Array<T, 3, serac::detail::host_memory_space> arr, std::string filename)
137 {
138  std::ofstream outfile(filename);
139 
140  outfile << std::setprecision(16);
141 
142  for (axom::IndexType i = 0; i < arr.shape()[0]; i++) {
143  outfile << "{";
144  for (axom::IndexType j = 0; j < arr.shape()[1]; j++) {
145  outfile << "{";
146  for (axom::IndexType k = 0; k < arr.shape()[2]; k++) {
147  outfile << arr(i, j, k);
148  if (k < arr.shape()[2] - 1) outfile << ", ";
149  }
150  outfile << "}";
151  if (j < arr.shape()[1] - 1) outfile << ", ";
152  }
153  outfile << "}\n";
154  }
155 
156  outfile.close();
157 }
158 
159 #ifdef __CUDACC__
160 #include <cuda_runtime.h>
165 #include <iostream>
166 void printCUDAMemUsage()
167 {
168  int deviceCount = 0;
169  cudaGetDeviceCount(&deviceCount);
170  int i = 0;
171  cudaSetDevice(i);
172 
173  size_t freeBytes, totalBytes;
174  cudaMemGetInfo(&freeBytes, &totalBytes);
175  size_t usedBytes = totalBytes - freeBytes;
176 
177  std::cout << "Device Number: " << i << std::endl;
178  std::cout << " Total Memory (MB): " << (totalBytes / 1024.0 / 1024.0) << std::endl;
179  std::cout << " Free Memory (MB): " << (freeBytes / 1024.0 / 1024.0) << std::endl;
180  std::cout << " Used Memory (MB): " << (usedBytes / 1024.0 / 1024.0) << std::endl;
181 }
182 
183 #endif
184 
185 } // namespace serac
This file defines the host memory space.
Accelerator functionality.
Definition: serac.cpp:36
std::string typeString(T &var)
Return string of given parameter's type.
Definition: debug_print.hpp:36
std::ostream & operator<<(std::ostream &out, DoF dof)
stream output for DoF
void writeToFile(std::vector< T > v, std::string filename)
write an array of values out to file, in a space-separated format
Definition: debug_print.hpp:62
a struct of metadata (index, sign, orientation) associated with a degree of freedom
int sign() const
get the sign field of this DoF
uint64_t index() const
get the index field of this DoF
uint64_t orientation() const
get the orientation field of this DoF