1 // ==========================================================================
2 // SeqAn - The Library for Sequence Analysis
3 // ==========================================================================
4 // Copyright (c) 2006-2018, Knut Reinert, FU Berlin
5 // All rights reserved.
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions are met:
9 //
10 // * Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 // * Redistributions in binary form must reproduce the above copyright
13 // notice, this list of conditions and the following disclaimer in the
14 // documentation and/or other materials provided with the distribution.
15 // * Neither the name of Knut Reinert or the FU Berlin nor the names of
16 // its contributors may be used to endorse or promote products derived
17 // from this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 // ARE DISCLAIMED. IN NO EVENT SHALL KNUT REINERT OR THE FU BERLIN BE LIABLE
23 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
29 // DAMAGE.
30 //
31 // ==========================================================================
32 // Author: Manuel Holtgrewe <manuel.holtgrewe@fu-berlin.de>
33 // Author: Enrico Siragusa <enrico.siragusa@fu-berlin.de>
34 // ==========================================================================
35 // Utility macros for parallelism.
36 // ==========================================================================
37
38 #ifndef SEQAN_PARALLEL_PARALLEL_MACROS_H_
39 #define SEQAN_PARALLEL_PARALLEL_MACROS_H_
40
41 /*!
42 * @macro SEQAN_OMP_PRAGMA
43 * @headerfile <seqan/parallel.h>
44 * @brief Portable conditional <tt>#pragma</tt> issuing if OpenMP is enabled.
45 *
46 * @signature SEQAN_OMP_PRAGMA(x)
47 *
48 * @param x The string to issue behind <tt>#pragma omp</tt>.
49 *
50 * @section Remarks
51 *
52 * This macro uses portable pragma generation, dependent on the macro <tt>_OPENMP</tt> being defined (as by
53 * the OpenMP standard).
54 *
55 * This is useful for disabling OpenMP pragmas on compilers that do not support OpenMP or when OpenMP is not enabled to
56 * suppress warnings.
57 *
58 * @section Example
59 *
60 * Parallelize loop with OpenMP if OpenMP is enabled:
61 *
62 * @code{.cpp}
63 * SEQAN_OMP_PRAGMA(parallel for) // becomes: #pragma omp parallel for
64 * for (int i = 0; i < x; ++i)
65 * {
66 * // Do work.
67 * }
68 * @endcode
69 *
70 * Make an addition atomic if OpenMP is enabled:
71 *
72 * @code{.cpp}
73 * SEQAN_OMP_PRAGMA(parallel atomic) // becomes: #pragma omp parallel atomic
74 * i += 1;
75 * @endcode
76 */
77
78 #ifdef _OPENMP
79
80 #include <omp.h>
81
82 #if defined(COMPILER_MSVC) || defined(COMPILER_WINTEL)
83 // MSVC __pragma-operator
84 #define SEQAN_OMP_PRAGMA(x) __pragma(omp x)
85 #else
86 // GCC _Pragma operator
87 #define SEQAN_DO_PRAGMA(x) _Pragma(# x)
88 #define SEQAN_OMP_PRAGMA(x) SEQAN_DO_PRAGMA(omp x)
89 #endif
90
91 #else // #ifdef _OPENMP
92
93 #define SEQAN_OMP_PRAGMA(x)
94
95 // low-level OpenMP runtime compatibility
omp_set_num_threads(int)96 inline void omp_set_num_threads(int)
97 {}
98
omp_get_num_threads()99 inline int omp_get_num_threads()
100 {
101 return 1;
102 }
103
omp_get_max_threads()104 inline int omp_get_max_threads()
105 {
106 return 1;
107 }
108
omp_get_thread_num()109 inline int omp_get_thread_num()
110 {
111 return 0;
112 }
113
omp_get_wtime()114 inline double omp_get_wtime()
115 {
116 return seqan::sysTime();
117 }
118
119 #endif // #ifdef _OPENMP
120
121 // ----------------------------------------------------------------------------
122 // Function getThreadId()
123 // ----------------------------------------------------------------------------
124
getThreadId()125 inline unsigned getThreadId()
126 {
127 #if defined(_OPENMP)
128 return omp_get_thread_num();
129
130 #else
131 return 0;
132
133 #endif
134 }
135
136 #endif // SEQAN_PARALLEL_PARALLEL_MACROS_H_
137