7
$\begingroup$

Let us start with a variant of an example in the documentation of AbortKernels.

We launch four subkernels:

LaunchKernels[4];

We create four simple evaluation objects. Each of them is an infinite loop, increasing a common shared variable after a random time. While showing the value of the shared variable, we parallel evaluate the tasks until we manually abort the evaluation.

found=0.;
SetSharedVariable[found];
tasks=Table[ParallelSubmit[While[True, Pause[RandomReal[]];found=found+1]], {4}]
PrintTemporary[Dynamic[found]];
CheckAbort[WaitAll[tasks], AbortKernels[]];
found

Run the above command and abort manually a number of times. Ultimately, Mathematica will hang. When this happens is highly inpredictable. In my tests, it varied from 1 (immediate hanging) to 8 (7 succesfull abortions).

The problem also turns up when we use CloseKernels instead of AbortKernels. Run and abort the next command a number of times.

LaunchKernels[4];
Dynamic[Kernels[], UpdateInterval->1]
found=0.;
SetSharedVariable[found];
tasks=Table[ParallelSubmit[While[True, Pause[RandomReal[]];found=found+1]], {4}]
PrintTemporary[Dynamic[found]];
CheckAbort[WaitAll[tasks], CloseKernels[]];
found

When Mathematica hangs after abortion, it seems theat the subkernels are not closed.

It has nothing to with evaluation objects, as is shown in the next command:

LaunchKernels[4];
found=0.;
SetSharedVariable[found];
PrintTemporary[Dynamic[found]];
CheckAbort[ParallelDo[Pause[RandomReal[]];found=found+1, {10000}], CloseKernels[]];
found

WRI CASE:4041721, 2018-04-16.

$\endgroup$
1
  • $\begingroup$ I can reproduce the issues with Mma 11.3.0 on Win 8.1 Pro. I agree that it seems like the hanging could be a bug. $\endgroup$
    – Edmund
    Commented Apr 7, 2018 at 10:17

1 Answer 1

3
$\begingroup$

As I understand it now, the title of my question should be: Aborting a parallel computation with a shared variable may crash Mathematica. It seems to have nothing to do with AbortKernels or CloseKernels. I was mislead by the example in the documentation of AbortKernels, in which, after aborting a parallel computation, a command AbortKernels[] was given.

The following example suggests that aborting a parallel computation also aborts the subkernels, so that giving a command AbortKernels after Abort is superfluous.

First open Parallel Kernel Status. That launches four subkernels. Then evaluate the next cell.

found=0;
DistributeDefinitions[found];
tasks=Table[ParallelSubmit[While[True, Pause[RandomReal[]];found=found+1]], {4}]
WaitAll[tasks]

Parallel Kernel Status shows that the four subkernels are running. After some time, abort the computation. Then Parallel Kernel Status shows that the four subkernels become idle. (In 15 attempts no crash of Mathematica.) We can inspect the result by using ParallelEvaluate:

ParallelEvaluate[found]
(* {31,34,32,28} *)

Now let us introduce a shared variable.

found=0.;
SetSharedVariable[found];
tasks=Table[ParallelSubmit[While[True, Pause[RandomReal[]];found=found+1]], {4}]
PrintTemporary[Dynamic[found]];
WaitAll[tasks]
found

All assignments to the shared variable are done by the main kernel. When we abort the above computation, it seems that first the main kernel is aborted and a little bit later the subkernels. In the mean time, it might happen that a subkernel asks the main kernel to do an assignment, which now is impossible. In that case, Mathematica will crash. Indeed, after some attempts, aborting the above command crashes Mathematica.

A better way of aborting the above parallel computation is to abort the subkernels manually. To this end, create the following button and run the above command once again.

Button["Abort kernels", AbortKernels[]]

When we press the button, Parallel Kernel Status shows that the subkernels become idle. As a consequence, the execution of WaitAll finishes and the value of the variable found is returned.

This seems to be a safe way of aborting a parallel computation. But it is not absolutely safe: a subkernel can be aborted during the proces of assigning to the shared variable. In that case, this subkernel dies, but Mathematica does not crash. When we rerun the command, the died subkernel will be replaced by a new one.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.