-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Memcpy-like duplication of a MAD-X instance #88
Comments
It is an interesting feature, @coldfix is the expert here. One approach could using fork in Linux, but I am not sure how easy is in practice. One would need to fork the python process running libmadx. Another approach would be looping over globals, elements and sequences. Then one would miss expanded sequences ( tha you can regenerate ), read and load the error definitions, PTC universe ( that can be regenerated). I am not sure if this is enough for you, but for the application I am thinking it looks reasonable. |
I think Riccardo is correct in that you get something like this using POSIX fork semantics (which makes a copy-on-write clone of the current process as far as I understand). Consider this: import os
import cpymad.libmadx as l
l.start()
l.input("option, -info;")
l.input("x = 2;")
if os.fork():
print("In parent: x =", l.eval("x"))
l.input("x = 3;")
print("In parent: x =", l.eval("x"))
else:
print("In child: x =", l.eval("x"))
l.input("x = 4;")
print("In child: x =", l.eval("x")) which outputs:
(the parent/child outputs could of course be interleaved differently, but should always show the same values) On windows, we would need to copy stuff over manually - which will most likely go wrong. In that case it's probably better to just stick to current best practice which is to put the responsibility on the user - who knows best how execute sequence definition and other setup calls and then load knob values as e.g. globals. This should be sufficiently fast for medium sized sequences. But we can definitely think about adding a |
FYI, I looked around a bit, and found there is Note that the stdout/stderr of forked MAD-X processes will be garbled up unless we also take care of setting up these streams in the forked process. But once we have setup routines for that, it might be easier to fully replace minrpc with the functionality from the |
@coldfix @rdemaria Thanks for your quick reaction. It looks like we could find a way. I'll find a way to contribute time to this during the summer ( @Oskari-Tuormaa will also join us and should be willing to contribute ). |
I've been trying my hand at implementing this for a bit, and have been initially trying to implement functionality purely in from minrpc.client import Client
svc1, proc = Client.spawn_subprocess()
mad1 = svc1.get_module("cpymad.libmadx")
mad1.start()
mad1.input("option, -info")
mad1.input("x = 2")
# Fork the first client
svc2 = Client.fork_client(svc1)
mad2 = svc2.get_module("cpymad.libmadx")
print("MAD1, x =", mad1.eval("x"))
mad1.input("x = 3")
print("MAD1, x =", mad1.eval("x"))
print("MAD2, x =", mad2.eval("x"))
mad2.input("x = 4")
print("MAD2, x =", mad2.eval("x"))
mad1.finish()
mad2.finish() Which gives the output:
I however have a couple of problems/concerns that I don't quite know how to tackle:
|
looks very promising!
|
Furthur development in the forking epic: I've tried fully replacing the pipes with sockets, by replacing the
I've also implemented a lightweight class As per usual however, I've run into a new problem: I can create and destroy many forks of the |
I would like to find a way to efficiently copy the whole MAD-X instance. So far what we've been doing is to "replay" the full command-log. This works decently when just reading sequences, however if there is a long matching this is painfully slow.
@coldfix Do you think that's feasible? Hard?
I'd be happy to contribute of course.
The text was updated successfully, but these errors were encountered: