3 Replies - 11978 Views - Last Post: 18 March 2011 - 01:10 PM Rate Topic: -----

#1 YES i failed  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 29-April 10

matrix multiplication multithreading

Posted 05 November 2010 - 04:16 PM

Sorry for the long code, i promise its not too difficult to follow, i abbreviated what could be left out.

Simple and short:
I wrote 2 programs.
1 was single threaded and multiplied 2 matrices together.

This program below is the same as the first but using multiple threads.

Correct me if I'm wrong, but shouldnt the multithreaded version take less time to perform the matrix multiplication computation?

When I run both, the single threaded version takes less time to compute the multiplication. :huh:

can someone make sure im using thread.join() correctly - maybe iv used it in the wrong place/way?

btw, this is my first time using threads..

import java.io.*;
import java.util.*;

/**
 *
 * @author Ryan Davis
 *
 * Multi Thread
 * This program will read a matrix from a data file, transpose it, and then
 * multiply them.  The program will output the time it took to multiply the
 * matrices.
 */
public class MatrixMultiThread {

    private static String file = "C:\\data1.txt";
    private static final int rows = getRows(file);
    private static final int columns = getColumns(file);
    private static int[] temp = new int[rows*columns];
    private static int[][] matrix = new int[rows][columns];
    private static int[][] transpose =  new int[columns][rows];
    private static int[][] result = new int[rows][rows];
    private static myThread[] threadPool;

    public static void main(String[] args){
        System.out.println("rows: " + rows);
        System.out.println("columns: " + columns);
        readFile(file);
        buildMatrices();
        multiply();
    }

    private static int getColumns(String f){...}

    private static int getRows(String f){...}

    private static void readFile(String f){...}

    private static void buildMatrices(){...}


    /**
     * this method multiplies matrix and transpose together. the result matrix
     * is printed to the screen as well as the time in MS that it took to
     * perform the multiplication.  note: the time of printing to the screen is
     * not included - strictly the computation time.
     */
    private static void multiply(){

        threadPool = new myThread[rows];

        long start = System.nanoTime();//computation begins

        for(int i=0; i<rows; i++){
            threadPool[i] = new myThread(i);
            threadPool[i].start();
            try{
            threadPool[i].join();
            }catch (InterruptedException e){
            //thread was interrupted
            }
        }

       long end = System.nanoTime();//computation ends
       double time = (end-start)/1000000.0;

       //print result matrix
       for(int i=0; i<rows; i++){
           for(int j=0; j<rows; j++){
               System.out.print(result[i][j] + " ");
           }
           System.out.println();
       }
       
       System.out.println();//blank line
       System.out.println("Multiplication took " + time + " milliseconds.");
    }

    private static class myThread extends Thread{
        int index;
        
        myThread(int index){
            this.index = index;
        }
        
        public void run(){
            for(int i=0; i<rows; i++){
                for(int j=0; j<columns; j++){
                    result[index][i] += matrix[index][j] * transpose[j][i];
                }
            }
        }

    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: matrix multiplication multithreading

#2 guido-granobles  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 171
  • View blog
  • Posts: 617
  • Joined: 02-December 09

Re: matrix multiplication multithreading

Posted 05 November 2010 - 04:41 PM

You should use join in another for loop:
for(int i=0; i<rows; i++){
     threadPool[i] = new myThread(i);
     threadPool[i].start();
     
}
for(int i=0; i<rows; i++){	
try{
      threadPool[i].join();
 }catch (InterruptedException e){
   //thread was interrupted
 } 
}


If you do it in the same for loop then all the thread are executed sequentially and you are expecting concurrency.

This post has been edited by guidojavier: 05 November 2010 - 04:42 PM

Was This Post Helpful? 1
  • +
  • -

#3 YES i failed  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 29-April 10

Re: matrix multiplication multithreading

Posted 05 November 2010 - 04:55 PM

i see what you mean, makes perfect sense so i changed the code to what you said..however its still taking the multithreaded version longer then the single..

heres output on 3 runs of each program on the same matrix

single threaded:
Multiplication took 0.026405 milliseconds.
Multiplication took 0.023471 milliseconds.
Multiplication took 0.026404 milliseconds.

multi-threaded:
Multiplication took 1.012177 milliseconds.
Multiplication took 0.87233 milliseconds.
Multiplication took 0.90607 milliseconds.
Was This Post Helpful? 0
  • +
  • -

#4 YES i failed  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 29-April 10

Re: matrix multiplication multithreading

Posted 18 March 2011 - 01:10 PM

Ok so I guess I can update this by now. I figured it out, basically multithreaded is only faster than single-threaded once you reach a certain size matrix.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1