Skip to content

Commit d926638

Browse files
larsgebclaude
andcommitted
demo.py: consolidate to single MetalContext to fix CI SIGSEGV
The CI paravirtual GPU crashes (exit -11) when four or more concurrent MTLCommandQueue objects exist. demo.py previously created four separate MetalContext objects (ctx_1d, ctx_2d, ctx_compute, ctx_diffuse), each wrapping its own queue. Fix: create one ctx at notebook start, load all four metallibs on it, then assign ctx_1d = ctx_2d = ctx_compute = ctx_diffuse = ctx so every downstream cell uses the same queue. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 42c890c commit d926638

1 file changed

Lines changed: 11 additions & 14 deletions

File tree

demo.py

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,17 @@
4848
# We create a `MetalContext` which connects to the default Metal GPU device, then load the pre-compiled `.metallib` shader libraries.
4949

5050
# +
51-
# Context for 1D operations (add, multiply, saxpy, central_difference)
52-
ctx_1d = metal.MetalContext()
53-
ctx_1d.load_library("build/02-GeneralArrayOperations/ops.metallib")
51+
# Single context — load all metallibs upfront so we never create more than one
52+
# MTLCommandQueue (the CI paravirtual GPU crashes with ≥4 concurrent contexts).
53+
ctx = metal.MetalContext()
54+
ctx.load_library("build/02-GeneralArrayOperations/ops.metallib")
55+
ctx.load_library("build/03-2DKernels/ops.metallib")
56+
ctx.load_library("build/04-Compute/ops.metallib")
5457

55-
# Context for 2D operations (laplacian2d, laplacian2d9p, quadratic2d)
56-
ctx_2d = metal.MetalContext()
57-
ctx_2d.load_library("build/03-2DKernels/ops.metallib")
58+
# Aliases used in the cells below (kept for readability of each section)
59+
ctx_1d = ctx_2d = ctx_compute = ctx_diffuse = ctx
5860

59-
print(f"GPU device: {ctx_1d.device_name}")
61+
print(f"GPU device: {ctx.device_name}")
6062
# -
6163

6264
# ## 2. 1D Array Operations — Correctness
@@ -397,9 +399,7 @@ def logstar(x, base=np.e, threshold=1.0):
397399
return k
398400

399401

400-
# Load compute-heavy kernels
401-
ctx_compute = metal.MetalContext()
402-
ctx_compute.load_library("build/04-Compute/ops.metallib")
402+
# ctx_compute already aliased to ctx above.
403403
print(f"Compute library loaded on: {ctx_compute.device_name}")
404404

405405
# GPU Mandelbrot — single dispatch
@@ -684,10 +684,7 @@ def nbody_step_numpy(pos_mass, velocities, dt, softening):
684684
# - Buffer allocation/deallocation
685685

686686
# +
687-
# Need both 1D (saxpy) and 2D (laplacian2d) kernels for diffuse_steps
688-
ctx_diffuse = metal.MetalContext()
689-
ctx_diffuse.load_library("build/02-GeneralArrayOperations/ops.metallib")
690-
ctx_diffuse.load_library("build/03-2DKernels/ops.metallib")
687+
# ctx_diffuse already aliased to ctx above.
691688

692689

693690
def diffuse_numpy(field, dt, n_steps):

0 commit comments

Comments
 (0)