如何配置在一个的Parallel.For线程的最大数量 [英] How to configure a maximum number of threads in a Parallel.For
本文介绍了如何配置在一个的Parallel.For线程的最大数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是平行的例子微软礼物,我想知道如何配置这些代码的线程的最大数量。
//一个基本的矩阵乘法。
//并行化外环由行源数组分区。
System.Threading.Tasks.Parallel.For(0,matARows,我= GT;
{
为(INT J = 0; J< matBCols; J ++)
{
//使用临时来提高并行性能
双TEMP = 0;
的for(int k = 0; K< matACols; k ++)
{
温度+ =马塔[I,K] * MATB [K,J];
}
结果[I,J] =温度;
}
}); //的Parallel.For
解决方案
您需要指定一个< A HREF =http://msdn.microsoft.com/en-us/library/system.threading.tasks.paralleloptions.aspx> ParallelOptions
值以 MaxDegreeOfParallelism
:
例如:
的Parallel.For(0,10,新ParallelOptions {MaxDegreeOfParallelism = 4},算上= GT;
{
Console.WriteLine(计数);
} );
This is the example microsoft presents for the parallel for, and I'd like to know how configure a maximum number of threads for this code.
// A basic matrix multiplication.
// Parallelize the outer loop to partition the source array by rows.
System.Threading.Tasks.Parallel.For(0, matARows, i =>
{
for (int j = 0; j < matBCols; j++)
{
// Use a temporary to improve parallel performance.
double temp = 0;
for (int k = 0; k < matACols; k++)
{
temp += matA[i, k] * matB[k, j];
}
result[i, j] = temp;
}
}); // Parallel.For
解决方案
You need to specify a ParallelOptions
value with a MaxDegreeOfParallelism
:
For example:
Parallel.For(0, 10, new ParallelOptions { MaxDegreeOfParallelism = 4 }, count =>
{
Console.WriteLine(count);
});
这篇关于如何配置在一个的Parallel.For线程的最大数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文