Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 : Copyright (c) 2014-2018 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 : #ifndef __PLUMED_tools_OpenMP_h
23 : #define __PLUMED_tools_OpenMP_h
24 :
25 : #include <vector>
26 :
27 : namespace PLMD {
28 :
29 : class OpenMP {
30 :
31 : public:
32 :
33 : /// Get number of threads that can be used by openMP
34 : static unsigned getNumThreads();
35 :
36 : /// Returns a unique thread identification number within the current team
37 : static unsigned getThreadNum();
38 :
39 : /// get cacheline size
40 : static unsigned getCachelineSize();
41 :
42 : /// Get a reasonable number of threads so as to access to an array of size s located at x
43 : template<typename T>
44 : static unsigned getGoodNumThreads(const T*x,unsigned s);
45 :
46 : /// Get a reasonable number of threads so as to access to vector v;
47 : template<typename T>
48 : static unsigned getGoodNumThreads(const std::vector<T> & v);
49 :
50 : };
51 :
52 : template<typename T>
53 38127 : unsigned OpenMP::getGoodNumThreads(const T*x,unsigned n) {
54 38127 : unsigned long p=(unsigned long) x;
55 : (void) p; // this is not to have warnings. notice that the pointer location is not used actually.
56 : // a factor two is necessary since there is no guarantee that x is aligned
57 : // to cache line boundary
58 38127 : unsigned m=n*sizeof(T)/(2*getCachelineSize());
59 38127 : unsigned numThreads=getNumThreads();
60 38127 : if(m>=numThreads) m=numThreads;
61 34353 : else m=1;
62 38127 : return m;
63 : }
64 :
65 :
66 : template<typename T>
67 0 : unsigned OpenMP::getGoodNumThreads(const std::vector<T> & v) {
68 0 : if(v.size()==0) return 1;
69 0 : else return getGoodNumThreads(&v[0],v.size());
70 : }
71 :
72 :
73 : }
74 :
75 : #endif
|