Skip to content
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

Defining inside main() in multiprocess will report error #211

Closed
hainingpan opened this issue Aug 27, 2019 · 3 comments
Closed

Defining inside main() in multiprocess will report error #211

hainingpan opened this issue Aug 27, 2019 · 3 comments
Labels

Comments

@hainingpan
Copy link

Can I define the function inside main() when using MPIPoolExecutor()?
Here is a MWE:

import adaptive
import numpy as np
from mpi4py.futures import MPIPoolExecutor


def main():    
    offsets=np.linspace(0,1,2);
    def func(xy,offsets=offsets):
        x,y=xy
        return np.sin(-np.array([(x-offset)**2+(y-offset)**2 for offset in offsets]))
        
    loss=adaptive.learner.learner2D.resolution_loss_function(min_distance=0.01,max_distance=1)
    learner=adaptive.Learner2D(func,bounds=[(-1,1),(-1,1)],loss_per_triangle=loss)
    runner=adaptive.BlockingRunner(learner,goal=lambda l:l.loss()<0.01,executor=MPIPoolExecutor(),shutdown_executor=True)
    

if __name__=="__main__":
	main()                

I used mpiexec -n 4 python -m mpi4py.futures MWE.py to run the code. But it always prints errors like:

AttributeError: Can't pickle local object 'main.<locals>.func'

Since lambda function cannot be pickled 206 ,can I define a function that has parameters which cannot be determined in adavnce? I mean, can I def a func(xy, parameters) outside main() and call with func2=lambda xy: func(xy,parameters=para) inside main(), here para cannot be determined before running?

@hainingpan
Copy link
Author

Well, I found this.
It says def func() must be on the top level in order to be pickled.
But then the problem will be if the parameter in func(xy, parameters) cannot be determined before, how can I pass the parameter into func and is it possible to use adaptive with multiprocessing?

@basnijholt
Copy link
Member

basnijholt commented Aug 27, 2019

You can use functools.partial(func, parameters=...) like in the tutorial here.

That should be pickleble.

@hainingpan
Copy link
Author

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants