File: thread_management.cc

package info (click to toggle)
deal.ii 9.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 181,876 kB
  • sloc: cpp: 265,739; ansic: 52,054; python: 1,507; perl: 645; sh: 506; xml: 437; makefile: 73
file content (270 lines) | stat: -rw-r--r-- 7,471 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// ---------------------------------------------------------------------
//
// Copyright (C) 2000 - 2017 by the deal.II authors
//
// This file is part of the deal.II library.
//
// The deal.II library is free software; you can use it, redistribute
// it, and/or modify it under the terms of the GNU Lesser General
// Public License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// The full text of the license can be found in the file LICENSE at
// the top level of the deal.II distribution.
//
// ---------------------------------------------------------------------

#include <deal.II/base/thread_management.h>

#include <atomic>
#include <cstdlib>
#include <iostream>

#ifdef DEAL_II_HAVE_UNISTD_H
#  include <unistd.h>
#endif


DEAL_II_NAMESPACE_OPEN


namespace Threads
{
  namespace internal
  {
    static std::atomic<unsigned int> n_existing_threads_counter(1);


    void register_thread ()
    {
      ++n_existing_threads_counter;
    }



    void deregister_thread ()
    {
      --n_existing_threads_counter;
      Assert (n_existing_threads_counter >= 1,
              ExcInternalError());
    }



    [[noreturn]]
    void handle_std_exception (const std::exception &exc)
    {
      // lock the following context
      // to ensure that we don't
      // print things over each other
      // if we have trouble from
      // multiple threads. release
      // the lock before calling
      // std::abort, though
      static Mutex mutex;
      {
        Mutex::ScopedLock lock(mutex);

        std::cerr << std::endl << std::endl
                  << "---------------------------------------------------------"
                  << std::endl
                  << "In one of the sub-threads of this program, an exception\n"
                  << "was thrown and not caught. Since exceptions do not\n"
                  << "propagate to the main thread, the library has caught it.\n"
                  << "The information carried by this exception is given below.\n"
                  << std::endl
                  << "---------------------------------------------------------"
                  << std::endl;
        std::cerr << "Exception message: " << std::endl
                  << "  " << exc.what() << std::endl
                  << "Exception type: " << std::endl
                  << "  " << typeid(exc).name() << std::endl;
        std::cerr << "Aborting!" << std::endl
                  << "---------------------------------------------------------"
                  << std::endl;
      }

      std::abort ();
    }



    [[noreturn]]
    void handle_unknown_exception ()
    {
      // lock the following context
      // to ensure that we don't
      // print things over each other
      // if we have trouble from
      // multiple threads. release
      // the lock before calling
      // std::abort, though
      static Mutex mutex;
      {
        Mutex::ScopedLock lock(mutex);

        std::cerr << std::endl << std::endl
                  << "---------------------------------------------------------"
                  << std::endl
                  << "In one of the sub-threads of this program, an exception\n"
                  << "was thrown and not caught. Since exceptions do not\n"
                  << "propagate to the main thread, the library has caught it.\n"
                  << std::endl
                  << "---------------------------------------------------------"
                  << std::endl;
        std::cerr << "Type of exception is unknown, but not std::exception.\n"
                  << "No additional information is available.\n"
                  << "---------------------------------------------------------"
                  << std::endl;
      }
      std::abort ();
    }
  }



  unsigned int n_existing_threads ()
  {
    return internal::n_existing_threads_counter;
  }


  unsigned int this_thread_id ()
  {
#ifdef SYS_gettid
    const pid_t this_id = syscall(SYS_gettid);
#elif defined(DEAL_II_HAVE_UNISTD_H) && defined(DEAL_II_HAVE_GETPID)
    const pid_t this_id = getpid();
#else
    const unsigned int this_id = 0;
#endif

    return static_cast<unsigned int>(this_id);
  }



#ifndef DEAL_II_WITH_THREADS
  DummyBarrier::DummyBarrier (const unsigned int  count,
                              const char *,
                              void *)
  {
    (void)count;
    Assert (count == 1, ExcBarrierSizeNotUseful(count));
  }


#else
#  ifdef DEAL_II_USE_MT_POSIX


#ifndef DEAL_II_USE_MT_POSIX_NO_BARRIERS
  PosixThreadBarrier::PosixThreadBarrier (const unsigned int  count,
                                          const char *,
                                          void *)
  {
    pthread_barrier_init (&barrier, nullptr, count);
  }

#else

  PosixThreadBarrier::PosixThreadBarrier (const unsigned int  count,
                                          const char *,
                                          void *)
    : count (count)
  {
    // throw an exception unless we
    // have the special case that a
    // count of 1 is given, since
    // then waiting for a barrier is
    // a no-op, and we don't need the
    // POSIX functionality
    AssertThrow (count == 1,
                 ExcMessage ("Your local POSIX installation does not support\n"
                             "POSIX barriers. You will not be able to use\n"
                             "this class, but the rest of the threading\n"
                             "functionality is available."));
  }
#endif



  PosixThreadBarrier::~PosixThreadBarrier ()
  {
#ifndef DEAL_II_USE_MT_POSIX_NO_BARRIERS
    pthread_barrier_destroy (&barrier);
#else
    // unless the barrier is a no-op,
    // complain again (how did we get
    // here then?)
    if (count != 1)
      std::abort ();
#endif
  }



  int
  PosixThreadBarrier::wait ()
  {
#ifndef DEAL_II_USE_MT_POSIX_NO_BARRIERS
    return pthread_barrier_wait (&barrier);
#else
    // in the special case, this
    // function is a no-op. otherwise
    // complain about the missing
    // POSIX functions
    if (count == 1)
      return 0;
    else
      {
        std::abort ();
        return 1;
      };
#endif
  }




#  endif
#endif



  std::vector<std::pair<unsigned int,unsigned int> >
  split_interval (const unsigned int begin,
                  const unsigned int end,
                  const unsigned int n_intervals)
  {
    Assert (end >= begin, ExcInternalError());

    const unsigned int n_elements              = end-begin;
    const unsigned int n_elements_per_interval = n_elements / n_intervals;
    const unsigned int residual                = n_elements % n_intervals;

    std::vector<std::pair<unsigned int,unsigned int> > return_values (n_intervals);

    return_values[0].first = begin;
    for (unsigned int i=0; i<n_intervals; ++i)
      {
        if (i != n_intervals-1)
          {
            return_values[i].second = (return_values[i].first
                                       + n_elements_per_interval);
            // distribute residual in
            // division equally among
            // the first few
            // subintervals
            if (i < residual)
              ++return_values[i].second;
            return_values[i+1].first = return_values[i].second;
          }
        else
          return_values[i].second = end;
      };
    return return_values;
  }
}   // end namespace Thread


DEAL_II_NAMESPACE_CLOSE