1193323Sed/* Copyright (C) 2007-2020 Free Software Foundation, Inc.
2193323Sed   Contributed by Danny Smith <dannysmith@users.sourceforge.net>
3193323Sed
4193323Sed   This file is part of the GNU Offloading and Multi Processing Library
5193323Sed   (libgomp).
6193323Sed
7193323Sed   Libgomp is free software; you can redistribute it and/or modify it
8193323Sed   under the terms of the GNU General Public License as published by
9193323Sed   the Free Software Foundation; either version 3, or (at your option)
10193323Sed   any later version.
11193323Sed
12193323Sed   Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
13193323Sed   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14193323Sed   FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15193323Sed   more details.
16193323Sed
17193323Sed   Under Section 7 of GPL version 3, you are granted additional
18193323Sed   permissions described in the GCC Runtime Library Exception, version
19193323Sed   3.1, as published by the Free Software Foundation.
20193323Sed
21193323Sed   You should have received a copy of the GNU General Public License and
22193323Sed   a copy of the GCC Runtime Library Exception along with this program;
23193323Sed   see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
24193323Sed   <http://www.gnu.org/licenses/>.  */
25193323Sed
26193323Sed/* This file contains system specific routines related to counting
27193323Sed   online processors and dynamic load balancing.  It is expected that
28193323Sed   a system may well want to write special versions of each of these.
29193323Sed
30193323Sed   The following implementation uses win32 API routines.  */
31193323Sed
32193323Sed#include "libgomp.h"
33195340Sed#include <windows.h>
34193323Sed
35193323Sed/* Count the CPU's currently available to this process.  */
36193323Sedstatic unsigned int
37193323Sedcount_avail_process_cpus ()
38193323Sed{
39193323Sed  DWORD_PTR process_cpus;
40193323Sed  DWORD_PTR system_cpus;
41
42  if (GetProcessAffinityMask (GetCurrentProcess (),
43			      &process_cpus, &system_cpus))
44    {
45      unsigned int count;
46      for (count = 0; process_cpus != 0; process_cpus >>= 1)
47	if (process_cpus & 1)
48	  count++;
49      return count;
50    }
51  return 1;
52}
53
54/* At startup, determine the default number of threads.  It would seem
55   this should be related to the number of cpus available to the process.  */
56
57void
58gomp_init_num_threads (void)
59{
60  gomp_global_icv.nthreads_var = count_avail_process_cpus ();
61}
62
63/* When OMP_DYNAMIC is set, at thread launch determine the number of
64   threads we should spawn for this team.  FIXME:  How do we adjust for
65   load average on MS Windows?  */
66
67unsigned
68gomp_dynamic_max_threads (void)
69{
70  unsigned int n_onln = count_avail_process_cpus ();
71  unsigned int nthreads_var = gomp_icv (false)->nthreads_var;
72  return n_onln > nthreads_var ? nthreads_var : n_onln;
73}
74
75int
76omp_get_num_procs (void)
77{
78  return count_avail_process_cpus ();
79}
80
81ialias (omp_get_num_procs)
82