我正在运行一个使用 2 个并行组的 OpenMDAO 代码。我已经在虚拟 python 环境中安装了 PETSc4py 和 mpi4py。运行代码时出现以下错误。错误如下:“内存不足。分配:0,由进程使用:236814336”这是完整的错误消息:
File "PETSc/Scatter.pyx", line 42, in petsc4py.PETSc.Scatter.create
petsc4py.PETSc.Error: error code 55
[1] VecScatterCreate() line 282 in /tmp/pycharm-packaging/petsc/src/vec/vscat/interface/vscreate.c
[1] VecScatterSetUp() line 211 in /tmp/pycharm-packaging/petsc/src/vec/vscat/interface/vscatfce.c
[1] VecScatterSetUp_MPI1() line 2543 in /tmp/pycharm-packaging/petsc/src/vec/vscat/impls/mpi1/vpscat_mpi1.c
[1] VecScatterSetUp_vectype_private() line 865 in /tmp/pycharm-packaging/petsc/src/vec/vscat/impls/vscat.c
[1] VecScatterCreate_PtoP() line 746 in /tmp/pycharm-packaging/petsc/src/vec/vscat/impls/vscat.c
[1] VecScatterCreateLocal_PtoP_MPI1() line 2436 in /tmp/pycharm-packaging/petsc/src/vec/vscat/impls/mpi1/vpscat_mpi1.c
[1] PetscMallocA() line 390 in /tmp/pycharm-packaging/petsc/src/sys/memory/mal.c
[1] VecScatterCreateLocal_PtoP_MPI1() line 2436 in /tmp/pycharm-packaging/petsc/src/vec/vscat/impls/mpi1/vpscat_mpi1.c
[1] Out of memory. Allocated: 0, Used by process: 237649920
[1] Memory requested 18446744062962991104
-------------------------------------------------------
Primary job terminated normally, but 1 process returned
a non-zero exit code.. Per user-direction, the job has been aborted.
-------------------------------------------------------
--------------------------------------------------------------------------
mpirun detected that one or more processes exited with non-zero status, thus causing
the job to be terminated. The first process to do so was:
Process name: [[43240,1],1]
Exit code: 1
--------------------------------------------------------------------------
我使用以下代码调用该过程:
mpirun -np 2 python ./parallel_processing.py
下面是 IDF 优化的代码:
from __future__ import print_function
import pickle
import numpy as np
import pandas as pd
import matplotlib.pylab as plt
from openmdao.api import Problem, ScipyOptimizeDriver, SqliteRecorder
import time
import random
from openmdao.recorders.case_reader import CaseReader
from ssbj_vanaret_mda import SsbjMda
from ssbj_vanaret_idf2_mda import SsbjIdf2Mda
def idf_run2(nx, ny):
# make a counter for discipline calls
[str_count, aer_count, pro_count] = np.zeros(3)
a = ["str_count.p", "aer_count.p", "pro_count.p"]
for i in a:
with open(i, "wb") as f:
pickle.dump(0, f)
# Initialize an MDA to generate the starting point for IDF
prob_init = Problem() # initialize the optimization problem
prob_init.model = SsbjMda(nx_input=nx) # create the MDA
# Design variables
prob_init.model.add_design_var('z', lower=np.zeros(nx), upper=np.ones(nx))
prob_init.model.add_design_var('x1', lower=np.zeros(nx), upper=np.ones(nx))
prob_init.model.add_design_var('x2', lower=np.zeros(nx), upper=np.ones(nx))
prob_init.model.add_design_var('x3', lower=np.zeros(nx), upper=np.ones(nx))
# Objective function
prob_init.model.add_objective('range')
# Constraints
for i in range(nx):
prob_init.model.add_constraint('con_g1' + str(i + 1), upper=0)
prob_init.model.add_constraint('con_g2' + str(i + 1), upper=0)
prob_init.model.add_constraint('con_g3' + str(i + 1), upper=0)
prob_init.driver = ScipyOptimizeDriver(optimizer='SLSQP')
prob_init.driver.options['maxiter'] = 0
prob_init.setup(mode='fwd')
prob_init.set_solver_print(1)
prob_init.run_driver()
prob_init.cleanup()
y12_initial = prob_init['y12']
y23_initial = prob_init['y23']
y32_initial = prob_init['y32']
y21_initial = prob_init['y21']
y31_initial = prob_init['y31']
# : initialize MDA for IDF
prob = Problem()
prob.model = SsbjIdf2Mda(nx, ny, y12_initial, y23_initial, y32_initial, y21_initial, y31_initial)
# create the MDA
# Design variables
prob.model.add_design_var('z', lower=np.zeros(nx), upper=np.ones(nx)) # shared variables
prob.model.add_design_var('x1', lower=np.zeros(nx), upper=np.ones(nx)) # local variable for structural discipline
prob.model.add_design_var('x2', lower=np.zeros(nx), upper=np.ones(nx)) # local variable for aerodynamic discipline
prob.model.add_design_var('x3', lower=np.zeros(nx), upper=np.ones(nx)) # local variable for propulsion discipline
# # coupling variables
prob.model.add_design_var('y31')
prob.model.add_design_var('y12')
prob.model.add_design_var('y32')
prob.model.add_design_var('y23')
prob.model.add_design_var('y21')
# Objective function
prob.model.add_objective('obj')
# Constraints
for i in range(nx):
prob.model.add_constraint('con_g1' + str(i + 1), upper=0)
prob.model.add_constraint('con_g2' + str(i + 1), upper=0)
prob.model.add_constraint('con_g3' + str(i + 1), upper=0)
epsilon = 1e-9
# Coupling constraints
for i in range(ny):
prob.model.add_constraint('con_y12' + str(i + 1), upper=epsilon)
prob.model.add_constraint('con_y21' + str(i + 1), upper=epsilon)
prob.model.add_constraint('con_y23' + str(i + 1), upper=epsilon)
prob.model.add_constraint('con_y32' + str(i + 1), upper=epsilon)
prob.model.add_constraint('con_y31' + str(i + 1), upper=epsilon)
# Optimizer options
prob.driver = ScipyOptimizeDriver()
prob.set_solver_print(2)
prob.driver.options['optimizer'] = 'SLSQP'
for tol in [1e-3]:
prob.driver.options['maxiter'] = random.randint(40, 50)
prob.driver.options['tol'] = tol
prob.driver.add_recorder(SqliteRecorder("cases_idf.sql"))
# Run optimization
start_time = time.time()
prob.setup(mode='fwd')
# view_model(prob, outfile='n2_mdfgs.html', show_browser=True)
prob.run_driver()
prob.run_model()
# prob.check_partials()
prob.cleanup()
end_time = time.time()
total_time = end_time - start_time
if prob.driver.options['tol'] == 1e-6:
iters = len(CaseReader('cases_idf.sql').get_cases())
cr = CaseReader('cases_idf.sql')
case_ids = cr.get_cases()
obj_list = ['obj']
z = []
[z.append(case.get_objectives(case)[obj_list[0]]) for case in case_ids]
with open("df_idf.p", "rb") as f:
df_idf = pickle.load(f).append(pd.DataFrame({'total iterations[IDF]': [iters],
'total time[IDF]': [total_time],
'final_objective[IDF]': z[-1]}))
with open("df_idf.p", "wb") as f:
pickle.dump(df_idf, f)
elif prob.driver.options['tol'] == 1e-3:
iters = len(CaseReader('cases_idf.sql').get_cases())
cr = CaseReader('cases_idf.sql')
case_ids = cr.get_cases()
obj_list = ['obj']
z = []
a = ["str_count.p", "aer_count.p", "pro_count.p"]
k = []
for i in a:
with open(i, "rb") as f:
k.append(pickle.load(f))
[z.append(case.get_objectives(case)[obj_list[0]]) for case in case_ids]
with open("df_idf_tol.p", "rb") as f:
df_idf = pickle.load(f).append(pd.DataFrame({'10.total iterations[IDF_tol]': [iters],
'11.total time[IDF_tol]': [total_time],
'12.final_objective[IDF_tol]': z[-1],
'13.str_count[IDF_tol]': k[0],
'14.aer_count[IDF_tol]': k[1],
'15.pro_count[IDF_tol]': k[2]
}))
with open("df_idf_tol.p", "wb") as f:
pickle.dump(df_idf, f)
IDF 优化的 MDA 在这里:
from openmdao.api import Group
import numpy as np
from openmdao.api import IndepVarComp, ExecComp, ParallelGroup
from ssbj_vanaret_discipline import StructureDisc
from ssbj_vanaret_discipline import AerodynamicsDisc
from ssbj_vanaret_discipline import PropulsionDisc
from ssbj_vanaret_discipline import PerformanceDisc
class SsbjIdf2Mda(Group):
"""
Analysis for IDF formulation where couplings are managed as additional constraints
on input/output variables of related disciplines.
"""
def __init__(self, nx_input, ny_input, y12_initial, y23_initial, y32_initial, y21_initial, y31_initial):
super(SsbjIdf2Mda, self).__init__()
self.nx = nx_input
self.ny = ny_input
self.y12 = y12_initial
self.y23 = y23_initial
self.y32 = y32_initial
self.y31 = y31_initial
self.y21 = y21_initial
def setup(self):
# Design variables
self.add_subsystem('z_ini', IndepVarComp('z', .5 * np.ones(self.nx)), promotes=['*'])
self.add_subsystem('x1_ini', IndepVarComp('x1', .5 * np.ones(self.nx)), promotes=['*'])
self.add_subsystem('x2_ini', IndepVarComp('x2', .5 * np.ones(self.nx)), promotes=['*'])
self.add_subsystem('x3_ini', IndepVarComp('x3', .5 * np.ones(self.nx)), promotes=['*'])
# Couplings
self.add_subsystem('y31_ini', IndepVarComp('y31', self.y31), promotes=['*'])
self.add_subsystem('y12_ini', IndepVarComp('y12', self.y12), promotes=['*'])
self.add_subsystem('y32_ini', IndepVarComp('y32', self.y32), promotes=['*'])
self.add_subsystem('y23_ini', IndepVarComp('y23', self.y23), promotes=['*'])
self.add_subsystem('y21_ini', IndepVarComp('y21', self.y21), promotes=['*'])
# Disciplines
parallel = self.add_subsystem('parallel', ParallelGroup())
parallel.add_subsystem('Structure', StructureDisc())
parallel.add_subsystem('Aerodynamics', AerodynamicsDisc())
self.add_subsystem('Propulsion', PropulsionDisc())
self.add_subsystem('Performance', PerformanceDisc())
# Shared variables z
self.connect('z', 'parallel.Structure.z')
self.connect('z', 'parallel.Aerodynamics.z')
self.connect('z', 'Propulsion.z')
self.connect('z', 'Performance.z')
# Local variables
self.connect('x1', 'parallel.Structure.x1')
self.connect('x2', 'parallel.Aerodynamics.x2')
self.connect('x3', 'Propulsion.x3')
self.connect('x1', 'Performance.x1')
self.connect('x2', 'Performance.x2')
self.connect('x3', 'Performance.x3')
# Coupling variables
self.connect('y21', 'parallel.Structure.y21')
self.connect('y31', 'parallel.Structure.y31')
self.connect('y32', 'parallel.Aerodynamics.y32')
self.connect('y12', 'parallel.Aerodynamics.y12')
self.connect('y23', 'Propulsion.y23')
# Objective function
self.add_subsystem('Obj', ExecComp('obj=range'), promotes=['obj'])
# Connections
self.connect('Performance.range', 'Obj.range')
# self.connect('Propulsion.y34', 'Performance.y34')
# self.connect('Aerodynamics.y24', 'Performance.y24')
# self.connect('Structure.y14', 'Performance.y14')
self.connect('parallel.Aerodynamics.y21', 'Performance.y21')
self.connect('Propulsion.y31', 'Performance.y31')
self.connect('Propulsion.y32', 'Performance.y32')
self.connect('parallel.Structure.y12', 'Performance.y12')
self.connect('parallel.Aerodynamics.y23', 'Performance.y23')
# Coupling constraints
for i in range(self.ny):
self.add_subsystem('con_Y12' + str(i + 1),
ExecComp('con_y12' + str(i + 1) + '=(y12[' + str(i) + '] - y12k[' + str(i) + ']) ** 2',
y12=self.y12,
y12k=self.y12
),
promotes=['con_y12' + str(i + 1)])
self.connect('parallel.Structure.y12', 'con_Y12' + str(i + 1) + '.y12')
self.connect('y12', 'con_Y12' + str(i + 1) + '.y12k')
for i in range(self.ny):
self.add_subsystem('con_Y21' + str(i + 1),
ExecComp('con_y21' + str(i + 1) + '=(y21[' + str(i) + '] - y21k[' + str(i) + ']) ** 2',
y21=self.y21,
y21k=self.y21
),
promotes=['con_y21' + str(i + 1)])
self.connect('parallel.Aerodynamics.y21', 'con_Y21' + str(i + 1) + '.y21')
self.connect('y21', 'con_Y21' + str(i + 1) + '.y21k')
for i in range(self.ny):
self.add_subsystem('con_Y32' + str(i + 1),
ExecComp('con_y32' + str(i + 1) + '=(y32[' + str(i) + '] - y32k[' + str(i) + ']) ** 2',
y32=self.y32,
y32k=self.y32
),
promotes=['con_y32' + str(i + 1)])
self.connect('Propulsion.y32', 'con_Y32' + str(i + 1) + '.y32')
self.connect('y32', 'con_Y32' + str(i + 1) + '.y32k')
for i in range(self.ny):
self.add_subsystem('con_Y23' + str(i + 1),
ExecComp('con_y23' + str(i + 1) + '=(y23[' + str(i) + '] - y23k[' + str(i) + ']) ** 2',
y23=self.y23,
y23k=self.y23
),
promotes=['con_y23' + str(i + 1)])
self.connect('parallel.Aerodynamics.y23', 'con_Y23' + str(i + 1) + '.y23')
self.connect('y23', 'con_Y23' + str(i + 1) + '.y23k')
for i in range(self.ny):
self.add_subsystem('con_Y31' + str(i + 1),
ExecComp('con_y31' + str(i + 1) + '=(y31[' + str(i) + '] - y31k[' + str(i) + ']) ** 2',
y31=self.y31,
y31k=self.y31
),
promotes=['con_y31' + str(i + 1)])
self.connect('Propulsion.y31', 'con_Y31' + str(i + 1) + '.y31')
self.connect('y31', 'con_Y31' + str(i + 1) + '.y31k')
# Local constraints
for i in range(self.nx):
self.add_subsystem('con_G1' + str(i + 1),
ExecComp('con_g1' + str(i + 1) + '=g1[' + str(i) + ']', g1=np.zeros(self.nx)),
promotes=['con_g1' + str(i + 1)])
self.connect('parallel.Structure.g1', 'con_G1' + str(i + 1) + '.g1')
for i in range(self.nx):
self.add_subsystem('con_G2' + str(i + 1),
ExecComp('con_g2' + str(i + 1) + '=g2[' + str(i) + ']', g2=np.zeros(self.nx)),
promotes=['con_g2' + str(i + 1)])
self.connect('parallel.Aerodynamics.g2', 'con_G2' + str(i + 1) + '.g2')
for i in range(self.nx):
self.add_subsystem('con_G3' + str(i + 1),
ExecComp('con_g3' + str(i + 1) + '=g3[' + str(i) + ']', g3=np.zeros(self.nx)),
promotes=['con_g3' + str(i + 1)])
self.connect('Propulsion.g3', 'con_G3' + str(i + 1) + '.g3')