C# 如何让我的代码在多个内核上运行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/123792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make my code run on multiple cores?
提问by Mister Dev
I have built an application in C# that I would like to be optimized for multiple cores. I have some threads, should I do more?
我已经用 C# 构建了一个应用程序,我希望针对多核进行优化。我有一些线程,我应该做更多吗?
Updated for more detail
更新了更多细节
- C# 2.0
- Run on Windows Vista and Windows Server 2003
- C# 2.0
- 在 Windows Vista 和 Windows Server 2003 上运行
Updated again
再次更新
- This code is running as a service
- I do not want to have the complete code... my goal here is to get your experience and how to start. Like I say, I have already use threads. What more can I do?
- 此代码作为服务运行
- 我不想拥有完整的代码……我的目标是获得您的经验以及如何开始。就像我说的,我已经使用了线程。我还能做什么?
采纳答案by stephbu
I'd generalize that writing a highly optimized multi-threaded process is a lot harder than just throwing some threads in the mix.
我概括地说,编写一个高度优化的多线程进程比仅仅将一些线程混合在一起要困难得多。
I recommend starting with the following steps:
我建议从以下步骤开始:
- Split up your workloads into discrete parallel executable units
- Measure and characterize workload types - Network intensive, I/O intensive, CPU intensive etc - these become the basis for your worker pooling strategies. e.g. you can have pretty large pools of workers for network intensive applications, but it doesn't make sense having more workers than hardware-threads for CPU intensive tasks.
- Think about queuing/array or ThreadWorkerPool to manage pools of threads. Former more finegrain controlled than latter.
- Learn to prefer async I/O patterns over sync patterns if you can - frees more CPU time to perform other tasks.
- Work to eliminate or atleast reduce serialization around contended resources such as disk.
- Minimize I/O, acquire and hold minimum level of locks for minimum period possible. (Reader/Writer locks are your friend)
5.Comb through that code to ensure that resources are locked in consistent sequence to minimize deadly embrace. - Test like crazy - race conditions and bugs in multithreaded applications are hellish to troubleshoot - often you only see the forensic aftermath of the massacre.
- 将您的工作负载拆分为离散的并行可执行单元
- 测量和表征工作负载类型 - 网络密集型、I/O 密集型、CPU 密集型等 - 这些成为您的工作池策略的基础。例如,对于网络密集型应用程序,您可以拥有相当大的工作人员池,但是对于 CPU 密集型任务,拥有比硬件线程更多的工作人员是没有意义的。
- 考虑使用队列/数组或 ThreadWorkerPool 来管理线程池。前者比后者更精细控制。
- 如果可以,学习优先选择异步 I/O 模式而不是同步模式 - 释放更多 CPU 时间来执行其他任务。
- 努力消除或至少减少围绕竞争资源(如磁盘)的序列化。
- 最小化 I/O,在尽可能短的时间内获取和保持最低级别的锁。(Reader/Writer 锁是您的朋友)
5. 梳理该代码以确保资源以一致的顺序锁定,以最大程度地减少致命拥抱。 - 疯狂测试——多线程应用程序中的竞争条件和错误很难解决——通常你只能看到大屠杀的法医后果。
Bear in mind that it is entirely possible that a multi-threaded version could perform worse than a single-threaded version of the same app. There is no excuse for good engineering measurement.
请记住,多线程版本的性能完全有可能比同一应用程序的单线程版本更差。良好的工程测量没有任何借口。
回答by Chris Wenham
For C#, start learning the LINQ-way of doing things, then make use of the Parallel LINQlibrary and its .AsParallel() extension.
对于 C#,开始学习 LINQ 的做事方式,然后利用Parallel LINQ库及其 .AsParallel() 扩展。
回答by Lou Franco
You might want to take a look at the parallel extensions for .NET
您可能想看看 .NET 的并行扩展
回答by Lars Truijens
To be able to utilize multiple cores more efficiently you should divide your work up in parts that can be executed in parallel and use threads to divide the work over the cores. You could use threads, background workers, thread pools, etc
为了能够更有效地利用多个内核,您应该将您的工作分成可以并行执行的部分,并使用线程将工作分配到内核上。您可以使用线程、后台工作者、线程池等
回答by Xavier Nodet
回答by McKenzieG1
Understanding the parallelism (or potential for parallelism) in the problem(s) you are trying to solve, your application and its algorithms is much more important than any details of thread synchronization, libraries, etc.
了解您试图解决的问题中的并行性(或并行性的潜力),您的应用程序及其算法比线程同步、库等的任何细节都重要得多。
Start by reading Patterns for Parallel Programming(which focuses on 'finding concurrency' and higher-level design issues), and then move on to The Art of Multiprocessor Programming(practical details starting from a theoretical basis).
首先阅读并行编程模式(侧重于“发现并发”和更高级别的设计问题),然后继续阅读多处理器编程的艺术(从理论基础开始的实际细节)。