Thursday, October 04, 2007

Coding for Multi-core / Parellel processing

M$ is comming up with changes in it's .NET SDK ver 3.5 to support or coding for multi-core / parellel processing.

Visual Studio 2008 is fully supported and all parallelism is expressed using normal method calls. For example, suppose you have the following for loop that squares the elements of an array:

for (int i = 0; i < 100; i++) {
a[i] = a[i]*a[i];
}
Since the iterations are independent of each other, that is, subsequent iterations do not read state updates made by prior iterations, you can use TPL to express the potential parallelism with a call to the parallel for method, like this:

Parallel.For(0, 100, delegate(int i) {
a[i] = a[i]*a[i];
}
More info here