Hi Roman,

On Sat, Mar 28, 2009 at 4:40 PM, Roman Pearce <rpear...@gmail.com> wrote:
>
> /* Linux */
> #include <sched.h>
> int sched_getaffinity(pid_t pid, unsigned int cpusetsize, cpu_set_t
> *mask);
>
> static inline int num_processors()
> {
>        unsigned int bit;
>        int np;
>        cpu_set_t aff;
>        memset(&aff, 0, sizeof(aff) );
>        sched_getaffinity(0, sizeof(aff), &aff );
>        for(np = 0, bit = 0; bit < 8*sizeof(aff); bit++)
>                np += (((char *)&aff)[bit / 8] >> (bit % 8)) & 1;
>        return np;
> }
>
>
> /* Mac OS X */
> #include <sys/types.h>
> #include <sys/sysctl.h>
> static inline int num_processors()
> {
>        int np = 1;
>        size_t length = sizeof( np );
>        sysctlbyname("hw.ncpu", &np, &length, NULL, 0);
>        return np;
> }
>
>
> /* Windows NT */
> #include <windows.h>
> static inline int num_processors()
> {
>        SYSTEM_INFO info;
>        GetSystemInfo(&info);
>        return info.dwNumberOfProcessors;
> }

Thanks a lot for the code!

I just tried the following code on several linuxes (Debian, Ubuntu,
Gentoo, Red Hat, OpenSUSE) and on OS X 10.5 Intel and it seems to just
work everywhere:

#include "unistd.h"
#include "stdio.h"

int main()
{
    int ncpus;
    ncpus = sysconf(_SC_NPROCESSORS_ONLN);
    printf("%d", ncpus);
    return 0;
}



It will not work on windows, but is there some reason to use a
different code for linux and Mac, if the above seems to be working
just fine?

Ondrej

--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to sage-devel@googlegroups.com
To unsubscribe from this group, send email to 
sage-devel-unsubscr...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sage-devel
URLs: http://www.sagemath.org
-~----------~----~----~----~------~----~------~--~---

Reply via email to