I'm a Beginner of java.
Recently, I'm writing a program to calculate matrix multiplication. So I write a class to do this.
public class MultiThreadsMatrixMultipy{
public int[][] multipy(int[][] matrix1,int[][] matrix2) {
if(!utils.CheckDimension(matrix1,matrix2)){
return null;
}
int row1 = matrix1.length;
int col1 = matrix1[0].length;
int row2 = matrix2.length;
int col2 = matrix2[0].length;
int[][] ans = new int[row1][col2];
Thread[][] threads = new SingleRowMultipy[row1][col2];
for(int i=0;i<row1;i++){
for(int j=0;j<col2;j++){
threads[i][j] = new SingleRowMultipy(i,j,matrix1,matrix2,ans));
threads[i][j].start();
}
}
return ans;
}
}
public class SingleRowMultipy extends Thread{
private int row;
private int col;
private int[][] A;
private int[][] B;
private int[][] ans;
public SingleRowMultipy(int row,int col,int[][] A,int[][] B,int[][] C){
this.row = row;
this.col = col;
this.A = A;
this.B = B;
this.ans = C;
}
public void run(){
int sum =0;
for(int i=0;i<A[row].length;i++){
sum+=(A[row][i]*B[i][col]);
}
ans[row][col] = sum;
}
}
I want to use a thread to calculate matrix1[i][:] * matrix2[:][j]
, and the size of the matrix is 1000*5000
and 5000*1000
, so the number of threads is 1000*1000.When I run this program, it's super slow and it will cost about 38s
. If I just use the single-thread
to calculate the result, it will cost 17s
. The single-threads code is below:
public class SimpleMatrixMultipy
{
public int[][] multipy(int[][] matrix1,int[][] matrix2){
int row1 = matrix1.length;
int col1 = matrix1[0].length;
int row2 = matrix2.length;
int col2 = matrix2[0].length;
int[][] ans = new int[row1][col2];
for(int i=0;i<row1;i++){
for(int j=0;j<col2;j++){
for(int k=0;k<col1;k++){
ans[i][j] += matrix1[i][k]*matrix2[k][j];
}
}
}
return ans;
}
}
What can I do to speed up the program?