Force task <T> to another kernel?
Tpl and Plinq automatically assign work to threads (in core / s ... {well, if # threads > #cores ), then> 1 thread will run on the same core.}).
However, let's say I have MyMethod1(){..} and MyMethod2(){..} , and I need to make sure (!) That everyone will work on a different kernel! (e.g. intensive computing)
The closest solution I found is Plinq .WithExecutionMode (ParallelExecutionMode.ForceParallelism)
But this is for another situation, when Plink might think that it is better to do this in series, rather than in parallel. In addition, I do not use Plinq. I have only 2 methods that need to be run on a different kernel.
How can i do this?
ps in SO there was an answer that suggested using TaskCreationOptions.LongRunning , but only hinting at TaskScheduler that it should create thread pool threads more aggressively. but these threads can be on the same core. and my situation required them to be in a different core.
Thanks.
To do this, it is necessary to break down several levels of abstraction, and before doing this, I would recommend doing quite a lot of profiling to be sure that this will be better than allowing the allocation of resources for processing frames. I have some doubts (although I canβt say that I analyzed it).
The first thing you need to do is make sure that your two Task are running in different managed threads. Since this is trying to approve manual control over something that works with the framework, to be sure that it is, you need to write your own TaskScheduler . However, realistically, you can do this by specifying the TaskCreationOptions.LongRunning flag. In the current CLR working environment, at least a new thread will always be created. But this is just a hint, API wise.
The next abstraction for breaking is managed vs native threads. Each of your methods should be wrapped in a thread affinity block. The frame is allowed to switch the physical thread on which the managed thread is running. Since affinity for the processor is an operation with its own thread, you must say that the structure should not do this.
Then you need to get your own thread corresponding to the current managed thread. In each method, after calling BeginThreadAffinity get its own thread by calling GetCurrentThreadId via p / invoke.
Now you can do the rest of this on native or managed land, but I assume you want to do this in .NET. In this case, get a ProcessThread object that matches its own thread, and you can install or an 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()