7
$\begingroup$

I currently have a nested Table command running (in Mathematica 9), with two progress bars to track the variables. I have to shut down or hibernate the computer in a couple of minutes, and the calculation is only halfway done.

What can I do in such a situation. I'm pretty sure simply hibernating won't work, as I recall it not working in Mathematica 8. Will entering a subsession and then hibernating work? Or entering a subsession and dumping the kernel?

While I don't have any preference towards shutting down or hibernating in my current situation, a solution that allows me to close Mathematica and resume evaluation at some later time (after shutting down, etc) would be ideal.

Note: This question is about a cell which is already running (so I can't modify it), however if it's not possible to do this here, an answer that shows how to run Table in an interrupt-and-saveable manner would be OK1

1. I have a pretty good idea of how to do this (dynamically append data to a list with For instead f using Table), but there may be better ways of doing this.

$\endgroup$
3
  • 1
    $\begingroup$ Simply hibernating/sleeping has worked for me on my mac. Mathematica just continues from where it left off without any intervention. Things might be different on other OSes... $\endgroup$
    – rm -rf
    Commented Aug 14, 2013 at 3:47
  • $\begingroup$ @rm-rf Alright. That could have been a one-time issue then. I'll try that :) $\endgroup$ Commented Aug 14, 2013 at 3:50
  • 1
    $\begingroup$ Sleep/Hibernation also works fine for me on Win7 (and used to with Win XP). $\endgroup$
    – Yves Klett
    Commented Aug 14, 2013 at 6:28

1 Answer 1

8
$\begingroup$

Simply hibernating/sleeping has worked for me on my mac. Mathematica just continues from where it left off without any intervention. Things might be different on other OSes. I don't think you can manually continue an evaluation from where you left off (i.e., shut down and continue in a new session). As for your present scenario (already running Table), your best bet might be to enter a subsession, note the current progress, dump the variables and manually restart it from the current state.

Alternately, if you get in the habit of assigning the output to downvalues (i.e., store the $m,n$th result in result[m, n]), you could combine it with memoization to design a "poor man's interruptible Table". For a more robust approach with error checking and all that, see this answer by Leonid.

Here's a simple example of the poor man's version with a deliberately inefficient version of the Fibonacci sequence so that we can interrupt it:

ClearAll@fibonacci
fibonacci[n_] := fibonacci[n - 1] + fibonacci[n - 2]
fibonacci[1 | 2] = 1;

Clear@result
result[m_, n_] := result[m, n] = fibonacci[m + n] (* This is the function to be computed *)

Monitor[Table[result[m, n], {n, 25}, {m, 20}], {m, n}]

Now let that run till about {20, 10} and then interrupt the evaluation with Cmd+.. Save your current session (assuming it's in the Global` context) with

DumpSave["savedSession.mx", "Global`"]

Note that DumpSave is platform and version specific, so don't try to restart it on a different architecture/OS/Mathematica version.

Now restart your kernel and do

<<"savedSession.mx"
Monitor[Table[result[m, n], {n, 25}, {m, 20}], {m, n}]

Specifically, note where the evaluation continues from.

I generally prefer using the downvalues themselves instead of a list, but there are several reasons one might want a list as a result. You can get that out of the DownValues of result as (for the above example):

Cases[DownValues@result, HoldPattern[_@_[__Integer] :> val_] :> val] ~Partition~ 25 // Transpose

where 25 here is the inner iterator's length.

$\endgroup$
1
  • 1
    $\begingroup$ I have always appended results incrementally to a file and loaded them manually, like this. It never occurred to me to use this method. Still, without the incremental save I'd be worried that Mathematica or my PC would crash after XX hours computation, right before I was going to DumpSave the work, and I'd lose it all. $\endgroup$
    – Mr.Wizard
    Commented Aug 14, 2013 at 9:13

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.