www.mooseframework.org
ThreadedElementLoop.h
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
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 "ParallelUniqueId.h"
13 #include "FEProblemBase.h"
15 #include "ConsoleUtils.h"
16 
17 // Forward declarations
18 class SystemBase;
19 
27 static Threads::spin_mutex threaded_element_mutex;
28 
32 template <typename RangeType>
34 {
35 public:
37 
38  ThreadedElementLoop(ThreadedElementLoop & x, Threads::split split);
39 
40  virtual ~ThreadedElementLoop();
41 
42  virtual void caughtMooseException(MooseException & e) override;
43 
44  virtual bool keepGoing() override { return !_fe_problem.hasException(); }
45 
46  virtual void preElement(const Elem * elem) override;
47 
48  virtual void preInternalSide(const Elem * elem, unsigned int side) override;
49 
50  virtual void preBoundary(const Elem * elem,
51  unsigned int side,
52  BoundaryID bnd_id,
53  const Elem * lower_d_elem = nullptr) override;
54 
55  virtual void neighborSubdomainChanged() override;
56 
57 protected:
59 
71  template <typename T>
72  void printExecutionOrdering(const std::vector<T *> & objs,
73  const bool print_header = true,
74  const std::string & line_prefix = "[DBG]") const;
75  template <typename T>
76  void printExecutionOrdering(const std::vector<std::shared_ptr<T>> & objs_ptrs,
77  const bool print_header = true,
78  const std::string & line_prefix = "[DBG]") const;
79 };
80 
81 template <typename RangeType>
83  : ThreadedElementLoopBase<RangeType>(fe_problem.mesh()), _fe_problem(fe_problem)
84 {
85 }
86 
87 template <typename RangeType>
89  Threads::split /*split*/)
90  : ThreadedElementLoopBase<RangeType>(x), _fe_problem(x._fe_problem)
91 {
92 }
93 
94 template <typename RangeType>
96 {
97 }
98 
99 template <typename RangeType>
100 void
102 {
103  Threads::spin_mutex::scoped_lock lock(threaded_element_mutex);
104 
105  std::string what(e.what());
106  _fe_problem.setException(what);
107 }
108 
109 template <typename RangeType>
110 void
112 {
113  _fe_problem.setCurrentSubdomainID(el, ThreadedElementLoopBase<RangeType>::_tid);
114 }
115 
116 template <typename RangeType>
117 void
118 ThreadedElementLoop<RangeType>::preInternalSide(const Elem * el, unsigned int side)
119 {
120  _fe_problem.setNeighborSubdomainID(el, side, ThreadedElementLoopBase<RangeType>::_tid);
121 }
122 
123 template <typename RangeType>
124 void
126  unsigned int /*side*/,
127  BoundaryID bnd_id,
128  const Elem * /*=nullptr*/)
129 {
130  _fe_problem.setCurrentBoundaryID(bnd_id, ThreadedElementLoopBase<RangeType>::_tid);
131 }
132 
133 template <typename RangeType>
134 void
136 {
137  _fe_problem.neighborSubdomainSetup(ThreadedElementLoopBase<RangeType>::_neighbor_subdomain,
139 }
140 
141 template <typename RangeType>
142 template <typename T>
143 void
145  const bool print_header,
146  const std::string & line_prefix) const
147 {
148  if (!objs.size())
149  return;
150 
151  auto & console = _fe_problem.console();
152  const auto objects_type = MooseUtils::prettyCppType(objs[0]);
153  std::vector<MooseObject *> moose_objs;
154  for (auto obj_ptr : objs)
155  moose_objs.push_back(dynamic_cast<MooseObject *>(obj_ptr));
156  const auto names = ConsoleUtils::mooseObjectVectorToString(moose_objs);
157 
158  // Print string with a DBG prefix and with sufficient line breaks
159  std::string message = print_header ? "Executing " + objects_type + " on " +
160  _fe_problem.getCurrentExecuteOnFlag().name() + "\n"
161  : "";
162  message += (print_header ? "Order of execution:\n" : "") + names;
163  console << ConsoleUtils::formatString(message, line_prefix) << std::endl;
164 }
165 
166 template <typename RangeType>
167 template <typename T>
168 void
170  const std::vector<std::shared_ptr<T>> & objs_ptrs,
171  const bool print_header,
172  const std::string & line_prefix) const
173 {
174  std::vector<T *> regular_ptrs;
175  for (auto shared_ptr : objs_ptrs)
176  regular_ptrs.push_back(shared_ptr.get());
177  printExecutionOrdering<T>(regular_ptrs, print_header, line_prefix);
178 }
std::string mooseObjectVectorToString(const std::vector< MooseObject *> &objs, const std::string &sep=" ")
Routine to output the name of MooseObjects in a string.
Definition: ConsoleUtils.C:452
Base class for assembly-like calculations.
virtual const char * what() const
Get out the error message.
FEProblemBase & _fe_problem
virtual void neighborSubdomainChanged() override
Called every time the neighbor subdomain changes (i.e.
MeshBase & mesh
virtual void preInternalSide(const Elem *elem, unsigned int side) override
Called before evaluations on an element internal side.
virtual void preElement(const Elem *elem) override
Called before the element assembly.
Base class for a system (of equations)
Definition: SystemBase.h:85
Specialization of SubProblem for solving nonlinear equations plus auxiliary equations.
virtual void preBoundary(const Elem *elem, unsigned int side, BoundaryID bnd_id, const Elem *lower_d_elem=nullptr) override
Called before the boundary assembly.
void printExecutionOrdering(const std::vector< T *> &objs, const bool print_header=true, const std::string &line_prefix="[DBG]") const
Routine to output the ordering of objects within a vector of pointers to these objects.
virtual bool keepGoing() override
Whether or not the loop should continue.
std::string formatString(std::string message, const std::string &prefix)
Add new lines and prefixes to a string for pretty display in output NOTE: This makes a copy of the st...
Definition: ConsoleUtils.C:436
boundary_id_type BoundaryID
ThreadedElementLoop(FEProblemBase &feproblem)
tbb::split split
Provides a way for users to bail out of the current solve.
Base class for assembly-like calculations.
static Threads::spin_mutex threaded_element_mutex
This mutex is used by all derived classes of the ThreadedElementLoop.
virtual void caughtMooseException(MooseException &e) override
Called if a MooseException is caught anywhere during the computation.
virtual bool hasException()
Whether or not an exception has occurred.
std::string prettyCppType(const std::string &cpp_type)
Definition: MooseUtils.C:1235