Hi,

I am trying to figure out the best way to automatically determine the
number of processors and used that information to speed up Sage build.
What is the best way of doing it?

If I can assume python on the system, then one can just use:

def ncpus()
    #for Linux, Unix and MacOS
    if hasattr(os, "sysconf"):
        if os.sysconf_names.has_key("SC_NPROCESSORS_ONLN"):
            #Linux and Unix
            ncpus = os.sysconf("SC_NPROCESSORS_ONLN")
            if isinstance(ncpus, int) and ncpus > 0:
                return ncpus
        else:
            #MacOS X
            return int(os.popen2("sysctl -n hw.ncpu")[1].read())
    #for Windows
    if os.environ.has_key("NUMBER_OF_PROCESSORS"):
        ncpus = int(os.environ["NUMBER_OF_PROCESSORS"])
        if ncpus > 0:
            return ncpus
    #return the default value
    return 1

If Python is not available, then I can use this simple C program:

http://github.com/certik/sysconf/blob/master/ncpus.c

but I suspect this will not work on Mac or Windows. So if it only
works on linux, one can just run:

cat /proc/cpuinfo | grep processor | wc -l

no need to compile anything.

So it seems to me that a good strategy might be to write a bash script
that will:

1) try if python is installed, if so, runs ncpus()
2) try to compile the above C program, if it builds, run it
3) if it doesn't build, we are on Mac probably, so run sysctl -n
hw.ncpu     (I don't have any Mac to test it on, but I guess there
might be a way to actually write the C program in a portable way to
work both on linux and Mac)

Alternatively, I can change 2) to:

2) try: cat /proc/cpuinfo | grep processor | wc -l

In any case, this bash script would be used only up to building
Python. Once we have Python, we can use ncpus() from that point on.

Any ideas welcome.

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