PDF Print E-mail
Written by Batu   
Friday, 25 April 2008 04:06

Multithreading Matlab with Java

With the advance of technology, we have started seeing SMP (Symmetric Multiple Processing) CPUs (Central Processing Unit, operational brain of your computer) even in laptops. Though these architectures are great for listening to your favorite internet radio station and work on Matlab at the same time, when you actually want to use both CPUs to attack the same problem in Matlab it is quite tricky.

 

One way of achieving this is using Matlab's Parallel Computing Toolbox. There are several advantages (ease of use, compatibility, robustness) to use this option if you have it under your hand. Otherwise read on. ;) Also keep in mind that you can switch on multi core processing for BLAS for Matlab 2006b. For Matlab 2007a and 2007b you can simply go under Preferences and enable multi threading from the graphical interface. But this will not be enough to spread all the load to multiple cores in all cases.

What we will do here is simply to create a call to our Java routine from Matlab. Though keep in mind that I am not a Java guru. These are just my two cents on the topic. In order to do this, the first thing to do is to make sure that our Matlab Java Runtime Environment (JRE) and our Java compiler versions are compatible. The easiest way to do this is to install the latest Java and set the MATLAB_JAVA environment variable to the new JRE. On Linux with tcsh this can be done by typing:

 setenv MATLAB_JAVA /usr/java/jdk1.6.0_05/jre

Of course you might need to change the "/usr/java/jdk..." part depending on your installation. The folder you specify for MATLAB_JAVA needs to be such that it should be the parent folder for "/lib/rt.jar" file.

Now let's write a simple threaded Java class. Open a new text file in your favorite editor and copy/paste the following:

package test;
public class ThreadJob implements Runnable {
        private int id = 0;
        public ThreadJob(int num){
                id = num;
        }
        public void run(){
                System.out.println("Starting job " + id + "."); 
                try {
                        Thread.sleep(5000);
                } catch (InterruptedException e) {
                        e.printStackTrace();
                }
                System.out.println("Finished job " + id + "."); 
        }
        public static void main(String[] args){
                for(int i = 0; i < 20; i++){
                        Thread t = new Thread(new ThreadJob(i));
                        t.start();
                }
        }
}


In the above code we are simply creating a threaded class. Since it has a main routine you can run it after compiling it, and it will run 20 instances of this class. So go ahead and save this file as ThreadJob.java and compile it. I put it in a jar file called "mji.jar"To call it from Matlab we first need to start Matlab ;). Don't forget to set the MATLAB_JAVA environment variable though.

In Matlab type the following few lines to add the "jar" file to the Dynamic Java Path.

javaaddpath('/PATH/TO/JAR/FILE/mji.jar'); 

The best way I found to check if Java methods are accessible is using "methodsview" function.

k=int16(1);methodsview(test.ThreadJob(k)) 

We need the "k" to be int16 since we defined our Constructor with an "int" in the Java code.

If the methodsview test succeeds then you are ready to try your first Multi Threaded Java Call from Matlab. Go back to Matlab command window and type:

for k=1:20
kk=int16(k);
java.lang.Thread(test.ThreadJob(kk)).start;
end

You should see all the jobs starting and finishing almost at the same time. If they were to run sequentially they would take 5sec*20 but since they are all running in parallel they take about 5 sec to run them all. Of course when you have actual calculations it won't be as fast, but you will definitely gain some processing power.

 

Author's Note:

20080509 - In the earlier version of the code the package name was mistakenly written as "example" instead of test. Of course you can choose which ever package name you like for the Java code, but then after you add the JAR file to the Matlab's path, you need to call your methods like this: "packageName.methodName()".

 

Related Links:

Calling Java From Matlab Command Line

Calling Java From Matlab (Extenal Interfaces/API)

Calling Matlab From Java

Last Updated ( Friday, 09 May 2008 13:51 )