Search code examples
.netncrunch

Control number of cores for parallel execution


I have installed NCrunch which is a tool for (among other things) parallel tests execution. It provides two settings MaxNumberOfUsedCores and MaxNumberOfUsedThreads. The first setting looks intriguing. I don't remember any .NET means which would allow to control cores which are used to execute your code. So the question is how to do it?


Solution

  • Have you tried ProcessThread.ProcessorAffinity? ProcessorAffinity represents each processor as a bit. Bit 0 represents processor one, bit 1 represents processor two, and so on. For example:

    var currectProcess = System.Diagnostics.Process.GetCurrentProcess();
    
    foreach(System.Diagnostics.ProcessThread thread in currectProcess.Threads) 
    { 
        // this could give you something similar to MaxNumberOfUsedCores 
        thread.ProcessorAffinity = (IntPtr)0x0007; // Valid processors: 1, 2, or 3
    }
    

    Now I'm having following configuration at NCrunch, that is looking quite similar to previous c# sample:

    • CPU cores assigned to NCrunch: 0, 1, 2
    • CPU cores assigned to Visual Studio: 3

    But only author of NCrunch @remco-mulder could tell us, is it true or not.

    btw: ReSharper has similar options for controlling number of parallel running threads of unittests.