Matrix Multiplication (Square of a Matrix) using Threads in JAVA

This program will take a text file as a command line argument. The text file will have the first row indicating the total number of rows and columns which will be equal as this program is programmed to read a square matrix from a file and calculate its square using threads.

Content of the text file to be given as an argument

14
0 1 0 7 5 3 1 0 1 0 7 5 3 1
2 0 0 3 2 2 2 3 0 2 9 0 2 3
2 1 1 2 2 2 4 0 0 2 4 3 3 5
3 0 2 9 0 2 6 1 2 3 4 5 6 7
2 0 0 3 2 2 2 3 0 2 9 0 2 3
2 1 1 2 2 2 4 0 0 2 4 3 3 5
3 0 2 9 0 2 6 1 2 3 4 5 6 7
0 1 0 7 5 3 1 2 0 0 3 2 2 2
3 0 2 9 0 2 3 2 1 1 2 2 2 4
0 0 2 4 3 3 5 3 0 2 9 0 2 6
1 2 3 4 5 6 7 0 1 0 7 5 3 1
2 0 0 3 2 2 2 3 0 2 9 0 2 3
2 1 1 2 2 2 4 0 0 2 4 3 3 5
3 0 2 9 0 2 6 1 2 3 4 5 6 7

The first row indicates the total number of rows and columns, in this case, rows=cloumns=14.
And the next rows will be the actual matrix which we want to multiply with itself. The program will calculate the square of the matrix written in your file.


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import java.util.*;

import java.io.*; 

class MMThread extends Thread{  

  int row=0,k,j,sum=0;

 int first[][],multiply[][];

  MMThread(int row,int first[][],int multiply[][]){

  this.row = row;

  this.first = first;

  this.multiply = multiply;

 } 

 public void run(){  

  for (j = 0; j < first[row].length; j++){

           for (k = 0; k < first.length; k++){

              sum = sum + first[row][k]*first[k][j];

           }

           multiply[row][j] = sum;

           sum = 0;

        }

 } 

}

public class MM{

   public static void main(String args[])  throws IOException

   {

   int rows=0,cols=0,i=0,j=0,line=0;

    if(args.length==0){

    System.out.println("Matrix File Not Provided");

    return;

   }

    Scanner scanner = new Scanner(new File(args[0]));

    int first[][] = null;

    while(scanner.hasNextInt())

  {

   if(line==0){

    rows = scanner.nextInt();

    cols = rows;

    first = new int [rows][cols];

    line++;

   }

      first[i][j] = scanner.nextInt();

      if(j==rows-1){

       i++;

       j=0;

      }else{

       j++;

      }

  }

    System.out.println("\nOriginal Matrix\n");

    for (i = 0; i < rows; i++){

      for (j = 0; j < cols; j++)

         System.out.print(first[i][j]+"\t");

     System.out.println();

   }

     int second[][] = first;

     int multiply[][] = new int[rows][cols];

     MMThread mMThread[] = new MMThread[rows];

     for(int l=0;l<mMThread.length;l++){

      mMThread[l] = new MMThread(l,first,multiply);

      mMThread[l].start();

     }

     while(Thread.activeCount() > 1);

     System.out.println("\nProduct of the matrices:\n");

     for (i = 0; i < rows; i++){

        for (j = 0; j < cols; j++)

           System.out.print(multiply[i][j]+"\t");

        System.out.print("\n");

     }

   }

}

Output:

square of a matrix using threads in java

1 comment: