。NET中具有任务并行库的目标特定CPU内核?

问题描述 投票:0回答:2

我正在研究一个项目,我们需要为某些功能实施多任务处理。为此,我们使用了.NET的出色API之一-任务并行库(TPL)。

例如TPL的代码(取自here

static void Main()
{
    var options = new ParallelOptions()
    {
        MaxDegreeOfParallelism = 2
    };
    List<int> integerList = Enumerable.Range(0,10).ToList();
    Parallel.ForEach(integerList, options, i =>
    {
        Console.WriteLine(@"value of i = {0}, thread = {1}",
            i, Thread.CurrentThread.ManagedThreadId);
    });

    Console.WriteLine("Press any key to exist");
    Console.ReadLine();
} 

我们知道,我们可以使用'MaxDegreeOfParallelism'选项设置要使用的最大线程数。假设我们有16个虚拟核心VM,我想运行应仅针对几个选定核心(例如C1,C2,c8,c9)的应用程序。请指导我如何使用TPL做到这一点?

谢谢,维杰

c# .net task-parallel-library multicore
2个回答
0
投票

这种在运行时的微优化超出了C#的范围。而且,它应该definitely不能硬编码到程序本身中。通常,.NET和OS的负载平衡功能将有效地处理正在运行的线程。

由于存在这种差异,可能需要使用速度限制:https://ericlippert.com/2012/12/17/performance-rant/

但是,您甚至可以在OS中设置这样的限制,而无需查看应用程序代码:https://www.techjunkie.com/restrict-apps-cpu-cores-processor-affinity/这听起来更像您需要/应该使用的。


0
投票

我不认为您可以在TPL抽象级别上做到这一点,但是answer可能对您有用。

© www.soinside.com 2019 - 2024. All rights reserved.