0

I am trying to write program about sensor calculator and I would like to hear from you guys how can I improve execution time of my program?

In brief a sensor calculator is program that performs matrix multiplication. It could receive 50,000 matrices per second. Sensor calculator primary job is to receive matrices and calculate them with one of the 5 matrices that are already stored in program.

Sensor calculator has 5 methods and every method has own matrix which it multiplies with received(parameter) matrix(Matrix multiplication). And of course they return produced matrix.

  1. I have totally 50 000 virtual censors in various computers.
  2. Every sensor is sending every second one matrix to calculator(server) with UDP.
  3. Server which hosts sensor calculator, receive matrix and calculate it.
  4. Server will send back result to sensor(client) with UDP.

All matrices are 10x10 size.

For example the first method is:

public int[10][10] calculateWind(int[10][10] A){

 int[10][10] C = new int[10][10]; //

    for (int i = 0; i < 10; i++) { // Row
        for (int j = 0; j < 10; j++) { // Column
            for (int k = 0; k < 10; k++) { // Column
                C[i][j] += A[i][k] * B[k][j];//B is constant matrix(private attribute)
            }
        }
    }

    return C;}

I am using Java but someone told me that I could use FORTRAN & C with java and that could help?

I am trying to find the fastest way. Tell me guys everything what you think that could help my program improve it's performance. Changing programming language? Using unique algorithm?

Every advice is welcome except using ASSEMBLY and thanks for your advice.

user3521129
  • 91
  • 1
  • 1
  • 5
  • You could pass a matrix from Java to C or Fortran code via the Java Native Interface, JNI, as mentioned here: http://stackoverflow.com/questions/14725789/passing-a-2d-matrix-from-java-to-c-file-through-jni – ajm475du Apr 10 '14 at 21:28
  • Is there any delay to pass from java to Fortran? – user3521129 Apr 10 '14 at 21:33
  • There is some delay but it's on the order of the square of the number of rows in the matrix. The time to compute the matrix product tends to dominate that, being on the order of the cube of the number of rows in the matrix. That's a rule of thumb to guide you. In order to know precisely you'd want to measure. – ajm475du Apr 10 '14 at 21:44
  • Do you mean "sensor" rather than "censor" in your post? You use the word "censor" many times. – Fortranner Apr 10 '14 at 21:49
  • Yes, I mean sensor, thank you. – user3521129 Apr 10 '14 at 21:50
  • If you really want performance, install the blas library and call blas routines. if you are running linux you can get it from the package manager or you can download it from http://www.netlib.org/ – innoSPG Apr 10 '14 at 22:53

2 Answers2

0

There are libraries which implement matrix multiplication with faster algorithms than the straightforward three nested loops.

Consider this answer: Performance of Java matrix math libraries?

Community
  • 1
  • 1
jonny
  • 4,264
  • 4
  • 22
  • 29
  • For matrices as small as 10x10 the overhead of calling an external library may be larger than computing the product directly. – Joni Apr 11 '14 at 06:13
0

You should run the program in a profiler to find the hotspots, and measure the effect your changes have.

A possible improvement is reordering your loops to minimize cache misses:

for (int i = 0; i < 10; i++) {
    for (int k = 0; k < 10; k++) {
        for (int j = 0; j < 10; j++) {
            C[i][j] += A[i][k] * B[k][j];
        }
    }
}

For matrices as small as yours there's unlikely to be notable improvement because your entire data fits into CPU cache.

Using C will not likely give any performance improvement. After a few iterations JIT will have compiled the code to native and the CPU is running essentially the same code that a C compiler would produce. Fortran could be better thanks to automatic vectorization, but the difference is probably not big for matrixes that are this small.

Joni
  • 108,737
  • 14
  • 143
  • 193