Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 : Copyright (c) 2014-2019 The plumed team
3 : (see the PEOPLE file at the root of the distribution for a list of names)
4 :
5 : See http://www.plumed.org for more information.
6 :
7 : This file is part of plumed, version 2.
8 :
9 : plumed is free software: you can redistribute it and/or modify
10 : it under the terms of the GNU Lesser General Public License as published by
11 : the Free Software Foundation, either version 3 of the License, or
12 : (at your option) any later version.
13 :
14 : plumed is distributed in the hope that it will be useful,
15 : but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : GNU Lesser General Public License for more details.
18 :
19 : You should have received a copy of the GNU Lesser General Public License
20 : along with plumed. If not, see <http://www.gnu.org/licenses/>.
21 : +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
22 :
23 : #include "OpenMP.h"
24 : #include "Tools.h"
25 : #include <cstdlib>
26 : #if defined(_OPENMP)
27 : #include <omp.h>
28 : #endif
29 :
30 : namespace PLMD {
31 :
32 :
33 : struct OpenMPVars {
34 : unsigned cacheline_size=512;
35 : bool cache_set=false;
36 : unsigned num_threads=1;
37 : bool nt_env_set=false;
38 : };
39 :
40 397607 : static OpenMPVars & getOpenMPVars() {
41 : static OpenMPVars vars;
42 397607 : return vars;
43 : }
44 :
45 0 : void OpenMP::setNumThreads(const unsigned nt) {
46 0 : getOpenMPVars().num_threads=nt;
47 0 : }
48 :
49 54970 : unsigned OpenMP::getCachelineSize() {
50 54970 : if(!getOpenMPVars().cache_set) {
51 567 : if(std::getenv("PLUMED_CACHELINE_SIZE")) Tools::convert(std::getenv("PLUMED_CACHELINE_SIZE"),getOpenMPVars().cacheline_size);
52 567 : getOpenMPVars().cache_set = true;
53 : }
54 54970 : return getOpenMPVars().cacheline_size;
55 : }
56 :
57 342637 : unsigned OpenMP::getNumThreads() {
58 342637 : if(!getOpenMPVars().nt_env_set) {
59 1134 : if(std::getenv("PLUMED_NUM_THREADS")) Tools::convert(std::getenv("PLUMED_NUM_THREADS"),getOpenMPVars().num_threads);
60 567 : getOpenMPVars().nt_env_set = true;
61 : }
62 342637 : return getOpenMPVars().num_threads;
63 : }
64 :
65 1248150 : unsigned OpenMP::getThreadNum() {
66 : #if defined(_OPENMP)
67 1248150 : return omp_get_thread_num();
68 : #else
69 : return 0;
70 : #endif
71 : }
72 :
73 :
74 :
75 : }
76 :
77 :
78 :
|