|
| 1 | +using System; |
| 2 | +using System.Collections; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Diagnostics; |
| 5 | +using System.Linq; |
| 6 | +using System.Text; |
| 7 | +using System.Threading.Tasks; |
| 8 | + |
| 9 | +namespace Ex2 |
| 10 | +{ |
| 11 | + class Program |
| 12 | + { |
| 13 | + static void Main(string[] args) |
| 14 | + { |
| 15 | + Console.WriteLine("Enter the number of rows: "); |
| 16 | + long rows = long.Parse(Console.ReadLine()); |
| 17 | + |
| 18 | + Console.WriteLine("Enter the number of cols: "); |
| 19 | + long cols = long.Parse(Console.ReadLine()); |
| 20 | + |
| 21 | + long[,] firstMatrix = new long[rows, cols]; |
| 22 | + long[,] secondMatrix = new long[rows, cols]; |
| 23 | + |
| 24 | + firstMatrix = fillMatrix(rows, cols); |
| 25 | + secondMatrix = fillMatrix(rows, cols); |
| 26 | + |
| 27 | + var cores = Environment.ProcessorCount; |
| 28 | + |
| 29 | + Stopwatch sw = new Stopwatch(); |
| 30 | + |
| 31 | + sw.Start(); |
| 32 | + multiplyMatrix(firstMatrix, rows, cols); |
| 33 | + sw.Stop(); |
| 34 | + Console.WriteLine(sw.ElapsedMilliseconds); |
| 35 | + sw.Reset(); |
| 36 | + |
| 37 | + sw.Start(); |
| 38 | + parallelmultiplyMatrix(firstMatrix, rows, cols); |
| 39 | + sw.Stop(); |
| 40 | + Console.WriteLine(sw.ElapsedMilliseconds); |
| 41 | + |
| 42 | + Console.WriteLine("Press enter to finish"); |
| 43 | + Console.ReadLine(); |
| 44 | + } |
| 45 | + |
| 46 | + private static void parallelmultiplyMatrix(long[,] firstMatrix, long rows, long cols) |
| 47 | + { |
| 48 | + Parallel.For(0, rows, (row) => |
| 49 | + { |
| 50 | + Parallel.For(0, cols, (col) => |
| 51 | + { |
| 52 | + firstMatrix[row, col] *= 99; |
| 53 | + }); |
| 54 | + }); |
| 55 | + } |
| 56 | + |
| 57 | + private static void multiplyMatrix(long[,] firstMatrix, long rows, long cols) |
| 58 | + { |
| 59 | + for (int row = 0; row < rows; row++) |
| 60 | + { |
| 61 | + for (int col = 0; col < cols; col++) |
| 62 | + { |
| 63 | + firstMatrix[row, col] *= 99; |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + private static void parallelmultiplyTwoMatrices(long[,] firstMatrix, long[,] secondMatrix, long rows, long cols) |
| 69 | + { |
| 70 | + long[,] matrix = new long[rows, cols]; |
| 71 | + |
| 72 | + Parallel.For(0, rows, i => { |
| 73 | + Parallel.For(0, cols, j => { |
| 74 | + matrix[i, j] = 0; |
| 75 | + Parallel.For(0, cols, k => { |
| 76 | + matrix[i, j] += firstMatrix[i, k] * secondMatrix[k, j]; |
| 77 | + }); |
| 78 | + }); |
| 79 | + }); |
| 80 | + } |
| 81 | + |
| 82 | + private static void multiplyTwoMatrices(long[,] firstMatrix, long[,] secondMatrix, long rows, long cols) |
| 83 | + { |
| 84 | + long[,] matrix = new long[rows, cols]; |
| 85 | + |
| 86 | + for (int i = 0; i < rows; i++) |
| 87 | + { |
| 88 | + for (int j = 0; j < cols; j++) |
| 89 | + { |
| 90 | + matrix[i, j] = 0; |
| 91 | + for (int k = 0; k < cols; k++) |
| 92 | + { |
| 93 | + matrix[i, j] += firstMatrix[i, k] * secondMatrix[k, j]; |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + private static long[,] fillMatrix(long rows, long cols) |
| 100 | + { |
| 101 | + long[,] matrix = new long[rows, cols]; |
| 102 | + |
| 103 | + for (int row = 0; row < rows; row++) |
| 104 | + { |
| 105 | + for (int col = 0; col < cols; col++) |
| 106 | + { |
| 107 | + matrix[row, col] = row * col; |
| 108 | + } |
| 109 | + } |
| 110 | + return matrix; |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments