liveBook · Manning

Shared memory

As explained in Fast Python: High performance techniques for large datasets

Shared memory is a memory region or storage area accessible to multiple processes, threads, or other execution components for accessing or exchanging data.

Shared memory lets multiple objects or processes access the same stored data. The practical question is whether sharing avoids an expensive copy without making changes or coordination difficult to manage. A NumPy view shares an array’s data within a process; Arrow’s Plasma server provides a shared store for separate local processes. section 4.1.1 section 7.5.2

Check whether arrays share data

Create a flipped view with `np.flipud(image_arr)` and an independent array with `np.flipud(image_arr).copy()`. `np.shares_memory(image_arr, flipped_view)` is true; the same check for the copy is false. Changing the original array also changes what the view sees, but not the copy. Views remake metadata rather than copying raw data; in the reported size sweep, view times stayed nearly flat while copy times rose sharply. `np.may_share_memory` offers a faster guess when an exact check is impractical. section 4.1.1

Share a table across processes

Start a local Plasma store with a one-gigabyte allocation and a UNIX socket. One process reads a CSV into an Arrow table and registers it with `client.put(table, object_id)`. Another process lists stored objects and retrieves the table with `client.get(object_id)`; a later-started consumer can find it even after the producer exits. Object IDs must be 20 bytes, so the example pads a process-based name before encoding it. section 7.5.2

plasma_store -s /tmp/fast_python  -m 1000000000

The store command shows the socket path and memory allocation that the separate processes use. section 7.5.2

Do not assume shared memory is the first fix for slow multiprocessing. In a message-passing map-reduce experiment, increasing chunk size from 1 to 1,000 reduced runtime from 114.2 to 3.1 seconds. That measures chunking, not a shared-memory speedup. Naive shared-memory and low-level-lock designs are cautioned against because complex parallel communication is difficult to debug. section 3.4.4 section 3.6

How Fast Python: High performance techniques for large datasets covers it

How deep it goes
Works through NumPy sharing checks and Plasma setup, but defers naive shared-memory and low-level-lock implementations.
Examples
Uses a flipped image view and copy, plus a CSV-to-Plasma producer and separate consumers.
What it assumes
Assumes working familiarity with Python arrays and processes; supplies concrete setup and calls for the Plasma store.
What you can do after
Apply it via NumPy sharing checks and Arrow Plasma object storage while recognizing mutation and coordination trade-offs.
How it connects
Connects shared memory to NumPy views and copies, Apache Arrow, interprocess communication, and message passing.

Also explained in

Compare how 2 books explain shared memory →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Fast Python: High performance techniques for large datasets. The section links point to the parts of the book it draws on.