tensorshare

command
v0.6.21 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 4, 2026 License: MIT Imports: 15 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

tensorshare

Zero-copy tensor handoff between two Go processes via IOSurface.

The parent allocates a float32 IOSurface, fills it, and spawns this same binary as a consumer. The consumer maps the surface, and verifies the checksum — no bytes cross a pipe or socket. The parent then mutates one element in place and the consumer observes the new value, proving both processes see the same physical pages. Finally both routes are benchmarked: reading the shared mapping in place versus streaming the same buffer through a pipe.

The surface handoff itself is a mach port-right descriptor, the production route: the consumer registers a receive right under a bootstrap name, the producer sends the port from IOSurfaceCreateMachPort in a mach message with MoveSend, and the consumer recovers the surface via IOSurfaceLookupFromMachPort — all through x/mach (ports, messages, bootstrap). -transport=global keeps the original deprecated IOSurfaceIsGlobal/ID-lookup path for comparison.

go run ./examples/iosurface/tensorshare

Measured on an arm64 Mac (macOS 26.x, go1.26.3), 64 MiB tensor, best of 5:

bench: mapped in-place read      15.04 GiB/s  (64 MiB in 4.154ms)
bench: pipe stream + read         2.65 GiB/s  (64 MiB in 23.562ms)

Both arms run the identical scalar float32 sum, so the ~5x gap is the copy and syscall cost of the pipe, not a compute difference. Both transports read the mapping at the same rate — the handoff mechanism changes setup cost only. On a loaded machine raise -reps so best-of finds quiet windows, and compare arms at equal reps.

Seeing the shared mapping

go run ./examples/iosurface/tensorshare -hold

pauses after verification and prints vmmap invocations for both live processes. Each shows the surface as an IOSurface region with SM=SHM — the same kernel-shared memory mapped into two address spaces.

Notes

  • The port right crosses with MoveSend, so the producer has nothing to balance after the send — the right lives in the message, then in the consumer. Getting the disposition wrong leaks a right or produces a dead name; x/mach's tests assert both polarities.
  • bootstrap_register is deprecated but remains the only route for an unprivileged process to publish a port at runtime; the rendezvous name is PID-scoped to avoid collisions between concurrent runs.
  • With -transport=global the surface is created IOSurfaceIsGlobal (also deprecated) and found by bare ID — kept as the plumbing-free comparison arm.
  • Locking discipline: the producer takes the full lock around writes; the consumer takes kIOSurfaceLockReadOnly around reads. Skipping the lock can read stale data on surfaces that ever touch the GPU.
  • A subtle aliasing hazard this demo hit during development: a Go slice over the surface base address aliases the shared pages, so "save the old value, then mutate" must read the value before the in-place write — the slice does not snapshot.

Documentation

Overview

Command tensorshare demonstrates zero-copy tensor handoff between two Go processes via IOSurface.

The parent process allocates a float32 IOSurface, fills it with a deterministic pattern, and spawns this same binary as a consumer child. The child finds the surface with IOSurfaceLookup, maps it, and verifies the checksum without a single byte being copied between the processes. The parent then mutates one element in place and the child observes the new value on re-read — the same physical pages, mapped twice.

It then benchmarks the handoff against the conventional route: streaming the same buffer through a pipe.

go run ./examples/iosurface/tensorshare
go run ./examples/iosurface/tensorshare -floats $((64<<20)) -hold

With -hold, both processes pause after verification and print the vmmap invocations that show the shared region mapped into each address space.

By default the surface's mach port crosses to the child as a port-right descriptor in a mach message (IOSurfaceCreateMachPort → x/mach Send with MoveSend → IOSurfaceLookupFromMachPort), rendezvousing over a bootstrap name — the production route. -transport=global keeps the original deprecated IOSurfaceIsGlobal/IOSurfaceLookup path for comparison.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL