[gambit-list] Re: We want our SMP! (with apologies to MTV)

Marc Feeley feeley at iro.umontreal.ca
Tue Jan 10 17:59:14 EST 2006


I'm starting to look into support for multiprocessing (and multi-os- 
threads).  A basic information I need is the number of processors  
available.  This can sometimes be obtained with sysconf, sometimes  
with sysctl, and perhaps other ways.  I have written the test code  
below to verify which library call is needed.  Can people on this  
list with access to "unusual" operating-systems try it out and report  
the result (i.e. not Mac OS X, Windows, Linux, and SunOS which I have  
already checked).  You may have to define DONT_HAVE_SYSCTL_H on the  
command line (this will be taken care of by Gambit's configure script).

Marc

/* File: "ncpu.c" */

#include <stdio.h>

#ifdef _WIN32
#define ___OS_WIN32
#else
#define ___OS_POSIX
#endif

/ 
*----------------------------------------------------------------------- 
----*/

#ifdef ___OS_POSIX

#include <unistd.h>

#ifdef _SC_NPROCESSORS_ONLN
#define OP_SC_NPROCESSORS _SC_NPROCESSORS_ONLN
#else
#ifdef _SC_NPROCESSORS_CONF
#define OP_SC_NPROCESSORS _SC_NPROCESSORS_CONF
#endif
#endif

void get_ncpu_with_sysconf ()
{
#ifdef OP_SC_NPROCESSORS

   int ncpu;

   if ((ncpu = sysconf (OP_SC_NPROCESSORS)) < 0)
     perror ("sysconf error");
   else
     printf ("sysconf reported ncpu = %d\n", ncpu);

#else

   printf ("sysconf unusable for getting ncpu\n");

#endif
}

#ifndef DONT_HAVE_SYSCTL_H

#include <sys/types.h>
#include <sys/sysctl.h>

#ifdef HW_NCPU
#define OP_HW_NCPU HW_NCPU
#endif

void get_ncpu_with_sysctl ()
{
#ifdef OP_HW_NCPU

   int ncpu;
   int mib[2];
   size_t len;

   mib[0] = CTL_HW;
   mib[1] = HW_NCPU;
   len = sizeof (ncpu);

   if (sysctl (mib, 2, &ncpu, &len, NULL, 0) < 0)
     perror ("sysctl error");
   else
     printf ("sysctl reported ncpu = %d\n", ncpu);

#else

   printf ("sysctl unusable for getting ncpu\n");

#endif
}

#endif

#endif

/ 
*----------------------------------------------------------------------- 
----*/

#ifdef ___OS_WIN32

#include <windows.h>

void get_ncpu_with_SysInfo ()
{
   int ncpu;
   SYSTEM_INFO si;
   GetSystemInfo (&si);
   ncpu = si.dwNumberOfProcessors;

   printf ("SysInfo reported ncpu = %d\n", ncpu);
}

#endif

/ 
*----------------------------------------------------------------------- 
----*/

int main ()
{
#ifdef ___OS_POSIX
   get_ncpu_with_sysconf ();
#ifndef DONT_HAVE_SYSCTL_H
   get_ncpu_with_sysctl ();
#endif
#endif

#ifdef ___OS_WIN32
   get_ncpu_with_SysInfo ();
#endif

   return 0;
}

/ 
*----------------------------------------------------------------------- 
----*/




More information about the Gambit-list mailing list