You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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?
The text was updated successfully, but these errors were encountered:
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?
Can I define the function inside main() when using MPIPoolExecutor()?
Here is a MWE:
I used
mpiexec -n 4 python -m mpi4py.futures MWE.py
to run the code. But it always prints errors like: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)
outsidemain()
and call withfunc2=lambda xy: func(xy,parameters=para)
insidemain()
, here para cannot be determined before running?The text was updated successfully, but these errors were encountered: