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 : /// Keep a dynamically linked OpenMP runtime resident when the kernel is unloaded. 32 : /// Currently needed on Linux, where runtime worker threads can outlive dlclose(). 33 : /// Set PLUMED_OPENMP_RETAIN_RUNTIME=no before constructing any PLUMED instance 34 : /// to disable retention. Once retained, a runtime cannot be made unloadable again. 35 : void retainRuntime(); 36 : 37 : /// Set number of threads that can be used by openMP 38 : void setNumThreads(const unsigned nt); 39 : 40 : /// Get number of threads that can be used by openMP 41 : unsigned getNumThreads(); 42 : 43 : /// Returns a unique thread identification number within the current team 44 : unsigned getThreadNum(); 45 : 46 : /// get cacheline size 47 : unsigned getCachelineSize(); 48 : 49 : /// Get a reasonable number of threads so as to access to an array of size s located at x 50 : template<typename T> 51 917044 : unsigned getGoodNumThreads(const T* /*getTheType*/,unsigned n) { 52 : // this is more or less the equivalent of writing "unsigned getGoodNumThreads<T>(unsigned n)" 53 : 54 : // a factor two is necessary since there is no guarantee that x is aligned 55 : // to cache line boundary 56 917044 : unsigned m=n*sizeof(T)/(2*getCachelineSize()); 57 917044 : unsigned numThreads=getNumThreads(); 58 917044 : if(m>=numThreads) { 59 : m=numThreads; 60 : } else { 61 : //it is better to use either all the active threads or only one 62 : //this solves a performance problem as explained in issue #415 63 : m=1; 64 : } 65 917044 : return m; 66 : } 67 : 68 : /// Get a reasonable number of threads so as to access to vector v 69 : template<typename T> 70 1350 : unsigned getGoodNumThreads(const std::vector<T> & v) { 71 1350 : if(v.size()==0) { 72 : return 1; 73 : } else { 74 1314 : return getGoodNumThreads(&v[0],v.size()); 75 : } 76 : } 77 : 78 : }//namespace OpenMP 79 : }//namespace PLMD 80 : 81 : #endif