Advertisement
mvassilev

Untitled

Feb 25th, 2020
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.80 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.Threading.Tasks;
  4.  
  5. class MultiplyMatrices
  6. {
  7.     #region Sequential_Loop
  8.     static void MultiplyMatricesSequential(double[,] matA, double[,] matB,
  9.                                             double[,] result)
  10.     {
  11.         int matACols = matA.GetLength(1);
  12.         int matBCols = matB.GetLength(1);
  13.         int matARows = matA.GetLength(0);
  14.  
  15.         for (int i = 0; i < matARows; i++)
  16.         {
  17.             for (int j = 0; j < matBCols; j++)
  18.             {
  19.                 double temp = 0;
  20.                 for (int k = 0; k < matACols; k++)
  21.                 {
  22.                     temp += matA[i, k] * matB[k, j];
  23.                 }
  24.                 result[i, j] += temp;
  25.             }
  26.         }
  27.     }
  28.     #endregion
  29.  
  30.     #region Parallel_Loop
  31.     static void MultiplyMatricesParallel(double[,] matA, double[,] matB, double[,] result)
  32.     {
  33.         // Parallel.For
  34.     }
  35.     #endregion
  36.  
  37.     #region Main
  38.     static void Main(string[] args)
  39.     {
  40.         // Set up matrices. Use small values to better view
  41.         // result matrix. Increase the counts to see greater
  42.         // speedup in the parallel loop vs. the sequential loop.
  43.         int colCount = 180;
  44.         int rowCount = 2000;
  45.         int colCount2 = 270;
  46.         double[,] m1 = InitializeMatrix(rowCount, colCount);
  47.         double[,] m2 = InitializeMatrix(colCount, colCount2);
  48.         double[,] result = new double[rowCount, colCount2];
  49.  
  50.         // First do the sequential version.
  51.         Console.Error.WriteLine("Executing sequential loop...");
  52.         Stopwatch stopwatch = new Stopwatch();
  53.         stopwatch.Start();
  54.  
  55.         MultiplyMatricesSequential(m1, m2, result);
  56.         stopwatch.Stop();
  57.         Console.Error.WriteLine("Sequential loop time in milliseconds: {0}",
  58.                                 stopwatch.ElapsedMilliseconds);
  59.         stopwatch.Reset();
  60.         result = new double[rowCount, colCount2];
  61.  
  62.         // Do the parallel loop.
  63.         Console.Error.WriteLine("Executing parallel loop...");
  64.         stopwatch.Start();
  65.         MultiplyMatricesParallel(m1, m2, result);
  66.         stopwatch.Stop();
  67.         Console.Error.WriteLine("Parallel loop time in milliseconds: {0}",
  68.                                 stopwatch.ElapsedMilliseconds);
  69.  
  70.         Console.Error.WriteLine("Press any key to exit.");
  71.         Console.ReadKey();
  72.     }
  73.     #endregion
  74.  
  75.     #region Helper_Methods
  76.     static double[,] InitializeMatrix(int rows, int cols)
  77.     {
  78.         double[,] matrix = new double[rows, cols];
  79.  
  80.         Random r = new Random();
  81.         for (int i = 0; i < rows; i++)
  82.         {
  83.             for (int j = 0; j < cols; j++)
  84.             {
  85.                 matrix[i, j] = r.Next(100);
  86.             }
  87.         }
  88.         return matrix;
  89.     }
  90.     #endregion
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement