Line data Source code
1 : //* This file is part of the MOOSE framework 2 : //* https://mooseframework.inl.gov 3 : //* 4 : //* All rights reserved, see COPYRIGHT for full restrictions 5 : //* https://github.com/idaholab/moose/blob/master/COPYRIGHT 6 : //* 7 : //* Licensed under LGPL 2.1, please see LICENSE for details 8 : //* https://www.gnu.org/licenses/lgpl-2.1.html 9 : 10 : #pragma once 11 : 12 : #include "Moose.h" 13 : #include "PerfGuard.h" 14 : 15 : #ifndef MOOSE_NO_PERF_GRAPH 16 : #define TIME_SECTION1(id) \ 17 : mooseAssert(!Threads::in_threads, "PerfGraph timing cannot be used within threaded sections"); \ 18 : PerfGuard time_guard(this->_pg_moose_app.perfGraph(), id); 19 : #define CHECK_TIME_SECTION(id, section_name) \ 20 : mooseAssert(moose::internal::getPerfGraphRegistry().sectionInfo(id)._name == \ 21 : timedSectionName(section_name), \ 22 : "PerfGraph section '" + timedSectionName(section_name) + \ 23 : "' is already registered with name '" + \ 24 : moose::internal::getPerfGraphRegistry().sectionInfo(id)._name + "'"); 25 : #else 26 : #define TIME_SECTION1(id) 27 : #define CHECK_TIME_SECTION(id, section_name) 28 : #endif 29 : 30 : #define TIME_SECTION2(section_name, level) \ 31 : const PerfID __perf_id = this->registerTimedSection(section_name, level); \ 32 : CHECK_TIME_SECTION(__perf_id, section_name); \ 33 : TIME_SECTION1(__perf_id); 34 : 35 : #define TIME_SECTION3(section_name, level, live_message) \ 36 : const PerfID __perf_id = this->registerTimedSection(section_name, level, live_message); \ 37 : CHECK_TIME_SECTION(__perf_id, section_name); \ 38 : TIME_SECTION1(__perf_id); 39 : 40 : #define TIME_SECTION4(section_name, level, live_message, print_dots) \ 41 : const PerfID __perf_id = \ 42 : this->registerTimedSection(section_name, level, live_message, print_dots); \ 43 : CHECK_TIME_SECTION(__perf_id, section_name); \ 44 : TIME_SECTION1(__perf_id); 45 : 46 : // Overloading solution from https://stackoverflow.com/a/11763277 47 : #define GET_MACRO(_1, _2, _3, _4, NAME, ...) NAME 48 : #define TIME_SECTION(...) \ 49 : GET_MACRO(__VA_ARGS__, TIME_SECTION4, TIME_SECTION3, TIME_SECTION2, TIME_SECTION1, )(__VA_ARGS__) 50 : 51 : class MooseApp; 52 : class InputParameters; 53 : class LinearFVGradientInterface; 54 : class MooseObject; 55 : 56 : /** 57 : * Interface for objects interacting with the PerfGraph. 58 : * 59 : * Enables getting PerfGraph information and registering PerfGraph timed sections. 60 : */ 61 : class PerfGraphInterface 62 : { 63 : public: 64 : static InputParameters validParams(); 65 : 66 : /** 67 : * For objects that _are_ MooseObjects with a default prefix of type() 68 : */ 69 : PerfGraphInterface(const MooseObject * moose_object); 70 : 71 : /** 72 : * For objects that _are_ MooseObjects 73 : */ 74 : PerfGraphInterface(const MooseObject * moose_object, const std::string prefix); 75 : 76 : /** 77 : * For objects that aren't MooseObjects 78 : */ 79 : PerfGraphInterface(PerfGraph & perf_graph, const std::string prefix = ""); 80 : 81 : /** 82 : * For objects that construct the PerfGraphInterface _before_ the PerfGraph 83 : * is initialized (see MooseApp and OutputWarehouse) 84 : */ 85 : PerfGraphInterface(MooseApp & moose_app, const std::string prefix = ""); 86 : 87 : #ifdef MOOSE_KOKKOS_ENABLED 88 : /** 89 : * Special constructor used for Kokkos functor copy during parallel dispatch 90 : */ 91 : PerfGraphInterface(const PerfGraphInterface & object, const Moose::Kokkos::FunctorCopy & key); 92 : #endif 93 : 94 4713969 : virtual ~PerfGraphInterface() = default; 95 : 96 : /** 97 : * Get the PerfGraph 98 : */ 99 : PerfGraph & perfGraph(); 100 : 101 : friend class LinearFVGradientInterface; 102 : 103 : protected: 104 : /** 105 : * Call to register a named section for timing. 106 : * 107 : * @param section_name The name of the code section to be timed 108 : * @param level The importance of the timer - lower is more important (0 will always come out) 109 : * @return The ID of the section - use when starting timing 110 : */ 111 : PerfID registerTimedSection(const std::string & section_name, const unsigned int level) const; 112 : 113 : /** 114 : * Call to register a named section for timing. 115 : * 116 : * @param section_name The name of the code section to be timed 117 : * @param level The importance of the timer - lower is more important (0 will always come out) 118 : * @param live_message The message to be printed to the screen during execution 119 : * @param print_dots Whether or not progress dots should be printed for this section 120 : * @return The ID of the section - use when starting timing 121 : */ 122 : PerfID registerTimedSection(const std::string & section_name, 123 : const unsigned int level, 124 : const std::string & live_message, 125 : const bool print_dots = true) const; 126 : 127 : /** 128 : * @returns The name of the timed section with the name \p section_name. 129 : * 130 : * Optionally adds a prefix if one is defined. 131 : */ 132 : std::string timedSectionName(const std::string & section_name) const; 133 : 134 : /// The MooseApp that owns the PerfGraph 135 : MooseApp & _pg_moose_app; 136 : 137 : /// A prefix to use for all sections 138 : const std::string _prefix; 139 : };