Parallel For Loops by Bart De Smet

Safari, the world’s most comprehensive technology and business learning platform.

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required

O'Reilly logo

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(); ...

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required