Tuesday, May 11, 2010

New feature in .NET 4.0 called the Task Parallel Library

A new feature in .NET 4.0 called the Task Parallel Library. With this library, it is much easier to write code in a managed language that makes use of multiple cores where available. This gives us the option to write code as parallel tasks that are run concurrently across available processors; generally resulting in significantly speeded up code.



using System;
using System.Threading.Tasks;

namespace ParallelForSample
{
public class Multi
{
public static void Calculate(int val)
{
Utility util = new Utility();
util.Start();

int[,] G = new int[val, val];

Parallel.For(0, val,
delegate(int k)
{
Parallel.For(0, val, delegate(int i)
{
for (int j = 0; j < val; j++)
G[i, j] = Math.Min(G[i, j], G[i, k] + G[k, j]);
});
}
);

util.Stop();
}
}
}

No comments:

Post a Comment