Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Diagnostics;
- using System.Threading.Tasks;
- class MultiplyMatrices
- {
- #region Sequential_Loop
- static void MultiplyMatricesSequential(double[,] matA, double[,] matB,
- double[,] result)
- {
- int matACols = matA.GetLength(1);
- int matBCols = matB.GetLength(1);
- int matARows = matA.GetLength(0);
- for (int i = 0; i < matARows; i++)
- {
- for (int j = 0; j < matBCols; j++)
- {
- double temp = 0;
- for (int k = 0; k < matACols; k++)
- {
- temp += matA[i, k] * matB[k, j];
- }
- result[i, j] += temp;
- }
- }
- }
- #endregion
- #region Parallel_Loop
- static void MultiplyMatricesParallel(double[,] matA, double[,] matB, double[,] result)
- {
- // Parallel.For
- }
- #endregion
- #region Main
- static void Main(string[] args)
- {
- // Set up matrices. Use small values to better view
- // result matrix. Increase the counts to see greater
- // speedup in the parallel loop vs. the sequential loop.
- int colCount = 180;
- int rowCount = 2000;
- int colCount2 = 270;
- double[,] m1 = InitializeMatrix(rowCount, colCount);
- double[,] m2 = InitializeMatrix(colCount, colCount2);
- double[,] result = new double[rowCount, colCount2];
- // First do the sequential version.
- Console.Error.WriteLine("Executing sequential loop...");
- Stopwatch stopwatch = new Stopwatch();
- stopwatch.Start();
- MultiplyMatricesSequential(m1, m2, result);
- stopwatch.Stop();
- Console.Error.WriteLine("Sequential loop time in milliseconds: {0}",
- stopwatch.ElapsedMilliseconds);
- stopwatch.Reset();
- result = new double[rowCount, colCount2];
- // Do the parallel loop.
- Console.Error.WriteLine("Executing parallel loop...");
- stopwatch.Start();
- MultiplyMatricesParallel(m1, m2, result);
- stopwatch.Stop();
- Console.Error.WriteLine("Parallel loop time in milliseconds: {0}",
- stopwatch.ElapsedMilliseconds);
- Console.Error.WriteLine("Press any key to exit.");
- Console.ReadKey();
- }
- #endregion
- #region Helper_Methods
- static double[,] InitializeMatrix(int rows, int cols)
- {
- double[,] matrix = new double[rows, cols];
- Random r = new Random();
- for (int i = 0; i < rows; i++)
- {
- for (int j = 0; j < cols; j++)
- {
- matrix[i, j] = r.Next(100);
- }
- }
- return matrix;
- }
- #endregion
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement