Advertisement
AI_UBI

C# Test matrix mul

Apr 16th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.88 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Threading.Tasks;
  5. using System.IO;
  6.  
  7.  
  8.  
  9. namespace MatrixTest1
  10. {
  11.     class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             const int TRIALS = 10;
  16.             var A = Matrix.RandomMatrix(2000, 2000, 1.0);
  17.             var B = Matrix.RandomMatrix(2000, 2000, 1.0);
  18.             var mList = new List<Matrix>(TRIALS * 2);
  19.  
  20.             Console.WriteLine("Warming up, wait...");
  21.             //give VM chance to warm-up
  22.             for (int i = 0; i < TRIALS; i++)
  23.                 mList.Add(A * B);
  24.  
  25.             Console.WriteLine("Starting trials!");
  26.             //actual trials
  27.             var sw = new Stopwatch();
  28.             sw.Restart();
  29.             for (int i = 0; i < TRIALS; i++)
  30.                 mList.Add(A * B);
  31.             sw.Stop();
  32.             var sec = sw.Elapsed.TotalSeconds;
  33.             Console.WriteLine($"{TRIALS} trials in {sec:F} seconds, {sec / TRIALS:F4}s per trial");
  34.             Console.ReadLine();
  35.         }
  36.     }
  37.  
  38.  
  39.     public class Matrix
  40.     {
  41.         public readonly int rows;
  42.         public readonly int cols;
  43.         private readonly double[][] mat;
  44.  
  45.         public Matrix(int iRows, int iCols)
  46.         {
  47.             rows = iRows;
  48.             cols = iCols;
  49.             mat = new double[rows][];
  50.             for (int i = 0; i < rows; i++)
  51.                 mat[i] = new double[cols];
  52.         }
  53.  
  54.  
  55.  
  56.  
  57.         public double this[int iRow, int iCol]
  58.         {
  59.             get { return mat[iRow][iCol]; }
  60.             set { mat[iRow][iCol] = value; }
  61.         }
  62.  
  63.         private double[] this[int iRow]
  64.         {
  65.             get { return mat[iRow]; }
  66.             set { mat[iRow] = value; }
  67.         }
  68.  
  69.         public static Matrix RandomMatrix(int iRows, int iCols, double dispersion)
  70.         {
  71.             var random = new ThreadRandom();
  72.             var matrix = new Matrix(iRows, iCols);
  73.             for (int i = 0; i < iRows; i++)
  74.                 for (int j = 0; j < iCols; j++)
  75.                     matrix[i, j] = random.NextDouble(-dispersion, dispersion);
  76.             return matrix;
  77.         }
  78.  
  79.  
  80.         private static void CalcMulRow(Matrix result, Matrix m1, Matrix m2, int index)
  81.         {
  82.             var iRowA = m1[index];
  83.             var iRowC = result[index];
  84.             for (int k = 0; k < m1.cols; k++)
  85.             {
  86.                 var kRowB = m2[k];
  87.                 var ikA = iRowA[k];
  88.                 for (int j = 0; j < result.cols; j++)
  89.                     iRowC[j] += ikA * kRowB[j];
  90.             }
  91.         }
  92.  
  93.  
  94.         private static Matrix Multiply(Matrix m1, Matrix m2)
  95.         {
  96.             if (m1.cols != m2.rows)
  97.                 throw new Exception("Wrong dimensions of matrix, m1.cols (" + m1.cols + ") != m2.rows (" + m2.rows + ")!");
  98.             var result = new Matrix(m1.rows, m2.cols);
  99.             Parallel.For(0, result.rows, (i) => CalcMulRow(result, m1, m2, i));
  100.             return result;
  101.         }
  102.  
  103.  
  104.         public static Matrix operator *(Matrix m1, Matrix m2) => Matrix.Multiply(m1, m2);
  105.     }
  106.  
  107.  
  108.     public class ThreadRandom
  109.     {
  110.         private static readonly Random seedRNG = new Random();
  111.         private readonly Random localRNG = new Random(seedRNG.Next());
  112.  
  113.         public int Next()
  114.         {
  115.             return localRNG.Next();
  116.         }
  117.  
  118.         public int Next(int maxValue)
  119.         {
  120.             return localRNG.Next(maxValue);
  121.         }
  122.  
  123.         public int Next(int minValue, int maxValue)
  124.         {
  125.             return localRNG.Next(minValue, maxValue);
  126.         }
  127.  
  128.         public double NextDouble()
  129.         {
  130.             return localRNG.NextDouble();
  131.         }
  132.  
  133.         public double NextDouble(double minValue, double maxValue)
  134.         {
  135.             var r = localRNG.NextDouble() * (maxValue - minValue);
  136.             return minValue + r;
  137.         }
  138.     }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement