deal.II version GIT relicensing-6834-g5b78e6bcdf 2026-10-01 11:20:01+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: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
4// Copyright (C) 2000 - 2025 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Detailed license information governing the source code and contributions
9// can be found in LICENSE.md and CONTRIBUTING.md at the top level directory.
10//
11// -----------------------------------------------------------------------------
12
13#include <deal.II/base/mutex.h>
15
16#include <atomic>
17#include <cstdlib>
18#include <iostream>
19
20#ifdef DEAL_II_HAVE_UNISTD_H
21# include <unistd.h>
22#endif
23
24
26
27
28namespace Threads
29{
30 namespace internal
31 {
32 [[noreturn]] void
33 handle_std_exception(const std::exception &exc)
34 {
35 // lock the following context
36 // to ensure that we don't
37 // print things over each other
38 // if we have trouble from
39 // multiple threads. release
40 // the lock before calling
41 // std::abort, though
42 static Mutex mutex;
43 {
44 std::scoped_lock lock(mutex);
45
46 std::cerr
47 << std::endl
48 << std::endl
49 << "---------------------------------------------------------"
50 << std::endl
51 << "In one of the sub-threads of this program, an exception\n"
52 << "was thrown and not caught. Since exceptions do not\n"
53 << "propagate to the main thread, the library has caught it.\n"
54 << "The information carried by this exception is given below.\n"
55 << std::endl
56 << "---------------------------------------------------------"
57 << std::endl;
58 std::cerr << "Exception message: " << std::endl
59 << " " << exc.what() << std::endl
60 << "Exception type: " << std::endl
61 << " " << typeid(exc).name() << std::endl;
62 std::cerr << "Aborting!" << std::endl
63 << "---------------------------------------------------------"
64 << std::endl;
65 }
66
67 std::abort();
68 }
69
70
71
72 [[noreturn]] void
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::scoped_lock 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 {
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
*  iterator end()
*  *  iterator begin()
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:38
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:39
#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)