Reference documentation for deal.II version Git ca0b05a7a7 2020-12-01 11:49:21 -0500
\(\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 - 2019 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  // lock the following context
37  // to ensure that we don't
38  // print things over each other
39  // if we have trouble from
40  // multiple threads. release
41  // the lock before calling
42  // std::abort, though
43  static Mutex mutex;
44  {
45  std::lock_guard<std::mutex> lock(mutex);
46 
47  std::cerr
48  << std::endl
49  << std::endl
50  << "---------------------------------------------------------"
51  << std::endl
52  << "In one of the sub-threads of this program, an exception\n"
53  << "was thrown and not caught. Since exceptions do not\n"
54  << "propagate to the main thread, the library has caught it.\n"
55  << "The information carried by this exception is given below.\n"
56  << std::endl
57  << "---------------------------------------------------------"
58  << std::endl;
59  std::cerr << "Exception message: " << std::endl
60  << " " << exc.what() << std::endl
61  << "Exception type: " << std::endl
62  << " " << typeid(exc).name() << std::endl;
63  std::cerr << "Aborting!" << std::endl
64  << "---------------------------------------------------------"
65  << std::endl;
66  }
67 
68  std::abort();
69  }
70 
71 
72 
73  [[noreturn]] void handle_unknown_exception()
74  {
75  // lock the following context
76  // to ensure that we don't
77  // print things over each other
78  // if we have trouble from
79  // multiple threads. release
80  // the lock before calling
81  // std::abort, though
82  static Mutex mutex;
83  {
84  std::lock_guard<std::mutex> lock(mutex);
85 
86  std::cerr
87  << std::endl
88  << std::endl
89  << "---------------------------------------------------------"
90  << std::endl
91  << "In one of the sub-threads of this program, an exception\n"
92  << "was thrown and not caught. Since exceptions do not\n"
93  << "propagate to the main thread, the library has caught it.\n"
94  << std::endl
95  << "---------------------------------------------------------"
96  << std::endl;
97  std::cerr << "Type of exception is unknown, but not std::exception.\n"
98  << "No additional information is available.\n"
99  << "---------------------------------------------------------"
100  << std::endl;
101  }
102  std::abort();
103  }
104  } // namespace internal
105 
106 
107 
108  std::vector<std::pair<unsigned int, unsigned int>>
109  split_interval(const unsigned int begin,
110  const unsigned int end,
111  const unsigned int n_intervals)
112  {
113  Assert(end >= begin, ExcInternalError());
114 
115  const unsigned int n_elements = end - begin;
116  const unsigned int n_elements_per_interval = n_elements / n_intervals;
117  const unsigned int residual = n_elements % n_intervals;
118 
119  std::vector<std::pair<unsigned int, unsigned int>> return_values(
120  n_intervals);
121 
122  return_values[0].first = begin;
123  for (unsigned int i = 0; i < n_intervals; ++i)
124  {
125  if (i != n_intervals - 1)
126  {
127  return_values[i].second =
128  (return_values[i].first + n_elements_per_interval);
129  // distribute residual in
130  // division equally among
131  // the first few
132  // subintervals
133  if (i < residual)
134  ++return_values[i].second;
135  return_values[i + 1].first = return_values[i].second;
136  }
137  else
138  return_values[i].second = end;
139  }
140  return return_values;
141  }
142 } // namespace Threads
143 
144 
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:1466
void abort(const ExceptionBase &exc) noexcept
Definition: exceptions.cc:434
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:372
VectorType::value_type * end(VectorType &V)
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:371
VectorType::value_type * begin(VectorType &V)
void handle_std_exception(const std::exception &exc)
static ::ExceptionBase & ExcInternalError()