Reputation: 2305
Fairly new to ray, so please be patient. I created the following example to test the time difference between the ray and non-ray implementation of simple image arithmetics. Why is the following code not getting executed in parallel? I can see on the dashboard everything is loaded to one worker only
import ray
import cv2
import numpy as np
import time
ray.init(ignore_reinit_error=True)
@ray.remote
class image_import(object):
def __init__(self):
pass
def color(self):
return cv2.imread("frame30.png", flags=0)
def black(self):
return cv2.imread("frame30.png", flags=0)
def green(self):
return cv2.imread("frame30.png", flags=0)
def yellow(self):
return cv2.imread("frame30.png", flags=0)
def blue(self):
return cv2.imread("frame30.png", flags=0)
def orange(self):
return cv2.imread("frame30.png", flags=0)
def multiply_matrices_1(self,clr,b_1):
return np.dot(clr,b_1)
def multiply_matrices_2(self,b_2,b_3):
return np.dot(b_2,b_3)
def multiply_matrices_3(self,b_4,b_5):
return np.dot(b_4,b_5)
def multi_1(self,val_1,val_2):
return np.dot(val_1,val_2)
def multi_2(self,val_2):
return np.dot(val_2,val_2)
def multi_3(self,val_4,val_5):
return np.dot(val_4,val_5)
start = time.time()
counter_actor = image_import.remote()
clr= counter_actor.color.remote()
b_1= counter_actor.black.remote()
b_2= counter_actor.green.remote()
b_3= counter_actor.yellow.remote()
b_4= counter_actor.blue.remote()
b_5= counter_actor.orange.remote()
multi_1= counter_actor.multiply_matrices_1.remote(clr,b_1)
multi_2= counter_actor.multiply_matrices_2.remote(b_2,b_3)
multi_3= counter_actor.multiply_matrices_3.remote(b_3,b_4)
multi_4= counter_actor.multi_1.remote(multi_1,multi_2)
multi_5= counter_actor.multi_2.remote(multi_3)
addition= counter_actor.multi_3.remote(multi_4,multi_5)
results= ray.get(addition)
duration = time.time() - start
print(duration)
Upvotes: 1
Views: 740
Reputation: 261
You are creating an actor (a remote object of your class), and actor tasks are executed serially:
https://docs.ray.io/en/master/walkthrough.html#calling-the-actor
For parallel execution just get rid of the class:
@ray.remote
def color(self):
return cv2.imread("frame30.png", flags=0)
@ray.remote
def black(self):
return cv2.imread("frame30.png", flags=0)
futures = [color.remote(), black.remote()] # Will execute in parallel
ray.get(futures)
Upvotes: 4