Reference documentation for deal.II version GIT relicensing-437-g81ec864850 2024-04-19 07:30:02+00:00
\(\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\}}\)
Loading...
Searching...
No Matches
thread_management.cc
Go to the documentation of this file.
1// ------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: LGPL-2.1-or-later
4// Copyright (C) 2000 - 2021 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Part of the source code is dual licensed under Apache-2.0 WITH
9// LLVM-exception OR LGPL-2.1-or-later. Detailed license information
10// governing the source code and code contributions can be found in
11// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
12//
13// ------------------------------------------------------------------------
14
16
17#include <atomic>
18#include <cstdlib>
19#include <iostream>
20
21#ifdef DEAL_II_HAVE_UNISTD_H
22# include <unistd.h>
23#endif
24
25
27
28
29namespace Threads
30{
31 namespace internal
32 {
33 [[noreturn]] void
34 handle_std_exception(const std::exception &exc)
35 {
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
75 {
76 // lock the following context
77 // to ensure that we don't
78 // print things over each other
79 // if we have trouble from
80 // multiple threads. release
81 // the lock before calling
82 // std::abort, though
83 static Mutex mutex;
84 {
85 std::lock_guard<std::mutex> lock(mutex);
86
87 std::cerr
88 << std::endl
89 << std::endl
90 << "---------------------------------------------------------"
91 << std::endl
92 << "In one of the sub-threads of this program, an exception\n"
93 << "was thrown and not caught. Since exceptions do not\n"
94 << "propagate to the main thread, the library has caught it.\n"
95 << std::endl
96 << "---------------------------------------------------------"
97 << std::endl;
98 std::cerr << "Type of exception is unknown, but not std::exception.\n"
99 << "No additional information is available.\n"
100 << "---------------------------------------------------------"
101 << std::endl;
102 }
103 std::abort();
104 }
105 } // namespace internal
106
107
108
109 std::vector<std::pair<unsigned int, unsigned int>>
110 split_interval(const unsigned int begin,
111 const unsigned int end,
112 const unsigned int n_intervals)
113 {
114 Assert(end >= begin, ExcInternalError());
115
116 const unsigned int n_elements = end - begin;
117 const unsigned int n_elements_per_interval = n_elements / n_intervals;
118 const unsigned int residual = n_elements % n_intervals;
119
120 std::vector<std::pair<unsigned int, unsigned int>> return_values(
121 n_intervals);
122
123 return_values[0].first = begin;
124 for (unsigned int i = 0; i < n_intervals; ++i)
125 {
126 if (i != n_intervals - 1)
127 {
128 return_values[i].second =
129 (return_values[i].first + n_elements_per_interval);
130 // distribute residual in
131 // division equally among
132 // the first few
133 // subintervals
134 if (i < residual)
135 ++return_values[i].second;
136 return_values[i + 1].first = return_values[i].second;
137 }
138 else
139 return_values[i].second = end;
140 }
141 return return_values;
142 }
143} // namespace Threads
144
145
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:502
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:503
#define Assert(cond, exc)
static ::ExceptionBase & ExcInternalError()
std::vector< std::pair< unsigned int, unsigned int > > split_interval(const unsigned int begin, const unsigned int end, const unsigned int n_intervals)
void handle_std_exception(const std::exception &exc)