So-Bogus
A c++ sparse block matrix library aimed at Second Order cone problems
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
Threads.hpp
1 /*
2  * This file is part of bogus, a C++ sparse block matrix library.
3  *
4  * Copyright 2016 Gilles Daviet <gdaviet@gmail.com>
5  *
6  * This Source Code Form is subject to the terms of the Mozilla Public
7  * License, v. 2.0. If a copy of the MPL was not distributed with this
8  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 */
10 
11 #ifndef BOGUS_UTILS_THREADS_HPP
12 #define BOGUS_UTILS_THREADS_HPP
13 
14 #include "../Block/Constants.hpp"
15 
16 #ifndef BOGUS_DONT_PARALLELIZE
17 #include <omp.h>
18 #endif
19 
20 namespace bogus {
21 
22 #ifdef BOGUS_DONT_PARALLELIZE
23  struct WithMaxThreads {
24  explicit WithMaxThreads( int ) {}
25  int nThreads() const { return 1 ; }
26  } ;
27 
28 #else
29  struct WithMaxThreads {
30 
31  explicit WithMaxThreads( int maxThreads )
32  : m_prevMaxThreads( omp_get_max_threads() )
33  , m_newMaxThreads( maxThreads == 0 ? m_prevMaxThreads : maxThreads )
34  {
35  omp_set_num_threads( m_newMaxThreads ) ;
36  }
37 
38  ~WithMaxThreads() {
39  omp_set_num_threads( m_prevMaxThreads ) ;
40  }
41 
42 
43  int nThreads() const { return m_newMaxThreads ; }
44 
45  private:
46  const int m_prevMaxThreads ;
47  const int m_newMaxThreads ;
48 
49  };
50 #endif
51 
52 } // bogus
53 
54 #endif
Definition: Threads.hpp:23