/usr/include/libmesh/threads.h is in libmesh-dev 0.7.1-2ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 | // $Id: threads.h 3874 2010-07-02 21:57:26Z roystgnr $
// The libMesh Finite Element Library.
// Copyright (C) 2002-2008 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
// This library is free software; you can 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.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#ifndef __threads_h__
#define __threads_h__
// System includes
// Local includes
#include "libmesh_config.h"
#include "libmesh_common.h" // for libmesh_assert
// Threading building blocks includes
#ifdef LIBMESH_HAVE_TBB_API
# include "libmesh_logging.h" // only mess with the perflog if we are really multithreaded
# include "tbb/tbb_stddef.h"
# include "tbb/blocked_range.h"
# include "tbb/parallel_for.h"
# include "tbb/parallel_reduce.h"
# include "tbb/task_scheduler_init.h"
# include "tbb/partitioner.h"
# include "tbb/spin_mutex.h"
# include "tbb/recursive_mutex.h"
# include "tbb/atomic.h"
#endif
namespace libMesh
{
/**
* The Threads namespace is for wrapper functions
* for common general multithreading algorithms and tasks.
*/
namespace Threads
{
/**
* A boolean which is true iff we are in a Threads:: function
* It may be useful to assert(!Threads::in_threads) in any code
* which is known to not be thread-safe.
*/
extern bool in_threads;
/**
* We use a class to turn Threads::in_threads on and off, to be
* exception-safe.
*/
class BoolAcquire {
public:
BoolAcquire(bool& b) : _b(b) { libmesh_assert(!_b); _b = true; }
~BoolAcquire() { libmesh_assert(_b); _b = false; }
private:
bool& _b;
};
#ifdef LIBMESH_HAVE_TBB_API
//-------------------------------------------------------------------
/**
* Scheduler to manage threads.
*/
typedef tbb::task_scheduler_init task_scheduler_init;
//-------------------------------------------------------------------
/**
* Dummy "splitting object" used to distinguish splitting constructors
* from copy constructors.
*/
typedef tbb::split split;
//-------------------------------------------------------------------
/**
* Exectue the provided function object in parallel on the specified
* range.
*/
template <typename Range, typename Body>
inline
void parallel_for (const Range &range, const Body &body)
{
BoolAcquire b(in_threads);
#ifdef LIBMESH_ENABLE_PERFORMANCE_LOGGING
if (libMesh::n_threads() > 1)
libMesh::perflog.disable_logging();
#endif
if (libMesh::n_threads() > 1)
tbb::parallel_for (range, body, tbb::auto_partitioner());
else
body(range);
#ifdef LIBMESH_ENABLE_PERFORMANCE_LOGGING
if (libMesh::n_threads() > 1)
libMesh::perflog.enable_logging();
#endif
}
//-------------------------------------------------------------------
/**
* Exectue the provided function object in parallel on the specified
* range with the specified partitioner.
*/
template <typename Range, typename Body, typename Partitioner>
inline
void parallel_for (const Range &range, const Body &body, const Partitioner &partitioner)
{
BoolAcquire b(in_threads);
#ifdef LIBMESH_ENABLE_PERFORMANCE_LOGGING
if (libMesh::n_threads() > 1)
libMesh::perflog.disable_logging();
#endif
if (libMesh::n_threads() > 1)
tbb::parallel_for (range, body, partitioner);
else
body(range);
#ifdef LIBMESH_ENABLE_PERFORMANCE_LOGGING
if (libMesh::n_threads() > 1)
libMesh::perflog.enable_logging();
#endif
}
//-------------------------------------------------------------------
/**
* Exectue the provided reduction operation in parallel on the specified
* range.
*/
template <typename Range, typename Body>
inline
void parallel_reduce (const Range &range, Body &body)
{
BoolAcquire b(in_threads);
#ifdef LIBMESH_ENABLE_PERFORMANCE_LOGGING
if (libMesh::n_threads() > 1)
libMesh::perflog.disable_logging();
#endif
if (libMesh::n_threads() > 1)
tbb::parallel_reduce (range, body, tbb::auto_partitioner());
else
body(range);
#ifdef LIBMESH_ENABLE_PERFORMANCE_LOGGING
if (libMesh::n_threads() > 1)
libMesh::perflog.enable_logging();
#endif
}
//-------------------------------------------------------------------
/**
* Exectue the provided reduction operation in parallel on the specified
* range with the specified partitioner.
*/
template <typename Range, typename Body, typename Partitioner>
inline
void parallel_reduce (const Range &range, Body &body, const Partitioner &partitioner)
{
BoolAcquire b(in_threads);
#ifdef LIBMESH_ENABLE_PERFORMANCE_LOGGING
if (libMesh::n_threads() > 1)
libMesh::perflog.disable_logging();
#endif
if (libMesh::n_threads() > 1)
tbb::parallel_reduce (range, body);
else
body(range);
#ifdef LIBMESH_ENABLE_PERFORMANCE_LOGGING
if (libMesh::n_threads() > 1)
libMesh::perflog.enable_logging();
#endif
}
//-------------------------------------------------------------------
/**
* Spin mutex. Implements mutual exclusion by busy-waiting in user
* space for the lock to be acquired.
*/
typedef tbb::spin_mutex spin_mutex;
//-------------------------------------------------------------------
/**
* Recursive mutex. Implements mutual exclusion by busy-waiting in user
* space for the lock to be acquired. The same thread can aquire the
* same lock multiple times
*/
typedef tbb::recursive_mutex recursive_mutex;
//-------------------------------------------------------------------
/**
* Defines atomic operations which can only be executed on a
* single thread at a time. This is used in reference counting,
* for example, to allow count++/count-- to work.
*/
template <typename T>
class atomic : public tbb::atomic<T> {};
#else //LIBMESH_HAVE_TBB_API
//-------------------------------------------------------------------
/**
* Scheduler to manage threads.
*/
class task_scheduler_init
{
public:
static const int automatic = -1;
task_scheduler_init (int = automatic) {};
void initialize (int = automatic) {};
void terminate () {};
};
//-------------------------------------------------------------------
/**
* Dummy "splitting object" used to distinguish splitting constructors
* from copy constructors.
*/
class split {};
//-------------------------------------------------------------------
/**
* Exectue the provided function object in parallel on the specified
* range.
*/
template <typename Range, typename Body>
inline
void parallel_for (const Range &range, const Body &body)
{
BoolAcquire b(in_threads);
body(range);
}
//-------------------------------------------------------------------
/**
* Exectue the provided function object in parallel on the specified
* range with the specified partitioner.
*/
template <typename Range, typename Body, typename Partitioner>
inline
void parallel_for (const Range &range, const Body &body, const Partitioner &)
{
BoolAcquire b(in_threads);
body(range);
}
//-------------------------------------------------------------------
/**
* Exectue the provided reduction operation in parallel on the specified
* range.
*/
template <typename Range, typename Body>
inline
void parallel_reduce (const Range &range, Body &body)
{
BoolAcquire b(in_threads);
body(range);
}
//-------------------------------------------------------------------
/**
* Exectue the provided reduction operation in parallel on the specified
* range with the specified partitioner.
*/
template <typename Range, typename Body, typename Partitioner>
inline
void parallel_reduce (const Range &range, Body &body, const Partitioner &)
{
BoolAcquire b(in_threads);
body(range);
}
//-------------------------------------------------------------------
/**
* Spin mutex. Implements mutual exclusion by busy-waiting in user
* space for the lock to be acquired.
*/
class spin_mutex
{
public:
spin_mutex() {}
class scoped_lock
{
public:
scoped_lock () {}
scoped_lock ( spin_mutex& ) {}
void acquire ( spin_mutex& ) {}
void release () {}
};
};
//-------------------------------------------------------------------
/**
* Recursive mutex. Implements mutual exclusion by busy-waiting in user
* space for the lock to be acquired.
*/
class recursive_mutex
{
public:
recursive_mutex() {}
class scoped_lock
{
public:
scoped_lock () {}
scoped_lock ( recursive_mutex& ) {}
void acquire ( recursive_mutex& ) {}
void release () {}
};
};
//-------------------------------------------------------------------
/**
* Defines atomic operations which can only be executed on a
* single thread at a time.
*/
template <typename T>
class atomic
{
public:
atomic () : _val(0) {}
operator T& () { return _val; }
private:
T _val;
};
#endif // #ifdef LIBMESH_HAVE_TBB_API
/**
* Blocked range which can be subdivided and executed in parallel.
*/
template <typename T>
class BlockedRange
{
public:
/**
* Allows an \p StoredRange to behave like an STL container.
*/
typedef T const_iterator;
/**
* Constructor. Optionally takes the \p grainsize parameter, which is the
* smallest chunk the range may be broken into for parallel
* execution.
*/
BlockedRange (const unsigned int grainsize = 1000) :
_grainsize(grainsize)
{}
/**
* Constructor. Takes the beginning and end of the range.
* Optionally takes the \p grainsize parameter, which is the
* smallest chunk the range may be broken into for parallel
* execution.
*/
BlockedRange (const const_iterator first,
const const_iterator last,
const unsigned int grainsize = 1000) :
_grainsize(grainsize)
{
this->reset(first, last);
}
/**
* Copy constructor. The \p StoredRange can be copied into
* subranges for parallel execution. In this way the
* initial \p StoredRange can be thought of as the root of
* a binary tree. The root element is the only element
* which interacts with the user. It takes a specified
* range of objects and packs it into a contiguous vector
* which can be split efficiently. However, there is no need
* for the child ranges to contain this vector, so long as
* the parent outlives the children. So we implement
* the copy constructor to specifically omit the \p _objs
* vector.
*/
BlockedRange (const BlockedRange<T> &r):
_end(r._end),
_begin(r._begin),
_grainsize(r._grainsize)
{}
/**
* Splits the range \p r. The first half
* of the range is left in place, the second
* half of the range is placed in *this.
*/
BlockedRange (BlockedRange<T> &r, Threads::split ) :
_end(r._end),
_begin(r._begin),
_grainsize(r._grainsize)
{
const_iterator
beginning = r._begin,
ending = r._end,
middle = beginning + (ending - beginning)/2u;
r._end = _begin = middle;
}
/**
* Resets the \p StoredRange to contain [first,last).
*/
void reset (const const_iterator first,
const const_iterator last)
{
_begin = first;
_end = last;
}
/**
* Beginning of the range.
*/
const_iterator begin () const { return _begin; }
/**
* End of the range.
*/
const_iterator end () const { return _end; }
/**
* The grain size for the range. The range will be subdivided into
* subranges not to exceed the grain size.
*/
unsigned int grainsize () const {return _grainsize;}
/**
* Set the grain size.
*/
void grainsize (const unsigned int &gs) {_grainsize = gs;}
/**
* \return the size of the range.
*/
int size () const { return (_end -_begin); }
//------------------------------------------------------------------------
// Methods that implement Range concept
//------------------------------------------------------------------------
/**
* Returns true if the range is empty.
*/
bool empty() const { return (_begin == _end); }
/**
* Returns true if the range can be subdivided.
*/
bool is_divisible() const { return ((_begin + this->grainsize()) < _end); }
private:
const_iterator _end;
const_iterator _begin;
unsigned int _grainsize;
};
/**
* A spin mutex object which
*/
extern spin_mutex spin_mtx;
/**
* A recursive mutex object which
*/
extern recursive_mutex recursive_mtx;
} // namespace Threads
} // namespace libMesh
#endif // #define __threads_h__
|