Reference documentation for deal.II version Git 9e557027ad 2021-09-25 18:07:42 +0200
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
thread_management.cc
Go to the documentation of this file.
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2000 - 2020 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE.md at
12 // the top level directory of deal.II.
13 //
14 // ---------------------------------------------------------------------
15 
17 
18 #include <atomic>
19 #include <cstdlib>
20 #include <iostream>
21 
22 #ifdef DEAL_II_HAVE_UNISTD_H
23 # include <unistd.h>
24 #endif
25 
26 
28 
29 
30 namespace Threads
31 {
32  namespace internal
33  {
34  [[noreturn]] void
35  handle_std_exception(const std::exception &exc)
36  {
37  // lock the following context
38  // to ensure that we don't
39  // print things over each other
40  // if we have trouble from
41  // multiple threads. release
42  // the lock before calling
43  // std::abort, though
44  static Mutex mutex;
45  {
46  std::lock_guard<std::mutex> lock(mutex);
47 
48  std::cerr
49  << std::endl
50  << std::endl
51  << "---------------------------------------------------------"
52  << std::endl
53  << "In one of the sub-threads of this program, an exception\n"
54  << "was thrown and not caught. Since exceptions do not\n"
55  << "propagate to the main thread, the library has caught it.\n"
56  << "The information carried by this exception is given below.\n"
57  << std::endl
58  << "---------------------------------------------------------"
59  << std::endl;
60  std::cerr << "Exception message: " << std::endl
61  << " " << exc.what() << std::endl
62  << "Exception type: " << std::endl
63  << " " << typeid(exc).name() << std::endl;
64  std::cerr << "Aborting!" << std::endl
65  << "---------------------------------------------------------"
66  << std::endl;
67  }
68 
69  std::abort();
70  }
71 
72 
73 
74  [[noreturn]] void
76  {
77  // lock the following context
78  // to ensure that we don't
79  // print things over each other
80  // if we have trouble from
81  // multiple threads. release
82  // the lock before calling
83  // std::abort, though
84  static Mutex mutex;
85  {
86  std::lock_guard<std::mutex> lock(mutex);
87 
88  std::cerr
89  << std::endl
90  << std::endl
91  << "---------------------------------------------------------"
92  << std::endl
93  << "In one of the sub-threads of this program, an exception\n"
94  << "was thrown and not caught. Since exceptions do not\n"
95  << "propagate to the main thread, the library has caught it.\n"
96  << std::endl
97  << "---------------------------------------------------------"
98  << std::endl;
99  std::cerr << "Type of exception is unknown, but not std::exception.\n"
100  << "No additional information is available.\n"
101  << "---------------------------------------------------------"
102  << std::endl;
103  }
104  std::abort();
105  }
106  } // namespace internal
107 
108 
109 
110  std::vector<std::pair<unsigned int, unsigned int>>
111  split_interval(const unsigned int begin,
112  const unsigned int end,
113  const unsigned int n_intervals)
114  {
115  Assert(end >= begin, ExcInternalError());
116 
117  const unsigned int n_elements = end - begin;
118  const unsigned int n_elements_per_interval = n_elements / n_intervals;
119  const unsigned int residual = n_elements % n_intervals;
120 
121  std::vector<std::pair<unsigned int, unsigned int>> return_values(
122  n_intervals);
123 
124  return_values[0].first = begin;
125  for (unsigned int i = 0; i < n_intervals; ++i)
126  {
127  if (i != n_intervals - 1)
128  {
129  return_values[i].second =
130  (return_values[i].first + n_elements_per_interval);
131  // distribute residual in
132  // division equally among
133  // the first few
134  // subintervals
135  if (i < residual)
136  ++return_values[i].second;
137  return_values[i + 1].first = return_values[i].second;
138  }
139  else
140  return_values[i].second = end;
141  }
142  return return_values;
143  }
144 } // namespace Threads
145 
146 
std::vector< std::pair< unsigned int, unsigned int > > split_interval(const unsigned int begin, const unsigned int end, const unsigned int n_intervals)
#define Assert(cond, exc)
Definition: exceptions.h:1461
void abort(const ExceptionBase &exc) noexcept
Definition: exceptions.cc:452
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:401
VectorType::value_type * end(VectorType &V)
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:400
VectorType::value_type * begin(VectorType &V)
void handle_std_exception(const std::exception &exc)
static ::ExceptionBase & ExcInternalError()