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
It could start 1000 threads for some degenerate 'DoSomething'.
32767 in Framework 4.0 (64-bit environment)
Degree of parallelism is the maximum number of concurrently executing tasks that will be used to process the query. var result = Tabel.
You need to specify a ParallelOptions
value with a MaxDegreeOfParallelism
:
For example:
Parallel.For(0, 10, new ParallelOptions { MaxDegreeOfParallelism = 4 }, count => { Console.WriteLine(count); });
Use MaxDegreeOfParalelism property for running the loop
Parallel.For(0, 1000, new ParallelOptions { MaxDegreeOfParallelism = 2 }, ...);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With