关于c#:在Logical Core 32及更高版本上运行的程序

Running Program on Logical Core 32 and above

我正在尝试编写一个程序,该程序将对使用C#的系统上的所有逻辑核心(最多72个逻辑核心)进行压力测试,无论如何,System.Environment.ProcessorCount()仅返回32,无论如何。我在具有40个核心和72个核心的系统上运行它,但它只能看到32个核心。

例如,我什至尝试在内核32(索引0)上运行它,并且它重新缠绕并再次对CPU0 Node0进行压力测试。

任何人都对如何对所有内核进行压力测试有任何想法吗?我正在使用在本文中找到的逻辑(http://omegacoder.com/?p=94)

All

尝试编译为64位,但仍然没有。

编辑:在下面添加了代码示例:
用法:(输入CPU编号-> 31 = CPU12 Node1)每个物理处理器系统有20个逻辑核心)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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.");
                }
            }

        }

穗CPU:

1
2
3
4
5
6
7
8
9
10
11
12
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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;
            }

        }

也许这将帮助所有人理解。当我将每个CPU的峰值峰值提高到39(第40个CPU)时,我已经对其进行了标记:
19


所以我找出了问题,并且我怀疑是在这段代码中的:

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

因为int只有32位,所以当它尝试进行移位以将第33个处理器设置为1并将所有其他处理器设置为0时,它将被package,因为它无处可去。简单地解决了这个问题。必须为具有72个逻辑核心的系统找出解决方案,但这是明天的问题。

1
2
3
4
5
6
7
8
9
10
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);


如何使用更多内核:

  • 使用更多进程并并行启动它们
  • 手动创建线程(使用new Thread()...)直到使用核心计数为止

阿姆达尔定律基于锁强制对代码进行缩放,因此请确保所有计算尽可能并行。

对于我来说,我想说的是,如果您想工作一个(更现实的)场景,出于其他原因,我将启动多个进程:GC可以冻结所有32个(逻辑)内核,以不时清理内存。最好,如果您想用作网络服务器(或类似的服务器),而不冻结具有72个核的所有应用程序,因为它们正在等待Full GC(Gen2 GC)发生,那就太好了。假设只冻结了16个内核,而其他3个进程确实响应了请求并推进了计算,那就太好了。