public static void Main(string[] args) { int optimalThreads = Size * Size / Environment.ProcessorCount; Thread[] threads = new Thread[Environment.ProcessorCount]; Matrix = new MatrixThread(FirstMatrix, SecondMatrix, Size); for (int i = 0; i < threads.Length; i++) { threads[i] = new Thread(SetMartixThread); int[] indexes = { i *optimalThreads, (i + 1) * optimalThreads }; threads[i].Start(indexes); } var watch = System.Diagnostics.Stopwatch.StartNew(); foreach (var item in threads) { item.Join(); } watch.Stop(); var elapsedMs = watch.Elapsed; Console.WriteLine("Время вычисления в многопоточном режиме: " + elapsedMs); ResultMatrix2 = MultiplicationMatrix(); Console.WriteLine("Совпадение двух матриц: "); bool flag = true; for (int row = 0; row < Size; row++) { for (int col = 0; col < Size; col++) { if (ResultMatrix[row, col] != ResultMatrix2[row, col]) { flag = false; } } } Console.WriteLine(flag); Console.ReadLine(); }
public static void Main(string[] args) { FirstMatrix = InitializationMatrix(); SecondMatrix = InitializationMatrix(); int optimalThreads = Dimension * Dimension / Environment.ProcessorCount; Thread[] ThreadArray = new Thread[Environment.ProcessorCount]; Matrix = new MatrixThread(FirstMatrix, SecondMatrix, Dimension); for (int i = 0; i < ThreadArray.Length; i++) { ThreadArray[i] = new Thread(SetMartixThread); int[] indexes = { i *optimalThreads, (i + 1) * optimalThreads }; ThreadArray[i].Start(indexes); } var watch = System.Diagnostics.Stopwatch.StartNew(); foreach (var item in ThreadArray) { item.Join(); } watch.Stop(); Console.WriteLine("Время потоковое: " + watch.Elapsed); ResultMatrix2 = MultiplicationMatrix(); Console.Write("Матрицы совпадают: "); bool flag = true; for (int row = 0; row < Dimension; row++) { for (int col = 0; col < Dimension; col++) { if (ResultMatrix[row, col] != ResultMatrix2[row, col]) { flag = false; } } } Console.WriteLine(flag); Console.ReadLine(); }