Skip to content

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

@hainingpan

Description

@hainingpan

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions