Book Image

Learning .NET High-performance Programming

By : Antonio Esposito
Book Image

Learning .NET High-performance Programming

By: Antonio Esposito

Overview of this book

Table of Contents (16 chapters)
Learning .NET High-performance Programming
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Data parallelism with TPL


As already said, data parallelism with TPL happens only when we deal with a dataset (not the DataSet class). Within .NET, the easy way of doing this is with the Parallel.For and Parallel.ForEach methods from the Parallel module. The following example shows the basic usage of the Parallel module:

for (int i = 0; i < 10; i++)
{
    //do something
}

Parallel.For(0, 10, i =>
{
    //do something
});

The first thing that catches our eye is the singularity of logic. While in task parallelism we deal with multiple instances of logic; here, there is always only one type of logic. We simply execute it on multiple CPUs.

This example is obviously incompatible with the Set Theory previously exposed, because there is neither a simple collection of objects. In other words, the parallel For is an iterative structure as the normal For.

To parallelize some logic regarding just a simple collection, the Parallel class gives us the ForEach method:

var invoiceIdList = new[] { 1, 2...