Created
March 18, 2016 01:10
-
-
Save tanayseven/9d1106b10787eb8c3c6b to your computer and use it in GitHub Desktop.
Parallel Matrix Multiplication using different programming models
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading; | |
using System.Linq; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Threading.Tasks; | |
using Aneka; | |
using Aneka.Threading; | |
using Aneka.Tasks; | |
namespace Test | |
{ | |
class LongIntArray | |
{ | |
class GenPar | |
{ | |
public long start; | |
public long end; | |
public long i; | |
} | |
private long []array; | |
private long[] sum; | |
public LongIntArray(long n){ | |
array = new long[n]; | |
} | |
public void Generate(Object _obj) { | |
GenPar obj = (GenPar)(_obj); | |
var rnd = new Random (int.Parse(DateTime.Now.ToString("HHmmssfff"))+(int)(obj.end-obj.start)); | |
for (long i = obj.start ; i < obj.end ; ++i) { | |
array[i] = rnd.Next ((int)obj.start,(int)obj.end); | |
} | |
} | |
public void SeqGenerateRandom() { | |
var obj = new GenPar (); | |
obj.start = 1; | |
obj.end = array.Length; | |
Generate (obj); | |
} | |
public override string ToString () | |
{ | |
string str = ""; | |
foreach (int i in array) { | |
str += i + " "; | |
} | |
return str; | |
} | |
public long SeqSum (){ | |
long sum = 0; | |
foreach (int i in array) { | |
sum += i; | |
} | |
return sum; | |
} | |
private void PartialSum(Object _obj) { | |
GenPar obj = (GenPar)_obj; | |
for (long i = obj.start; i < obj.end; i++) { | |
this.sum[obj.i] += array [i]; | |
} | |
} | |
public long ParallelSum(int par){ | |
Thread[] th = new Thread[par]; | |
this.sum = new long[par]; | |
int slice = array.Length / (par - 1); | |
int extra = array.Length % (par - 1); | |
long totalSum = 0; | |
GenPar obj = new GenPar (); | |
obj.start = 0; | |
obj.end = extra; | |
obj.i = 0; | |
th [0] = new Thread (new ParameterizedThreadStart (PartialSum)); | |
th [0].Start (obj); | |
for (int i = 1, j = extra; i < th.Length; ++i, j += slice) { | |
obj = new GenPar (); | |
obj.start = j; | |
obj.i = i; | |
obj.end = j+slice; | |
th [i] = new Thread (new ParameterizedThreadStart(PartialSum)); | |
th [i].Start (obj); | |
} | |
for (int i = 0; i < th.Length; ++i) { | |
th [i].Join (); | |
totalSum += this.sum [i]; | |
} | |
return totalSum; | |
} | |
} | |
class Matrix | |
{ | |
long [,]matA; | |
long [,]matB; | |
long [,]matC; | |
long n,m,o; | |
class Element{ | |
public long x, y; | |
} | |
public void RandomizeMatAB(int n, int m, int o){ | |
this.m = m; | |
this.n = n; | |
this.o = o; | |
matA = new long[n, m]; | |
matB = new long[m, o]; | |
matC = new long[n,o]; | |
var rnd = new Random (int.Parse(DateTime.Now.ToString("HHmmssfff"))+(int)(n*o)); | |
for (long i = 0 ; i < n ; ++i) { | |
for (long j = 0; j < m; ++j) { | |
matA [i,j] = rnd.Next (0, n*o); | |
} | |
} | |
for (long i = 0 ; i < m ; ++i) { | |
for (long j = 0; j < o; ++j) { | |
matB [i,j] = rnd.Next (0, n*o); | |
} | |
} | |
} | |
public void SeqMultiply(){ | |
for (long i = 0; i < n; ++i) { | |
for (long j = 0; j < o; ++j) { | |
for (long k = 0; k < m; ++k) { | |
matC[i,j] += matA[i,k]*matB[k,j]; | |
} | |
} | |
} | |
} | |
private void ComputeElement(Object _elem) { | |
Element elem = (Element)_elem; | |
matC [elem.x, elem.y] = 0; | |
for (int i = 0; i < m; ++i) { | |
matC [elem.x, elem.y] += matA [elem.x, i] * matB [i, elem.y]; | |
} | |
} | |
public void ParallelMultiply(){ | |
Thread[,] th = new Thread[n,o]; | |
for (long i = 0; i < n; ++i) { | |
for (long j = 0; j < o; ++j) { | |
Element elem = new Element (); | |
elem.x = i; | |
elem.y = j; | |
th [i,j] = new Thread (new ParameterizedThreadStart(ComputeElement)); | |
th [i,j].Start(elem); | |
} | |
} | |
for (long i = 0; i < n; ++i) { | |
for (long j = 0; j < o; ++j) { | |
th [i, j].Join (); | |
} | |
} | |
} | |
public void ParallelForMultiply(){ | |
Parallel.For(0, n,i => { | |
Parallel.For(0, o,j => { | |
Element elem = new Element (); | |
elem.x = i; | |
elem.y = j; | |
ComputeElement(elem); | |
}); | |
}); | |
} | |
public override string ToString() { | |
string res = ""; | |
for (long i = 0; i < n; ++i) { | |
for (long j = 0; j < m; ++j) { | |
res += (matA [i, j] + " "); | |
} | |
res += "\n"; | |
} | |
res += "\n\n\n"; | |
for (long i = 0; i < m; ++i) { | |
for (long j = 0; j < o; ++j) { | |
res += (matB [i, j] + " "); | |
} | |
res += "\n"; | |
} | |
res += "\n\n\n"; | |
for (long i = 0; i < n; ++i) { | |
for (long j = 0; j < o; ++j) { | |
res += (matC [i, j] + " "); | |
} | |
res += "\n"; | |
} | |
return res; | |
} | |
} | |
class MainClass | |
{ | |
public static void Main (string[] args) | |
{ | |
Stopwatch sw = new Stopwatch(); | |
Matrix mat = new Matrix (); | |
mat.RandomizeMatAB (200,200,200); | |
sw = new Stopwatch(); | |
sw.Start (); | |
mat.SeqMultiply (); | |
sw.Stop (); | |
Console.WriteLine ("Time Sequential MatMul elapsed: "+sw.Elapsed); | |
sw.Restart (); | |
mat.ParallelMultiply (); | |
sw.Stop (); | |
Console.WriteLine ("Time for Thread Parallel MatMul elapsed: "+sw.Elapsed); | |
sw.Restart (); | |
mat.ParallelForMultiply (); | |
sw.Stop (); | |
Console.WriteLine ("Time Parallel For MatMul elapsed: "+sw.Elapsed); | |
sw.Restart (); | |
//mat.AnekaThreadsMultiply (); | |
sw.Stop (); | |
Console.WriteLine ("Time Aneka Threads MatMul elapsed: "+sw.Elapsed); | |
sw.Restart (); | |
//mat.AnekaTasksMultiply (); | |
sw.Stop (); | |
Console.WriteLine ("Time Aneka Tasks MatMul elapsed: "+sw.Elapsed); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment