如何才能充分利用 .NET 4.0 中新增强的并行性功能?
我对使用 .NET 4.0 中新增强的并行性功能非常感兴趣。
我还看到了在 F# 中使用它的一些可能性,就像在 C# 中一样。
尽管如此,我只能看到 PLINQ 必须提供的内容,例如以下内容:
var query = from c in Customers.AsParallel()
where (c.Name.Contains("customerNameLike"))
select c;
这种并行性肯定还有其他用途。
您还有其他使用它的例子吗?这是特别转向 PLINQ,还是有其他像 PLINQ 一样简单的用法?
谢谢! =)
I am pretty much interested into using the newly enhanced Parallelism features in .NET 4.0.
I have also seen some possibilities of using it in F#, as much as in C#.
Despite, I can only see what PLINQ has to offer with, for example, the following:
var query = from c in Customers.AsParallel()
where (c.Name.Contains("customerNameLike"))
select c;
There must for sure be some other use of this parallelism thing.
Have you any other examples of using it? Is this particularly turned toward PLINQ, or are there other usage as easy as PLINQ?
Thanks! =)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
.NET 4 中提供的新并行编程功能不仅限于 PLINQ。
正如 Microsoft 所言:“Visual Studio 2010 和 .NET Framework 4 通过提供新的运行时、新的类库类型和新的诊断工具,增强了对并行编程的支持。这些功能简化了并行开发,使您可以高效地编写代码以自然的方式编写细粒度且可扩展的并行代码,而无需直接使用线程或线程池。”
我建议您查看 .NET Framework 中的并行编程 是一个很好的起点。
总体而言,.NET 4 和 Visual Studio 2010 提供以下功能:
编程
ThreadPool及其基类
我个人
发现任务并行库中的 Task 和 Task{T} 类在创建和协调异步工作时更加灵活。
The new parallel programming features provided in .NET 4 are not limited to just PLINQ.
As stated by Microsoft: "Visual Studio 2010 and the .NET Framework 4 enhance support for parallel programming by providing a new runtime, new class library types, and new diagnostic tools. These features simplify parallel development so that you can write efficient, fine-grained, and scalable parallel code in a natural idiom without having to work directly with threads or the thread pool."
I would recommend that you review Parallel Programming in the .NET Framework as a good starting place.
Overall, .NET 4 and Visual Studio 2010 provide these features:
Programming
ThreadPool and the base class
libraries
I personally have found the Task and Task{T} classes in the Task Parallel Library to be more flexible when creating and coordinating asynchronous work.
我的团队也刚刚完成了一本关于此的书...
使用 Microsoft® .NET 进行并行编程:
多核架构上的分解和协调设计模式
Colin Campbell、Ralph Johnson、Ade Miller 和 Stephen Toub。前言:Tony Hey
您可以在此处下载草稿和示例:http://parallelpatterns.codeplex.com/
完整的书将于本月晚些时候在 MSDN 上发布,并于 10 月在亚马逊上发布。
对于明目张胆的插件表示歉意,但我认为您可能会发现这些内容确实很有帮助。
更新...
为了回答您选择的问题(如下),聚合的实现(从列表中根据列表内容创建聚合)恰好比 PLinq 更好地映射到 PLinq替代方案,Parallel.ForEach。 PDF 的 p72 上有一个使用 Parallel.ForEach 进行聚合的示例。
如果您只想使用 PLinq 更新集合的内容,则映射要简单得多。例如,以下代码循环访问帐户列表,计算余额趋势并标记预测余额超过透支限额的帐户。
Sequential:
PLinq:
Parallel.ForEach:
在某些情况下,PLinq 可能是最具表现力的选择。在其他情况下,Parallel.For/ForEach 可能更好,在某些情况下,这很大程度上取决于程序员的偏好。
但是,任务并行库支持的不仅仅是并行循环和聚合模式。它允许您构建计划在多核硬件上并行执行的任务(任务并行模式):
它允许您创建任务图,其中一个任务的输出被输入到另一个任务中(任务图或期货模式):
其中 F1 -F4 是输入和输出具有依赖性的函数。
它支持创建依赖任务树,以解决排序等分而治之的问题(动态任务并行模式):
它还实现了多个(线程安全)集合以在并行程序中使用。例如,它允许直接实现管道模式:
上述所有功能还包含对异常处理和取消的支持,尽管为了清楚起见,我没有在此处显示它们。
My team has also just finished a book on this...
Parallel Programming with Microsoft® .NET:
Design Patterns for Decomposition and Coordination on Multicore Architectures
Colin Campbell, Ralph Johnson, Ade Miller and Stephen Toub. Foreword by Tony Hey
You can download a draft and samples here: http://parallelpatterns.codeplex.com/
The full book will be available on MSDN later this month and on Amazon in October.
Apologies for the blatant plug but I think you might find the content really helpful.
Update...
To answer your question (below) the problem you picked, an implementation of an Aggregation (from a list create an aggregate based on the list content) happens to map far better to PLinq than the alternative, Parallel.ForEach. There's an example of aggregation with Parallel.ForEach on p72 of the PDF.
If you simply want to update the contents of a set using PLinq the mapping is much simpler. For example the following code loops over a list of accounts, calculates a balance trend and flags accounts with predicted balances more than their overdraft limit.
Sequential:
PLinq:
Parallel.ForEach:
In some cases, PLinq may be the most expressive choice. In others Parallel.For/ForEach may be better, in some cases it's largly a matter of programmer preference.
However, the Task Parallel Library supports more that the Parallel Loop and Aggregation patterns. It allows you to construct tasks that will be scheduled for parallel execution on multicore hardware (Task Parallelism pattern):
It allows you to create graphs of tasks where the output of one task is fed into another (Task Graph or Futures pattern):
Where F1-F4 are functions who's inputs and outputs have dependencies.
It supports creating trees of dependent tasks, for Divide and Conquer problems like sorting (the Dynamic Task Parallelism pattern):
It also implements several (threadsafe) collections for use in a parallel program. Which allows straightforward implementation of, for example, the Pipeline pattern:
All of the above features also contain support for exception handling and cancellation, although for clarity I've not shown them here.
观看此 Scott Hanselman 演示 39:30 至 48:36。 (从演讲第 39 分 30 秒开始)。
Watch this Scott Hanselman presentation from 39:30 to 48:36. (Starting at 39 minutes, 30 seconds into the talk).