Hi, I followed the tip and created actors to test this tip. However, I found there is no speed up for actors. Here is the code
import time
import logging
import ray
import numpy as np
logging.basicConfig(level=logging.INFO)
@ray.remote
class Runner(object):
def run(self):
pass
def get_global_t_(self):
time.sleep(1)
return 1
ray.init(num_cpus=4)
runners = [Runner.options(name=f'Runner_{i}').remote() for i in range(4)]
st = time.time()
results = [r.get_global_t_.remote() for r in runners]
results = ray.get(results)
t_cost = time.time() - st
logging.info(f"results: {results}, time cost: {t_cost}")
ray.shutdown()
and the output:
$ python tip.py
2022-06-08 19:00:37,471 INFO services.py:1462 -- View the Ray dashboard at http://127.0.0.1:8265
INFO:root:results: [1, 1, 1, 1], time cost: 4.214106321334839
The expected duration is 1 second, however, it costs ~4 seconds. Does this tip apply to actors?
@GoingMyWay the reason is that actor need some time to be created. So for your case, the extra 3s probably are spent on creating the actors.
Here is my updated code for your reference:
import time
import logging
import ray
import numpy as np
logging.basicConfig(level=logging.INFO)
@ray.remote
class Runner(object):
def ready(self):
pass
def run(self):
pass
def get_global_t_(self):
time.sleep(1)
return 1
ray.init(num_cpus=4)
runners = [Runner.options(name=f'Runner_{i}').remote() for i in range(4)]
ray.get([r.ready.remote() for r in runners])
st = time.time()
results = [r.get_global_t_.remote() for r in runners]
results = ray.get(results)
t_cost = time.time() - st
logging.info(f"results: {results}, time cost: {t_cost}")
st = time.time()
results = [r.get_global_t_.remote() for r in runners]
results = ray.get(results)
t_cost = time.time() - st
logging.info(f"results: {results}, time cost: {t_cost}")
ray.shutdown()
And the result is:
2022-06-08 19:49:03,251 INFO services.py:1477 -- View the Ray dashboard at http://127.0.0.1:8265
INFO:root:results: [1, 1, 1, 1], time cost: 1.0063087940216064
INFO:root:results: [1, 1, 1, 1], time cost: 1.0064780712127686
Here ready is just a placeholder to make sure the actors are created successfully.