This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[patch] libgomp detect # of cpus Darwin/FreeBSD
- From: Andreas Tobler <andreast-list at fgznet dot ch>
- To: GCC Patches <gcc-patches at gcc dot gnu dot org>
- Date: Fri, 19 Sep 2008 21:06:09 +0200
- Subject: [patch] libgomp detect # of cpus Darwin/FreeBSD
Hi all,
I followed a suggestion from Jakub and implemented the detection for the
# of cpus on Darwin. Afaik, this code should also work for FreeBSD.
Tested on ppc-darwin, 1 cpu, i686/x86_64-darwin 1 cpu/2 core. No
regressions.
The sample code snippet form here:
http://openmp.org/forum/viewtopic.php?f=3&t=186
gives the following output on my MacBook Pro (Core2Duo):
number of cpus = 2
Hello World from thread 0
Hello World from thread 1
There are 2 threads
Ok for trunk?
2008-09-19 Andreas Tobler <a.tobler@schweiz.org>
* config/posix/proc.c (omp_get_num_procs): Detect # of cpus for
Darwin/FreeBSD.
* configure.ac: Check for header <sys/sysctl.h>
* configure: Regenerate.
* config.h.in: Likewise.
Index: configure.ac
===================================================================
--- configure.ac (revision 140495)
+++ configure.ac (working copy)
@@ -154,7 +154,7 @@
AC_STDC_HEADERS
AC_HEADER_TIME
ACX_HEADER_STRING
-AC_CHECK_HEADERS(unistd.h semaphore.h sys/loadavg.h sys/time.h)
+AC_CHECK_HEADERS(unistd.h semaphore.h sys/loadavg.h sys/sysctl.h sys/time.h)
GCC_HEADER_STDINT(gstdint.h)
Index: config/posix/proc.c
===================================================================
--- config/posix/proc.c (revision 140495)
+++ config/posix/proc.c (working copy)
@@ -39,6 +39,9 @@
# include <sys/loadavg.h>
# endif
#endif
+#ifdef HAVE_SYS_SYSCTL_H
+# include <sys/sysctl.h>
+#endif
/* At startup, determine the default number of threads. It would seem
@@ -96,9 +99,15 @@
{
#ifdef _SC_NPROCESSORS_ONLN
return sysconf (_SC_NPROCESSORS_ONLN);
+#elif defined HW_NCPU
+ int ncpus = 1;
+ size_t len = sizeof(ncpus);
+ sysctl((int[2]) {CTL_HW, HW_NCPU}, 2, &ncpus, &len, NULL, 0);
+ return ncpus;
#else
return gomp_icv (false)->nthreads_var;
#endif
+
}
ialias (omp_get_num_procs)