我是 ray 新手,我正在尝试并行化我开发的模拟器。这是我的模拟器的示例,显然它更复杂。
import some_library
import sim_library_with_global_object
class Model(object):
def __init__(self,init_vals):
#initialize object using some of the global_object from sim_library.
#the Model object have it's own variables not global
def do_step(self,time):
#calculate Model step using the global_object from sim_library
#edit the Model variables with respect to the step
class ManyModel(object):
def init(self):
self.models=[]
def add_model(self,init_vals):
model = Model(init_vals)
self.model.append(model)
def step(self,time):
for model in self.models:
model.do_step(time)
def get_data_step(self):
data=[]
for model in self.models:
data.append(model.myvalues)
return data
sim=ManyModel()
inits=[] #####list of init_vals
times=[] ####list of times to simulate
for init in intis:
sim.add_model(init)
for time in times:
sim.step(time)
step_data=sim.get_data_step()
到目前为止,我已经尝试在 Model
类 (1) 和 ManyModel
类上使用带有装饰器 @ray.remote
的 ray (2)这两种方式:
(1)
############################## (1) ###############
import some_library
import sim_library_with_global_object
@ray.remote
class Model(object):
def __init__(self,init_vals):
#initialize object using some of the global_object from sim_library.
#the Model object have it's own variables not global
def do_step(self,time):
#calculate Model step using the global_object from sim_library
#edit the Model variables with respect to the step
class ManyModel(object):
def init(self):
self.models=[]
def add_model(self,init_vals):
model = Model.remote(init_vals)
self.model.append(model)
def step(self,time):
futures=[]
for model in self.models:
futures.append(model.do_step.remote(time))
return futures
def get_data_step(self,futures):
data=[]
while len(futures)>0:
ready, not_ready = ray.wait(ids)
results=ray.get(ready)
data.append(results)
return data
ray.init()
sim=ManyModel()
inits=[] #####list of init_vals
times=[] ####list of times to simulate
for init in intis:
sim.add_model(init)
for time in times:
sim.step(time)
step_data=sim.get_data_step()
和(2)
########################## (2) #################
import some_library
import sim_library_with_global_object
class Model(object):
def __init__(self,init_vals):
#initialize object using some of the global_object from sim_library.
#the Model object have it's own variables not global
def do_step(self,time):
#calculate Model step using the global_object from sim_library
#edit the Model variables with respect to the step
@ray.remote
class ManyModel(object):
def init(self):
self.models=[]
self.data=[]
def add_model(self,init_vals):
model = Model(init_vals)
self.model.append(model)
def step(self,time):
for model in self.models:
model.do_step(time)
def get_data_step(self):
self.data=[]
for model in self.models:
self.data.append(model.myvalues)
return self.data
ray.init()
sim=ManyModel.remote()
inits=[] #####list of init_vals
times=[] ####list of times to simulate
for init in intis:
sim.add_model.remote(init)
for time in times:
sim.step.remote(time)
future=sim.get_data_step.remote()
step_data=ray.get(future)
无论哪种方式,我都没有从使用光线库中获得任何好处。你能帮我使用吗?
方法(1)的更新 第一种方法的问题是我收到此警告消息
2020-11-09 11:33:20,517 警告worker.py:1779 -- 警告:12 个 PYTHON 工作线程已启动。这可能是使用大量参与者的结果,也可能是使用嵌套任务的结果(请参阅 https://github.com/ray-project/ray/issues/3644)以获取一些解决方法的讨论。
对于 10 x 模型
,这是性能结果:
不使用射线:
10 x 模型
-> do_step
0.11 [s]
对于射线 (1):
10 x 模型
-> do_step
0.22 [s]
此外,每次我使用方法 (1) 创建一个 Actor 时,它都会为导入的库创建所有 global_objects 的副本,并且内存消耗会变得疯狂。我需要使用超过 100k+ 个模型
对象进行午餐模拟。
总的来说,我不明白在 ray 中创建许多 actor 是否是一个好主意。
最佳答案
放大一些核心元素
ray.init()
sim=ManyModel.remote()
for time in times:
sim.step.remote(time)
future=sim.get_data_step.remote()
step_data=ray.get(future)
最重要的一点是,您只创建一个 Ray actor(在 sim=ManyModel.remote()
行中)。 Ray actor 按顺序执行提交给它们的任务(默认情况下),因此创建一个 actor 不会创造任何并行机会。要获得 Ray Actor 的并行性,您需要创建并使用多个 Actor。
第二点是您在 for 循环内部调用 ray.get
。这意味着在 for 循环的每次迭代中,您都会提交一个任务,然后调用 ray.get 等待任务完成并检索结果。相反,您需要提交多个任务(可能在循环内),然后在循环外调用 ray.get。
关于python - 使用ray并行化模拟器python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64738368/