Is it possible to clone the state of a kernel into a second kernel for long evaluations?
Here's a quick and dirty way to save kernel state. First try to get the packages your have built into the system. This is obviously an incomplete list and doesn't include new paclets or paclets you download, etc, but it's a start:
Quit
Quiet[Get /@ Contexts[]];
$contexts =
Flatten[
{
#,
# <> "Private`",
# <> "PackagePrivate`",
# <> "PackageScope`"
} & /@ Contexts[]
];
$contextStateFile =
FileNameJoin@{$TemporaryDirectory, "std_contexts.mx"};
Export[
$contextStateFile,
$contexts
];
Then use DumpSave
to dump new contexts:
$statefile =
FileNameJoin@{
$TemporaryDirectory,
"kernel_state.mx"
};
statedump[] :=
With[{
conts =
DeleteCases[Contexts[],
Alternatives @@
DeleteCases[
Replace[$contexts, {
Except[_List] :>
($contexts =
Get@FileNameJoin@{$TemporaryDirectory, "std_contexts.mx"})
}],
$Context]
]
},
DumpSave[
$statefile,
conts
]
];
Then I'll load my main package:
In[9]:= << BTools`
In[10]:= statedump[] // AbsoluteTiming
Out[10]= {0.112649, {"BTools`", "BTools`Private`Package`", "Global`",
"GridDump`", "Image`InteractiveImageDump`",
"NotebookTools`ControlsDump`", "System`Convert`DumpDump`",
"System`Convert`PackageDump`"}}
You'll notice it's pretty fast. But that's because not a lot got dumped:
In[11]:= $statefile // FileByteCount
Out[11]= 368906
If you've made more definitions it'll be slower.
Then I can quit and try reloading:
Quit
In[1]:= Get@FileNameJoin@{
$TemporaryDirectory,
"kernel_state.mx"
}
In[2]:= $contexts // Length
Out[2]= 4936
And also the old contexts were loaded:
In[3]:= DeleteCases[Contexts[],
Alternatives @@
DeleteCases[
Replace[$contexts, {
Except[_List] :>
($contexts =
Get@FileNameJoin@{$TemporaryDirectory, "std_contexts.mx"})
}],
$Context]
]
Out[3]= {"BTools`", "BTools`Private`Package`", \
"Explore`ExploreDump`", "Global`", "GridDump`", \
"Image`InteractiveImageDump`", "NotebookTools`ControlsDump`", \
"PredictionStartupDump`", "System`Convert`DumpDump`", \
"System`Convert`PackageDump`"}
This won't properly cache all state but it's a start. An alternative is to define set of contexts to store, too.