1 /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2    Copyright (c) 2014-2021 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 
getOpenMPVars()40 static OpenMPVars & getOpenMPVars() {
41   static OpenMPVars vars;
42   return vars;
43 }
44 
setNumThreads(const unsigned nt)45 void OpenMP::setNumThreads(const unsigned nt) {
46   getOpenMPVars().num_threads=nt;
47 }
48 
getCachelineSize()49 unsigned OpenMP::getCachelineSize() {
50   if(!getOpenMPVars().cache_set) {
51     if(std::getenv("PLUMED_CACHELINE_SIZE")) Tools::convert(std::getenv("PLUMED_CACHELINE_SIZE"),getOpenMPVars().cacheline_size);
52     getOpenMPVars().cache_set = true;
53   }
54   return getOpenMPVars().cacheline_size;
55 }
56 
getNumThreads()57 unsigned OpenMP::getNumThreads() {
58   if(!getOpenMPVars().nt_env_set) {
59     if(std::getenv("PLUMED_NUM_THREADS")) Tools::convert(std::getenv("PLUMED_NUM_THREADS"),getOpenMPVars().num_threads);
60     getOpenMPVars().nt_env_set = true;
61   }
62   return getOpenMPVars().num_threads;
63 }
64 
getThreadNum()65 unsigned OpenMP::getThreadNum() {
66 #if defined(_OPENMP)
67   return omp_get_thread_num();
68 #else
69   return 0;
70 #endif
71 }
72 
73 
74 
75 }
76 
77 
78 
79