Open In App

How to Perform Java Parallel Matrix Multiplication?

Last Updated : 21 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In Java, Parallelism can be used to perform Matrix Multiplication using multiple threads concurrently. The major advantage of this technique is that it uses distributed computing environments to speed up the computation. In this article, we will learn how to perform Parallel Matrix Multiplication in Java.

Key terminologies:

  • Matrix Multiplication: It is an operation in which, two matrices are taken as an input, and it produces the product of two matrices as a third matric as an output.
  • Parallelism: This can be achieved by dividing the matrices into smaller submatrices. And then, by generating the products of these submatrices we can achieve parallelism.

Step-by-Step Implementation to Perform Parallel Matrix Multiplication

  • Create the class named GfGParallelMatrixMultiplication and write the main method into the class.
  • In the main method, create the two matrices, named matrix1 and matrix2, to assign the elements to them.
  • After that, call the multiply method, pass matrix 1 and matrix 2 as parameters, and the result of that will print the elements through the iteration.
  • In the multiply method, we can create numRow1, numCol1, and numCol2, and it can assign the lengths of the matrix rows, respectively.
  • Create the result array with the numRow1 and numCol2 indexes of the result array.
  • We can implement the parallel matrix function and return the result.
  • Print the result array.

Program to Perform Parallel Matrix Multiplication in Java

Java




// Java Program to perform Parallel Matrix Multiplication
import java.util.Arrays;
  
// Driver Class
public class GfGParallelMatrixMultiplication 
{
    // Main Function
    public static int[][] multiply(int[][] matrix1, int[][] matrix2) 
    {
        // Get the number of rows in matrix1
        int numRow1 = matrix1.length;
        // Get the number of columns in matrix1
        int numCol1 = matrix1[0].length;
        // Get the number of columns in matrix2
        int numCol2 = matrix2[0].length;
  
        // Create a 2D array to store the result of multiplication
        int[][] result = new int[numRow1][numCol2];
  
        // Use parallel streams to perform parallel multiplication
        Arrays.parallelSetAll(result, i -> multiplyRow(matrix1, matrix2, i));
  
        // Return the result
        return result;
    }
  
    // Helper function to multiply a row of matrix1 with matrix2
    private static int[] multiplyRow(int[][] matrix1, int[][] matrix2, int row) 
    {
        // Get the number of columns in matrix1
        int numCol1 = matrix1[0].length;
        // Get the number of columns in matrix2
        int numCol2 = matrix2[0].length;
        // Create an array to store the result row
        int[] resultRow = new int[numCol2];
  
        // Iterate over each column in matrix2
        for (int j = 0; j < numCol2; j++) 
        {
            // Iterate over each element in the row of matrix1
            for (int k = 0; k < numCol1; k++) 
            {
                // Multiply the corresponding elements and add the result to the result row
                resultRow[j] += matrix1[row][k] * matrix2[k][j];
            }
        }
  
        // Return the result row
        return resultRow;
    }
  
    // Main function to test the matrix multiplication
    public static void main(String[] args) {
        // Define matrix1
        int[][] matrix1 = {
            {11, 45, 5},
            {38, 23, 13}
        };
          
        // Define matrix2
        int[][] matrix2 = {
            {18, 5},
            {14, 10},
            {25, 19}
        };
  
        // Perform matrix multiplication
        int[][] result = multiply(matrix1, matrix2);
  
        // Print the result
        for (int[] row : result) {
            System.out.println(Arrays.toString(row));
        }
    }
}


Output

[953, 600]
[1331, 667]

Explanation of the above Program:

  • In the above program, we have implemented the parallel matrix multiplication using arrays. parallelSetAll method.
    • Arrays.parallelSetAll(returns): This method can be used to set the elements of the array in parallel using the generator function, and it is part of the java.util.Arrays package.
  • This lambda function can be a result array whose elements are to be set.
  • This is a lambda expression passed as the generator function.
  • It takes an index i as input and returns the value to be set at that index in the result array.
  • In this case, it calls the multiplyRow() method to compute the values for a specific row of the resulting matrix.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads