|
48 | 48 | # We create a `MetalContext` which connects to the default Metal GPU device, then load the pre-compiled `.metallib` shader libraries. |
49 | 49 |
|
50 | 50 | # + |
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") |
54 | 57 |
|
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 |
58 | 60 |
|
59 | | -print(f"GPU device: {ctx_1d.device_name}") |
| 61 | +print(f"GPU device: {ctx.device_name}") |
60 | 62 | # - |
61 | 63 |
|
62 | 64 | # ## 2. 1D Array Operations — Correctness |
@@ -397,9 +399,7 @@ def logstar(x, base=np.e, threshold=1.0): |
397 | 399 | return k |
398 | 400 |
|
399 | 401 |
|
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. |
403 | 403 | print(f"Compute library loaded on: {ctx_compute.device_name}") |
404 | 404 |
|
405 | 405 | # GPU Mandelbrot — single dispatch |
@@ -684,10 +684,7 @@ def nbody_step_numpy(pos_mass, velocities, dt, softening): |
684 | 684 | # - Buffer allocation/deallocation |
685 | 685 |
|
686 | 686 | # + |
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. |
691 | 688 |
|
692 | 689 |
|
693 | 690 | def diffuse_numpy(field, dt, n_steps): |
|
0 commit comments