Exemplo n.º 1
0
        private static void CompareTimeOfWorkDependingOnMatrixSize()
        {
            var multipliers = new List <(string Name, IMatrixMultiplier Multiplier)>
            {
                ("Synchronous Multiplier", new SynchronousMatrixMultiplier()),
                ("Parallel.For Multiplier", new ParallelForMatrixMultiplier()),
                ("Striped Parallelizing Multiplier", new StripedParallelizingMatrixMultiplier()),
                ("Sequentially Parallelizing Multiplier", new SequentiallyParallelizingMatrixMultiplier())
            };

            var generator = new MatrixGenerator();

            for (var i = 1; i < 1001; i += i < 100 ? 1 : i < 500 ? 10 : 100)
            {
                Console.WriteLine($"Size: {i} * {i}");
                var left  = generator.Generate(i, i);
                var right = generator.Generate(i, i);
                foreach (var(name, multiplier) in multipliers)
                {
                    var timestamp = ElapseWorkTime(multiplier, left, right);
                    Console.WriteLine($"{name} - {timestamp.TotalMilliseconds}");
                }
                Console.WriteLine();
            }
        }
Exemplo n.º 2
0
        public static void Main()
        {
            int    matrixLength = ConsoleInput.GetInput();
            Matrix matrix       = MatrixGenerator.Generate(matrixLength);

            ConsoleOutput.PrintMatrix(matrix);
        }
        public void ThrowArgumentOutOfRangeException_WhenProvidedMatrixSizeIsNotPositive(int matrixSize)
        {
            // arrange
            var matrixGenerator = new MatrixGenerator();

            // act and assert
            Assert.Throws <ArgumentOutOfRangeException>(() => matrixGenerator.Generate(matrixSize));
        }
        public void NotThrow_WhenValidMatrixSizeIsPassed()
        {
            // arrange
            var matrixGenerator = new MatrixGenerator();
            int matrixSize      = 10;

            // act and assert
            Assert.DoesNotThrow(() => matrixGenerator.Generate(matrixSize));
        }
Exemplo n.º 5
0
        private static void Main(string[] args)
        {
            const int n = 6;

              var generator = new MatrixGenerator(n);
              var matrix = generator.Generate();
              for (var y = 0; y < n; y++)
              {
            for (var x = 0; x < n; x++)
              Console.Write("{0,4:D} ", matrix[x, y]);

            Console.WriteLine();
            Console.WriteLine();
              }
        }
        public void ReturnCorrectlyFilledMatrix_WhenPassedMatrixSizeIsOne()
        {
            // arrange
            var expectedMatrix = new int[, ]
            {
                { 1 }
            };

            var matrixGenerator = new MatrixGenerator();
            int matrixsize      = 1;

            // act
            var resultMatrix = matrixGenerator.Generate(matrixsize);

            // assert
            CollectionAssert.AreEqual(expectedMatrix, resultMatrix);
        }
Exemplo n.º 7
0
        static void Main(string[] args)
        {
            Console.WriteLine("Enter maximum size of matrix to multiply");
            int N = int.Parse(Console.ReadLine());

            Console.WriteLine("Enter count of reiteration");
            int numberOfTests = int.Parse(Console.ReadLine());

            var results = new List <Result>();
            var start   = DateTime.Now;

            for (int i = 1; i <= N; i *= 2)
            {
                Result r = new Result(i.ToString());
                for (int n = 1; n <= numberOfTests; n++)
                {
                    Console.WriteLine("Testing {0} for {1}/{2} times.", i, n, numberOfTests);

                    var a = MatrixGenerator.Generate(i);
                    var b = MatrixGenerator.Generate(i);

                    Stopwatch t1     = Stopwatch.StartNew();
                    var       normal = Matrix.NormalMultiply(a, b);
                    t1.Stop();

                    Stopwatch t2       = Stopwatch.StartNew();
                    var       strassen = Matrix.StrassenMultiply(a, b);
                    t2.Stop();

                    r.Times1.Add(t1.Elapsed.TotalSeconds);
                    r.Times2.Add(t2.Elapsed.TotalSeconds);
                }
                results.Add(r);
            }

            string consoleFormat = "{0}\t{1:N6}s\t{2:N6}s";

            WriteResults(Console.Out, results, consoleFormat, "Size \t Normal \t Straussen");
            Console.WriteLine(Environment.NewLine + "Total time " + (DateTime.Now - start).ToString());
            string fileFormat = "{0};{1:N6};{2:N6}";
            string fileName   = string.Format("results_{0}_{1}_{2}.csv", N, numberOfTests, DateTime.Now.ToFileTime());

            SaveResultsToCSV(results, fileFormat, "Size of matrix;Normal multiply;Straussen multiply", fileName);
            Console.ReadKey();
        }
        public void ReturnCorrectlyFilledMatrix_WhenPassedMatrixSizeIsThree()
        {
            // arrange
            var expectedMatrix = new int[, ]
            {
                { 1, 7, 8 },
                { 6, 2, 9 },
                { 5, 4, 3 }
            };

            var matrixGenerator = new MatrixGenerator();
            int matrixSize      = 3;

            // act
            var resultMatrix = matrixGenerator.Generate(matrixSize);

            CollectionAssert.AreEqual(expectedMatrix, resultMatrix);
        }
        public void ReturnCorrectlyFilledMatrix_WhenPassedMatrixSizeIsFive()
        {
            // arrange
            var expectedMatrix = new int[, ]
            {
                { 1, 13, 14, 15, 16 },
                { 12, 2, 21, 22, 17 },
                { 11, 23, 3, 20, 18, },
                { 10, 25, 24, 4, 19 },
                { 9, 8, 7, 6, 5 }
            };

            var matrixGenerator = new MatrixGenerator();
            int matrixsize      = 5;

            // act
            var resultMatrix = matrixGenerator.Generate(matrixsize);

            // assert
            CollectionAssert.AreEqual(resultMatrix, expectedMatrix);
        }
        public void ReturnCorrectlyFilledMatrix_WhenPassedMatrixSizeIsSix()
        {
            // arrange
            var expectedMatrix = new int[, ]
            {
                { 1, 16, 17, 18, 19, 20, },
                { 15, 2, 27, 28, 29, 21, },
                { 14, 31, 3, 26, 30, 22, },
                { 13, 36, 32, 4, 25, 23, },
                { 12, 35, 34, 33, 5, 24, },
                { 11, 10, 9, 8, 7, 6 }
            };

            var matrixGenerator = new MatrixGenerator();
            int matrixSize      = 3;

            // act
            var resultMatrix = matrixGenerator.Generate(matrixSize);

            CollectionAssert.AreEqual(expectedMatrix, resultMatrix);
        }
Exemplo n.º 11
0
        private static void CompareTimeOfWorkOnConstantMatrixSize()
        {
            Console.WriteLine("Generating matrices...");
            var generator = new MatrixGenerator();
            var left      = generator.Generate(500, 500);
            var right     = generator.Generate(500, 500);

            var cores        = Environment.ProcessorCount;
            var threadCounts = new int[8] {
                1, 2, 3, cores, cores + 1, cores * 2, cores * 3, cores * 4
            };

            Console.WriteLine("Calculating the time of Synchronous Parallelizing Matrix Multiplier work...");
            var synchronousMultiplierTimestamps = new List <(string, TimeSpan)>
            {
                ("1", ElapseWorkTime(new SynchronousMatrixMultiplier(), left, right))
            };

            Console.WriteLine("Calculating the time of Parallel.For Matrix Multiplier work...");
            var parallelForMultiplierTimestamps = new List <(string, TimeSpan)>
            {
                ("Auto", ElapseWorkTime(new ParallelForMatrixMultiplier(), left, right))
            };

            Console.WriteLine("Calculating the time of Striped Parallelizing Matrix Multiplier work...");
            var strippedMultiplierTimestamps = new List <(string, TimeSpan)>();

            foreach (var threadCount in threadCounts)
            {
                var strippedMultiplier = new StripedParallelizingMatrixMultiplier(threadCount);
                strippedMultiplierTimestamps.Add((threadCount.ToString(), ElapseWorkTime(strippedMultiplier, left, right)));
            }

            Console.WriteLine("Calculating the time of Sequentially Parallelizing Matrix Multiplier work...");
            var sequentialMultiplierTimestamps = new List <(string, TimeSpan)>();

            foreach (var threadCount in threadCounts)
            {
                var sequentialMultiplier = new SequentiallyParallelizingMatrixMultiplier(threadCount);
                sequentialMultiplierTimestamps.Add((threadCount.ToString(), ElapseWorkTime(sequentialMultiplier, left, right)));
            }

            var multipliersWorkTime = new List <(string Name, List <(string ThreadsCount, TimeSpan Timespan)> Timespans)>
            {
                ("SynchronousMatrixMultiplier", synchronousMultiplierTimestamps),
                ("ParallelForMatrixMultiplier", parallelForMultiplierTimestamps),
                ("SequentiallyParallelizingMatrixMultiplier", sequentialMultiplierTimestamps),
                ("StripedParallelizingMatrixMultiplier", strippedMultiplierTimestamps)
            };

            foreach (var(name, timespans) in multipliersWorkTime)
            {
                Console.WriteLine(name);
                Console.WriteLine("Threads - Time");
                foreach (var(threadsCount, timestamp) in timespans)
                {
                    Console.WriteLine($"{threadsCount} - {FormatTimeSpan(timestamp)}");
                }
                Console.WriteLine();
            }
        }