Код: Выделить всё
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)
Код: Выделить всё
############################## (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) #################
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 WARNING worker.py:1779 -- WARNING: 12 PYTHON workers have been started. This could be a result of using a large number of actors, or it could be a consequence of using nested tasks (see https://github.com/ray-project/ray/issues/3644) for some a discussion of workarounds.Без использования лучей:
Модель 10 x -> do_step 0,11 [с]
С лучом (1):
10 x Модель -> do_step 0,22 [с]
Более того, каждый раз Я создаю актера с помощью метода (1), он делает копию всех global_objects для импортированных библиотек, и потребление оперативной памяти становится сумасшедшим. Мне нужно провести симуляцию с более чем 100 тыс.+ объектов Model .
В общем, я не понял, хорошая ли идея в ray — создавать много актеров или нет.< /п>
Подробнее здесь: https://stackoverflow.com/questions/647 ... tor-python
Мобильная версия