1

I am trying to compare sequantial and concurent matrix multiplication.Everytime sequential is more fast.For example 60 x 60 matrix sequantial find 4 ms while concurent 277 ms.In my code is something wrong?

concurent:

private static void multiplyMatrixConcurent() {
     result_concurent =new Matrix(rows, columns);

     for (int i = 0; i < cell; i++) {
         Runnable task = new MatrixMultiplicationThread(i);
         Thread worker = new Thread(task);
         worker.start();


    }


}

private static class MatrixMultiplicationThread implements Runnable{
         private int cell;

         MatrixMultiplicationThread(int cell) {
             this.cell=cell;
            }
         @Override
            public void run() {
             int row = cell / columns ;
             int column = cell % columns;
                for (int i = 0; i < rows; i++) {
                        double t1 = matrix.getCell(row, i);
                        double t2=  matrix.getCell(i, column);
                        double temp= t1*t2;
                        double res = result_concurent.getCell(row, column) +temp;
                        result_concurent.setCell(res, row, column);


                }

            }

     }

sequential:

private static void multiplyMatrixSequence() {
     result_sequantial =new Matrix(rows, columns);
     for (int i = 0; i < rows; i++) {
        for (int j = 0; j <rows; j++) {
            for (int k = 0; k < columns; k++) { 
                double t1=matrix.getCell(i,k);
                double t2=matrix.getCell(k, j);

                double temp= t1*t2;
                double res = result_sequantial.getCell(i, j) + temp;
                result_sequantial.setCell(res,i,j);
            }
        }
    }

}
Tim
  • 41,901
  • 18
  • 127
  • 145
cyo
  • 193
  • 2
  • 4
  • 17
  • possible duplicate of [Can increasing thread count degrade the overall performance in java?](http://stackoverflow.com/questions/23442921/can-increasing-thread-count-degrade-the-overall-performance-in-java) – Basilevs May 11 '14 at 15:24
  • What about your `Matrix` class? Are the `getCell` and `setCell` methods `synchronized`? That would destroy any advantage of parallel computing… – Holger May 12 '14 at 09:09
  • possible duplicate of [Why single thread is faster than multithreading in Java?](http://stackoverflow.com/questions/19901915/why-single-thread-is-faster-than-multithreading-in-java) – Raedwald May 14 '14 at 19:06
  • Try it with a 1000 * 1000 matrix. – kamaci Mar 22 '15 at 17:34

2 Answers2

3

I don't see anything obviously wrong. You don't set cell to rows*columns in the concurrent startup code you posted but I assume that is an issue in the posting not the code you ran.

Threads have overhead. They have memory to allocate and require extra management of the CPU resources. If the number of threads is modest and the hardware can handle multiple threads in parallel, then you win. However, for pure cpu bound tasks, having more threads than there are processing elements is just overhead without any gain. In this case, you have 3600 threads. I'm guessing you have a processor that can handle between 2 and 8 threads at once. Your thread count dwarfs the processor's ability and so you get a slowdown.

Note that when the threads are performing blocking operations such as disk or network I/O then more threads can allow interleaving. The statements also don't apply in the GPU computing case where even memory accesses allow efficient thread interleaving.

BTW, if your goal is actually to produce a fast matrix multiply - use an existing library. These libraries are developed by people who take advantage of processor cache structures, specialized hardware instruction sets and subtle details of floating point to produce libraries that are faster and more accurate than anything a casual coder can produce.

DrC
  • 7,528
  • 1
  • 22
  • 37
3

Creating a Thread takes some time (compared to other operations it is expensive). Instead of creating a new Thread for every cell you could use a ThreadPool and re-use existing(finished) threads. This reduces the time spend for creating new threads. But still you are in a very low execution time per thread scenario where setting up the threads takes more time than running it sequential.

private static void multiplyMatrixConcurent() {
   result_concurent =new Matrix(rows, columns);
   ExecutorService executor = Executors.newFixedThreadPool(4);
   for (int i = 0; i < cell; i++) {
     Runnable worker = new MatrixMultiplicationThread(i);
     executor.execute(worker);
   }
   executor.shutdown();
}
Simulant
  • 19,190
  • 8
  • 63
  • 98