How can I get one Mathematica kernel to service multiple applications?
 When creating multiple programs that access Mathematica's kernel, it would be most useful to have one Mathematica kernel service all of these programs. It is highly desirable to allow GUI windows to turn around and drive the Mathematica kernel themselves without tying up the kernel's attention and, thus, interfering with the user's interactive session in the notebook front end. These are described in the J/Link User Guide as “modeless” interfaces, meaning modeless with respect to the kernel; in other words, they do not require its exclusive attention while they are active. J/Link provides a function, ShareKernel, that enables one to have a single Mathematica kernel service multiple programs. The documentation of the ShareKernel command follows for your reference. ShareKernel initiates sharing of the kernel between the front end and Java. You call ShareKernel to allow the kernel to respond to Java-based user interfaces. If the Java link is already sharing the kernel, ShareKernel does nothing. ShareKernel[linkobject] specifies that the front end should share the kernel with the specified link. The best part is that this functionality is written in top-level Mathematica code, and it really has nothing to do with J/Link specifically. Any program that needs to enable the state of having one kernel service multiple front end-type programs can use the feature, as long as J/Link is loaded into memory. Here is an example of how you might use it for your application. In[1]:= |  |
The following statement begins an application. In[2]:= |  |
In[3]:= |  |
Any initialization that needs to occur would take place at this point. In[4]:= |  |
ShareKernel should return right away and the front end session should continue normally. The OpenGLApplication program can now call Mathematica as if it had launched the kernel itself. Both the front end and the OpenGLApplication operate as if they had exclusive access to the kernel. Provided your application allows it, you can continue to call it from Mathematica. In other words, you can have it both ways: the application can be scripted by calls executed from the front end, and it can also be operated as a stand-alone interface that calls the kernel for computations. Please note that ShareKernel is not truly independent of the rest of J/Link. ShareKernel puts the kernel into a state where it is continually busy and, thus, it will utilize a lot of CPU time. This is because yielding cannot be done effectively in top-level Mathematica code. ShareKernel relies on a call into the Java runtime for yielding, and this reduces the CPU load to negligible levels. ShareKernel works fine without Java running, but it will keep your CPU load at 100%. To fix this, all you need to do is call InstallJava. In[5]:= |  |
Out[5]= |
 |
The documentation of the InstallJava commands follows for your reference. InstallJava launches the Java runtime and prepares it to be used from Mathematica. InstallJava has a number of options to control the name of the Java runtime program, the classpath used, etc. Only one Java runtime is ever launched; subsequent calls to InstallJava after the first have no effect. Currently, we are aware of two other solutions to this problem. There are two programs that allow one kernel to “multiplex” more than one front end-type program. One is called Serializer, which is discussed in the forthcoming book, MathLink: Network Programming with Mathematica, written by Chikara Miyaji and Paul Abbott. The other program is entitled MultiLink, which comes with Mathematica Link for Excel. These programs are similar in that they sit between the kernel and the front end(s), listening to multiple links and allowing them sequential access to the kernel. The drawback to these solutions is that it is necessary to know ahead of time, when you first launch the kernel, that you will need this multiplexing facility. This is because you connect your front end program not to the kernel, but to Serializer or MultiLink. With ShareKernel, you can turn on (and off) sharing in the middle of a session. Download this FAQ as a Mathematica 5.2 Notebook
Questions or comments? Send email to support@wolfram.com.
|