3 Replies - 3728 Views - Last Post: 21 November 2009 - 12:55 PM Rate Topic: -----

#1 butchi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 21-November 09

mltiply 2 matrix by thread

Post icon  Posted 21 November 2009 - 11:05 AM

Concurrent Matrix Multiplication
Purpose
Using threads to perform the matrix multiplication operation.
Description
Given 2 matrices M1 (n*m), M2 (m*k), its required to multiply M1, M2 and generate
the result R(n*k).
Let the cell at i , j in R = R[i,j]
To calculate R[I,j]:
R[i,j] =  =
=
k m
k 1 M1[i,k] * M2[k,j]
Where i = 1 : n
j = 1 : m
Example:
M1 = [ 1 2
1 4]
M2 = [ 3 1
2 5]
R[1,1] = M1[1,1]*M2[1,1] + M1[1,2] * M2[2,1] = 1*3 + 2*2 = 7
R[1,2] = M1[1,1]*M2[1,2] + M1[1,2] * M2[2,2] = 1*1 + 2*5 = 11
R[2,1] = M1[2,1] *M2[1,1] + M1[2,2]*M2[2,1]= 1*3+4*2 = 11
R[2,2] = M1[2,1]*M2[1,2] + M1[2,2]*M2[2,2] = 1*1 + 4*5 = 21
R = [7 11
11 21]
Your program should calculate R using concurrent threads.
For example:
1. You can calculate each column in R using a separate thread OR
2. You can calculate each row in R using a separate thread OR
3. You can make a thread for each cell in R
Your program should print the resulted matrix R.

by java pleeeeeeeeese due tommorw
thanks

Is This A Good Question/Topic? 0
  • +

Replies To: mltiply 2 matrix by thread

#2 butchi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 21-November 09

Re: mltiply 2 matrix by thread

Posted 21 November 2009 - 11:26 AM

this is code i don't know why while compile all the result = 0
pleeeeeeeeeas help me thnks



[code]

import java.util.Scanner;


public class Main {

/** Creates a new instance of Main */
public Main()
{
Scanner cin=new Scanner(System.in);
System.out.println("Enter the number of rows in matrix A : ");
A_row=cin.nextInt();
System.out.println("Enter the number of columns in matrix A : ");
A_col=cin.nextInt();
System.out.println("Enter the number of rows in matrix B : ");
B_row= cin.nextInt();
System.out.println("Enter the number of columns in matrix B : ");
B_col=cin.nextInt();

if(A_col==B_row)
{
matrix_A=new int[A_row][A_col];
matrix_B=new int[B_row][B_col];
mult_
matrix_B[i][j]=cin.nextInt();

This post has been edited by butchi: 21 November 2009 - 12:42 PM

Was This Post Helpful? 0
  • +
  • -

#3 butchi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 21-November 09

Re: mltiply 2 matrix by thread

Posted 21 November 2009 - 12:44 PM

matrix evry time the result = 0
import java.util.Scanner;


public class Main {
	
	/** Creates a new instance of Main */
	public Main() 
	{
		Scanner cin=new Scanner(System.in);
		System.out.println("Enter the number of rows in matrix A : ");
		A_row=cin.nextInt();
		System.out.println("Enter the number of columns in matrix A : ");
		A_col=cin.nextInt();
		System.out.println("Enter the number of rows in matrix B : ");
		B_row= cin.nextInt();
		System.out.println("Enter the number of columns in matrix B : ");
		B_col=cin.nextInt();
		
		if(A_col==B_row)
		{
			matrix_A=new int[A_row][A_col];
			matrix_B=new int[B_row][B_col];
			mult_ans=new int[A_row][B_col];
		  System.out.println("Enter the elements of matrix A : ");
			 for(int i=0;i<A_row;i++)
				 for(int j=0;j<A_col;j++)
				 {
					matrix_A[i][j]=cin.nextInt();
				 }
		  System.out.println("Enter the elements of matrix B : ");
			for(int i=0;i<B_row;i++)
				 for(int j=0;j<B_col;j++)
				 {
					matrix_B[i][j]=cin.nextInt();  
				 }
			 thread_pool=new mythread[A_row];
			 
			for(int i=0;i<A_row;i++)
			{
				thread_pool[i]=new mythread(i);
				thread_pool[i].start();
			}
		   System.out.println("The answer of the matrix A*B : "); 
			 printer_thread=new threadprint[3];

				printer_thread[2]=new threadprint(mult_ans);
				printer_thread[2].start();

}
		else
		{
		   System.out.print("!!! Can't multiply this matrixes !!! "); 
		}
		 
	}
	
	/**
	 * @param args the command line arguments
	 */
	public static void main(String[] args) {

new Main();

// TODO code application logic here
	}

private class mythread extends Thread
	{
		int index;
		 
		 mythread(int index)
		 {
			 this.index=index;
		 }
		public void run() 
		{
			for(int i=0;i<B_col;i++)
			{
				for(int j=0;j<B_col;j++)
				{
					mult_ans[index][i]+=matrix_A[index][j]*matrix_B[j][i];
				}
			}
		}
	}
	private class threadprint extends Thread
	{
		threadprint (int[][] matrix)
		{
			this.matrix=matrix;
		}
		public synchronized  void run()
		{
			for(int i=0;i<matrix.length;i++)
			{
				System.out.print("| ");
				for(int j=0;j<matrix[0].length;j++)
				{
				  System.out.print(matrix[i][j]+"  ");
				}
				System.out.println(" |");
			}
		}
		int[][] matrix;
	}
			
	private int A_row;
	private int A_col;
	private int B_row;
	private int B_col;
	private int[][] matrix_A;
	private int[][] matrix_B;
	private int[][] mult_ans;
	private mythread[] thread_pool;
	private threadprint[] printer_thread;
}

This post has been edited by butchi: 21 November 2009 - 12:46 PM

Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: mltiply 2 matrix by thread

Posted 21 November 2009 - 12:55 PM

Duplicated topics merged
Please avoid multiple threads :angry:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1