.net 和多核
从我读到的 .net 4.0 预览到现在,已经有很多关于下一个 .net 版本将如何处理和使用多核 CPU 的讨论。 我们将添加像 plinq 这样的附加功能,这将帮助我们利用多核。 我的问题是,当我只想让应用程序运行得更快时,为什么我必须费心处理多个核心。 为什么不能有一种虚拟 cpu 层将所有核心作为 1 个核心公开给我的应用程序?
编辑: 我想重新表述我的问题以避免误解, 是否可以制作一个软件,将虚拟线程暴露给我的应用程序,速度会快 10 倍,因为底层使用 10 个内核。 我不想让不同的线程并行执行任务,我只想让一个线程运行得更快。 我想这在今天并不是一个大问题,但很快我们就会有 80 个核心处理器可供使用,然后我会觉得只使用其中 1 个会有点人手不足。
From the .net 4.0 previews I have read until now there has been lots of talk on how the next .net version will handle and use cpus with multiple cores. We will have additions like plinq that will help us make use of multiple cores. My question is why should I have to bother my mind with handling multiple cores when all I want is to make my application run faster. Why can't there be a kind of virtual cpu layer that exposes all cores as 1 core to my application?
Edit:
I would like to rephrase my question to avoid misunderstanding,
Could there be made a software that would expose a virtual thread to my application that would be 10 times faster because underlaying it was using 10 cores. I do not want to have different threads doing things in paralell, I just want my one thread running faster. I guess this is not a big problem today but soon we'll have 80 core processors to play with and then I would feel a bit shorthanded only using 1 of them.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
因为如果没有人类的帮助,并行化就无法完成。
可以完成一定的任务 - 并且已经在单个内核中完成了。 出于显而易见的原因,微并行性比“分析整个程序并使其并行运行”要容易得多。 一般来说,编写并行程序时必须做出的许多困难决定取决于您希望程序执行的操作以及它在各种条件下应如何运行。
随着时间的推移,可能会有更多的任务能够自动并行化,但我对目前手动操作变得更加容易的进步感到高兴。
编辑:看到您问题中的编辑后,没有办法让它跑得更快。 有一个非常简单的物理比喻:怀孕需要 9 个月。 如果你有9名孕妇,你可以在9个月后生下9个孩子,但你不能让9个女人在1个月内生下1个孩子。 它只是不那样工作。 并非所有问题都可以并行化。 (弗雷德·布鲁克斯:“无论分配多少女性,生孩子都需要九个月的时间。”(《人月神话》,第 17 页))
Because parallelisation just can't be done without the aid of a human.
There's a certain amount which can be done - and already is done within a single core. Microparallelism is a lot easier than "analyse this whole program and make it run in parallel" for obvious reasons. In general, many of the difficult decisions which have to be made when writing a parallel program depend on what you want the program to do, and how it should behave under various conditions.
It's possible that more will be able to be parallelised automatically over time, but I'm happy with the advance of making it significantly easier to do manually for the moment.
EDIT: Having seen the edit in your question, there's no way of making it go faster. There's a really easy physical metaphor for this: a pregnancy takes 9 months. If you have 9 pregnant women, you can end up with 9 babies after 9 months, but you can't make 9 women have 1 baby in 1 month. It just doesn't work like that. Not all problems are parallelisable. (Fred Brooks: “The bearing of a child takes nine months, no matter how many women are assigned.” (The Mythical Man Month, p. 17))
您首先误解了多核的意义。
使用多个核心,您可以并行(同时)执行两个或多个不同的操作。 这称为线程化。
这不能自动完成,因为您编写的“正常”程序是线性的。 在不考虑副作用的情况下,您无法轻松地将一系列指令转换为多个系列指令。
例如,GPU(显卡上的专用 CPU)可能包含数百个“核心”(流处理器),它们并行运行以渲染屏幕上的像素。 如果只有一个处理器,您必须在超快速处理器上逐像素渲染才能达到相同的效果。
CPU 制造商转向多个核心的原因是,以更高的速度制造单个核心变得更加困难和昂贵,而且单个核心会比速度减半的两个核心吸收更多的功率并产生更多的热量(基本上;并非所有情况都是如此)。
You are misunderstanding the point of multiple cores in the first place.
With more than one core, you can perform two or more different operations in parallel (at the same time). This is called threading.
This can't be done automatically because the "normal" programs you write are linear. You can't easily convert one series of instructions into several series of instructions without taking into consideration side effects.
GPU's (specialized CPU's on graphics cards), for example, may contain hundreds of "cores" (stream processors) which operate in parallel to render the pixels on your screen. With only one processor, you'd have to render pixel-by-pixel on a super-fast processor to achieve the same effect.
The reason CPU manufacturers moved to more than one core was because manufacturing a single core at higher speeds was getting more difficult and expensive, and that a single core would suck up more power and produce more heat than two cores at half the speed (basically; this isn't true in all cases).
是和否。如果您希望能够正常编程,但让系统“神奇地”使用多个线程,那么您需要查看一些多处理框架。 例如:OpenMP。
如果您想使用所有核心来运行数组,请使用类似于以下的代码:
只需添加 #pragma 即可使您的代码神奇地成为多线程。 现在,您必须了解一些限制,例如。 您不能在处理下一个元素时使用一个元素的值(即您不能将 a[0] 设置为 1,然后 a1 到 a[0]+1 等,因为它们无疑会由不同的线程处理,因此在您需要它们时可能不会被计算),否则您会从 openmp 编译器收到错误,但如果你在这些限制(而且有很多)范围内工作,那么你就能得到你想要的。
这些限制使得您的最终目标无法实现您的代码。 正如在我的示例中一样,除非第一个元素已经计算出来,否则您根本无法根据另一个元素计算一个元素。 这实际上意味着这样的代码只能是单线程的,无论您有多少个内核。
这也意味着 80 核芯片不会很快真正起飞,除非它们都是 3Ghz 通用处理器,所以您不必太担心为它们编写代码。 现在,附加协处理器可以处理像我的示例中那样编码的数组,这是另一回事,我希望将来会看到更多这样的情况。 您可能会将显卡用于此类事情并拥有数千个“cpu”而不是仅仅80。
Yes and no.. If you want to be able to program as normal, but having the system 'magically' use multiple threads, then you need to look at some of the multi-processing frameworks out there. For example: OpenMP.
If you want to run through an array, using all your cores, code similar to this:
Just adding the #pragma makes your code magically multi-threaded. Now, you will have to understand some restrictions, eg. you cannot use the value of one element in the processing of the next (ie you cannot set a[0] to 1, then a1 to a[0]+1, etc as they'll no doubt be processed by different threads, and therefore may not be computed when you need them) or you'll get an error from the openmp compiler, but if you work within these restrictions (and there are many) then you can get what you want.
The restrictions are what makes your ultimate goal of making your code impossible. As in my example, you simply cannot compute 1 element based on another unless the first element is already computed. This effectively means code like that can only be single-threaded, no matter how many cores you have.
It also means that 80-core chips will not really take off anytime soon, not unless they are all 3Ghz general purpose processors, so you don't need to worry too much about having to code for them. Now, add-on co-processors that can crunch arrays coded like in my example, that's another matter, and I expect to see more of that in the future. You'll probably use the graphics card for that kind of thing and have thousands of 'cpus' instead of a mere 80.