简体   繁体   English

应用程序如何在.NET或Java中使用多个内核或CPU?

[英]How can an application use multiple cores or CPUs in .NET or Java?

When launching a thread or a process in .NET or Java, is there a way to choose which processor or core it is launched on? 在.NET或Java中启动线程或进程时,有没有办法选择启动哪个处理器或核心? How does the shared memory model work in such cases? 在这种情况下,共享内存模型如何工作?

如果您使用多个线程,操作系统将自动使用多个核心。

is there a way to choose which processor or core it is launched on? 有没有办法选择启动哪个处理器或核心?

You can use the task manager to tell windows what CPU(s) your program should be allowed to run on. 您可以使用任务管理器告诉Windows应该允许您运行程序的CPU。 Normally this is only useful for troubleshooting legacy programs which have broken implementations of multi-threading. 通常,这仅用于对破坏了多线程实现的遗留程序进行故障排除。 To do this, 去做这个,

  • Run task manager 运行任务管理器
  • Find your process in the Processes window. 在“ Processes窗口中找到您的Processes
  • Right click and choose Set Affinity... 右键单击并选择Set Affinity...
  • Tick the checkboxes next to the CPU's you want to allow your application to run on. 勾选要允许运行应用程序的CPU旁边的复选框。 Windows will then only schedule threads from that process onto those particular CPU's 然后,Windows只会将该进程中的线程调度到这些特定的CPU上

If I recall correctly, windows will 'remember' these settings for subsequent times your process is run, but please don't quote me on that - run some tests yourself :-) 如果我没记错的话,Windows会在以后的运行过程中“记住”这些设置,但请不要引用我的话 - 自己运行一些测试:-​​)

You can also do this programatically in .NET after your program has launched using using the System.Diagnostics.Process.ProcessorAffinity property, but I don't think it will 'remember' the settings, so there will always be a short period in which your app is run on whichever CPU windows sees fit. 您可以使用System.Diagnostics.Process.ProcessorAffinity属性在程序启动后以编程方式在.NET中执行此操作,但我认为它不会“记住”这些设置,因此总会有一段短暂的时间您的应用程序在任何CPU窗口都适合运行。 I don't know how to do this in java sorry. 我不知道如何在java中做到这一点抱歉。

Note: 注意:

This applies at the entire process level. 这适用于整个流程级别。 If you set affinity for CPU0 only, and then launch 50 threads, all 50 of those threads will run on CPU0, and CPU1, 2, 3, etc will sit around doing nothing. 如果仅为CPU0设置亲和性,然后启动50个线程,则所有50个线程将在CPU0上运行,而CPU1,2,3等将无所事事。

Just to reiterate the point, this is primarily useful for troubleshooting broken legacy software. 重申一点,这对于破坏遗留的旧软件非常有用。 If your software is not broken, you really shouldn't mess with any of these settings, and let windows decide the best CPU(s) to run your program on, so it can take the rest of the system's performance into account. 如果你的软件没有损坏,你真的不应该搞乱任何这些设置,并让windows决定运行你的程序的最佳CPU,因此它可以考虑系统的其余部分性能。


As for the 'shared memory' model, it works the same, but there are more things that can go subtly wrong when your app is running on multiple CPU's as opposed to just timeslices on a single one. 至于“共享内存”模型,它的工作原理是相同的,但是当你的应用程序在多个CPU上运行时,有更多的东西可能会出现严重错误,而不是单个时间段上的时间片。

For an eye-opening example, read this ridiculousfish article about CPU's and Memory Barriers . 有关令人大开眼界的例子,请阅读这篇关于CPU和内存障碍的荒谬鱼类文章

It's aimed at OSX development on PowerPC, but general enough that it should apply everywhere. 它的目标是在PowerPC上进行OSX开发,但总的来说它应该适用于所有地方。 IMHO it's one of the top ten 'all developers should read this' articles I've read. 恕我直言,这是我读过的十大“所有开发者都应该阅读这些”文章之一。

The operating system takes care of multi-threading when the virtual machine is using native threads (as opposed to green-threads), and you can't specify low level details, like choosing a processor for a certain thread. 当虚拟机使用本机线程(而不是绿色线程)时,操作系统负责多线程,并且您无法指定低级别详细信息,例如为某个线程选择处理器。 It is better that way because you usually have many more threads than you have processors available, so the operating system needs to do time-slicing to give all threads a chance to run. 这种方式更好,因为您通常拥有的线程比可用处理器多得多,因此操作系统需要进行时间切片以使所有线程都有机会运行。

That being said, you can set threads priorities if you have a critical task, and a threading API usually provides this possibility. 话虽这么说,如果你有一个关键任务,你可以设置线程优先级,并且线程API通常提供这种可能性。 See the Java API for example: http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Thread.html#setPriority(int) 请参阅Java API,例如: http//java.sun.com/j2se/1.5.0/docs/api/java/lang/Thread.html#setPriority(int)

PS: there's something broken in the parsing engine ... I had to add the above link as plain text PS:解析引擎中有一些东西坏了...我不得不将上面的链接添加为纯文本

I have used this in a couple of programs because my core 0 was kind of messed up. 我已经在几个程序中使用了这个,因为我的核心0有点搞砸了。

// Programmatically set process affinity
var process = System.Diagnostics.Process.GetCurrentProcess();

// Set Core 0
process.ProcessorAffinity = new IntPtr(0x0001);

or 要么

// Set Core 1
process.ProcessorAffinity = new IntPtr(0x0002);

More on this in " Process.ProcessorAffinity Property ". 更多关于“ Process.ProcessorAffinity Property ”的内容。

I would have a look at the Parallel extensions to the .NET framework. 我将看一下.NET框架的并行扩展。 It is still in CTP , however it supposed to make the best use of multi core processors. 它仍然在CTP中 ,但它应该充分利用多核处理器。 The easiest place to get started for .NET is on the parallel teams blog . 开始使用.NET的最简单方法是在并行团队博客上

As for Java I have no idea. 至于Java,我不知道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM