两个方阵的并行乘法

Parallel multiplication of two square matrices

我的程序是将两个矩阵相乘生成一个矩阵,C。 当我创建一个大小为 3x3 (9) 的矩阵时,一切正常,但是当我想调整 M 和 N = 4(即 4x4 矩阵)的大小时,出现错误 ArrayIndexOutOfBoundsException.

修改了很多次还是看不出我的简单错误。 谁能告诉我如何修改数组以“自动”适应矩阵的大小?

public class ParallelMultiplier {
    public static int M = 4;
    public static int N = 4;

    public static int[][] A = {{1, 4}, {2, 5}, {3, 6}};
    public static int[][] B = {{8, 7, 6}, {5, 4, 3}};
    public static int[][] C = new int[M][N];
    public static WorkerThread[][] Threads = new WorkerThread[3][3];

    public static void main(String[] args) {
        for (int i = 0; i < M; i++) {
            for (int j = 0; j < N; j++) {
                Threads[i][j] = new WorkerThread(i, j, A, B, C);
                Threads[i][j].start();
            }
        }

        System.out.println("Elements of Matrix C:");
        for (int i = 0; i < M; i++) {
            for (int j = 0; j < N; j++) {
                System.out.println("[" + i + "," + j + "] = " + C[i][j]);
            }
        }
    }
}

和工人

class WorkerThread extends Thread {
    private int row;
    private int col;
    private int[][] A;
    private int[][] B;
    private int[][] C;

    public WorkerThread(int row, int col, int[][] A, int[][] B, int[][] C) {
        this.row = row;
        this.col = col;
        this.A = A;
        this.B = B;
        this.C = C;
    }

    public void run() {
        C[row][col] = (A[row][0] * B[0][col]) + (A[row][1] * B[1][col]);
    }
}

您需要的不仅仅是变量 M 和 N:

  1. 矩阵A的行数和列数;
  2. 矩阵B的行数和列数;
  3. 矩阵C的行数和列数;

根据1.和2.的值可以推导出3.的值,即:

  • 矩阵C的行数与矩阵A的行数相同;
  • 矩阵C的列数与矩阵B的列数相同;

my program is to multiply two matrix and display it in matrix C.

你的矩阵乘法算法是错误的,顺序代码应该是这样的:

for (int i = 0; i < rowsA; i++)
    for (int j = 0; j < colsB; j++)
        for (int k = 0; k < colsA; k++)
            C[i][j] += A[i][k] * B[k][j];

你还需要确保矩阵A和B可以相互复数,你不能随意设置这些矩阵的不同大小。有大量在线资源解释了矩阵 A 和 B 必须遵守的约束才能 A x B 成为可能。

如果静态设置矩阵:

public static int[][] A = {{1, 4}, {2, 5}, {3, 6}};
public static int[][] B = {{8, 7, 6}, {5, 4, 3}};

假设矩阵的每一行都具有相同的大小,您可以分别使用 A.lengthA[0].length 来获取行数和列数。这样,无论何时更改矩阵的大小时,它都会立即反映在代码中。

最后,您还需要确保在实际打印矩阵 C 的值之前等待线程完成其工作(例如, 调用 join)。例如通过做:

for (int i = 0; i < M; i++)
    for (int j = 0; j < N; j++)
        Threads[i][j].join();

未经测试的 运行 示例:

class WorkerThread extends Thread {
    private final int row_a;
    private final int col_b;
    private final int col_a;
    private final int[][] A;
    private final int[][] B;
    private final int[][] C;

    public WorkerThread(int row_a, int col_b, int col_a,
                        int[][] A, int[][] B, int[][] C) {
        this.row_a = row_a;
        this.col_b = col_b;
        this.col_a = col_a;
        this.A = A;
        this.B = B;
        this.C = C;
    }

    public void run() {
        C[row_a][col_b] += A[row_a][col_a] * B[col_a][col_b];
    }
}
public class ParallelMultiplier {
    public static int[][] A = {{1, 4}, {2, 5}, {3, 6}};
    public static int[][] B = {{8, 7, 6}, {5, 4, 3}};
    public static int M = A.length;
    public static int N = B[0].length;
    public static int[][] C = new int[M][N];
    public static WorkerThread[][] Threads = new WorkerThread[M][N];

    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < A.length; i++)
            for (int j = 0; j < B[0].length; j++)
                for (int k = 0; k < A[0].length; k++) {
                    Threads[i][j] = new WorkerThread(i, j, k, A, B, C);
                    Threads[i][j].start();
                }

        System.out.println("Wait for work to finish ");
        for (int i = 0; i < M; i++)
            for (int j = 0; j < N; j++)
                Threads[i][j].join();

        System.out.println("Elements of Matrix C:");
        for (int i = 0; i < M; i++) {
            for (int j = 0; j < N; j++) {
                System.out.println("[" + i + "," + j + "] = " + C[i][j]);
            }
        }
    }
}