Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2008

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Re: Multiple threads using Mathlink


Hello Steve,

Thank you for your answer. It assured me that I understand the architecture 
correctly.

However, my problem remains unresolved. I've done some experiments to track 
down the issue. Apparently, the problem is related to the Mathlink.close() 
method (I'm using Java). Here are the results:

1/ when I reuse the same Mathlink object to perform all computations, without 
closing it, everything works fine

2/ when I create a separate object in every webservice invocation, but don't 
use the close() method, it also works. In this case, I kill the kernel after 
it completes the computations (with Unix "killall" command). I'm forced to do 
this, because otherwise the webservice thread doesn't end (it's waiting for 
the child - Mathlink - process to complete)

3/ when I create a separate object in every webservice invocation and invoke 
the close() method, the application server crashes mysteriously afterwards 
(sometimes during a webservice invocation which doesn't use Mathlink at all).

In cases 2/ and 3/, I use "synchronize" clauses to protect pieces of code 
which open and close the link. This is probably not needed, as the java 
Mathlink wrapper already does it (using, for instance, the environmentLock 
object in NativeLink.java).

The schema of my Mathlink invocations is as follows:

synchronized(lock) {
	ml = MathLinkFactory.createKernelLink(MLINK_PARAMS);
	ml.connect();
	ml.discardAnswer();
}

ml.evaluate("<< " + scriptFile);
ml.discardAnswer();

ml.evaluate(expression);
ml.waitForAnswer();

ml.getXYZ(); // for instance: getDouble()

synchronized(lock) {
            ml.close();
}


I'm still not sure if the issue lies in Mathlink, my usage of it, or the 
webservice framework I'm using (Tomcat & Axis2).

Regards,
Pawel Banasik



> Pawel,
>
> Generally speaking it is safe to use MathLink with threads.   Here are
> the relevant stipulations:
>
> 1.  Do not concurrently access the same MLENV object from more than
> one thread at a time.   This means that you do not want multiple
> threads invoking any of the MLOpen* functions using the same MLENV
> object simultaneously.  You will need to protect access to the MLENV
> object with a thread locking mechanism.
>
> 2.  Do not concurrently access the same MLINK object from more than
> one thread at a time.   You will need to protect access to the MLINK
> object with a thread locking mechanism.
>
> To answer your question about MathLink and Kernels, you should
> understand  the following about MathLink.   MathLink does not
> specifically create Kernel processes.   MathLink creates a
> communication link between one process and another process.   MathLink
> supports the ability to start the other process, but fundamentally
> what you are dealing with is a full duplex communication link between
> two processes.   MathLink links function in a one-to-one paradigm,
> thus one link can start and communicate with one process.
>
> Steve
> Senior Network/System Protocol Developer
> Wolfram Research Inc.
>
> On May 16, 2008, at 3:32 AM, Pawe=C5=82 Banasik wrote:
> > Hello,
> >
> > I have a question regarding multithreaded access to Mathematica via
> > MathLink.
> >
> > I'm developing a set of webservices for use with the Taverna Workbench
> > (taverna.sourceforge.net), a computational biology tool. These
> > webservices
> > call Mathematica to perform some computations. Obviously, each
> > webservice
> > invocation creates a new thread. As I'd like the Mathematica
> > environments
> > belonging to different threads to be completely separated one from
> > another,
> > I've implemented a mechanism which creates a new MathLink object for
> > every
> > thread. Unfortunately, this leads to serious problems, including an
> > application service crash, occuring from time to time. Apparently,
> > this is
> > some kind of a concurrent access problem - it doesn't happen when I
> > run only
> > one thread at a time, with subsequent invocations separated by a few
> > seconds.
> >
> > Perhaps there is a problem with my understanding of the relation
> > between
> > MathLink and Mathematica kernels? Is it true that each Mathlink object
> > creates a separate kernel process? If not, how to achieve such
> > behavior? Do
> > the MathLink native libraries support multithreading?
> >
> > Thank you for your help,
> > Regards,
> > Pawel Banasik


  • Prev by Date: Re: An Elegant way of plotting the Pole-Zero diagram
  • Next by Date: Re: Re: NthSubset function of Combinatorica package
  • Previous by thread: Re: Multiple threads using Mathlink
  • Next by thread: Problem with cell brackets and Manipulate