Search code examples
eiffel

Number of available processors using Eiffel


I'm toying with Eiffels SCOOP.

In my program a bunch of worker run in parallel. I want to create as many worker as processor are available for me.

Is there and "easy" way in Eiffel to find the number of available processors?


Solution

  • There is no such a feature in the current standard library. However you can use the following:

    frozen available_cpus: NATURAL_8
            -- Number of logical CPUs reported by OS.
        external
            "C inline use %"eif_scoop.h%""
        alias
            "[
                #ifdef EIF_WINDOWS
                    SYSTEM_INFO sysinfo;
                    GetSystemInfo (&sysinfo);
                    return sysinfo.dwNumberOfProcessors;
                #elif EIF_MACOSX
                    int nm [2];
                    size_t len = 4;
                    uint32_t count;
    
                    nm [0] = CTL_HW; nm [1] = HW_AVAILCPU;
                    sysctl (nm, 2, &count, &len, NULL, 0);
    
                    if(count < 1) {
                        nm[1] = HW_NCPU;
                        sysctl(nm, 2, &count, &len, NULL, 0);
                        if (count < 1) {count = 1;}
                    }
                    return count;
                #else
                    return sysconf (_SC_NPROCESSORS_ONLN);
                #endif
            ]"
        end