简体   繁体   中英

Why is this parallel matrix addition so inefficient?

I'm very new to multithreading and don't have much experience using inner classes.

The task is to add two matrices containing double values in a parallelized way.

My idea was to do this recursively, splitting the big matrixes into smaller ones and performing the addition when the matrices reached a certain size limit, then fusing them.

The parallelized code runs 40-80x slower than the serialized code.

I suspect that I'm doing something wrong here. Perhaps it's because I create so many new matrices, or because I traverse them so many times.

Here is the code:

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

}

Thankful for any help.

Creating an object is many time slower than performing a + even for double .

This means creating an object is not a good trade off for addition. To make matters worse, using more memory mean your CPU cache don't work as efficiently and in the worst case what was working in your L1/L2 cpu caches is now in your L3 cache which is shared and not so scaleable, or even worse you end up using main memory.

I suggest you rewrite this so that

  • you don't create any objects.
  • you consider that working across a cache line is more efficient than breaking it up. ie break up the work by rows not columns.
  • working on a 1D array in Java can be more efficient so think about how you might do this with a 1D array which only appears as a 2D martix.

You create new arrays all the time. It is expensive. Why do You not compute in current arrays? you can just provide borders for each thread.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM