簡體   English   中英

兩個方陣的並行乘法

[英]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的行數和列數;

3.的值可以根據1.和2.的值推導出來,即:

  • 矩陣C的行數與矩陣A的行數相同;
  • 矩陣C的列數與矩陣B的列數相同;

我的程序是將兩個矩陣相乘並將其顯示在矩陣 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 的值之前等待線程完成它們的工作(例如,調用連接)。 例如通過這樣做:

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]);
            }
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM