Group Abstract Group Abstract

Message Boards Message Boards

How do parallel kernel JLink object methods execute?

Posted 6 years ago

I am trying to execute a sequence of timed Java method calls in a manner that does not block the Mathematica front end. To this end I have been trying to make these calls from a parallel kernel, as one might ParallelSubmit native Mathematica code. This has produced results I've found confusing. Side effects of the Java methods are observed only when those methods are directly returned to the front end, and these methods are blocking execution sequences, leaving code unevaluated until it is returned to the front end.

I have constructed the following small example to demonstrate.

(* Set up a dedicated parallel submission kernel *)
CloseKernels[]; LaunchKernels[1]; 
reservedKernel =  First@Parallel`Protected`$sortedkernels;

(* Parallel install a java robot class *)
Parallel`Developer`Send[reservedKernel,
  JLink`ReinstallJava[];
  robotclass = JLink`JavaNew["java.awt.Robot"];
];

(* Parallel submit a command sequence *)
Parallel`Developer`Send[reservedKernel, 
  Unevaluated[robotclass[mouseMove[100, 100]]; Pause[1];]
];

(* Wait for the expected completion of the sequence *)
Pause[2];

(* Time the reception of the results back into the front end *)
AbsoluteTiming[Parallel`Developer`Receive[reservedKernel]]

A naive (or perhaps optimistic) expectation of this code is that it will, at the submission of the parallel command sequence, immediately move the mouse to the {100,100} coordinate, and then Pause the parallel kernel for one second. As this second is overlapped by the Pause[2] executed in the front end, one would then expect the blocking Receive command to execute extremely quickly.

What actually happens is the code executes with no effect on the mouse, and the submitted Pause instruction is not run until the front end receives it, causing the Receive call to take more than one second.

I do not understand why this is the case. Any input would be very welcome, both in order to work out what is actually going on, and also to meet my aim of submitting unblocking sequences with immediate side effects.

POSTED BY: David Gathercole
2 Replies
Anonymous User
Anonymous User
Posted 6 years ago
POSTED BY: Anonymous User

I might clarify a few elements for you.

The parallel command I'm using is not JLink specific. It's akin to ParallelSubmit, as I mentioned, in that it submits parallel evaluations without automatically waiting for their execution. I'm using it in preference to ParallelSubmit in this example as it removes the queue interactions that would obfuscate the core issue. I'm using it in my target use case as I require traditional ParallelSubmit functionality on a subset of my parallel cores, and simultaneous controlled parallel execution for this purpose. A similar situation is seen in this stackexchange question. I am thus under the impression that your primary suggestion, to run in parallel directly from the front end, is what the code I posted already does. I do not understand how I would invoke a second front end from the parallel kernel as you mention. I would further be concerned with the undesired side effects (i.e. a literal second front end) of doing so.

I am indeed using the Java interface to create OS side effects. In my constructed example this involves mouse movement. Whilst I suspect diagnostically this is using the Mathematica front end, at a high level I do not see that this use of the front end is a conceptual necessity. As to if the front end is a literal necessity in implementation, and if this can be either achieved or subverted, is the core of my original question.

Your expectations on the causality of the waiting of the front end do not hold under experimentation. I have omitted a large suite of diagnostic experiments I have performed to try and convey the issue as directly as possible. It's a valid hypothesis that the front end is explicitly waiting for the Receive output, causing the connection, but this is not the case.

You mention that JLink may be inherently connected to the front end. This aligns with my suspicion that behaviour differences between JLink executions on the front end slaved master kernel and the parallel kernels are caused by a lack of a direct front end connection. I would like to both explicitly confirm this hypothesis, and understand its consequences. You mention JLink code for disconnecting from the front end. Would you be able to come up with any examples I could try, or specific documentation?

I am unconcerned with losing Mathematica view of the mouse.

POSTED BY: David Gathercole
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard