53
$\begingroup$

If I save a notebook, I only save what I see. That is, if I close Mathematica and reopen the notebook later, all previous calculations are lost, except for those things I've output. Now I know that you can save single definitions (and their dependencies) with Save, however is there a way to save the whole kernel state (preferably including internally cached data, e.g. from FullSimplify) so that when I reload both notebook and kernel state, I can continue to work exactly where I left off?

If there's no ready solution for it:

From what I understand, the explicit definitions are stored in UpValues, DownValues and OwnValues; also Options and Attributes affect evaluation, and therefore would have to be saved. However, those all expect an argument specifying a symbol to give information about. Is there any way to get a complete set of them? And would saving those actually suffice, or is there something else needed, too? Also, is there some way to explicitly get at internal cached information (it doesn't need to be in an understandable format, just being able to save and reload it would be sufficient)?

$\endgroup$

4 Answers 4

59
$\begingroup$

While it is true that you can not save a full state of the kernel, in some cases it may be enough for your purposes to save all symbols' definitions in some context(s), such as Global` (or whatever other contexts are of interest to you). This can be done via DumpSave, like

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

The .mx file generated by DumpSave will be platform-specific though. By using Get at some later point, you can reconstruct the values stored in symbols in those contexts you saved:

Get["state.mx"]

As stated already by @ruebenko, this will not generally fully reconstruct the kernel state. But if you manage to correctly account for all symbols (defined by you) which affect your computations, and depending on the circumstances, this may be enough for many practical purposes.

$\endgroup$
5
  • 5
    $\begingroup$ Thanks, that seems be close enough for most purposes (although it would be nice to store cached data as well). To be sure I don't miss anything out, I've tried DumpState["test.mx",Evaluate[$ContextPath]] and even things like %2 work correctly (the only problem is that the "Out[i]" labels are not saved with the notebook so that it's not obvious which number belongs to a given cell). $\endgroup$
    – celtschk
    Commented Jan 18, 2012 at 8:58
  • $\begingroup$ @celtschk Glad I could help! Thanks for the accept. $\endgroup$ Commented Jan 18, 2012 at 9:45
  • $\begingroup$ Correct me if I'm wrong, but this method can not reinitiate the state of the random generator to have the exact same state it was when one DumpSave-d. For that, you have to explicitly specify random=whatever;SeedRandom[random] at the start of your code. $\endgroup$ Commented Jan 18, 2012 at 9:49
  • $\begingroup$ @Istvan I think you are right, and this is one of the things which will be lost, unless you dig deep enough into the system and save also the context where this state is stored. This will be, however, a fragile and point solution. $\endgroup$ Commented Jan 18, 2012 at 9:57
  • $\begingroup$ @IstvánZachar: I've just tried it out, and you are right. Too bad that it's obviously not stored in System` . $\endgroup$
    – celtschk
    Commented Jan 18, 2012 at 10:38
7
$\begingroup$

No, currently you can not save the kernel state; restarting the Kernel from a previous state would need to modify and re-instantiate all sort of things.

$\endgroup$
3
$\begingroup$

I have added to this question the possibility of saving the current context, storing it inside the notebook, so that it can be loaded after. It is still very rough, but I think that it can work for same cases, and give an idea for a more adapted tool.

$\endgroup$
1
$\begingroup$

I needed some similar and explored alot of options, after don't find nothing useful I created next two functions.

For saving variable

ClearAll[GuardarVar];
SetAttributes[GuardarVar, HoldFirst];
GuardarVar[var_] := {
  file = StringJoin[StringDrop[NotebookFileName[],-3], ".dat"];
  If[FileExistsQ[file],
   Close[file];
   OpenAppend[file];
   WriteLine[file, 
               
StringJoin[StringDrop[StringDrop[TextString[Defer[var]], 6], -1], 
     "=", TextString[var]]]
   ,
   file = OpenWrite[file];
   WriteLine[file, 
        
StringJoin[StringDrop[StringDrop[TextString[Defer[var]], 6], -1], 
     "=", TextString[var]]]
   ]
  }
 

For load variables

CargarVars[] := {
  file = StringJoin[StringDrop[NotebookFileName[], -3], ".dat"];
  If[FileExistsQ[file],
   Close[file];
   OpenRead[file];
   vars = ReadList[file, "String"];
   For[i = 1, i < Length[vars] + 1, i++, {
     var = StringSplit[vars[[i]], "="];
     Print[ToExpression[var[[1]], StandardForm, Hold]];
     ToExpression[var[[1]], InputForm, 
      Function[name, name = ToExpression[var[[2]]], HoldAll]]}
    ]
   ,
   Print[StringJoin["No existen variables guardadas "]]
   ]
  
  }

Usage

x=5;
GuardarVar[x];

This one save each variable into a file called as your Notebook but with extention .dat, after that you can load all variables saved with

CargarVars[]
x
...

I hope it will be useful.

$\endgroup$
2

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.