Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Discover multiple processors programmatically?



Hello,

Thanks for all the replies.

First, I tried sysconf() but it doesn't seem capable of doing what I need under Mac OS? The two variables
_SC_NPROCESSORS_CONF and _SC_NPROCESSORS_ONLN are undefined in unistd.h -- am I missing something?


I also looked into sysctl as per Daniel's and Eric's suggestions -- I noticed even the sysconf manpage suggests that the sysctl interface is much richer. On Mac OS X, I got some code working right away (attached at end) but I haven't figured out how to get it to go under redhat (everything I do must work under both Mac OS X and redhat at least). redhat has a sys/sysctl.h but it only contains the declaration of sysctl() and none of the keywords. I realize this may be a little off-topic, but any clues?

By the way, in the code below, what are the HW_ keywords (if any) that correspond to the commented out entries in the data[] array? I really only need HW_NCPU and HW_AVAILCPU for now but just out of curiosity?

Thanks.

--
ivan


#include <stdio.h> #include <assert.h> #include <sys/sysctl.h>

struct foo { const char *s;  int id; int val;  size_t len; };
struct foo data[] = {
  { "hw.ncpu",             HW_NCPU,     -1, sizeof(int) },
  { "hw.availcpu",         HW_AVAILCPU, -1, sizeof(int) },
/*   { "hw.cpufrequency",     HW_, -1, sizeof(int) }, */
/*   { "hw.activecpu",        HW_, -1, sizeof(int) }, */
/*   { "hw.physicalcpu",      HW_, -1, sizeof(int) }, */
/*   { "hw.physicalcpu_max",  HW_, -1, sizeof(int) }, */
/*   { "hw.logicalcpu",       HW_, -1, sizeof(int) }, */
/*   { "hw.logicalcpu_max",   HW_, -1, sizeof(int) }, */
/*   { "hw.cputype",          HW_, -1, sizeof(int) }, */
/*   { "hw.cpusubtype",       HW_, -1, sizeof(int) }, */
/*   { "hw.cpufrequency",     HW_, -1, sizeof(int) }, */
/*   { "hw.cpufrequency_min", HW_, -1, sizeof(int) }, */
/*   { "hw.cpufrequency_max", HW_, -1, sizeof(int) } */
};

int main() {
  size_t array_len = (sizeof(data)/sizeof(data[0]));
  size_t len = 2;
  int    mib[len];
  int    i;

  mib[0] = CTL_HW;

  for ( i = 0 ; i < array_len; i++ ) {
    mib[1] = data[i].id;

    printf( "**********\n%d: %s: ", i, data[i].s );
    sysctlnametomib( data[i].s, mib, &len );

if ( sysctl( mib, len, &(data[i].val), &(data[i].len), NULL, 0) == -1 ) {
perror( "sysctl" ); printf( "\n" );
} else {
assert( data[i].len > 0 );
printf( "%d\n", data[i].val );
}
}
}




On Dec 18, 2005, at 1:31 PM, Dave Zarzycki wrote:

On Dec 18, 2005, at 1:04 AM, Eric Albert wrote:

On Dec 16, 2005, at 8:24 PM, Ivan S. Kourtev wrote:

Is there an API that would allow me to discover the availability of more then one physical processor cores? If so, is it somewhat standard -- would it work on say, Linux? I am parallelizing an application and would like to have two- and four-processor versions of some computationally intense code.

Anyone know of such an API, particularly one that is someone platform-independent?

There are a number of APIs that'll return the number of processors. The most basic BSD-level one is the hw.ncpu sysctl, which you can access either by name or by number (CTL_HW/HW_NCPU).


I'm not aware of any cross-platform API to get this information, but if anyone else knows of one I'd be interested in hearing about it.

sysconf() is the POSIX way of answering this question.

davez


_______________________________________________ Do not post admin requests to the list. They will be ignored. Unix-porting mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/unix-porting/email@hidden

This email sent to email@hidden
References: 
 >Discover multiple processors programmatically? (From: "Ivan S. Kourtev" <email@hidden>)
 >Re: Discover multiple processors programmatically? (From: Eric Albert <email@hidden>)
 >Re: Discover multiple processors programmatically? (From: Dave Zarzycki <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.