Exemple #1
0
        public void IsCorrectMultiplyingTest(int[,] left, int[,] right, int[,] expected, int numberOfTestCase)
        {
            var threads = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 100 };

            if (typeof(T) == typeof(StripedParallelizingMatrixMultiplier) ||
                typeof(T) == typeof(SequentiallyParallelizingMatrixMultiplier))
            {
                foreach (var thread in threads)
                {
                    multiplier = typeof(T) == typeof(StripedParallelizingMatrixMultiplier)
                        ? new StripedParallelizingMatrixMultiplier(thread)
                        : new SequentiallyParallelizingMatrixMultiplier(thread) as IMatrixMultiplier;

                    var result = multiplier.Multiply(left, right);

                    Assert.IsTrue(FunctionsOnMatrices.AreEqual(result, expected));
                }

                return;
            }

            var actualResult = multiplier.Multiply(left, right);

            Assert.IsTrue(FunctionsOnMatrices.AreEqual(actualResult, expected));
        }
Exemple #2
0
        private void RunMultiplyTest(IMatrixMultiplier matrixMultiplier)
        {
            int length = 4;

            int[][] a = new int[length][];
            int[][] b = new int[length][];
            for (int i = 0; i < length; i++)
            {
                a[i] = new int[length];
                b[i] = new int[length];
                for (int j = 0; j < length; j++)
                {
                    a[i][j] = i + j;
                    b[i][j] = i + j;
                }
            }
            int[][] c = matrixMultiplier.Multiply(a, b);
            Assert.AreEqual(5, c[0][0]);
            Assert.AreEqual(8, c[0][1]);
            Assert.AreEqual(11, c[0][2]);
            Assert.AreEqual(8, c[1][0]);
            Assert.AreEqual(14, c[1][1]);
            Assert.AreEqual(20, c[1][2]);
            Assert.AreEqual(11, c[2][0]);
            Assert.AreEqual(20, c[2][1]);
            Assert.AreEqual(29, c[2][2]);
        }
Exemple #3
0
        private static TimeSpan ElapseWorkTime(IMatrixMultiplier multiplier, int[,] left, int[,] right)
        {
            var stopwatch = new Stopwatch();

            stopwatch.Start();
            multiplier.Multiply(left, right);
            stopwatch.Stop();
            return(stopwatch.Elapsed);
        }
Exemple #4
0
 private void FailIfNotException <TException>(IMatrixMultiplier matrixMultiplier, int[][] a, int[][] b) where TException : System.Exception
 {
     try
     {
         matrixMultiplier.Multiply(a, b);
         Assert.Fail();
     }
     catch (TException)
     {
     }
 }
Exemple #5
0
        private static long PerformTest(int size, IMatrixMultiplier multiplier)
        {
            var matrix = MatrixGenerator.GenerateRandom(size);

            //Console.WriteLine("Source:\n" + matrix);

            var identity = MatrixGenerator.GenerateIdentity(size);

            //Console.WriteLine("Identity:\n" + identity);

            var elapsed = TimeMeasurer.Measure(() => { multiplier.Multiply(matrix, identity); });

            //Console.WriteLine("Result:\n" + result);

            return(elapsed);
        }
Exemple #6
0
        static async Task MainAsync()
        {
            Console.WriteLine("Please, choose option");
            Console.WriteLine("Press 1 to compare multipliers time of work on different number of threads and constant matrix size");
            Console.WriteLine("Press 2 to compare multipliers time of work on different matrix sizes");
            Console.WriteLine("Press any key to perform matrix multiplication");

            var choice = Console.ReadLine();

            switch (choice)
            {
            case "1":
                CompareTimeOfWorkOnConstantMatrixSize();
                break;

            case "2":
                CompareTimeOfWorkDependingOnMatrixSize();
                break;
            }

            Console.WriteLine("Enter path to file with left matrix");
            var pathLeft = Console.ReadLine();

            Console.WriteLine("Enter path to file with right matrix");
            var pathRight = Console.ReadLine();

            Console.WriteLine("Enter path to file, where result will be written");
            var resultPath = Console.ReadLine();

            int[,] left, right;
            try
            {
                var fileReader = new MatrixFileReader();
                left = await fileReader.ReadAsync(pathLeft);

                right = await fileReader.ReadAsync(pathRight);
            }
            catch (FileNotFoundException e)
            {
                Console.WriteLine(e.Message);
                return;
            }

            Console.WriteLine("\nChoose type of multiplier");
            Console.WriteLine("1 - Synchronous Matrix Multiplier");
            Console.WriteLine("2 - Parallel.For Matrix Multiplier");
            Console.WriteLine("3 - Striped Parallelizing Matrix Multiplier");
            Console.WriteLine("4 - Sequentially Parallelizing Matrix Multiplier");
            Console.WriteLine("Press any key to exit");

            choice = Console.ReadLine();
            IMatrixMultiplier multiplier = choice switch
            {
                "1" => new SynchronousMatrixMultiplier(),
                "2" => new ParallelForMatrixMultiplier(),
                "3" => new StripedParallelizingMatrixMultiplier(),
                "4" => new SequentiallyParallelizingMatrixMultiplier(),
                _ => null
            };

            if (multiplier == null)
            {
                return;
            }

            Console.WriteLine("Multiplying performs...");
            try
            {
                var result     = multiplier.Multiply(left, right);
                var fileWriter = new MatrixFileWriter();
                await fileWriter.WriteAsync(result, resultPath);
            }
            catch (ArgumentOutOfRangeException e)
            {
                Console.WriteLine(e.Message);
                return;
            }

            Console.WriteLine("Ready!");
        }
    }