gomp-discuss
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Gomp-discuss] Number of CPU's


From: Steven Bosscher
Subject: Re: [Gomp-discuss] Number of CPU's
Date: 17 Feb 2003 12:27:16 +0100

Op ma 17-02-2003, om 11:08 schreef Lars Segerlund:
> 
>   Cool, I'm all for it, I thought we might have a problem :-) .

As usual, it's not all paradise.  It turns out that SGI has to do it in
a way that differs from what everybody else is doing.  They have
_SC_NPROC_{CONF,ONLN} instead of _SC_NPROCESSORS_{CONF,ONLN}.  CRAY
UNICOS and HP-UX only have those defined in recent versions.  And the
GNU C library has extensions to get the number of CPUs avail/configured,
but they return int instead of the long as returned by sysconf. :-/

Here are two functions that mimic the GNU C; this should work on most
*nix systems (not on old HP-UX).  For Windows you'd need something else,
but GCC is not available for Windows without a unix compatiblity layer
(like cygwin), right??

Greetz
Steven



#include <unistd.h>

/* Don't override GNU C Library extensions.  */
#ifndef __GLIBC__

/* Get the number of configured CPUs.  This may not be the same as the
   number of available CPUs.  Use get_nprocs instead to get that
   number.

   Returns the number of configured CPUs, assumes this is a single CPU
   machine if the number is not defined on the system.  */

int
get_nprocs_conf (void)
{
  long nprocs_conf = -1;

#if defined _SC_NPROCESSORS_CONF
  nprocs_conf = sysconf (_SC_NPROCESSORS_CONF);
#elif defined _SC_NPROC_CONF
  nprocs_conf = sysconf (_SC_NPROC_CONF);
#elif defined _SC_CRAY_NCPU
  return get_ncpus ();  /* Apparently, CRAY machines always work.  */
#endif

  if (nprocs_conf < 1)
    nprocs_conf = 1;

  return (int) nprocs_conf;
}


/* Get the number of available CPUs.  This may not be the same number
   as the number of configured CPUs, for example when some CPU slots
   are empty or when there's some kind of system maintenance going on.

   Returns the number of online CPUs, assumes this is a single CPU
   machine if the number is not defined on the system.  */

int
get_nprocs (void)
{
  long nprocs = -1;

#if defined _SC_NPROCESSORS_ONLN
  nprocs = sysconf (_SC_NPROCESSORS_ONLN);
#elif defined _SC_NPROC_ONLN
  nprocs = sysconf (_SC_NPROC_ONLN);
#elif defined _SC_CRAY_NCPU
  nprocs = sysconf (_SC_CRAY_NCPU);
#endif

  if (nprocs < 1)
    nprocs = 1;

  return (int) nprocs;
}

#endif  /* !__GLIBC__  */






reply via email to

[Prev in Thread] Current Thread [Next in Thread]