部队任务< T>不同的核心是什么? [英] Force Task<T> to different core ?

查看:180
本文介绍了部队任务< T>不同的核心是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

太平人寿 PLINQ 自动分配的工作线程(在核心/ S ... {以及如果#主题> #cores 那么,> 1个线程上运行相同的核心的})

Tpl and Plinq automatically assign work to threads (at core/s... {well ,if #threads> #cores then , >1 thread will run on the same core. }).

不过,比方说,我有 MyMethod1(){...} MyMethod2(){...} 而我需要做的确定的(!),每个将运行在的不同的的核心! (例如密集型计算)

However , Let's say I have MyMethod1(){..} and MyMethod2(){..} and I need to make sure(!) that each will run on a different core! (e.g. intensive computing)

最近的解决方案,我发现是PLINQ的 .WithExecutionMode(ParallelExecutionMode.ForceParallelism)

The nearest solution I found is Plinq's .WithExecutionMode (ParallelExecutionMode.ForceParallelism)

但是,这是一个不同的情况下PLINQ可能会认为这是更好地做到这一点的依次的而不是parallel.Also,我不使用PLINQ。我只有2种方法,需要对不同的内核上运行。

But this is for a different situation where Plinq might think that it is better to do it sequentially rather then parallel.Also ,I dont use Plinq. I only have 2 methods which needs to run on a different core.

我该怎么办呢?

P.S。有在这里SO的答案,建议使用 TaskCreationOptions.LongRunning ,但它只是暗示的的TaskScheduler,它应该更积极地创建线程池中的线程。但这些线程可以在相同的核心。和我的情况,要求他们必须在不同的核心。

p.s. there was an answer here at SO which suggested to use TaskCreationOptions.LongRunning but it is only to hint to the TaskScheduler that it should more aggressively create thread pool threads. but those threads can be on the same core. and my situation required them to be at a different core.

感谢你。

推荐答案

要做到这一点,需要打破抽象的几层,做之前,我建议做相当多的分析中是的某种的,这样做会比让框架处理资源分配更好的性能。我有些怀疑的(虽然我不能说我已经分析了它)。

To do this requires breaking down several layers of abstraction and before doing it I would recommend doing quite a bit of profiling to be certain that doing this will be better performance than letting the framework handle resource allocation. I am somewhat doubtful (though I can't say I've analyzed it).

您需要做的第一件事情就是要确保你的两个工作取值得到不同的管理线程执行。因为这是试图断言手动控制的东西的框架处理,可以肯定,这是你需要编写自己的的TaskScheduler 的情况。然而,现实则可以通过指定 TaskCreationOptions.LongRunning 标志做到这一点。在目前的桌面CLR,至少,总是会创建一个新的线程。但它只是一个提示,API-明智的。

The first thing you need to do is to make sure that your two Tasks get executed on different managed threads. Because this is trying to assert manual control over something the framework handles, to be certain that this is the case you would need to write your own TaskScheduler. However, realistically you can do this by specifying the TaskCreationOptions.LongRunning flag. On the current desktop CLR, at least, that will always create a new thread. But it's just a hint, API-wise.

接下来的抽象打破的是,管理VS本地线程。你的每一个方法应该被包裹在一个线程关联块。该框架允许切换托管线程上运行的物理线程。由于处理器亲和力是本地线程操作,你要告诉框架没有做到这一点。

The next abstraction to break is that of managed vs native threads. Each of your methods should be wrapped in a thread affinity block. The framework is allowed to switch the physical thread a managed thread is running on. Since processor affinity is a native thread operation, you have to tell the framework not to do that.

接下来,你需要获得对应于当前托管线程的本地线程。在每个方法,调用后 BeginThreadAffinity ,获得本地线程调用的 GetCurrentThreadId 通过P /调用。

Next, you need to get the native thread that corresponds to the current managed thread. In each method, after calling BeginThreadAffinity, get the native thread by calling GetCurrentThreadId via p/invoke.

现在你可以做这样的休息无论是本机或托管地,但我会假设你想这样做的.NET。在这种情况下,获得 ProcessThread 对应于本地线程对象,您可以设置<一个href="http://msdn.microsoft.com/en-us/library/system.diagnostics.processthread.processoraffinity.aspx"相对=nofollow>处理器关联或从那里理想的处理器

Now you can do the rest of this in either native or managed land, but I'll assume you want to do it in .NET. In that case, get the ProcessThread object which corresponds to the native thread, and you can set processor affinity or ideal processor from there:

Thread.BeginThreadAffinity();
int threadId = GetCurrentThreadId();
Process proc = Process.GetCurrentProcess();
ProcessThread procThread = proc.Threads.Cast<ProcessThread>().Single(
    pt => pt.Id == threadId
);
procThread.ProcessorAffinity = new IntPtr(0x01);
//
// work
//
procThread.ProcessorAffinity = new IntPtr(0xFFFF);
Thread.EndThreadAffinity()

这篇关于部队任务&LT; T&GT;不同的核心是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆