[Python] mpi4py: only rank 0 participating in scipy minimize after first iteration

Discussão em 'Python' iniciado por Stack, Junho 3, 2025.

  1. Stack

    Stack Membro Participativo

    Given the code below, I am unable to remedy the fact that only rank 0 participates in evaluations of Objective after the first iteration of the (SciPy) minimizer. Obviously the rank!=0 workers finish their initial tasking and the minimizer does not reengage these workers. My question is how can I reengage them?

    from scipy.optimize import minimize
    from mpi4py import MPI
    import numpy as np

    class Solver():
    def __init__(self, SampleTimes, InitialArray):
    self.comm = MPI.COMM_WORLD
    self.rank = self.comm.Get_rank()
    self.size = self.comm.Get_size()

    self.SampleTimes = SampleTimes
    self.InitialArray = InitialArray

    self.Max = None

    def f(x_):
    return <some scalar result>

    def Objective(self, x):

    print(f"Entering Objective on rank {self.rank}")
    self.x = self.comm.bcast(x if self.rank == 0 else None, root=0)

    tstep_select = np.array_split(self.SampleTimes, self.size)[self.rank]

    local_results = []
    for t in tstep_select:
    result = self.f(self.x) # Some function of x
    local_results.append( (t, result) )

    all_results = self.comm.gather(local_results, root=0)

    if self.rank==0:
    all_results = [item for sublist in all_results for item in sublist]
    all_results = np.array(all_results)
    all_results = all_results[all_results[:,0].argsort()]

    scalar = np.trapz(all_results[:,1], all_results[:,0])

    return -scalar

    def Maximize(self,):

    if self.rank == 0:
    self.Max = minimize(self.Objective, self.InitialArray)
    self.Max = self.comm.bcast(self.Max if self.rank==0 else None, root=0)

    else:
    while not self.Max:
    self.Objective(None)


    if __name__=='__main__':
    t_eval = np.linspace(0, 100, 100)
    Instance = Solver(SampleTimes=t_eval)
    print(Instance.Maximize())

    Continue reading...

Compartilhe esta Página