Untitled

mail@pastecode.io avatar
unknown
csharp
a year ago
1.1 kB
1
Indexable
Never
using System;
using System.Threading.Tasks;

class Program {
    static void Main(string[] args) {
        int[,] matrixA = new int[,] { { 1, 2, 3 }, { 4, 5, 6 } };
        int[,] matrixB = new int[,] { { 7, 8 }, { 9, 10 }, { 11, 12 } };
        int[,] resultMatrix = new int[matrixA.GetLength(0), matrixB.GetLength(1)];
        int maxThreads = Environment.ProcessorCount;

        Parallel.For(0, matrixA.GetLength(0), new ParallelOptions { MaxDegreeOfParallelism = maxThreads }, i => {
            for (int j = 0; j < matrixB.GetLength(1); j++) {
                int sum = 0;
                for (int k = 0; k < matrixB.GetLength(0); k++) {
                    sum += matrixA[i, k] * matrixB[k, j];
                }
                resultMatrix[i, j] = sum;
            }
        });

        Console.WriteLine("Result matrix:");
        for (int i = 0; i < resultMatrix.GetLength(0); i++) {
            for (int j = 0; j < resultMatrix.GetLength(1); j++) {
                Console.Write(resultMatrix[i, j] + " ");
            }
            Console.WriteLine();
        }
        Console.ReadLine();
    }
}