简体   繁体   English

C#在核心之间有效地划分任务

[英]C# Efficiently Dividing Tasks Between Cores

I'm working on a small simulation that is running on my 8-core workstation. 我正在研究在我的8核工作站上运行的小型模拟。 The simulation involves modeling the interactions between a large number of independent nodes. 模拟涉及对大量独立节点之间的交互进行建模。 During one phase I need to perform a series of simple atomic operations to each node in parallel. 在一个阶段中,我需要并行地对每个节点执行一系列简单的原子操作。 I have been using Parallel.ForEach from System.Threading.Tasks to apply the operation concurrently to each node in the list of all nodes. 我一直在使用System.Threading.Tasks中的Parallel.ForEach将操作同时应用到所有节点列表中的每个节点。

This worked well for the 100-500 nodes I used for testing. 这适用于我用于测试的100-500个节点。 The load was balanced very well with all cores constantly utilized. 负载平衡得很好,所有核心都在不断使用。 Unfortunately, when I attempt to run the simulation with the main dataset (5000+ nodes), everything goes wrong. 不幸的是,当我尝试使用主数据集(5000+节点)运行模拟时,一切都会出错。 All 8 cores stay idle most of the time, spiking to 100% every few seconds and then returning to 1% utilization. 所有8个核心在大多数时间保持空闲状态,每几秒钟增加到100%,然后恢复到1%的利用率。 After a few minutes of this an OutOfMemoryException is thrown and the program crashes. 几分钟后抛出OutOfMemoryException并且程序崩溃。

I am not completely sure what is wrong, but remain suspicious that my current code is spawning many more threads than would be optimal for the task. 我不完全确定有什么问题,但仍然怀疑我的当前代码产生的线程数多于对任务最佳的线程数。 I think the ideal method would be for the model to detect the number of available cores N, partition the list of nodes into N segments, then spawn N threads, giving each thread a separate partition of the list. 我认为理想的方法是模型检测可用核心数N,将节点列表划分为N个段,然后生成N个线程,给每个线程一个单独的列表分区。

What I'd like to ask is if this is indeed a good solution to the problem, do better ones exist, and how should it be implemented in C#? 我想问的是,如果这确实是问题的一个很好的解决方案,那么存在更好的解决方案,它应该如何在C#中实现? Any advice or comments are welcome. 欢迎任何建议或意见。

EDIT: Code sample by request 编辑:按要求编码样本

Parallel.ForEach(listOfNodes, tempNode =>
{
   tempNode.foo();
} );

<snip>

void foo()
{
   foreach(myType bar in listOfmyType)
   {
       if (bar.isActive)
           setNodeActive();
   }
} 

See this thread, which discusses limiting the number of threads that Parallel.For uses to avoid memory starvation: 请参阅此线程,该线程讨论限制Parallel.For用于避免内存不足的线程数:

http://connect.microsoft.com/VisualStudio/feedback/details/534571/parallel-foreach-may-create-an-inordinate-number-of-threads http://connect.microsoft.com/VisualStudio/feedback/details/534571/parallel-foreach-may-create-an-inordinate-number-of-threads

I would try setting ParallelOptions.MaxDegreeOfParallelism to about 500, and see what happens. 我会尝试将ParallelOptions.MaxDegreeOfParallelism设置为大约500,看看会发生什么。

I think the ideal method would be for the model to detect the number of available cores N, partition the list of nodes into N segments, then spawn N threads, giving each thread a separate partition of the list. 我认为理想的方法是模型检测可用核心数N,将节点列表划分为N个段,然后生成N个线程,给每个线程一个单独的列表分区。

Which is exactly what Parallel.ForEach does, so there must be another problem. 这正是Parallel.ForEach所做的,所以必然会有另一个问题。

It's going to be very hard to come up with a better (Thread-management) system yourself. 要自己想出一个更好的(线程管理)系统是非常困难的。 But you can use custom schedulers in the Task Library. 但您可以在任务库中使用自定义调度程序。

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

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