Parallel For Loops
One first form of data parallelism is exposed by the
Parallel.For set of methods, which provide a parallel
for loop. The idea of a parallel
for loop is to run the iterations of the loop in parallel, followed by a wait operation for all the iterations to complete. This is similar to the
Parallel.Invoke construct we looked at in the “Parallel Invocation” section, earlier in this chapter.
An example of one of the simplest overloads of
Parallel.For is shown here. Notice how similar this loop looks to a regular C#
for loop, thanks to the concise syntax of lambda expressions:
double[,] xs = GetMatrix(300, 400);double[,] ys = GetMatrix(400, 500);double[,] zs = new double[300, 500];var sw = Stopwatch.StartNew(); ...