# How can I save the current evaluation state of a notebook and start from it later?

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.

-
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... – R. M. Aug 14 '13 at 3:47
@rm-rf Alright. That could have been a one-time issue then. I'll try that :) – Manishearth Aug 14 '13 at 3:50
Sleep/Hibernation also works fine for me on Win7 (and used to with Win XP). – Yves Klett Aug 14 '13 at 6:28

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.

-
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. – Mr.Wizard Aug 14 '13 at 9:13