tomkr4l
tomkr4l

Reputation: 322

How to parallelize for loop in java?

I have sequential code for LU matrix decomposition in java. My question is, how to easily make decompose function parallel? Is there any similar techniques like OpenMP or Parallel.for in c#?

I think, that It's possible to paralellize for loop but I don't know how to easily achieve this in Java.

Here is my code:

public class LuDecomposition {
    private int size;
    private double[][] matrix;

    public LuDecomposition(double[][] matrix, int size) {
        this.matrix = matrix;
        this.size = size;
    }

    public void printMatrix(double[][] matrix) {
        for(int i=0; i<size; i++) {
            for(int j=0; j<size; j++) {
                System.out.printf("%.2f   ", matrix[i][j]);
            }
            System.out.println();
        }
        System.out.println();
    }

    public void decompose() {
        double[][] upper = new double[this.size][this.size];
        double[][] lower = new double[this.size][this.size];
        double sum = 0.0;

        for(int i=0; i<size; i++) {
            for(int j=0; j<size; j++) {
                upper[i][j] = 0.0;
                lower[i][j] = 0.0;
            }
        } 

        long start = System.nanoTime();

        for(int i=0; i<size; i++) {
            // U matrix
            for(int k=i; k<size; k++) {
                sum = 0.0;
                for(int j=0; j<i; j++) {
                    sum += (lower[i][j] * upper[j][k]);
                }
                upper[i][k] = matrix[i][k] - sum;
            }
            // L matrix
            for(int k=i; k<size; k++) {
                if(i==k) {
                    lower[i][i] = 1;
                } else {
                    sum = 0.0;
                    for(int j=0; j<i; j++) {
                        sum += (lower[k][j] * upper[j][i]);
                    }
                    lower[k][i] = ((matrix[k][i] - sum) / upper[i][i]);
                }

            }
        }
        long end = System.nanoTime();

        //printMatrix(matrix);
        //printMatrix(lower);
        //printMatrix(upper);
        System.out.println("LU decomposition duration(seconds): " + (end - start) / 1000000000.0);
    }

}


Thank you.

Upvotes: 2

Views: 6889

Answers (2)

Ivan
Ivan

Reputation: 8758

In Java you can use parallel streams (https://examples.javacodegeeks.com/core-java/java-8-parallel-streams-example/) but to utilize it your for loops have to have somehow independent iterations. You cannot parallelize for loop if next iteration depends on results of previous iteration. So you first need to make your algo parallel or use existing Java libraries for parallel LU decomposition like https://github.com/alexkaratarakis/Parallel-Colt

Upvotes: 0

mnesarco
mnesarco

Reputation: 2778

If you replace your for loops with Int Streams, it is very easy to make it run in parallel:

Example:

  IntStream.range(0, 10).parallel().forEach(i -> {
    System.out.println(i);
  });

Upvotes: 7

Related Questions