Running Program on Logical Core 32 and above

I'm trying to write a program that will stress test ALL logical cores on a system in C# (Up to 72 logical cores) and no matter what, System.Environment.ProcessorCount() only returns 32, no matter what. I ran it on a system with 40 cores and 72 cores and it only sees 32 cores.

I even tried running it on core 32 (index 0), for example, and it wraps around and stress tests CPU0 Node0 again.

Anyone have any ideas how to stress test ALL cores? I'm using the logic I found in this article (http://omegacoder.com/?p=94)

Tried compiling as 64 bit and still nothing.

Edit: Added Code Sample Below: Usage: (Pass in the CPU number -> 31 = CPU12 Node1) System has 20 logical cores per physical processor)

public static void Usage(int cpuToStart) {
            // set cpu 
            int cpu = cpuToStart;
            ThreadProcessor tp = new ThreadProcessor();
            // Spikes CPU 1
            Console.WriteLine("Spike CPU 1");
            tp.SpikeCPU(cpu);

            // ouput error
            if (tp._ex != null) {
                Console.WriteLine(tp._ex.Message);
            }
            // No error
            else {
                // if multiple processors (logical cores)
                if (Environment.ProcessorCount > 1) {
                    while (++cpu < Environment.ProcessorCount) {
                        Thread.Sleep(1000);
                        // Spike each CPU
                        Console.WriteLine("Spike CPU " + (cpu + 1).ToString());
                        tp.SpikeCPU(cpu);

                        if (tp._ex != null) {
                            Console.WriteLine(tp._ex.Message);
                            break;
                        }
                    }

                }
                else // Either a single CPU or hyperthreading not enabled in the OS or the BIOS.
            {
                    Console.WriteLine("This PC does not have two processors available.");
                }
            }

        }

Spike CPU:

public void SpikeCPU(int targetCPU) {

            // Create a worker thread for the work.
            _worker = new Thread(DoBusyWork);

            // Background is set so not to not prevent the
            // mainprocess from terminating if someone closes it.
            _worker.IsBackground = true;

            _worker.Start((object)targetCPU);
            _worker.Join(); // Wait for it to be done.
        }

DoWork

public void DoBusyWork(object target) {
            try {
                int processor = (int)target;
                Thread tr = Thread.CurrentThread;

                if (Environment.ProcessorCount > 1) {
                    SetThreadAffinityMask(GetCurrentThread(),
                        new IntPtr(1 << processor));
                }

                CalculatePI.Process(PiSignificantDigits);
            }
            catch (Exception ex) {
                _ex = ex;
            }

        }

Maybe this will help everyone understand. I've labeled every CPU when I spike them up to 39 (40th CPU):

Answers


How to use more cores:

  • use more processes and launch them in parallel
  • create threads by hand (with new Thread() ...) up to the point the core counts are used

Amdahl's law forces the scaling of your code based on locks, so make sure all computations are as parallel as possible.

As for me, I would say that if you want to work a (more) realistic scenario, I would launch multiple processes for other reasons: the GC can freeze all the 32+ (logical) cores to clean up memory from time to time, it would be great if you want to use as a web server (or something similar) to not freeze all your application with 72 cores as they are waiting the Full GC (Gen2 GC) to happen. It would be great that let's say just 16 cores are frozen and the other 3 processes do answer to requests and advance your computations.


So I figured out the issue and as I suspected, it was in this piece of code:

int processor = (int)target;
Thread tr = Thread.CurrentThread;
if (Environment.ProcessorCount > 1) {
    SetThreadAffinityMask(GetCurrentThread(), new IntPtr(1 << processor));
}

Because int is only 32 bits, when it tried to bit shift to set the 33rd processor to 1 and all the others to 0, it would wrap because it had nowhere to go. Simply passing a long fixed this issue. Will have to figure out a solution for a system with 72 logical cores, but that's tomorrow's problem.

int processor = (int)target;
long shift = 1;
long cpuToTest = shift << processor;
Thread tr = Thread.CurrentThread;

if (Environment.ProcessorCount > 1) {
    SetThreadAffinityMask(GetCurrentThread(), new IntPtr(cpuToTest));
}

CalculatePI.Process(PiSignificantDigits);

Need Your Help

using alt for an anchor link

html5 anchor alt

Reading google prompt for using alt in internal links (https://support.google.com/webmasters/answer/47334) I looked for answers. I found a bit old thread (Is it correct to use alt tag for an anchor

How to save a Vector<Vector<PointF>> into a ArrayList?

java android vector arraylist vector-graphics

I have a simple question which I am unable to solve. I have a Vector&lt;Vector&lt;PointF&gt;&gt; object that I want to save into a ArrayList. I tried doing this:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.