Line data Source code
1 :
2 : /* Copyright (c) 2007-2015, Stefan Eilemann <eile@equalizergraphics.com>
3 : *
4 : * This library is free software; you can redistribute it and/or modify it under
5 : * the terms of the GNU Lesser General Public License version 2.1 as published
6 : * by the Free Software Foundation.
7 : *
8 : * This library is distributed in the hope that it will be useful, but WITHOUT
9 : * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10 : * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
11 : * details.
12 : *
13 : * You should have received a copy of the GNU Lesser General Public License
14 : * along with this library; if not, write to the Free Software Foundation, Inc.,
15 : * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 : */
17 :
18 : #include "omp.h"
19 : #include "debug.h"
20 :
21 : #include <cstdlib>
22 :
23 : namespace lunchbox
24 : {
25 : namespace
26 : {
27 : static unsigned _setupNThreads();
28 27 : unsigned _nThreads = _setupNThreads();
29 :
30 27 : static unsigned _setupNThreads()
31 : {
32 : #ifdef LUNCHBOX_USE_OPENMP
33 27 : const char* nThreadsEnv = getenv( "OMP_NUM_THREADS" );
34 27 : unsigned nThreads = 0;
35 :
36 27 : if( nThreadsEnv )
37 0 : nThreads = atoi( nThreadsEnv );
38 :
39 27 : if( nThreads < 1 )
40 27 : nThreads = omp_get_num_procs();
41 :
42 27 : LBASSERT( nThreads > 0 );
43 27 : omp_set_num_threads( nThreads );
44 27 : return nThreads;
45 : #else
46 : return 1;
47 : #endif
48 : }
49 : }
50 :
51 0 : unsigned OMP::getNThreads()
52 : {
53 0 : return _nThreads;
54 : }
55 :
56 0 : int OMP::getThreadNum()
57 : {
58 : #ifdef LUNCHBOX_USE_OPENMP
59 0 : return omp_get_thread_num();
60 : #else
61 : return 0;
62 : #endif
63 : }
64 :
65 81 : }
|