Example Concurrent Operations

From Efficient Java Matrix Library
Jump to: navigation, search

Concurrent or Mult Threaded operations are a relatively recent to EJML. EJML has traditionally been focused on single threaded performance but this recently changed when "low hanging fruit" has been converted into threaded code. Not all and in fact most operations don't have threaded variants yet and it is always possible to call code which is purely single threaded. See below for more details.


External Resources:

Example Code

/**
 * Concurrent or multi-threaded algorithms are a recent addition to EJML. Classes with concurrent implementations
 * can be identified with _MT_ in the class name. For example CommonOps_MT_DDRM will contain concurrent implementations
 * of operations such as matrix multiplication for dense row-major algorithms. Not everything has a concurrent
 * implementation yet and in some cases entirely new algorithms will need to be implemented.
 *
 * @author Peter Abeles
 */
public class ExampleConcurrent {
    public static void main( String[] args ) {
        // Create a few random matrices that we will multiply and decompose
        var rand = new Random(0xBEEF);
        DMatrixRMaj A = RandomMatrices_DDRM.rectangle(4000,4000,-1,1,rand);
        DMatrixRMaj B = RandomMatrices_DDRM.rectangle(A.numCols,1000,-1,1,rand);
        DMatrixRMaj C = new DMatrixRMaj(1,1);

        // First do a concurrent matrix multiply using the default number of threads
        System.out.println("Matrix Multiply, threads="+EjmlConcurrency.getMaxThreads());
        long time0 = System.currentTimeMillis();
        CommonOps_MT_DDRM.mult(A,B,C);
        long time1 = System.currentTimeMillis();
        System.out.println("Elapsed time "+(time1-time0)+" (ms)");

        // Set it to two threads
        EjmlConcurrency.setMaxThreads(2);
        System.out.println("Matrix Multiply, threads="+EjmlConcurrency.getMaxThreads());
        long time2 = System.currentTimeMillis();
        CommonOps_MT_DDRM.mult(A,B,C);
        long time3 = System.currentTimeMillis();
        System.out.println("Elapsed time "+(time3-time2)+" (ms)");

        // Then let's compare it against the single thread implementation
        System.out.println("Matrix Multiply, Single Thread");
        long time4 = System.currentTimeMillis();
        CommonOps_DDRM.mult(A,B,C);
        long time5 = System.currentTimeMillis();
        System.out.println("Elapsed time "+(time5-time4)+" (ms)");

        // Setting the number of threads to 1 then running am MT implementation actually calls completely different
        // code than the regular function calls and will be less efficient. This will probably only be evident on
        // small matrices though

        // If the future we will provide a way to optionally automatically switch to concurrent implementations
        // for larger when calling standard functions.
    }
}