K
K
Kostya Bakay2015-12-11 17:06:36
Java
Kostya Bakay, 2015-12-11 17:06:36

How to correctly multiply a matrix by another matrix in multiple threads?

My task is this. I need to multiply a 200x248 matrix by a 248x333 matrix in parallel on 8 threads. On the internet I found a simple example of multiplying two 4x4 matrices on 4 threads, but I don't quite understand the logic behind dividing this task between threads. Why does each thread have different loop boundaries, and how do they even form? Why does each thread already have 3 cycles, and not 2? Can you explain the algorithm to me so that I can do the multiplication of huge matrices on 8 threads by analogy?
Here is a part of this code (there is also input from a file and output of the result to another file, a graphical interface and more, but this is not so important in this matter).
Initialization of static fields:

public static int[][] a;
public static int[][] b;
public static int[][] c;

Somewhere in main, threads are created and started:
c = new int[a.length][b[0].length];

            Thread1 thread1 = new Thread1();
            Thread2 thread2 = new Thread2();
            Thread3 thread3 = new Thread3();
            Thread4 thread4 = new Thread4();

            thread1.start();
            thread2.start();
            thread3.start();
            thread4.start();

            try {
                thread1.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            try {
                thread2.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            try {
                thread3.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            try {
                thread4.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

Four threads code:
public static class Thread1 extends Thread {

    @Override
    public void run() {
        int m = a.length;
        int n = b[0].length;
        int k = (a.length) / 4;

        for (int i = 0; i <= k; i++) {
            for (int j = 0; j < n; j++) {
                for (int l = 0; l < b.length; l++) {
                    c[i][j] = c[i][j] + a[i][l] * b[l][j];
                }
            }
        }
    }
}

public static class Thread2 extends Thread {

    @Override
    public void run() {
        int m = a.length;
        int n = b[0].length;
        int k = (a.length) / 2 + 1;
        int s = ((a.length) / 4) + 1;

        for (int i = s; i < k; i++) {
            for (int j = 0; j < n; j++) {
                for (int l = 0; l < b.length; l++) {
                    c[i][j] = c[i][j] + a[i][l] * b[l][j];
                }
            }
        }
    }
}

public static class Thread3 extends Thread {

    @Override
    public void run() {
        int m = a.length;
        int n = b[0].length;
        int k = ((3 * (a.length)) / 4) + 1;
        int s = (a.length) / 2 + 1;

        for (int i = s; i < k; i++) {
            for (int j = 0; j < n; j++) {
                for (int l = 0; l < b.length; l++) {
                    c[i][j] = c[i][j] + a[i][l] * b[l][j];
                }
            }
        }
    }
}

public static class Thread4 extends Thread {

    @Override
    public void run() {
        int m = a.length;
        int n = b[0].length;
        int k = ((3 * (a.length)) / 4) + 1;


        for (int i = k; i < m; i++) {
            for (int j = 0; j < n; j++) {
          for (int l = 0; l < b.length; l++) {
                        c[i][j] = c[i][j] + a[i][l] * b[l][j];
                    }
                }
            }
        }
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nicholas, 2015-12-11
@kostyabakay

Well, in general, what is the principle: divide your matrix, which should turn out into 8 blocks.
For each block, define the boundaries and count the values ​​of the elements.
Why cycles 3? Solve the problem on one thread, then you will need to introduce small ones into the main counting algorithm to transfer to many threads.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question