MFEM v4.8.0
Finite element discretization library
Loading...
Searching...
No Matches
kernel_reporter.hpp
Go to the documentation of this file.
1// Copyright (c) 2010-2025, 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_KERNEL_REPORTER_HPP
13#define MFEM_KERNEL_REPORTER_HPP
14
16#include <set>
17#include <sstream>
18#include <string>
19
20#define MFEM_STR_(X) #X
21#define MFEM_STR(X) MFEM_STR_(X)
22#define MFEM_KERNEL_NAME(KernelName) \
23 __FILE__ ":" MFEM_STR(__LINE__) " : " #KernelName
24
25namespace mfem
26{
27
28namespace internal
29{
30
31template <typename Last>
32static void Stringify_(std::ostream &o, Last &&arg)
33{
34 o << arg;
35}
36
37template <typename T1, typename T2, typename... Rest>
38static void Stringify_(std::ostream &o, T1 &&a1, T2 &&a2, Rest&&... rest)
39{
40 o << int(a1) << ",";
41 Stringify_(o, a2, rest...);
42}
43
44template <typename... Args>
45static std::string Stringify(Args&&... args)
46{
47 std::stringstream o;
48 Stringify_(o, args...);
49 return o.str();
50}
51
52} // namespace
53
54/// @brief Singleton class to report fallback kernels.
55///
56/// Writes the first call to a fallback kernel to mfem::err
57///
58/// @note This class is only enabled when the environment variable
59/// MFEM_REPORT_KERNELS is set to a value other than 'NO' or if
60/// KernelReporter::Enable() is called.
62{
63 bool enabled = false;
64 std::set<std::string> reported_fallbacks;
66 {
67 const char *env = GetEnv("MFEM_REPORT_KERNELS");
68 if (env)
69 {
70 if (std::string(env) != "NO") { enabled = true; }
71 }
72 }
73 static KernelReporter &Instance()
74 {
75 static KernelReporter instance;
76 return instance;
77 }
78public:
79 /// Enable reporting of fallback kernels.
80 static void Enable() { Instance().enabled = true; }
81 /// Disable reporting of fallback kernels.
82 static void Disable() { Instance().enabled = false; }
83 /// Report the fallback kernel with given parameters.
84 template <typename... Params>
85 static void ReportFallback(const std::string &kernel_name, Params&&... params)
86 {
87 if (!Instance().enabled) { return; }
88 auto &reported_fallbacks = Instance().reported_fallbacks;
89 const std::string requested_kernel =
90 kernel_name + "<" + internal::Stringify(params...) + ">";
91 if (reported_fallbacks.find(requested_kernel) == reported_fallbacks.end())
92 {
93 reported_fallbacks.insert(requested_kernel);
94 mfem::err << "Fallback kernel. Requested "
95 << requested_kernel << std::endl;
96 }
97 }
98};
99
100} // namespace mfem
101
102#endif
Singleton class to report fallback kernels.
static void Enable()
Enable reporting of fallback kernels.
static void Disable()
Disable reporting of fallback kernels.
static void ReportFallback(const std::string &kernel_name, Params &&... params)
Report the fallback kernel with given parameters.
const char * GetEnv(const char *name)
Wrapper for std::getenv.
Definition globals.cpp:79
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
kernels::InvariantsEvaluator2D::Buffers Args