Menu

#591 TIP 377 : Portably determine number of processors

open
5
2010-09-16
2010-09-13
No

For initialization of thread pools to a sensible size it would be useful to know the number of processors/cores/... the current machine has.
The following link has quite a bit of info for various platforms:
http://stackoverflow.com/questions/150355/programmatically-find-the-number-of-cores-on-a-machine

The C-level API might be best to have in Tcl itself, the Tcl command however should be in the Thread package, IMHO.

Discussion

  • Andreas Kupries

    Andreas Kupries - 2010-09-14

    Incomplete implementation of the C-level function

     
  • Andreas Kupries

    Andreas Kupries - 2010-09-16
    • labels: 342021 --> 54. Configuration Reporting
    • summary: Portable C-API and Tcl cmd to determine number of processors --> TIP 377 : Portably determine number of processors
     
  • Andreas Kupries

    Andreas Kupries - 2010-09-16

    Now a proper TIP.

     
  • Andreas Kupries

    Andreas Kupries - 2010-09-16

    Updated implementation.

     
  • Emiliano

    Emiliano - 2010-09-24

    On modern BSDs, you can get the number of CPUs used by the kernel with

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

    /* set the mib for hw.ncpu */
    mib[0] = CTL_HW;
    mib[1] = HW_NCPU;

    /* get the number of CPUs from the system */
    sysctl(mib, 2, &numCPU, &len, NULL, 0);

    the #includes needed are

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

    for {Free|Dragonfly}BSD and

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

    For {Open|Net}BSD