Java Builder - Multithreading issues

Hello!
I built and compiled my first serious Matlab program to Java class using the Java Builder. I wrote Java driver, which converts the necessary data types from Java to Matlab.
But now I have a problem - I try to use the same class in two parallel threads in my Java application and keep getting exceptions. When I run only one thread it works fine. What can be the problem? Please help me...
Update:
My intention is to use a Random Forest Ensemble, trained previously in Matlab, in a Java application. My Matlab function, which I compiled, using deploytool is:
function [Probability]=PredictingTest (Predictor)
load (['D:/Test/Tree.mat'], 'Tree')
[~, prob]=predict(Tree , Predictor);
Probability=prob(:,1);
end
Where:
Predictor input is an integer between -10 to 10. Probability output is a double between 0 to 1. Tree is a .mat file with a CompactTreeBagger object stored in it. Next I deploy the jar files to my Eclipse project, and try to run this Java code:
/* Necessary package imports */
import com.mathworks.toolbox.javabuilder.*;
import PredictTest.*;
public class predict_test {
static MWNumericArray rhs = null; /* Stores input value */
static PredictTest prediction;
static Object[] result = null; /* Stores the result */
public static void main(String[] args) {
try {
prediction = new PredictTest();
rhs=new MWNumericArray(5,MWClassID.DOUBLE);
result=prediction.PredictingTest(1, rhs);
}
catch (MWException e) {
e.printStackTrace();
}}}
Segmentation violation detected at Fri Jul 08 12:02:26 2011:
Configuration:
MATLAB Version: 7.11.0.584 (R2010b)
Processor ID: x86 Family 6 Model 14 Stepping 5, GenuineIntel
Virtual Machine: Java 1.6.0_22-b04 with Sun Microsystems Inc. Java HotSpot™ Client VM mixed mode

 채택된 답변

Titus Edelhofer
Titus Edelhofer 2011년 7월 10일

1 개 추천

Hi,
which version of MATLAB are you using? And could you post both the exception you are seeing and how you call your class?
Titus

댓글 수: 7

Artik Crazy
Artik Crazy 2011년 7월 10일
Hi!
I updated my question with some information. Actually I copied it from my previous question about Java Builder - one that you helped me with :)
Titus Edelhofer
Titus Edelhofer 2011년 7월 11일
Hi,
but how do you do the multithreaded call? Usually nothing bad should happen, because the MCR (the MATLAB Compiler Runtime which runs behind the scene executing the MATLAB files) serializes all calls...
Titus
Artik Crazy
Artik Crazy 2011년 7월 11일
Is there a maximal number of simultaneous threads MCR can handle, even if it serializes the calls? Cause it may be a problem...
And even if these maximal number is very large I suppose that it can be easily achieved if every call takes large amount of time to process.
For example the most time consuming function in my compiled class is predict function for a Tree Bagger - 6 seconds at average for a single call. I run 10 threads simultaneously EVERY 30 seconds. So after a minute the MCR executed about 10 threads, but there are already 10*2=20 threads waiting in line...
Tell me if I got it right please. If so, is there any way I can overcome these limit? Why can't MCR run 4 simultaneous processes using 4 cores, just like regular MATLAB does?
Titus Edelhofer
Titus Edelhofer 2011년 7월 12일
Hi,
there are two issues here: MATLAB uses multiple cores on basic linear algebra (eig, \, plus, minus). That will the MCR do as well (it's called implicit parallelization). But you need some explicit parallelization. Two ways to do this: the parallel computing toolbox can help. But in your case I suggest to take a look at the following chapter in the Builder JA doc:
web([docroot '/toolbox/javabuilder/ug/br9gbeg.html']). Don't be confused that it's called "Web applications", but the exact same methodology would work for your case as well.
Titus
Friedrich
Friedrich 2011년 7월 12일
Titus is right. When you are looking for an example with Parallel Computing Toolbox look here:
http://www.mathworks.com/support/solutions/en/data/1-ELVN0M/index.html?product=CO&solution=1-ELVN0M
Artik Crazy
Artik Crazy 2011년 7월 13일
Hello again, thank you for your help!
Titus, I couldn't figure out the example you suggested :( Can you please give me the link again?
Friedrich, I tried to use batches in my application and it seems to be working - I will test it in full Java application tomorrow. Thank you again for an advice!
Artik Crazy
Artik Crazy 2011년 7월 14일
It does not work... I get the same error for the example specified and my own application: "The local scheduler cannot be used in deployed applications".
I Googled and understood that it is not possible to compile and deploy applications that use the local scheduler. I don't understand why Mathworks posted this example if it is not possible at all...
I'm really confused now...

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by