// Collection of code snippets by Arne Vajhøj
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now)
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace T
{
///
/// Demo class for comp.os.vms post.
///
public class Program
{
///
/// Data holder class with:
/// array reference
/// start index and end index of slice
/// result sum of elements in slice
///
public class SumTask
{
public double[] X { get; set; }
public int IxStart { get; set; }
public int IxEnd { get; set; }
public double Result { get; set; }
///
/// Calculate sum for slice using for loop.
///
public void Run()
{
double[] temp = X;
int ix1 = IxStart;
int ix2 = IxEnd;
double res = 0;
for(int i = ix1; i < ix2; i++) res += temp[i];
Result = res;
}
}
///
/// Sum array using a for loop.
///
/// array
/// sum
private static double ManSeq(double[] x)
{
double res = 0;
for(int i = 0; i < x.Length; i++) res += x[i];
return res;
}
///
/// Sum array using 4 threads each processing 1/4 of the array.
///
/// array
/// sum
private static double ManThread(double[] x)
{
Thread[] t = new Thread[4]; // array size must be multipla of number of threads to get slices defined correct
SumTask[] st = new SumTask[t.Length];
for(int k = 0; k < t.Length; k++)
{
// define slice and start thread
st[k] = new SumTask { X = x, IxStart = k * x.Length / t.Length, IxEnd = (k + 1) * x.Length / t.Length };
t[k] = new Thread(st[k].Run);
t[k].Start();
}
double res = 0;
for(int k = 0; k < t.Length; k++)
{
// wait for thread to complete and add sums
t[k].Join();
res += st[k].Result;
}
return res;
}
///
/// Sum array by submitting tasks to a thread pool (thread managed by pool).
///
/// array
/// sum
private static double ManPool(double[] x)
{
Task[] t = new Task[16];
SumTask[] st = new SumTask[t.Length]; // array size must be multipla of number of tasks to get slices defined correct
for(int k = 0; k < t.Length; k++)
{
// define slice and and submit task
st[k] = new SumTask { X = x, IxStart = k * x.Length / t.Length, IxEnd = (k + 1) * x.Length / t.Length };
t[k] = Task.Factory.StartNew(st[k].Run);
}
double res = 0;
for(int k = 0; k < t.Length; k++)
{
// wait for task to complete and add sums
t[k].Wait();
res += st[k].Result;
}
return res;
}
///
/// Sum array using LINQ sum method - sequential (single threaded).
///
/// array
/// sum
private static double LinqSeq(double[] x)
{
return x.Sum();
}
///
/// Sum array using LINQ sum method - parallel (multi threaded using thread pool).
///
///
///
private static double LinqPar(double[] x)
{
return x.AsParallel().Sum();
}
private const int N = 100000000;
private const int M = 10;
private const int REP = 5;
private delegate double Summer(double[] x);
///
/// Test sum function.
///
/// label
/// sum function
/// array
private static void Test(string lbl, Summer sumf, double[] x)
{
DateTime dt1 = DateTime.Now;
for(int j = 0; j < M; j++)
{
double res = sumf(x);
// for such a big array we should end up between 0.49 and 0.51
if(res < 0.49 * N || 0.51 * N < res) throw new Exception("We have a bad RNG");
}
DateTime dt2 = DateTime.Now;
Console.WriteLine("{0,-20} : {1,5:F0}", lbl, (dt2 - dt1).TotalMilliseconds);
}
private static readonly Random rng = new Random();
///
/// Main program.
///
/// -
public static void Main(string[] args)
{
double[] x = new Double[N];
for(int i = 0; i < x.Length; i++) x[i] = rng.NextDouble();
for(int j = 0; j < REP; j++)
{
Test("Manual sequential", ManSeq, x);
Test("Manual threading", ManThread, x);
Test("Manual thread pool", ManPool, x);
Test("LINQ sequential", LinqSeq, x);
Test("LINQ parallel", LinqPar, x);
Console.WriteLine("----");
}
}
}
}
\