Question

Java Question: Write a multithread Java program that performs matrix multiplication. In the program, you need...

Java

Question: Write a multithread Java program that performs matrix multiplication. In the program, you need to calculate each element in the result matrix in a separate thread. The code should be generic with regard to matrix size. The sizes of three matrices should be stored in variable and used. The two operand matrices and the result matrix should be printed.

Thanks

0 0
Add a comment Improve this question Transcribed image text
Answer #1

ANSWER:

import java.util.Random;

class MatrixMultiplier extends Thread {
    private int a[][], b[][];
    private int i;
    private int j;
    private volatile int value = 0;

    public MatrixMultiplier(int a[][], int b[][], int i, int j) {
        this.a = a;
        this.b = b;
        this.i = i;
        this.j = j;
    }

    @Override
    public void run() {
        int n = b.length;
        for (int k = 0; k < n; k++) {
            value += a[i][k] * b[k][j];
        }
    }

    public int getValue() {
        return value;
    }
}
public class ThreadedMatrixMultiplication {


    public static void initializeMatrix(int mat[][]) {
        // to get new random number
        Random r = new Random();

        for (int i = 0; i < mat.length; i++) {
            for (int j = 0; j < mat[i].length; j++) {
                mat[i][j] = Math.abs(r.nextInt()) % 100;
            }
        }
    }

    public static void printMatrix(int mat[][]) {
        for (int i = 0; i < mat.length; i++) {
            for (int j = 0; j < mat[i].length; j++) {
                System.out.printf("%10d", mat[i][j]);
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
        int m = 4;
        int n = 5;
        int p = 3;

        int a[][] = new int[m][n];
        int b[][] = new int[n][p];
        int product[][] = new int[m][p];

        initializeMatrix(a);
        initializeMatrix(b);

        // multiply both matrix
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < p; j++) {
                MatrixMultiplier t = new MatrixMultiplier(a, b, i, j);
                t.start();
                try {
                    t.join();
                    product[i][j] = t.getValue();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

        System.out.println("Matrix 1: ");
        printMatrix(a);
        System.out.println("
Matrix 2: ");
        printMatrix(b);
        System.out.println("

Product: ");
        printMatrix(product);
    }

}

Add a comment
Know the answer?
Add Answer to:
Java Question: Write a multithread Java program that performs matrix multiplication. In the program, you need...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA PROGRAM. I need to write a program in java that will perform matrix addition and both matric...

    JAVA PROGRAM. I need to write a program in java that will perform matrix addition and both matrices have N rows and M columns, N>1 and M>1. The two matrices must be divided to four equal size sub-matrices and each sub-matrix has dimensions N/2 X M/2. I need to create four threads and each thread performs a sub-set of addition on one pair of the sub-matrices. I also need an extra thread for networking. The network part of this uses...

  • Write a C program to implement matrix multiplication operations. First, you need to prompt the user...

    Write a C program to implement matrix multiplication operations. First, you need to prompt the user for the size of both matrix. Then take inputs for both matrices and perform matrix multiplication operation. Finally, print the resulting matrix into the output.

  • I need help writing a program in python that reads from a file and performs matrix...

    I need help writing a program in python that reads from a file and performs matrix multiplication. the file has this format 1 4 2 1 1 1 1 1 1 2 2 3 3 4 4 which means : 1 #rows in A 4 #cols in A, rows in B 2 #cols in B Matrix A contents: 1 1 1 1 Matrix B contents: 1 1 2 2 3 3 4 4 i need to be able to read...

  • ** Use Java programming language Program the following algorithms in JAVA: A. Classical matrix multiplication B....

    ** Use Java programming language Program the following algorithms in JAVA: A. Classical matrix multiplication B. Divide-and-conquer matrix multiplication In order to obtain more accurate results, the algorithms should be tested with the same matrices of different sizes many times. The total time spent is then divided by the number of times the algorithm is performed to obtain the time taken to solve the given instance. For example, you randomly generate 1000 sets of input data for size_of_n=16. For each...

  • It is required to write a Matrix Calculator Program using C Programming Language that could only...

    It is required to write a Matrix Calculator Program using C Programming Language that could only perform addition, subtraction, and multiplication of two matrices of appropriate dimensions. Your program should prompt the user to select a matrix operation from a list of options that contains the aforementioned operations. Then, the user should be prompted to enter the dimensions of the two operand matrices. Next, your program should prompt the user to enter the elements of each matrix row-wise. Your program...

  • READ CAREFULLY AND CODE IN C++ Dynamic Programming: Matrix Chain Multiplication Description In this assignment you...

    READ CAREFULLY AND CODE IN C++ Dynamic Programming: Matrix Chain Multiplication Description In this assignment you are asked to implement a dynamic programming algorithm: matrix chain multiplication (chapter 15.2), where the goal is to find the most computationally efficient matrix order when multiplying an arbitrary number of matrices in a row. You can assume that the entire input will be given as integers that can be stored using the standard C++ int type and that matrix sizes will be at...

  • Problem 3 (20 pts) It is required to write a Matrix Calculator Program using C Programming...

    Problem 3 (20 pts) It is required to write a Matrix Calculator Program using C Programming Language that could only perform addition, subtraction, and multiplication of two matrices of appropriate dimensions. Your program should prompt the user to select a matrix operation from a list of options that contains the aforementioned operations. Then, the user should be prompted to enter the dimensions of the two operand matrices. Next, your program should prompt the user to enter the elements of each...

  • The matrix operations that you should include are Addition, Subtraction and Multiplication. The driver can be...

    The matrix operations that you should include are Addition, Subtraction and Multiplication. The driver can be as simple as asking the user to enter two matrices and then presenting a simple menu that allows the user to select the operation they want to test. Have the menu in a loop so that the user can test any other operation unless they choose to exit the menu. Also, provide the user an option to select two new matrices. Make sure that...

  • Write a java program that will read the values for 3 matrices A, B, and C...

    Write a java program that will read the values for 3 matrices A, B, and C and store the result of their summation in matrix D. You need to use a method to read matrix values. Use another method to add the three 3x3 matrices (each is a 2 dimensional array with 3 rows and 3 columns). Finally, use a third method to print the value of the summation (matrix D). Write a test program that will cal the three...

  • Write a method to multiply two matrices. The header of the method is: public static double[][]...

    Write a method to multiply two matrices. The header of the method is: public static double[][] multiplyMatrix(double[][] a, double[][] b) To multiply matrix a by matrix b, the number of columns in a must be the same as the number of rows in b, and the two matrices must have elements of the same or compatible types. Let c be the result of the multiplication. Assume the column size of matrix a is n. Each element is For example, for...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT