proc.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /* Copyright (C) 2005-2022 Free Software Foundation, Inc.
  2. Contributed by Jakub Jelinek <jakub@redhat.com>.
  3. This file is part of the GNU Offloading and Multi Processing Library
  4. (libgomp).
  5. Libgomp is free software; you can redistribute it and/or modify it
  6. under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3, or (at your option)
  8. any later version.
  9. Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. more details.
  13. Under Section 7 of GPL version 3, you are granted additional
  14. permissions described in the GCC Runtime Library Exception, version
  15. 3.1, as published by the Free Software Foundation.
  16. You should have received a copy of the GNU General Public License and
  17. a copy of the GCC Runtime Library Exception along with this program;
  18. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. <http://www.gnu.org/licenses/>. */
  20. /* This file contains system specific routines related to counting
  21. online processors and dynamic load balancing. */
  22. #ifndef _GNU_SOURCE
  23. #define _GNU_SOURCE 1
  24. #endif
  25. #include "libgomp.h"
  26. #include "proc.h"
  27. #include <errno.h>
  28. #include <stdlib.h>
  29. #include <unistd.h>
  30. #ifdef HAVE_GETLOADAVG
  31. # ifdef HAVE_SYS_LOADAVG_H
  32. # include <sys/loadavg.h>
  33. # endif
  34. #endif
  35. #ifdef HAVE_PTHREAD_AFFINITY_NP
  36. unsigned long gomp_cpuset_size;
  37. static unsigned long gomp_get_cpuset_size;
  38. cpu_set_t *gomp_cpusetp;
  39. unsigned long
  40. gomp_cpuset_popcount (unsigned long cpusetsize, cpu_set_t *cpusetp)
  41. {
  42. #ifdef CPU_COUNT_S
  43. /* glibc 2.7 and above provide a macro for this. */
  44. return CPU_COUNT_S (cpusetsize, cpusetp);
  45. #else
  46. #ifdef CPU_COUNT
  47. if (cpusetsize == sizeof (cpu_set_t))
  48. /* glibc 2.6 and above provide a macro for this. */
  49. return CPU_COUNT (cpusetp);
  50. #endif
  51. size_t i;
  52. unsigned long ret = 0;
  53. extern int check[sizeof (cpusetp->__bits[0]) == sizeof (unsigned long int)
  54. ? 1 : -1] __attribute__((unused));
  55. for (i = 0; i < cpusetsize / sizeof (cpusetp->__bits[0]); i++)
  56. {
  57. unsigned long int mask = cpusetp->__bits[i];
  58. if (mask == 0)
  59. continue;
  60. ret += __builtin_popcountl (mask);
  61. }
  62. return ret;
  63. #endif
  64. }
  65. #endif
  66. /* At startup, determine the default number of threads. It would seem
  67. this should be related to the number of cpus online. */
  68. void
  69. gomp_init_num_threads (void)
  70. {
  71. #ifdef HAVE_PTHREAD_AFFINITY_NP
  72. #if defined (_SC_NPROCESSORS_CONF) && defined (CPU_ALLOC_SIZE)
  73. gomp_cpuset_size = sysconf (_SC_NPROCESSORS_CONF);
  74. gomp_cpuset_size = CPU_ALLOC_SIZE (gomp_cpuset_size);
  75. #else
  76. gomp_cpuset_size = sizeof (cpu_set_t);
  77. #endif
  78. gomp_cpusetp = (cpu_set_t *) gomp_malloc (gomp_cpuset_size);
  79. do
  80. {
  81. int ret = pthread_getaffinity_np (pthread_self (), gomp_cpuset_size,
  82. gomp_cpusetp);
  83. if (ret == 0)
  84. {
  85. /* Count only the CPUs this process can use. */
  86. gomp_global_icv.nthreads_var
  87. = gomp_cpuset_popcount (gomp_cpuset_size, gomp_cpusetp);
  88. if (gomp_global_icv.nthreads_var == 0)
  89. break;
  90. gomp_get_cpuset_size = gomp_cpuset_size;
  91. #ifdef CPU_ALLOC_SIZE
  92. unsigned long i;
  93. for (i = gomp_cpuset_size * 8; i; i--)
  94. if (CPU_ISSET_S (i - 1, gomp_cpuset_size, gomp_cpusetp))
  95. break;
  96. gomp_cpuset_size = CPU_ALLOC_SIZE (i);
  97. #endif
  98. return;
  99. }
  100. if (ret != EINVAL)
  101. break;
  102. #ifdef CPU_ALLOC_SIZE
  103. if (gomp_cpuset_size < sizeof (cpu_set_t))
  104. gomp_cpuset_size = sizeof (cpu_set_t);
  105. else
  106. gomp_cpuset_size = gomp_cpuset_size * 2;
  107. if (gomp_cpuset_size < 8 * sizeof (cpu_set_t))
  108. gomp_cpusetp
  109. = (cpu_set_t *) gomp_realloc (gomp_cpusetp, gomp_cpuset_size);
  110. else
  111. {
  112. /* Avoid gomp_fatal if too large memory allocation would be
  113. requested, e.g. kernel returning EINVAL all the time. */
  114. void *p = realloc (gomp_cpusetp, gomp_cpuset_size);
  115. if (p == NULL)
  116. break;
  117. gomp_cpusetp = (cpu_set_t *) p;
  118. }
  119. #else
  120. break;
  121. #endif
  122. }
  123. while (1);
  124. gomp_cpuset_size = 0;
  125. gomp_global_icv.nthreads_var = 1;
  126. free (gomp_cpusetp);
  127. gomp_cpusetp = NULL;
  128. #endif
  129. #ifdef _SC_NPROCESSORS_ONLN
  130. gomp_global_icv.nthreads_var = sysconf (_SC_NPROCESSORS_ONLN);
  131. #endif
  132. }
  133. static int
  134. get_num_procs (void)
  135. {
  136. #ifdef HAVE_PTHREAD_AFFINITY_NP
  137. if (gomp_places_list == NULL)
  138. {
  139. /* Count only the CPUs this process can use. */
  140. if (gomp_cpusetp
  141. && pthread_getaffinity_np (pthread_self (), gomp_get_cpuset_size,
  142. gomp_cpusetp) == 0)
  143. {
  144. int ret = gomp_cpuset_popcount (gomp_get_cpuset_size, gomp_cpusetp);
  145. return ret != 0 ? ret : 1;
  146. }
  147. }
  148. else
  149. {
  150. /* We can't use pthread_getaffinity_np in this case
  151. (we have changed it ourselves, it binds to just one CPU).
  152. Count instead the number of different CPUs we are
  153. using. gomp_init_affinity updated gomp_available_cpus to
  154. the number of CPUs in the GOMP_AFFINITY mask that we are
  155. allowed to use though. */
  156. return gomp_available_cpus;
  157. }
  158. #endif
  159. #ifdef _SC_NPROCESSORS_ONLN
  160. return sysconf (_SC_NPROCESSORS_ONLN);
  161. #else
  162. return gomp_icv (false)->nthreads_var;
  163. #endif
  164. }
  165. /* When OMP_DYNAMIC is set, at thread launch determine the number of
  166. threads we should spawn for this team. */
  167. /* ??? I have no idea what best practice for this is. Surely some
  168. function of the number of processors that are *still* online and
  169. the load average. Here I use the number of processors online
  170. minus the 15 minute load average. */
  171. unsigned
  172. gomp_dynamic_max_threads (void)
  173. {
  174. unsigned n_onln, loadavg, nthreads_var = gomp_icv (false)->nthreads_var;
  175. n_onln = get_num_procs ();
  176. if (n_onln > nthreads_var)
  177. n_onln = nthreads_var;
  178. loadavg = 0;
  179. #ifdef HAVE_GETLOADAVG
  180. {
  181. double dloadavg[3];
  182. if (getloadavg (dloadavg, 3) == 3)
  183. {
  184. /* Add 0.1 to get a kind of biased rounding. */
  185. loadavg = dloadavg[2] + 0.1;
  186. }
  187. }
  188. #endif
  189. if (loadavg >= n_onln)
  190. return 1;
  191. else
  192. return n_onln - loadavg;
  193. }
  194. int
  195. omp_get_num_procs (void)
  196. {
  197. return get_num_procs ();
  198. }
  199. ialias (omp_get_num_procs)