www.mooseframework.org
ParallelUniqueId.h
Go to the documentation of this file.
1 /****************************************************************/
2 /* DO NOT MODIFY THIS HEADER */
3 /* MOOSE - Multiphysics Object Oriented Simulation Environment */
4 /* */
5 /* (c) 2010 Battelle Energy Alliance, LLC */
6 /* ALL RIGHTS RESERVED */
7 /* */
8 /* Prepared by Battelle Energy Alliance, LLC */
9 /* Under Contract No. DE-AC07-05ID14517 */
10 /* With the U. S. Department of Energy */
11 /* */
12 /* See COPYRIGHT for full restrictions */
13 /****************************************************************/
14 
15 #ifndef PARALLELUNIQUEID_H
16 #define PARALLELUNIQUEID_H
17 
18 #include "MooseTypes.h" // included for namespace usage and THREAD_ID
19 
20 #include "libmesh/libmesh_common.h"
21 #include "libmesh/threads.h"
22 
23 #ifdef LIBMESH_HAVE_TBB_API
24 #include "tbb/concurrent_queue.h"
25 #include "tbb/tbb_thread.h"
26 #elif LIBMESH_HAVE_OPENMP
27 #include <omp.h>
28 #elif LIBMESH_HAVE_PTHREAD
29 #include <queue>
30 #include "MooseException.h"
31 #endif
32 
33 using namespace libMesh;
34 
36 {
37 public:
39  {
40 #ifdef LIBMESH_HAVE_TBB_API
41  _ids.pop(id);
42 #elif LIBMESH_HAVE_OPENMP
43  id = omp_get_thread_num();
44 #elif LIBMESH_HAVE_PTHREAD
45  Threads::spin_mutex::scoped_lock lock(_pthread_id_mutex);
46 
47  if (_ids.empty())
48  throw MooseException(
49  "No Thread IDs available in ParallelUniqueID. Did you forget to initialize()?");
50 
51  id = _ids.front();
52  _ids.pop();
53 #else
54  // There is no thread model active, so we're always on thread 0.
55  id = 0;
56 #endif
57  }
58 
60  {
61 #ifdef LIBMESH_HAVE_TBB_API
62  _ids.push(id);
63 #elif !defined(LIBMESH_HAVE_OPENMP) && defined(LIBMESH_HAVE_PTHREAD)
64  Threads::spin_mutex::scoped_lock lock(_pthread_id_mutex);
65 
66  _ids.push(id);
67 #endif
68  }
69 
74  static void initialize()
75  {
76  if (!_initialized)
77  {
78  _initialized = true;
79 
80 #if defined(LIBMESH_HAVE_TBB_API) || \
81  (!defined(LIBMESH_HAVE_OPENMP) && defined(LIBMESH_HAVE_PTHREAD))
82  for (unsigned int i = 0; i < libMesh::n_threads(); ++i)
83  _ids.push(i);
84 #endif
85  }
86  }
87 
89 
90 private:
91 #ifdef LIBMESH_HAVE_TBB_API
92  static tbb::concurrent_bounded_queue<unsigned int> _ids;
93 #elif !defined(LIBMESH_HAVE_OPENMP) && defined(LIBMESH_HAVE_PTHREAD)
94  static std::queue<unsigned int> _ids;
95  static Threads::spin_mutex _pthread_id_mutex;
96 #endif
97 
98  static bool _initialized;
99 };
100 
101 #endif // PARALLELUNIQUEID_H
static void initialize()
Must be called by main thread before any threaded computation! Do NOT call in a worker thread! ...
static tbb::concurrent_bounded_queue< unsigned int > _ids
The following methods are specializations for using the libMesh::Parallel::packed_range_* routines fo...
static bool _initialized
static Threads::spin_mutex _pthread_id_mutex
Provides a way for users to bail out of the current solve.
static std::queue< unsigned int > _ids
unsigned int THREAD_ID
Definition: MooseTypes.h:79