Thursday, January 15, 2009

Managed Parallel Computing with Parallel Extensions (Listing)

Here is the complete listing of my previous post. To run it you need to download and install June 2008 CTP of Parallel Extensions

See previous post for more details

using System;
using System.Diagnostics;
using System.Threading.Tasks;

internal class Program
{
    private static void Main()
    {
        Console.WriteLine("Building the tree");
        Tree tree = Tree.Create();
        Console.Out.WriteLine("Pause");
        Console.ReadLine();
        Console.Out.WriteLine("Calculating");

        while (true)
        {
            Stopwatch sw = Stopwatch.StartNew();
            int sum = tree.Sum();
            sw.Stop();
            Console.WriteLine("Sum is {0} in {1}", sum, sw.ElapsedMilliseconds);

            Console.Out.WriteLine("Pause");
            Console.ReadLine();

            sw = Stopwatch.StartNew();
            sum = tree.ParallelSum1();
            sw.Stop();
            Console.WriteLine("ParallelSum1 is {0} in {1}", sum, sw.ElapsedMilliseconds);

            Console.Out.WriteLine("Pause");
            Console.ReadLine();

            sw = Stopwatch.StartNew();
            sum = tree.ParallelSum2();
            sw.Stop();
            Console.WriteLine("ParallelSum2 is {0} in {1}", sum, sw.ElapsedMilliseconds);

            Console.Out.WriteLine("Pause");
            Console.ReadLine();

            sw = Stopwatch.StartNew();
            sum = tree.ParallelSum3();
            sw.Stop();
            Console.WriteLine("ParallelSum3 is {0} in {1}", sum, sw.ElapsedMilliseconds);

            Console.Out.WriteLine("Pause");
            Console.ReadLine();
        }
    }
}

internal class Tree
{
    public Tree Left { get; set; }
    public Tree Right { get; set; }
    public int Value { get; set; }

    public static Tree Create()
    {
        return Build(1, 22);
    }

    public static Tree Build(int start, int level)
    {
        Tree tree = new Tree {Value = start};
        if (level <= 0) return tree;
        var left = Build(start + 1, level - 1);
        var right = Build(start + 1, level - 1);
        tree.Left = left;
        tree.Right = right;
        return tree;
    }

    public int Sum()
    {
        int left = Left == null ? 0 : Left.Sum();
        int right = Right == null ? 0 : Right.Sum();

        return Value + left + right;
    }

    public int ParallelSum1()
    {
        int left = 0;
        int right = 0;

        Task[] tasks = new[]
        {
            Task.Create(x => left = Left.Sum()),
            Task.Create(x => right = Right.Sum())
        };

        Task.WaitAll(tasks);
        return Value + left + right;
    }

    public int ParallelSum2()
    {
        Future<int> futureLeft = Future<int>.Create(() => Left == null ? 0 : Left.Sum());
        Future<int> futureRight = Future<int>.Create(() => Right == null ? 0 : Right.Sum());
        return Value + futureLeft.Value + futureRight.Value;
    }

    public int ParallelSum3()
    {
        Future<int> futureLeft = Future<int>.Create(() => Left == null ? 0 : Left.ParallelSum3());
        int futureRight = Right == null ? 0 : Right.Sum();
        return Value + futureLeft.Value + futureRight;
    }
}

No comments: