简体   繁体   中英

Java Matrix Multiplication using Thread Pool

I'm trying to implement a program that can do matrix multiplication using Callable (thread pool). I have this program below. But, I don't see any significant difference in execution time when I run this on one thread or 8 threads.

I took 5 samples for one thread and 8 thread, they're as follows (all in milliseconds):

1 thread - 5433.982472 , 6872.947063 , 6371.205237 , 6079.367443 , 5842.946494

8 threads - 5260.792683 , 5517.047691 , 5314.208147 , 5739.747367 , 5585.621661

I'm new to this, am I doing anything wrong?

package naivematmul;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.Callable;

 class NaiveMatMul implements Callable<Integer>
{
    private int n;
    private int a[][];
    private int b[][];
    private int sum;
    private int i;
    private int j;


    public NaiveMatMul(int n, int a[][], int b[][], int i , int j )
    {
            this.n = n;
            this.a = a;
            this.b = b;
            this.i = i;
            this.j = j;
            this.sum = sum;
    }

    public Integer call() throws Exception
    {
        for (int k = 0 ; k < n ; k++)
         {
             sum = sum + a[i][k] * b[k][j];

         }
         return sum;
    }

    public static void main(String[] args) throws InterruptedException, ExecutionException 
    {

        int n;
        int[][] a, b, c;

        n = 512;
        a = new int[n][n];
        b = new int[n][n];
        c = new int[n][n];

         int threads = 8;

        ExecutorService executor = Executors.newFixedThreadPool(threads);

        for(int i=0; i<n; i++)
        {
            for(int j=0; j<n; j++)
            {
                a[i][j] = 1;
            }
        }

        for(int i=0; i<n; i++)
        {
            for(int j=0; j<n; j++)
            {

                b[i][j] = 1;

            }
        }


          int sum = 0;
         long start_time = System.nanoTime();
      Future<Integer> future;

        for (int i = 0; i < n ; i++)
        {
            for (int j = 0 ; j < n ; j++)
            {
                future = executor.submit(new NaiveMatMul(n, a, b, i, j));
                c[i][j] = future.get();
                sum = 0;
            }

        }


        long end_time = System.nanoTime();
        double difference = (end_time - start_time)/1e6;
        executor.shutdown();




                System.out.println("Time taken : " + difference);



    }
}

Running a program in multiple threads doesn't necessarily means better performance. In few cases it can result in worse performance. You have to check what all other processes running on your system? How many CPU cores you have?

If you have dual core processor and you run 8 threads means more work for java to coordinates between threads. For best performance try running the same number of threads as number of CPU cores with minimum services running on your PC/server.

By calling future.get() right after executor.submit(...) , you are preventing any actual multithreading. Your program waits for the first computation to complete before it submits the second one.

To illustrate this, try replacing your loop with the following:

Future<Integer> futures[][] = new Future[n][n];

for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
        future = executor.submit(new NaiveMatMul(n, a, b, i, j));
        futures[i][j] = future;
    }
}

for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
        c[i][j] = futures[i][j].get();
    }
}

This is not exactly a great way to do it, but you should see a significant improvement in your execution time. The difference is that now you are starting up all the computations in all your threads and then start collecting the results.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM