Multiple Ray instances on one node accessing shared memory

Medium: It contributes to significant difficulty to complete my task, but I can work around it.

Setup

I have a large numpy A and one node with 20 CPUs, where each CPU has a different vector. I would like to use Ray to store the matrix A in shared memory, and have each CPU multiply its vector by the same A.

What I want to do

Steps:

  1. Have one CPU do ray.put(A) to send A to shared memory.
  2. Each CPU/worker launch a python script with Ray.
  3. Each separate python script (one per CPU/worker) access A to do matrix vector multiplication. The CPUs are all on the same node.

Question: How can I accomplish step 3?

Note

This is a continuation of “Simultaneous numpy matrix vector multiplication”. Although it has the same setup, there I was concerned about zero-copy and here I am concerned with using multiple Ray instances, hence a different question.

The recommended way to do this would be to wrap these all in one Ray job. The setup is a bit different compared to something like MPI or multiprocessing; instead of you launching python workers explicitly, Ray will automatically start workers and execute tasks for you. Also, all of the tasks and shared-memory objects created during one “application” are associated with the “driver”, aka the process that executes the main python script, so once that script exits, all of the associated tasks and objects will be cleaned up as well.

The code from the other post is a good start:

import ray

@ray.remote
def multiply(A, v):
    return A * v  # Put your worker code here.

A_ref = ray.put(A)  # Put A in Ray's shared-memory object store.
refs = [multiply.remote(A_ref, v) for v in vs]
results = ray.get(refs)

Thanks! This is super helpful.

1 Like