LCOV - code coverage report
Current view: top level - tools - OpenMP.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 17 17 100.0 %
Date: 2024-10-18 13:59:31 Functions: 4 4 100.0 %

          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             : 
      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             : namespace OpenMP {
      33             : ///singleton struct to treat the openMP setting as a global variables, but with a layer of encapsulation
      34             : struct OpenMPVars {
      35             :   unsigned cacheline_size=512;
      36             :   bool cache_set=false;
      37             :   unsigned num_threads=1;
      38             :   bool nt_env_set=false;
      39             :   static OpenMPVars & get() {
      40             :     static OpenMPVars vars;
      41             :     return vars;
      42             :   }
      43             : private:
      44             :   OpenMPVars()=default;
      45             :   OpenMPVars(OpenMPVars&)=delete;
      46             :   OpenMPVars& operator=(OpenMPVars&&)=delete;
      47             : };
      48             : 
      49           1 : void setNumThreads(const unsigned nt) {
      50           1 :   OpenMPVars::get().num_threads=nt;
      51           1 : }
      52             : 
      53      354821 : unsigned getCachelineSize() {
      54      354821 :   if(!OpenMPVars::get().cache_set) {
      55         965 :     if(std::getenv("PLUMED_CACHELINE_SIZE")) {
      56           1 :       Tools::convert(std::getenv("PLUMED_CACHELINE_SIZE"),OpenMPVars::get().cacheline_size);
      57             :     }
      58         965 :     OpenMPVars::get().cache_set = true;
      59             :   }
      60      354821 :   return OpenMPVars::get().cacheline_size;
      61             : }
      62             : 
      63    35076034 : unsigned getNumThreads() {
      64    35076034 :   if(!OpenMPVars::get().nt_env_set) {
      65         965 :     if(std::getenv("PLUMED_NUM_THREADS")) {
      66         965 :       Tools::convert(std::getenv("PLUMED_NUM_THREADS"),OpenMPVars::get().num_threads);
      67             :     }
      68         965 :     OpenMPVars::get().nt_env_set = true;
      69             :   }
      70    35076034 :   return OpenMPVars::get().num_threads;
      71             : }
      72             : 
      73    61696114 : unsigned getThreadNum() {
      74             : #if defined(_OPENMP)
      75    61696114 :   return omp_get_thread_num();
      76             : #else
      77             :   return 0;
      78             : #endif
      79             : }
      80             : 
      81             : }//namespace OpenMP
      82             : }//namespace PLMD

Generated by: LCOV version 1.16