TransWikia.com

Interrogating a running evaluation

Mathematica Asked by Carl Woll on July 27, 2021

Is it possible to find the values of variables of a running Mathematica evaluation, without interrupting it? For example, suppose I do:

Do[Pause[1]; a++, {50}]

and then afterwards realize that the code is taking a long time. If I had realized before the computation started that it would be slow, I could have initially evaluated Dynamic[a], and then run the computation. Is there any way to do something similar after the computation has started, without interrupting the computation?

4 Answers

For simplistic purposes there is Evaluate in Subsession which is in the Evaluation menu, and has the shortcut key F7 under Windows & Linux (and alt+shift+enter under macOS).

Simply make a new Cell while the evaluation is running, containing e.g. a, make sure the cell has focus, and press F7; Mathematica evaluates this and returns the current value of a, and continues the computation.

If you prefer an ongoing count you can put e.g. Dynamic[a] in the new cell.

enter image description here

Correct answer by Mr.Wizard on July 27, 2021

Update

The other answers provide a good way to add dynamic monitoring using a single kernel when access to the front end is available. The method I proposed using multiple kernels will work even when the front end is not accessible. So, you might ask, when would the front end not be available?

Suppose you have a remote machine, with two kernels, a master/monitor kernel, and a slave/working kernel. It is possible to communicate with the master kernel using the Channel framework. In this situation, one might want to ask the master kernel to check on the status of the slave kernel, and report back diagnostic information. This is in fact a situation that I currently use, and one where I don't think the other answers will work.

I had a stylesheet approach that is not nearly as effective as MrWizard's answer, so I have removed that part of my original answer.

Original answer

Yes, this is possible. First, you need to make sure that your version of Mathematica has multiple kernels defined. Then, you can use Dynamic (or other symbols that take an Evaluator option, e.g., Button or Cell) to display the value of a symbol in another kernel.

Multiple kernels

You can create additional kernels using the Evaluation | Kernel Configuration Options menu item. Another method is to use CurrentValue. For instance, the following returns the list of evaluators that have been defined:

CurrentValue[$FrontEnd, EvaluatorNames]

{"Local" -> {"AutoStartOnLaunch" -> True}, "Local 2" -> {}}

Let's add a new evaluator:

CurrentValue[$FrontEnd, {EvaluatorNames, "Debug"}] = {"AppendNameToCellLabel"->True}

{"AppendNameToCellLabel" -> True}

Check:

CurrentValue[$FrontEnd, EvaluatorNames]

{"Local" -> {"AutoStartOnLaunch" -> True}, "Local 2" -> {}, "Debug" -> {"AppendNameToCellLabel" -> True}}

Dynamic

With multiple kernels, you can use Dynamic with the Evaluator option to display the value of a symbol in a different kernel. For example, suppose you have a running computation in kernel "Local". Then, in a notebook running the kernel "Debug", you can evaluate:

Dynamic[a, Evaluator->"Local"]

to display the value of the symbol a in kernel "Local".

For example, in the following animation, I create two notebooks, one using the kernel "Local", and the other using the kernel "Debug". Then, I start a computation in the "Local" notebook. Subsequently, in the "Debug" notebook, I execute the Dynamic expression above, which displays the value of a in the "Local" kernel:

enter image description here

Answered by Carl Woll on July 27, 2021

Here is a quick solution in case you do not want / can not play with additional Kernels.

Steps

Assuming, the calculation already started:

  • create e.g. an input cell, just by typing below

  • Show cell expression Ctrl+Shift+E

  • Replace cell expression with:

    Cell[BoxData[ DynamicBox[ToBoxes[$CellContext`a, StandardForm]  ]]]
    
  • Switch back Ctrl+Shift+E

enter image description here

Why does it work?

Shortly, by default Dynamic content is evaluated via preemtive link which means that this evaluation can interrupt main link evaluations (our Do loop).

See: tutorial/AdvancedDynamicFunctionality#1938125129

We just need DynamicBox to appear in the notebook structure without using kernel.

We are editing cell expressions manually in the front end, the kernel is not needed. Once we finish the front end will try to handle our cell.

Answered by Kuba on July 27, 2021

Some function, such as the InverseBetaRegularized, will not honour any of the mechanism for abort it. A simple code example using subkernels that can be killed in process explorer without crash the master session is as follows:

SetSharedVariable[results];
PrintTemporary[Dynamic[results]];
CheckAbort[
 ParallelDo[
  AppendTo[results, 
   Flatten[{data[[x]], 
     TimeConstrained[InverseBetaRegularized @@ data[[x]], 
      1, -1]}]], {x, 500002}], results]

Answered by Frank on July 27, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP