Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 : Copyright (c) 2014-2023 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 : namespace OpenMP { 30 : 31 : /// Set number of threads that can be used by openMP 32 : void setNumThreads(const unsigned nt); 33 : 34 : /// Get number of threads that can be used by openMP 35 : unsigned getNumThreads(); 36 : 37 : /// Returns a unique thread identification number within the current team 38 : unsigned getThreadNum(); 39 : 40 : /// get cacheline size 41 : unsigned getCachelineSize(); 42 : 43 : /// Get a reasonable number of threads so as to access to an array of size s located at x 44 : template<typename T> 45 353527 : unsigned getGoodNumThreads(const T* /*getTheType*/,unsigned n) { 46 : // this is more or less the equivalent of writing "unsigned getGoodNumThreads<T>(unsigned n)" 47 : 48 : // a factor two is necessary since there is no guarantee that x is aligned 49 : // to cache line boundary 50 353527 : unsigned m=n*sizeof(T)/(2*getCachelineSize()); 51 353527 : unsigned numThreads=getNumThreads(); 52 353527 : if(m>=numThreads) { 53 : m=numThreads; 54 : } else { 55 : //it is better to use either all the active threads or only one 56 : //this solves a performance problem as explained in issue #415 57 : m=1; 58 : } 59 353527 : return m; 60 : } 61 : 62 : /// Get a reasonable number of threads so as to access to vector v 63 : template<typename T> 64 1350 : unsigned getGoodNumThreads(const std::vector<T> & v) { 65 1350 : if(v.size()==0) return 1; 66 1314 : else return getGoodNumThreads(&v[0],v.size()); 67 : } 68 : 69 : }//namespace OpenMP 70 : }//namespace PLMD 71 : 72 : #endif