English 中文(简体)
PLINQ:如何在超过4条深线上运行平行质量?
原标题:PLINQ: how to run a ParallelQuery on more than 4 threads?
  • 时间:2011-07-27 11:51:11
  •  标签:
  • c#
  • plinq

更新——改变了问题的标题,以反映我真正在后面的内容。

审议以下法典:

// this query generates 12 instances of Func<int>, which each when executed
// print something to the console and wait for 1 second.
var actions = Enumerable.Range(0, 12).Select(i => new Func<int>(() =>
{
    Console.WriteLine("{0} - waiting 1 sec", i);
    Thread.Sleep(1000);
    return 1;
}));

// define a parallel query. Note the WithDegreeOfParallelism call here.
var query = from action in actions.AsParallel().WithDegreeOfParallelism(12)
            select action();

// execute, measuring total duration
var stopw = Stopwatch.StartNew();
query.ToList();
Console.WriteLine(stopw.Elapsed);
Console.WriteLine(Environment.ProcessorCount); // 3 on my machine

When omitting the call to WithDegreeOfParallelism, this executes in 4 chunks, taking about 4 seconds in total, which is what I would expect since my CPU count is 3.

However, when calling WithDegreeOfParallelism with any number above 4, I always get 3 chunks, and the total duration does not go under 3 seconds. I would expect that a value of 12 would get a total duration of (a little more than) 1 second.

我失踪了什么? 我如何能够同时执行4个非万国邮联的密集任务,这是我之后的吗?

Update: I could of course go back to manually spinning up threads, but I was hoping that the new PFX library would make this a bit easier... Anyway, the code below gives me about 1 second total execution time

List<Thread> threads = new List<Thread>();
for (int i = 0; i < 12; i++)
{
    int i1 = i;
    threads.Add(new Thread(() =>
    {
        Console.WriteLine(i1);
        Thread.Sleep(1000);
    }));
}
threads.ForEach(t => t.Start());
threads.ForEach(t => t.Join());
最佳回答

As I said WithDegreeOfParallelism is setting only a upper bound.Try increasing your tasks from 10 to 100. You will ended up with around 10 seonds for all 100 of them. Your code is good for larger number of tasks having smaller operations. and add Console.WriteLine("{0} threads " ,Process.GetCurrentProcess().Threads.Count); inside your task then you can see how many threads are created.( Thread count is not the count of plinq created threads. See how it increasing).

有许多办法可以采取与普林克平行的做法。 http://msdn.microsoft.com/en-us/library/dd997411.aspx” rel=“nofollow”http://msdn.microsoft.com/en-us/library/d997411.aspx 。 你们需要选择最佳途径,满足相关要求,以取得更好的业绩。

问题回答

在你平行地开始新任务,选择<代码>。 他们将开始走下去,而不是坐等到在校对池上铺面。

与DegreeOfParallelism一道决定有多少人。 任务应当由文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、社会、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、文化、

自2006年以来 任务作为ThreadPool上的工作项目执行,执行询问的校对数量将限制ThreadPool的大小。 ThreadPool将视需要增加校对,但可以同时使用——ThreadPool可能每秒增加2个校对。

如果你想迅速为ThreadPool增加校对面,你可以使用《神 Set法》。 如果你在你的法典一开始就把这一守则摆在桌面上,那么这一检验就应当二次完成:

int prevThreads, prevPorts;
ThreadPool.GetMinThreads(out prevThreads, out prevPorts);
ThreadPool.SetMinThreads(12, prevPorts);

你们可以决定你们需要多少条read子,然后使用“三明星”和“三思”来在ThreadPool的大小上设定约束。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签