IMP  2.2.0
The Integrative Modeling Platform
base/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-2014 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( \
44  task default(none) firstprivate privatev if (omp_in_parallel())) { \
45  IMP::base::CreateLogContext task_context(name); \
46  action; \
47  } \
48  } else { \
49  action; \
50  }
51 
52 #define IMP_TASK_SHARED(privatev, sharedv, action, name) \
53  if (IMP::base::get_number_of_threads() > 1) { \
54  IMP_OMP_PRAGMA(task default(none) firstprivate privatev shared \
55  sharedv if (omp_in_parallel())) { \
56  IMP::base::CreateLogContext task_context(name); \
57  action; \
58  } \
59  } else { \
60  action; \
61  }
62 
63 #define IMP_THREADS(variables, action) \
64  if (IMP::base::get_number_of_threads() > 1) { \
65  IMP_OMP_PRAGMA(parallel shared variables \
66  num_threads(IMP::base::get_number_of_threads())) { \
67  IMP_PRAGMA(omp single) { \
68  IMP::base::CreateLogContext parallel_context("parallel"); \
69  action; \
70  } \
71  } \
72  } else { \
73  action; \
74  }
75 #endif
76 
77 #endif /* IMPBASE_THREAD_MACROS_H */
Logging and error reporting support.
Logging and error reporting support.
Various general useful macros for IMP.
Control for using multiple threads in IMP.