IMP  2.0.1
The Integrative Modeling Platform
thread_macros.h
Go to the documentation of this file.
1 /**
2  * \file IMP/base/thread_macros.h
3  * \brief Control for OpenMP
4  *
5  * Copyright 2007-2013 IMP Inventors. All rights reserved.
6  *
7  */
8 
9 #ifndef IMPBASE_THREAD_MACROS_H
10 #define IMPBASE_THREAD_MACROS_H
11 
12 #include "threads.h"
13 #include "utility_macros.h"
14 #include "log_macros.h"
15 #ifdef _OPENMP
17 #include <omp.h>
18 #endif
19 
20 #if defined(IMP_DOXYGEN) || !defined(_OPENMP)
21 
22 /** Start a new OpenMP task with the next block passing the
23  list of passed variables.*/
24 #define IMP_TASK(privatev, action, name) action
25 
26 /** Start a new OpenMP task with the next block passing the
27  list of passed variables.*/
28 #define IMP_TASK_SHARED(privatev, sharedv, action, name) action
29 
30 /** Start a parallel section if one is not already started.
31  */
32 #define IMP_THREADS(variables, action) action
33 
34 /** Pragma for OpenMP */
35 #define IMP_OMP_PRAGMA(x)
36 
37 #else
38 
39 #define IMP_OMP_PRAGMA(x) IMP_PRAGMA(omp x)
40 
41 #define IMP_TASK(privatev, action, name) \
42  if (IMP::base::get_number_of_threads() > 1) { \
43  IMP_OMP_PRAGMA(task default(none) firstprivate privatev \
44  if (omp_in_parallel()) \
45  \
46  ) \
47  { \
48  IMP::base::CreateLogContext task_context(name); \
49  action; \
50  } \
51  } else { \
52  action; \
53  }
54 
55 #define IMP_TASK_SHARED(privatev, sharedv, action, name) \
56  if (IMP::base::get_number_of_threads() > 1) { \
57  IMP_OMP_PRAGMA(task default(none) firstprivate privatev \
58  shared sharedv \
59  if (omp_in_parallel())) \
60  { \
61  IMP::base::CreateLogContext task_context(name); \
62  action; \
63  } \
64  } else { \
65  action; \
66  }
67 
68 
69 #define IMP_THREADS(variables, action) \
70  if (IMP::base::get_number_of_threads() > 1) { \
71  IMP_OMP_PRAGMA(parallel shared variables \
72  num_threads(IMP::base::get_number_of_threads())) \
73  { \
74  IMP_PRAGMA(omp single) \
75  { \
76  IMP::base::CreateLogContext parallel_context("parallel"); \
77  action; \
78  } \
79  } \
80  } else { \
81  action; \
82  }
83 #endif
84 
85 #endif /* IMPBASE_THREAD_MACROS_H */