1  /* Copyright (C) 2007-2023 Free Software Foundation, Inc.
       2     Contributed by Danny Smith <dannysmith@users.sourceforge.net>
       3  
       4     This file is part of the GNU Offloading and Multi Processing Library
       5     (libgomp).
       6  
       7     Libgomp is free software; you can redistribute it and/or modify it
       8     under the terms of the GNU General Public License as published by
       9     the Free Software Foundation; either version 3, or (at your option)
      10     any later version.
      11  
      12     Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
      13     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
      14     FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
      15     more details.
      16  
      17     Under Section 7 of GPL version 3, you are granted additional
      18     permissions described in the GCC Runtime Library Exception, version
      19     3.1, as published by the Free Software Foundation.
      20  
      21     You should have received a copy of the GNU General Public License and
      22     a copy of the GCC Runtime Library Exception along with this program;
      23     see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
      24     <http://www.gnu.org/licenses/>.  */
      25  
      26  /* This file contains system specific routines related to counting
      27     online processors and dynamic load balancing.  It is expected that
      28     a system may well want to write special versions of each of these.
      29  
      30     The following implementation uses win32 API routines.  */
      31  
      32  #include "libgomp.h"
      33  #define WIN32_LEAN_AND_MEAN
      34  #include <windows.h>
      35  
      36  /* Count the CPU's currently available to this process.  */
      37  static unsigned int
      38  count_avail_process_cpus ()
      39  {
      40    DWORD_PTR process_cpus;
      41    DWORD_PTR system_cpus;
      42  
      43    if (GetProcessAffinityMask (GetCurrentProcess (),
      44  			      &process_cpus, &system_cpus))
      45      {
      46        unsigned int count;
      47        for (count = 0; process_cpus != 0; process_cpus >>= 1)  
      48  	if (process_cpus & 1)
      49  	  count++;
      50        return count;
      51      }
      52    return 1;
      53  }
      54  
      55  /* At startup, determine the default number of threads.  It would seem
      56     this should be related to the number of cpus available to the process.  */
      57  
      58  void
      59  gomp_init_num_threads (void)
      60  {
      61    gomp_global_icv.nthreads_var = count_avail_process_cpus ();
      62  }
      63  
      64  /* When OMP_DYNAMIC is set, at thread launch determine the number of
      65     threads we should spawn for this team.  FIXME:  How do we adjust for
      66     load average on MS Windows?  */
      67  
      68  unsigned
      69  gomp_dynamic_max_threads (void)
      70  {
      71    unsigned int n_onln = count_avail_process_cpus ();
      72    unsigned int nthreads_var = gomp_icv (false)->nthreads_var;
      73    return n_onln > nthreads_var ? nthreads_var : n_onln;
      74  }
      75  
      76  int
      77  omp_get_num_procs (void)
      78  {
      79    return count_avail_process_cpus ();
      80  }
      81  
      82  ialias (omp_get_num_procs)