如何为多处理器机器编译 C#? (使用 VS 2010 或 csc.exe)
问候!
我在 MSDN 中搜索了编译器 (csc.exe) 选项我在 Stackoverflow 找到了一个关于 使用多个处理器进行编译。但我的问题是针对多个处理器进行编译,如下所示。
我即将毕业的大学有一个 11 台机器集群(其中有 6 个四核和 5 个四核双处理机器)。它在linux下运行,但我可以在那里安装MONO。我不想使用多个处理器或内核进行编译,而是想针对多处理器机器进行编译。那么:
- 是否有关于如何执行此操作的任何特定细节,或者该系统上的 CLR 应该处理执行以将其分布到核心上?
- 如果有办法做到这一点,我该如何使用 VS2010 或 csc.exe 命令行编译器来做到这一点?
提前致谢,如果这个问题没有意义,我很抱歉。我真的不知道如何处理多核,因为我只是一个物理学家,而不是计算机科学家!:)
Greetings!
I've searched for compiler (csc.exe) options at MSDN and I found an answer here, at Stackoverflow, about compiling with multiple processors. But my problem is about compiling for multiple processors, as follows.
The university where I'm graduating has a 11 machine cluster (which has 6 quad-cores and 5 four-core bi-processed machines). It runs under linux, but I can install MONO there. And instead of compiling with multiple processors or cores, I want to compile for multiple processors machine. So:
- Is there any particular detail on how to do it or the CLR on that system should handle the execution to spread it across the cores?
- If there's a way to do this, how can I do it with VS2010 or with csc.exe command line compiler?
Thanks in advance and I'm sorry if this question makes no sense. I really don't know how to handle multiple cores, as I'm a mere physicist, not a computer scientist! :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您无需进行任何不同的编译即可考虑多核。不过,您需要以不同的方式编写代码,才能使用多线程。如果您可以在您的环境中使用 .NET 4 中的类(Mono 的最新版本应该支持此功能),您可以使用任务并行库,这会让这变得更容易一些。
基本上,您不会免费获得并发性 - 您必须考虑代码的哪些部分可以合理地并行运行。您可能需要阅读并行编程模式和实践小组的输出。 (这本书是一个非常好的起点。)
You don't need to compile any differently to take account of multiple cores. You need to write your code differently though, to use multiple threads. If you can use classes from .NET 4 in your environment (a recent version of Mono should support this) you can use the Task Parallel Library which makes this a bit easier.
Basically you don't get concurrency for free - you have to think about which bits of your code can sensibly run in parallel. You might want to read the output of the Patterns and Practices group for parallel programming. (The book is a very good starting point.)
你的假设是正确的;你的问题没有意义。
不可能神奇地并行化任意代码;您需要修改代码以使用多线程。
您可以通过使用
Thread
或ThreadPool
类,或者使用并行 LINQ 或 TPL,在 C# 中显式使用多个内核。不涉及特殊的编译器。
Your supposition is correct; your question makes no sense.
It is not possible to magically parallelize arbitrary code; you need to modify the code to use multiple threads.
You can use explicitly multiple cores in C# by using the
Thread
orThreadPool
classes, or by using Parallel LINQ or the TPL.There is no special compiler involved.
默认情况下,CLR 不会执行任何特殊操作来将工作分散到多个核心上。在开发应用程序时,您有责任充分利用机器的资源。 .NET Framework 确实有一些库和技术可以使多线程操作的实现变得简单:查找 Thread 类、Delegate.BeginInvoke/EndInvoke 和任务并行库。
The CLR, by default, will not do anything special to spread the work out across multiple cores. YOU, in developing the application, are responsible for making the best use of your machine's resources. The .NET Framework does have several libraries and technologies that make multithreaded operations simple to implement: look up the Thread class, Delegate.BeginInvoke/EndInvoke, and the Task Parallel Library.
由于它是一个集群,因此您必须依赖某种形式的消息传递并行性,没有编译器会自动转换您的代码。至少,支持一个好的旧 MPI: http://osl.iu.edu/research/ mpi.net/
Since it is a cluster, you have to rely on some form of a message-passing parallelism, no compiler will transform your code automatically. At least, a good old MPI is supported: http://osl.iu.edu/research/mpi.net/
您的问题的答案以两个看似矛盾的陈述的形式出现:
1:它已经这样做了
,
2:您不能
在现代操作系统以及开发环境中使用线程。从根本上来说,线程代表处理器将执行的一系列连续步骤(以及不以“S”开头的单词)。这些线程由操作系统和处理器体系结构管理,其中处理器将执行线程的某些部分(或全部),保存其状态,然后切换到另一个线程。
在存在多个内核的情况下(无论是多核处理器还是简单的多个处理器或两者兼而有之),计算机实际上可以同时执行两个线程,假设它们读取和写入内存中的不同位置(利用相同的资源需要通过在核心之间分配线程来进行同步(这是一个复杂的球赛)。
冒着使用过于简单化的比喻的风险,请这样想:您的代码,就目前而言,只是一个非常执行完成特定任务的步骤列表。现在,您已将此指令列表带入一个满是人的房间(每个人代表一个处理核心),并且您希望尽可能高效地使用这些人。虽然一屋子的博士生可能有背景和主题知识来弄清楚如何将你的指示分解为每个人的任务,但你已经把你的清单带到了一个满是擅长遵循指示的人的房间但在演绎方面却完全愚蠢。在这种情况下,您需要为每个人提供一组不同的指令,当所有指令都被执行时,您最终会得到相同的结果。
简而言之,为了让您的代码利用多个内核或处理器,您必须将您的工作分解为小的、最好是原子的代码块。用于将代码分解为多个线程的具体方法可能会有所不同;使用 System.Threading.ThreadPool 或最近引入的任务并行库可以使其中一些事情变得更容易,尽管在效率或控制方面总是存在权衡(与所有事情一样)。
比这更详细的内容需要查看您的实际代码。您最好找一个有编写可靠、高性能多线程代码经验的人(如果可能的话,最好是最近有 .NET 经验的人,因为这将有助于确定哪些库是合适的)。
The answer to your question comes in the form of two seemingly-contradictory statements:
1: It already does
and
2: You can't
Modern operating systems and, thus, development environments, use threads. A thread, fundamentally, represents a single series of sequential steps (and words that don't start with "S") that the processor will execute. These threads are managed by the operating system and by the processor architecture, wherein the processor will execute some portion (or all) of a thread, save its state, then switch to another thread.
In the presence of multiple cores (whether by multi-core processors or simply multiple processors or both), it's actually possible for the computer to execute two threads at the same time, assuming that they read and write different locations in memory (threads that utilize the same resources require synchronization, which is a complex ballgame inside this one), by distributing threads across cores.
At the risk of using an overly simplistic simile, think of it this way: your code, as it stands right now, is just a very long list of steps to execute to accomplish a particular task. You've now taken this list of instructions into a room full of people (each representing a processing core), and you'd like to use each of these people as efficiently as possible. While a room full of PhD students might have the context and subject-matter knowledge to figure out how to break out your instructions into tasks for each individual person, you've taken your list to a room full of people who are excellent at following directions but entirely stupid when it comes to deduction. In this case, you need to bring a different set of instructions for each person that when all of them are executed, you end up with the same result.
Put simply, in order to have your code take advantage of multiple cores or processors, you have to break your work down into small, preferably atomic chunks of code. The specific method that you use to break up your code into multiple threads can vary; using
System.Threading.ThreadPool
or the more recently introduced Task Parallel Library can make some of these things easier, though there's always a tradeoff (as with everything) in either efficiency or control.Going into much more detail than that would require looking at your actual code. You'd do better finding someone with experience writing solid, performant multithreaded code (if possible, someone with recent .NET experience doing this, as this will help make the determination about which libraries would be appropriate).