Suppose i need to find all the paths between all the pairs of nodes in an undirected graph. If the graph (represented with facts edge/2) is big (for example, composed of 100.000 facts), the computation is very expensive in terms of both memory and execution time. I would like to run this code only for few hours a day, pause the execution and then restart it later. Is there a way to save (in a file) the state of the execution and then restart the execution from this point onward? Clearly, i’m not looking for something like ctrl z in the terminal. Thanks
No. Once upon a time several Prolog systems including SWI-Prolog used to define save/1 that could be called from anywhere and restoring this state would simply continue as if the save/1 had succeeded. That was based on a library called undump. Prolog created a core dump (without stopping) of itself and used this library to turn the core dump into a new executable.
This was really nice and quite doable in the old days when operating systems and the way they handled processes was simple. Currently this really hard or impossible.
qsave_program/2 saves the state of the program. In theory we could snap the state of the Prolog VM (stacks and registers) as well and restore this. This would be a serious effort, but not terrible (couple of weeks programming I’d estimate). It would also provide some interesting options, such as migrating an ongoing computation to a different Prolog instance or forking a Prolog computation. That does come with severe limitations though, such as not allowing for recursive Prolog->C->Prolog calls, no references to external resources such as open streams and the requirement that both instances have exactly the same predicates (as far as referenced from the stack).
As is, you need your own explicit representation of the state of the search and save that. Alternatively, virtual machines can typically be suspended and resumed any time later, even on a different host.
If you are on Linux, I wonder if you can use kill -TSTP [pid] and kill -CONT [pid] to suspend and resume your long-running SWI prolog process as another alternative? https://unix.stackexchange.com/questions/2107/how-to-suspend-and-resume-processes. I guess this is no different to just suspending your job on the terminal and resuming it.
In effect your process state will be written to a file (the swap file) if memory becomes tight on your system, so it should work reasonably well in practice?