Hello all,
I am writing a Monte Carlo simulation and trying to use ray to the best of my abilities, which is not much.
Basically, I wanted to have a DataHolder class which is basically a wrapper for a huge numpy array with some neat tricks. I also made a DataProcessor class which, ideally, would view of the numpy array in DataHolder, would randomly pick a sub-array, process it and call DataHolder to write it down in master array. I was thinking that this approach allows me to use a read-only view of shared arrays between process and efficiently use my cores.
What I was able to do is produce major memory leak that crashes the program. Here is a stripped down minimal working example:
import time
import ray
import numpy as npCPU = 8
if not ray.is_initialized():
ray.init(num_cpus=CPU)@ray.remote
class DataHolder:big_array = None def __init__(self): self.big_array = np.zeros ((10,10)) def get_big_array (self): return self.big_array def update_big_array (self, ar): # somehow i know this goes to (2,2) self.big_array[2:5,2:5] = ar.copy()
@ray.remote
class DataProcessor:def __init__(self, data_holder): self.data_holder = data_holder self.big_array = ray.get (data_holder.get_big_array.remote ()) def work (self, steps = 1000000): for n in range (steps): # Q: pick a number, any number! A: (2,2) ar = self.big_array[2:5,2:5] br = np.copy (ar) br = self.process_subarray(br) self.data_holder.update_big_array.remote (ray.put (br)) def process_subarray (self, sub_ar): return np.cumsum (sub_ar, axis = 0)
dh = DataHolder.remote ()
dps = [DataProcessor.remote (dh) for _ in range (CPU)]
time.sleep (5)
results = [dps[i].work.remote () for i in range (CPU)]
ray.get (results)
What happens is that on Ubuntu 20.04, python 3.7.9. ray 1.0.1.post1 is that data_processors are gobbling up memory until whole program fails. I tried to force gc.collect () whenever psutil.virtual_memory().percent goes over some value but no luck - does that mean I am filling memory with valid objects? If I was bombing shared memory with gazzillion of numpy views or arrays, it certantly doesnāt show in ray dashboard. I also tried deleting ar and br at the end of each loop - no go.
So, Iām misusing ray but canāt figure out exactly how. Any pointers or text to read is welcomed!