-
Notifications
You must be signed in to change notification settings - Fork 29
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
check/set multi-processing start method in patpass #347
Conversation
I don't want to default the start method to 'fork' on MacOS since it's discouraged by python. I never got the error mentioned by @simoneliuzzo, so I think we should first try and understand in which conditions this error occurs. The fact that In addition, as of python 3.10, the fork method is not available any more on MacOS… In the mean time, the workaround you found may be applied by the user if necessary, and it could be suggested in the "Changed in version 3.8: On macOS, the spawn start method is now the default. The fork start method should be considered unsafe as it can lead to crashes of the subprocess. See bpo-33725." More globally, the startup of subprocesses in python seems to evolve rapidly, so I think it would be safer not to rely on the specificity of the fork method, and have code with runs with any start method… |
Sorry, it's not so clear from the documentation, depending whether MacOS is considered Unix or not… |
I don't understand everything I must admit but python switched from fork to spawn for macos at version 3.8, since you never got the error could you check the start method you are using? For all the test I did using spawn produced errors or did not provide any speed up, so it is basically useless. I do not see any interest in providing a default behavior that does not work. It can be stated in the help that the default is fork, with the warning you mention. |
The only thing I could find (also in python 3.10 docs) is for python 3.8+ the default method switched from "fork" to "spawn" for MACOS, my feeling is that you did not get error because you were using fork |
@swhite2401: Do you have an example (simple enough) triggering the problem? I cannot reproduce it, neither on python 3.8 nor 3.9 and spawn method… |
Replacing line 50 in this version of patpass by: Using linux with python3.8 on grappa will give this error |
@swhite2401: I only have my laptop here, so I run the tests with only 2 cores. But for the hmba test lattice, 500 particles, I do get a factor ~2 improvement with MacOS 12.5, python 3.9, AT on the master branch. |
I can't give you any better without a mac computer... |
Do you also get the error on linux ? I thought it was only Windows and MacOS? |
Yes on linux forcing the "spawn" start method I get it |
I found a workaround calling
but is not an option for an at module... |
@simoneliuzzo got the error on MacOS 12.0.1 and python 3.9... not sure if this explains why you do not get it |
Instead of using a For tests, you can just call However, your Linux example is wrong, because I recall that GitHub runs a test of Again, without a example of failure of the master branch of AT, I do not want to change anything. And only @simoneliuzzo can provide that… |
Well for linux there was this global variable issue as well, this is why we decided to put this check on the OS to start with, but it is true this has to be cleaned up once we know exactly what to do |
Easy modification. if platform.startswith('linux') with if (get_start_method() == 'fork') Only 'fork' allows inheriting global variables, independent of platform. |
Why did we allow it only for linux in the first place? Was it because of this unsafe warning for MACOS? |
Also this may be a stupid question but why is spawn working for MACOS and not linux, I do not see any reason for that... |
I think we just assumed "linux <=> fork", which is the default, but is not always true! |
It doesn't, I get the following error:
and this is normal because spawn re-instanciates the full python for each process, this is why you have to guard the part the launches the multiprocessing inside I have no idea why this works on your MAC... |
Forget contexts and __main__. Make the modification in patpass, start a new python session and call set_start_method('spawn') right after importing |
Same error, context already set... |
Some news on test cases with spawn: Not sure how the git tests work... is it equivalent to 1 or 2? For sure the For information, fork method for my example is 3x faster than spawn. The initial proposal to have 'fork' as default for MAC and Linux is therefore well justified in my opinion (faster, easier). In case 'spawn' has to be used a warning should be issued such that the user can properly set his code to avoid RunTime errors. |
Yes...I don't understand what is going on... My terminal loads automatically my home pyenv (3.8) -> no error |
@simoneliuzzo: you should remove the try/except in you test: here we do not know if the set_start_method succeeded or failed. For me, it always succeeds, I never got a failure… |
@swhite2401: it indeed looks like a configuration problem. GitHub uses a fresh python install and has no problems (though we do not try using 'spawn'). Another test on grappa, still using the default python3 (I do not know anything else): grappa:~ $ python3
Python 3.8.5 (default, Jul 28 2020, 12:59:40)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import multiprocessing as mp
>>> mp.set_start_method('spawn')
>>> import at
>>> mp.get_start_method()
'spawn'
>>> Here I set the method before importing at, and it works (up to there, I did not spawn anything). But again I strongly discourage deviating from the default: many risks, no benefit ! We already lost a lot of time on that… |
|
I do not know if I can reproduce the conditions of the error immediately. If there is a code snippet to test, I am glad to run it! |
@simoneliuzzo: I never got any error with I would just be interested in a speed comparison of 'fork' and 'spawn' on Mac, on a rather long tracking, so that the overhead of spawning in small. Thanks ! |
@simoneliuzzo could you run the following?
|
For info: from what I have read online (although this is very poorly documented) using fork is unsafe only in multi-threaded programs which is not the case here, so one may claim that fork is safe for AT but this I really cannot guaranty... Benefits: never fails (unless you run into this unsafe condition), faster. This is not lost time! We are using this function extensively, it is part of an ongoing development for DA and lifetime calculation and we want it to work for all the machines we are using in all conditions without having to place expert parameters |
@lfarv, @simoneliuzzo this latest version uses the default python as suggested and fixes issues present in the previous version. I think this is ready to merge. |
At 1st glance, it looks ok. I'll look more carefully this afternoon, give me some time. |
grappa is a mystery... but it looks like it is related to some configuration issue that I do not understand |
Ah by the way if you feel uneasy about the force keyword, we can use |
I am not happy with this version:
|
Ah right, even though set_start_method(None) restore the default so it is not a problem the logic is strange I agree. Anyway I was hesitant with context, if you like it better it can do it easily |
I think that the |
Right! |
Well, again that's not documented: "method can be 'fork', 'spawn' or 'forkserver'." |
Done. Can you check if this runs fine for you, again I can only check fork...spawn still fails with this runtime error suggesting to use freeze_support() |
OK with 'spawn' and 'fork'. It works for me ! Ok for merging. |
This branch fixes a problem that was found by @simoneliuzzo when running python 3.9 and MACOS 12.0.1: python mulitprocessing uses "spawn" as default start method which causes the following Runtime error:
This error is not clear to me as
freeze_support()
is supposed to be acitve only for Windows...Nevertheless, for now I can propose the following:
-linux change nothing it is working
-MACOS: force "fork" start_method
-Windows: return a warning and run single process
lattice_pass
This may not be the best approach but it is very difficult for me to validate this as I have no way of testing either MAC or Windows execution.
Help would be most appreciated!