簡體   English   中英

為什么這種並行矩陣加法效率如此低下?

[英]Why is this parallel matrix addition so inefficient?

我對多線程非常陌生,並且對使用內部類沒有太多經驗。

任務是以並行方式添加兩個包含雙精度值的矩陣。

我的想法是遞歸執行此操作,將大矩陣拆分為較小的矩陣,並在矩陣達到一定大小限制時執行加法,然后將其融合。

並行代碼的運行速度比串行代碼慢40-80倍。

我懷疑我在這里做錯了。 也許是因為我創建了很多新矩陣,或者是因為我遍歷了很多次。

這是代碼:

package concurrency;

import java.util.Random;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveTask;

public class ParallelMatrixAddition {
public static void main(String[] args) {

    Random rand = new Random();

    final int SIZE = 1000;
    double[][] one = new double[SIZE][SIZE];
    double[][] two = new double[SIZE][SIZE];
    double[][] serialSums = new double[SIZE][SIZE];
    double[][] parallelSums = new double[SIZE][SIZE];

    for (int i = 0; i < one.length; i++) {
        for (int j = 0; j < one.length; j++) {
            one[i][j] = rand.nextDouble();
            two[i][j] = rand.nextDouble();
        }
    }

    long serialStartTime = System.currentTimeMillis();

    for (int i = 0; i < SIZE; i++) {
        for (int j = 0; j < SIZE; j++) {
            serialSums[i][j] = one[i][j] + two[i][j];
        }
    }

    long serialEndTime = System.currentTimeMillis();

    System.out.println("Serial runtime is: " + (serialEndTime - serialStartTime) + " milliseconds");

    long startTime = System.currentTimeMillis();

    parallelSums = parallelAddMatrix(one, two);

    long endTime = System.currentTimeMillis();

    System.out.println("Parallel execution took " + (endTime - startTime) + " milliseconds.");

}

public static double[][] parallelAddMatrix(double[][] a, double[][] b) {
    RecursiveTask<double[][]> task = new SumMatricesTask(a, b);
    ForkJoinPool pool = new ForkJoinPool();
    double[][] result = new double[a.length][a.length];
    result = pool.invoke(task);
    return result;
}

@SuppressWarnings("serial")
private static class SumMatricesTask extends RecursiveTask<double[][]> {
    private final static int THRESHOLD = 200;

    private double[][] sumz;
    private double[][] one;
    private double[][] two;

    public SumMatricesTask(double[][] one, double[][] two) {
        this.one = one;
        this.two = two;
        this.sumz = new double[one.length][one.length];
    }

    @Override
    public double[][] compute() {
        if (this.one.length < THRESHOLD) {
            // Compute a sum here.
            // Add the sums of the matrices and store the result in the
            // matrix we will return later.

            double[][] aStuff = new double[this.one.length][this.one.length];

            for (int i = 0; i < one.length; i++) {
                for (int j = 0; j < one.length; j++) {
                    aStuff[i][j] = this.one[i][j] + this.two[i][j];
                }
            }

            return aStuff;

        } else {

            // Split a matrix into four smaller submatrices.
            // Create four forks, then four joins.

            int currentSize = this.one.length;

            int newSize = currentSize / 2;

            double[][] topLeftA = new double[newSize][newSize];
            double[][] topLeftB = new double[newSize][newSize];
            double[][] topLeftSums = new double[newSize][newSize];

            double[][] topRightA = new double[newSize][newSize];
            double[][] topRightB = new double[newSize][newSize];
            double[][] topRightSums = new double[newSize][newSize];

            double[][] bottomLeftA = new double[newSize][newSize];
            double[][] bottomLeftB = new double[newSize][newSize];
            double[][] bottomLeftSums = new double[newSize][newSize];

            double[][] bottomRightA = new double[newSize][newSize];
            double[][] bottomRightB = new double[newSize][newSize];
            double[][] bottomRightSums = new double[newSize][newSize];

            // Populate topLeftA and topLeftB
            for (int i = 0; i < newSize; i++) {
                for (int j = 0; j < newSize; j++) {
                    topLeftA[i][j] = this.one[i][j];
                    topLeftB[i][j] = this.two[i][j];
                }
            }

            // Populate bottomLeftA and bottomLeftB

            for (int i = 0; i < newSize; i++) {
                for (int j = 0; j < newSize; j++) {
                    bottomLeftA[i][j] = this.one[i + newSize][j];
                    bottomLeftB[i][j] = this.two[i + newSize][j];
                }
            }

            // Populate topRightA and topRightB

            for (int i = 0; i < newSize; i++) {
                for (int j = 0; j < newSize; j++) {
                    topRightA[i][j] = this.one[i][j + newSize];
                    topRightB[i][j] = this.two[i][j + newSize];
                }
            }

            // Populate bottomRightA and bottomRightB

            for (int i = 0; i < newSize; i++) {
                for (int j = 0; j < newSize; j++) {
                    bottomRightA[i][j] = this.one[i + newSize][j + newSize];
                    bottomRightB[i][j] = this.two[i + newSize][j + newSize];
                }
            }

            SumMatricesTask topLeft = new SumMatricesTask(topLeftA, topLeftB);
            SumMatricesTask topRight = new SumMatricesTask(topRightA, topRightB);
            SumMatricesTask bottomLeft = new SumMatricesTask(bottomLeftA, bottomLeftB);
            SumMatricesTask bottomRight = new SumMatricesTask(bottomRightA, bottomRightB);

            topLeft.fork();
            topRight.fork();
            bottomLeft.fork();
            bottomRight.fork();

            topLeftSums = topLeft.join();
            topRightSums = topRight.join();
            bottomLeftSums = bottomLeft.join();
            bottomRightSums = bottomRight.join();

            // Fuse the four matrices into one and return it.

            for (int i = 0; i < newSize; i++) {
                for (int j = 0; j < newSize; j++) {
                    this.sumz[i][j] = topLeftSums[i][j];
                }
            }

            for (int i = newSize; i < newSize * 2; i++) {
                for (int j = 0; j < newSize; j++) {
                    this.sumz[i][j] = bottomLeftSums[i - newSize][j];
                }
            }

            for (int i = 0; i < newSize; i++) {
                for (int j = newSize; j < newSize * 2; j++) {
                    this.sumz[i][j] = topRightSums[i][j - newSize];
                }
            }

            for (int i = newSize; i < newSize * 2; i++) {
                for (int j = newSize; j < newSize * 2; j++) {
                    this.sumz[i][j] = bottomRightSums[i - newSize][j - newSize];
                }
            }

            return this.sumz;
        }
    }
}

}

感謝您的幫助。

創建一個對象比對一個double執行+慢許多倍。

這意味着創建對象並不是添加的良好折衷。 更糟糕的是,使用更多的內存意味着您的CPU緩存無法高效地工作,並且在最壞的情況下,L1 / L2 cpu緩存中正在工作的內容現在位於L3緩存中,這是共享的,並且不可擴展,甚至更糟您最終將使用主內存。

我建議你重寫一下

  • 您不創建任何對象。
  • 您認為跨高速緩存行工作比將其分解更有效。 即按行而不是按列來分解工作。
  • 在Java中處理一維數組可能會更高效,因此請考慮如何使用僅顯示為二維martix的一維數組來實現此目的。

您一直在創建新的數組。 它是昂貴的。 為什么不在當前數組中計算? 您可以為每個線程提供邊框。

暫無
暫無

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

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