1

So I'm interested in this concept. I have a lot of experience with programming in C and Fortran, but little to no Java experience. Is it feasible to be able to call C code (even C++) to multiply matrices within a Java code?

My idea, in concept, is something like this

public class MatrixMultiplication
{
    public static void main(String[] args)
    {
        // Parameters
        int MATRIX_SIZE_M = 5000;
        int MATRIX_SIZE_N = 5000;

        // Allocate matrices for multiplication
        double matrixA[][] = new double[MATRIX_SIZE_M][MATRIX_SIZE_N];
        double matrixB[][] = new double[MATRIX_SIZE_N][MATRIX_SIZE_M];
        double matrixC[][] = new double[MATRIX_SIZE_M][MATRIX_SIZE_N];

        // Initialize matrices
        ...

        // Call C code here to multiply C=A*B
        ...

        // Do some other stuff with A, B, and C
        ...
    }
}

If this can be accomplished, the next step would be to call MKL to do linear algebra computations, which would be pretty cool.

Thoughts?

mjswartz
  • 715
  • 1
  • 6
  • 19

1 Answers1

0

Is it feasible to call C code to multiply matrices out

Can you do it? Yes you can. I don't really understand why you'd need to. You could very easily multiply the matrices out in java.

int aLength = A[0].Length; // A Column Length
int bRowLength = B.Length;    // B Row Length
int newRowL = A.Length;    // m result rows length
int newColL = B[0].Length; // m result columns length
double[][] matrixC = new double[newRowL][newColL];

if(aLength == bRowLength)
{ 

    for( int i = 0; i < newRolL; i++)
        for(int j = 0; j < newColL; j++)
            for(int k = 0; k < aLength; k++)
                 matrixC[i][j] += A[i][k] * B[k][j];
}

return matrixC
Adam
  • 2,422
  • 18
  • 29
  • 2
    Java's matrix algebra is slower out of the box than C (without even adding -O3), and substantially slower than MKL – mjswartz Jul 23 '15 at 15:40
  • Well naturally it's going to be slower than unloading MKL. If you want to use MKL, you should specify that. Link to http://arma.sourceforge.net/ It's a C++ Library that is capable of calling MKL routines. I wrote this to keep the coding in java. – Adam Jul 23 '15 at 16:03