Skip to content

Release the GIL in Python-entry-point Numba kernels - #1755

Merged
cmdupuis3 merged 8 commits into
UXARRAY:mainfrom
cmdupuis3:cmd/nogil
Sep 23, 2026
Merged

cmdupuis3 merged 8 commits into
UXARRAY:mainfrom
cmdupuis3:cmd/nogil

Conversation

@cmdupuis3

@cmdupuis3 cmdupuis3 commented Sep 11, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #1756

Overview

Only 2 of the 140 @njit sites in the package passed nogil=True (grid/connectivity.py, grid/area.py). Every other compiled kernel held the GIL for its whole execution, so calling them from dask's threaded scheduler as dask="parallelized" does serialized them completely, regardless of how many workers were configured.

Adds nogil=True to the 20 or so remaining top-level sites making @njit(cache=True, nogil=True) the default pattern. These are all nopython, allocation-light leaf kernels with no object-mode fallback.

Measured on _construct_edge_node_distances (2M edges, best of 3), the same kernel jitted with and without nogil:

threads     GIL held        nogil   speedup
      1        95.0ms        95.3ms    1.00x
      2       191.5ms       101.1ms    1.89x
      4       381.0ms       104.1ms    3.66x
      8       762.8ms       115.1ms    6.63x

The GIL-held column scales linearly with thread count -- that is the signature of zero parallelism.

Two sites are deliberately left alone:

  • the guvectorize kernel in grid/neighbors.py -- numba rejects nogil as a @guvectorize option, and the gufunc machinery already drops the GIL around the loop.
  • the 15 parallel=True kernels keep parallel=True for now. Nested under dask's threadpool it oversubscribes; dropping it belongs with the work that gives dask the chunk loop.

_build_node_edge_connectivity in grid/connectivity.py is also the one kernel still missing cache=True; left as-is to keep this diff to nogil.

The nogil_scaling.GILScaling.track_gil_scaling is added to prove that nogil is working as expected.

PR Checklist

General

  • An issue is created and linked
  • Added appropriate labels (if your uxarray repo permissions allow it)
  • Filled out Overview and Expected Usage (if applicable) sections

Testing & Benchmarking

  • If this PR could affect performance, ran ASV benchmarks and confirmed they show expected behavior (add a new benchmark if necessary)

AI Disclosure

AI Usage: Claude Opus 5

  • I have tested and take responsibility for all AI-generated content in my PR.

@cmdupuis3 cmdupuis3 added run-benchmark Run ASV benchmark workflow scalability Related to scalability & performance efforts labels Sep 11, 2026
@cmdupuis3 cmdupuis3 self-assigned this Sep 11, 2026
@github-actions

github-actions Bot commented Sep 11, 2026 •

Copy link
Copy Markdown

ASV Benchmarking

Benchmark Comparison Results

Benchmarks that have improved:

Change Before [44a7414] After [0e066aa] Ratio Benchmark (Parameter)
- 354M 283M 0.8 import.Imports.track_peakmem_import_uxarray
- 354M 320M 0.9 mpas_ocean.GradientColdStartRss.track_peakmem_gradient('480km')
- 1.9966815809447649 1.0300432788505798 0.52 nogil_scaling.GILScaling.track_gil_scaling

Benchmarks that have stayed the same:

Change Before [44a7414] After [0e066aa] Ratio Benchmark (Parameter)
5.37±0.03ms 5.41±0.09ms 1.01 bench_connectivity.Connectivity.time_edge_face('120km')
2.00±0.02ms 1.97±0.01ms 0.99 bench_connectivity.Connectivity.time_edge_face('480km')
4.37±0.08ms 4.38±0.05ms 1.00 bench_connectivity.Connectivity.time_edge_node('120km')
1.57±0.02ms 1.54±0.01ms 0.98 bench_connectivity.Connectivity.time_edge_node('480km')
4.43±0.05ms 4.37±0.06ms 0.99 bench_connectivity.Connectivity.time_face_edge('120km')
1.60±0.02ms 1.55±0.01ms 0.97 bench_connectivity.Connectivity.time_face_edge('480km')
6.29±0.1ms 6.27±0.07ms 1.00 bench_connectivity.Connectivity.time_face_face('120km')
2.35±0.03ms 2.32±0.02ms 0.99 bench_connectivity.Connectivity.time_face_face('480km')
53.8±0.9μs 52.9±0.8μs 0.98 bench_connectivity.Connectivity.time_face_node('120km')
52.1±0.4μs 52.6±2μs 1.01 bench_connectivity.Connectivity.time_face_node('480km')
428±20μs 418±10μs 0.98 bench_connectivity.Connectivity.time_n_nodes_per_face('120km')
363±10μs 352±10μs 0.97 bench_connectivity.Connectivity.time_n_nodes_per_face('480km')
5.76±0.05ms 5.75±0.1ms 1.00 bench_connectivity.Connectivity.time_node_edge('120km')
2.00±0.02ms 1.97±0.01ms 0.98 bench_connectivity.Connectivity.time_node_edge('480km')
78.5±2ms 78.3±2ms 1.00 bench_connectivity.Connectivity.time_node_face('120km')
5.08±0.04ms 5.06±0.02ms 1.00 bench_connectivity.Connectivity.time_node_face('480km')
8.42±0.06ms 8.53±0.1ms 1.01 face_bounds.FaceBounds.time_face_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/mpas/QU/oQU480.231010.nc'))
2.71±0.06ms 2.70±0.3ms 1.00 face_bounds.FaceBounds.time_face_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/scrip/outCSne8/outCSne8.nc'))
7.11±7s 10.1±10ms ~0.00 face_bounds.FaceBounds.time_face_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/ugrid/geoflow-small/grid.nc'))
1.53±0.04ms 1.51±0.04ms 0.99 face_bounds.FaceBounds.time_face_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/ugrid/quad-hexagon/grid.nc'))
57.3k 57.3k 1.00 face_bounds.FaceBounds.track_nbytes_face_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/mpas/QU/oQU480.231010.nc'))
12.3k 12.3k 1.00 face_bounds.FaceBounds.track_nbytes_face_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/scrip/outCSne8/outCSne8.nc'))
123k 123k 1.00 face_bounds.FaceBounds.track_nbytes_face_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/ugrid/geoflow-small/grid.nc'))
128 128 1.00 face_bounds.FaceBounds.track_nbytes_face_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/ugrid/quad-hexagon/grid.nc'))
1.27M 1.27M 1.00 face_bounds.FaceBounds.track_nbytes_grid_with_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/mpas/QU/oQU480.231010.nc'))
50.1k 50.1k 1.00 face_bounds.FaceBounds.track_nbytes_grid_with_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/scrip/outCSne8/outCSne8.nc'))
1.48M 1.48M 1.00 face_bounds.FaceBounds.track_nbytes_grid_with_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/ugrid/geoflow-small/grid.nc'))
712 712 1.00 face_bounds.FaceBounds.track_nbytes_grid_with_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/ugrid/quad-hexagon/grid.nc'))
1.98M 1.98M 1.00 face_bounds.FaceBounds.track_peakmem_face_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/mpas/QU/oQU480.231010.nc'))
1.97M 1.97M 1.00 face_bounds.FaceBounds.track_peakmem_face_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/scrip/outCSne8/outCSne8.nc'))
2.13M 2.14M 1.00 face_bounds.FaceBounds.track_peakmem_face_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/ugrid/geoflow-small/grid.nc'))
35.5k 35.5k 1.00 face_bounds.FaceBounds.track_peakmem_face_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/ugrid/quad-hexagon/grid.nc'))
354M 326M 0.92 face_bounds.FaceBoundsColdStartRss.track_peakmem_open_and_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/mpas/QU/oQU480.231010.nc'))
369M 367M 0.99 face_bounds.FaceBoundsColdStartRss.track_peakmem_open_and_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/scrip/outCSne8/outCSne8.nc'))
354M 327M 0.92 face_bounds.FaceBoundsColdStartRss.track_peakmem_open_and_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/ugrid/geoflow-small/grid.nc'))
354M 326M 0.92 face_bounds.FaceBoundsColdStartRss.track_peakmem_open_and_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/ugrid/quad-hexagon/grid.nc'))
1.17±0.03μs 1.11±0.01μs 0.95 geometry_kernels.AccucrossKernels.time_accucross
2.57±0.01μs 2.56±0.03μs 0.99 geometry_kernels.AccucrossKernels.time_accucross_pair
441±20ns 431±20ns 0.98 geometry_kernels.EFTPrimitives.time_acc_sqrt_re
446±20ns 416±10ns 0.93 geometry_kernels.EFTPrimitives.time_diff_of_products
361±20ns 390±10ns 1.08 geometry_kernels.EFTPrimitives.time_two_prod
396±70ns 381±20ns 0.96 geometry_kernels.EFTPrimitives.time_two_sum
661±30ns 666±9ns 1.01 geometry_kernels.GCAConstLatIntersection.time_accux_constlat_kernel
712±8ns 762±40ns 1.07 geometry_kernels.GCAConstLatIntersection.time_gca_const_lat_intersection
792±20ns 776±20ns 0.98 geometry_kernels.GCAConstLatIntersection.time_try_gca_const_lat_intersection
847±20ns 827±50ns 0.98 geometry_kernels.GCAGCAIntersection.time_accux_gca_kernel
932±40ns 921±20ns 0.99 geometry_kernels.GCAGCAIntersection.time_gca_gca_intersection
1.05±0.03μs 993±40ns 0.94 geometry_kernels.GCAGCAIntersection.time_try_gca_gca_intersection
52.6±0.5μs 51.1±0.6μs 0.97 geometry_kernels.OrientPredicates.time_on_minor_arc
53.3±0.7μs 52.1±1μs 0.98 geometry_kernels.OrientPredicates.time_orient3d_on_sphere
3.06±0ms 3.06±0ms 1.00 geometry_samebody.SameBodyConstLat.time_accux_dispatch
1.16±0ms 1.16±0ms 1.00 geometry_samebody.SameBodyConstLat.time_accux_kernel
2.29±0.01ms 2.29±0.01ms 1.00 geometry_samebody.SameBodyConstLat.time_fp64_dispatch
148±0.7μs 148±0.8μs 1.00 geometry_samebody.SameBodyConstLat.time_fp64_kernel
29.6±0.03ms 29.5±0.03ms 1.00 geometry_samebody_gcagca.SameBodyGcaGca.time_accux_dispatch
6.27±0.01ms 6.28±0.02ms 1.00 geometry_samebody_gcagca.SameBodyGcaGca.time_accux_kernel
23.6±0.07ms 23.6±0.03ms 1.00 geometry_samebody_gcagca.SameBodyGcaGca.time_fp64_dispatch
859±1μs 859±0.2μs 1.00 geometry_samebody_gcagca.SameBodyGcaGca.time_fp64_kernel
915±10ms 922±10ms 1.01 import.Imports.timeraw_import_uxarray
2.49±0.03ms 2.51±0.04ms 1.01 mpas_ocean.CheckNorm.time_check_norm('120km')
2.02±0.03ms 2.00±0.01ms 0.99 mpas_ocean.CheckNorm.time_check_norm('480km')
1.16±0.01ms 1.16±0.01ms 1.00 mpas_ocean.ConnectivityConstruction.time_face_face_connectivity('120km')
568±10μs 551±20μs 0.97 mpas_ocean.ConnectivityConstruction.time_face_face_connectivity('480km')
656±10μs 656±10μs 1.00 mpas_ocean.ConnectivityConstruction.time_n_nodes_per_face('120km')
610±20μs 599±20μs 0.98 mpas_ocean.ConnectivityConstruction.time_n_nodes_per_face('480km')
5.35±0.01ms 5.26±0.03ms 0.98 mpas_ocean.ConstructFaceLatLon.time_cartesian_averaging('120km')
3.84±0.04ms 3.78±0.01ms 0.98 mpas_ocean.ConstructFaceLatLon.time_cartesian_averaging('480km')
97.5±0.7ms 96.7±0.2ms 0.99 mpas_ocean.ConstructFaceLatLon.time_welzl('120km')
10.3±0.2ms 9.71±0.4ms 0.94 mpas_ocean.ConstructFaceLatLon.time_welzl('480km')
18.7±0.02ms 18.8±0.2ms 1.01 mpas_ocean.ConstructTreeStructures.time_ball_tree('120km')
955±20μs 963±10μs 1.01 mpas_ocean.ConstructTreeStructures.time_ball_tree('480km')
10.1±0.03ms 9.99±0.02ms 0.99 mpas_ocean.ConstructTreeStructures.time_kd_tree('120km')
633±50μs 626±20μs 0.99 mpas_ocean.ConstructTreeStructures.time_kd_tree('480km')
604±9ms 585±2ms 0.97 mpas_ocean.CrossSections.time_const_lat('120km', 1)
305±3ms 301±4ms 0.98 mpas_ocean.CrossSections.time_const_lat('120km', 2)
161±2ms 160±3ms 1.00 mpas_ocean.CrossSections.time_const_lat('120km', 4)
562±9ms 551±9ms 0.98 mpas_ocean.CrossSections.time_const_lat('480km', 1)
275±7ms 273±4ms 0.99 mpas_ocean.CrossSections.time_const_lat('480km', 2)
140±2ms 138±0.3ms 0.98 mpas_ocean.CrossSections.time_const_lat('480km', 4)
354M 345M 0.98 mpas_ocean.CrossSectionsPeakMem.track_peakmem_const_lat('120km', 1)
354M 346M 0.98 mpas_ocean.CrossSectionsPeakMem.track_peakmem_const_lat('120km', 2)
354M 345M 0.98 mpas_ocean.CrossSectionsPeakMem.track_peakmem_const_lat('120km', 4)
354M 331M 0.93 mpas_ocean.CrossSectionsPeakMem.track_peakmem_const_lat('480km', 1)
354M 329M 0.93 mpas_ocean.CrossSectionsPeakMem.track_peakmem_const_lat('480km', 2)
354M 329M 0.93 mpas_ocean.CrossSectionsPeakMem.track_peakmem_const_lat('480km', 4)
26.3±0.03ms 26.1±0.1ms 0.99 mpas_ocean.DualMesh.time_dual_mesh_construction('120km')
3.03±0.1ms 2.92±0.01ms 0.96 mpas_ocean.DualMesh.time_dual_mesh_construction('480km')
14.3±0.4ms 14.7±0.3ms 1.03 mpas_ocean.FaceAreas.time_face_areas('120km')
4.51±0.4ms 4.36±0.2ms 0.97 mpas_ocean.FaceAreas.time_face_areas('480km')
229k 229k 1.00 mpas_ocean.FaceAreas.track_nbytes_face_areas('120km')
14.3k 14.3k 1.00 mpas_ocean.FaceAreas.track_nbytes_face_areas('480km')
2.12M 2.12M 1.00 mpas_ocean.FaceAreas.track_peakmem_face_areas('120km')
714k 715k 1.00 mpas_ocean.FaceAreas.track_peakmem_face_areas('480km')
886±4ms 900±3ms 1.02 mpas_ocean.GeoDataFrame.time_to_geodataframe('120km', False)
51.8±0.6ms 51.2±0.9ms 0.99 mpas_ocean.GeoDataFrame.time_to_geodataframe('120km', True)
81.2±0.9ms 79.9±0.2ms 0.98 mpas_ocean.GeoDataFrame.time_to_geodataframe('480km', False)
5.33±0.2ms 5.19±0.2ms 0.97 mpas_ocean.GeoDataFrame.time_to_geodataframe('480km', True)
13.2±0.1ms 13.4±0.1ms 1.02 mpas_ocean.Gradient.time_gradient('120km')
1.81±0.02ms 1.80±0.01ms 0.99 mpas_ocean.Gradient.time_gradient('480km')
457k 457k 1.00 mpas_ocean.Gradient.track_nbytes_gradient('120km')
28.7k 28.7k 1.00 mpas_ocean.Gradient.track_nbytes_gradient('480km')
3.2M 3.2M 1.00 mpas_ocean.Gradient.track_peakmem_gradient('120km')
204k 204k 1.00 mpas_ocean.Gradient.track_peakmem_gradient('480km')
354M 341M 0.96 mpas_ocean.GradientColdStartRss.track_peakmem_gradient('120km')
276±9μs 273±9μs 0.99 mpas_ocean.HoleEdgeIndices.time_construct_hole_edge_indices('120km')
146±2μs 147±10μs 1.01 mpas_ocean.HoleEdgeIndices.time_construct_hole_edge_indices('480km')
202±9μs 198±1μs 0.98 mpas_ocean.Integrate.time_integrate('120km')
187±3μs 185±2μs 0.99 mpas_ocean.Integrate.time_integrate('480km')
18.4M 18.4M 1.00 mpas_ocean.Integrate.track_nbytes_integrate('120km')
1.2M 1.2M 1.00 mpas_ocean.Integrate.track_nbytes_integrate('480km')
179±0.9ms 178±2ms 0.99 mpas_ocean.MatplotlibConversion.time_dataarray_to_polycollection('120km', 'exclude')
179±2ms 177±0.6ms 0.99 mpas_ocean.MatplotlibConversion.time_dataarray_to_polycollection('120km', 'include')
178±0.6ms 178±1ms 1.00 mpas_ocean.MatplotlibConversion.time_dataarray_to_polycollection('120km', 'split')
12.8±0.1ms 12.6±0.08ms 0.99 mpas_ocean.MatplotlibConversion.time_dataarray_to_polycollection('480km', 'exclude')
12.8±0.2ms 12.6±0.06ms 0.98 mpas_ocean.MatplotlibConversion.time_dataarray_to_polycollection('480km', 'include')
12.9±0.2ms 12.8±0.1ms 0.99 mpas_ocean.MatplotlibConversion.time_dataarray_to_polycollection('480km', 'split')
247±2ms 246±0.7ms 0.99 mpas_ocean.NeighborhoodBuild.time_build('120km', 1.0)
1.31±0s 1.32±0.01s 1.01 mpas_ocean.NeighborhoodBuild.time_build('120km', 15.0)
504±0.4ms 507±2ms 1.00 mpas_ocean.NeighborhoodBuild.time_build('120km', 5.0)
13.5±0.2ms 13.4±0.1ms 0.99 mpas_ocean.NeighborhoodBuild.time_build('480km', 1.0)
25.5±0.06ms 25.2±0.1ms 0.99 mpas_ocean.NeighborhoodBuild.time_build('480km', 15.0)
16.5±0.04ms 16.4±0.03ms 1.00 mpas_ocean.NeighborhoodBuild.time_build('480km', 5.0)
241±0.3ms 244±1ms 1.01 mpas_ocean.NeighborhoodBuild.time_query_radius('120km', 1.0)
1.28±0s 1.28±0s 1.00 mpas_ocean.NeighborhoodBuild.time_query_radius('120km', 15.0)
499±0.5ms 500±1ms 1.00 mpas_ocean.NeighborhoodBuild.time_query_radius('120km', 5.0)
13.0±0.1ms 13.1±0.01ms 1.01 mpas_ocean.NeighborhoodBuild.time_query_radius('480km', 1.0)
24.7±0.1ms 25.0±0.1ms 1.01 mpas_ocean.NeighborhoodBuild.time_query_radius('480km', 15.0)
16.0±0.02ms 16.1±0.07ms 1.00 mpas_ocean.NeighborhoodBuild.time_query_radius('480km', 5.0)
1.19 1.19 1.00 mpas_ocean.NeighborhoodBuild.track_mean_neighbors('120km', 1.0)
612.76 612.76 1.00 mpas_ocean.NeighborhoodBuild.track_mean_neighbors('120km', 15.0)
74.17 74.17 1.00 mpas_ocean.NeighborhoodBuild.track_mean_neighbors('120km', 5.0)
1.0 1.0 1.00 mpas_ocean.NeighborhoodBuild.track_mean_neighbors('480km', 1.0)
37.29 37.29 1.00 mpas_ocean.NeighborhoodBuild.track_mean_neighbors('480km', 15.0)
6.57 6.57 1.00 mpas_ocean.NeighborhoodBuild.track_mean_neighbors('480km', 5.0)
728k 728k 1.00 mpas_ocean.NeighborhoodBuild.track_nbytes_neighbors('120km', 1.0)
141M 141M 1.00 mpas_ocean.NeighborhoodBuild.track_nbytes_neighbors('120km', 15.0)
17.4M 17.4M 1.00 mpas_ocean.NeighborhoodBuild.track_nbytes_neighbors('120km', 5.0)
43k 43k 1.00 mpas_ocean.NeighborhoodBuild.track_nbytes_neighbors('480km', 1.0)
563k 563k 1.00 mpas_ocean.NeighborhoodBuild.track_nbytes_neighbors('480km', 15.0)
123k 123k 1.00 mpas_ocean.NeighborhoodBuild.track_nbytes_neighbors('480km', 5.0)
5.72M 5.72M 1.00 mpas_ocean.NeighborhoodBuild.track_peakmem_build('120km', 1.0)
145M 145M 1.00 mpas_ocean.NeighborhoodBuild.track_peakmem_build('120km', 15.0)
21.5M 21.5M 1.00 mpas_ocean.NeighborhoodBuild.track_peakmem_build('120km', 5.0)
362k 362k 1.00 mpas_ocean.NeighborhoodBuild.track_peakmem_build('480km', 1.0)
825k 825k 1.00 mpas_ocean.NeighborhoodBuild.track_peakmem_build('480km', 15.0)
384k 384k 1.00 mpas_ocean.NeighborhoodBuild.track_peakmem_build('480km', 5.0)
44.4±0.6ms 44.5±0.9ms 1.00 mpas_ocean.NeighborhoodDask.time_mean('120km', 'grid_chunks')
22.7±0.06ms 22.7±0.03ms 1.00 mpas_ocean.NeighborhoodDask.time_mean('120km', 'numpy')
40.4±0.1ms 40.4±0.3ms 1.00 mpas_ocean.NeighborhoodDask.time_mean('120km', 'time_chunks')
11.7±0.1ms 11.8±0.3ms 1.01 mpas_ocean.NeighborhoodDask.time_mean('480km', 'grid_chunks')
714±10μs 672±10μs 0.94 mpas_ocean.NeighborhoodDask.time_mean('480km', 'numpy')
8.50±0.1ms 8.35±0.1ms 0.98 mpas_ocean.NeighborhoodDask.time_mean('480km', 'time_chunks')
5.77M 5.84M 1.01 mpas_ocean.NeighborhoodDask.track_peakmem_mean('120km', 'grid_chunks')
2.75M 2.75M 1.00 mpas_ocean.NeighborhoodDask.track_peakmem_mean('120km', 'numpy')
5.69M 5.68M 1.00 mpas_ocean.NeighborhoodDask.track_peakmem_mean('120km', 'time_chunks')
635k 675k 1.06 mpas_ocean.NeighborhoodDask.track_peakmem_mean('480km', 'grid_chunks')
177k 177k 1.00 mpas_ocean.NeighborhoodDask.track_peakmem_mean('480km', 'numpy')
554k 542k 0.98 mpas_ocean.NeighborhoodDask.track_peakmem_mean('480km', 'time_chunks')
12.6±0.01s 12.7±0.03s 1.00 mpas_ocean.NeighborhoodReduce.time_dataset_reduce('120km', 'mean')
13.3±0s 13.3±0.02s 1.00 mpas_ocean.NeighborhoodReduce.time_dataset_reduce('120km', 'median')
226±0.7ms 226±0.2ms 1.00 mpas_ocean.NeighborhoodReduce.time_dataset_reduce('480km', 'mean')
231±0.2ms 232±0.1ms 1.00 mpas_ocean.NeighborhoodReduce.time_dataset_reduce('480km', 'median')
1.35±0s 1.35±0.01s 1.01 mpas_ocean.NeighborhoodReduce.time_neighborhood_reduce('120km', 'mean')
1.53±0s 1.55±0.01s 1.01 mpas_ocean.NeighborhoodReduce.time_neighborhood_reduce('120km', 'median')
25.9±0.09ms 25.8±0.09ms 1.00 mpas_ocean.NeighborhoodReduce.time_neighborhood_reduce('480km', 'mean')
27.0±0.08ms 27.3±0.4ms 1.01 mpas_ocean.NeighborhoodReduce.time_neighborhood_reduce('480km', 'median')
38.1±0.3ms 38.0±0.2ms 1.00 mpas_ocean.NeighborhoodReduce.time_reduce('120km', 'mean')
232±0.3ms 233±1ms 1.01 mpas_ocean.NeighborhoodReduce.time_reduce('120km', 'median')
477±20μs 483±5μs 1.01 mpas_ocean.NeighborhoodReduce.time_reduce('480km', 'mean')
1.76±0.01ms 1.69±0.03ms 0.96 mpas_ocean.NeighborhoodReduce.time_reduce('480km', 'median')
239k 239k 1.00 mpas_ocean.NeighborhoodReduce.track_peakmem_reduce('120km', 'mean')
245k 245k 1.00 mpas_ocean.NeighborhoodReduce.track_peakmem_reduce('120km', 'median')
19.3k 19.4k 1.00 mpas_ocean.NeighborhoodReduce.track_peakmem_reduce('480km', 'mean')
19.8k 19.9k 1.00 mpas_ocean.NeighborhoodReduce.track_peakmem_reduce('480km', 'median')
435±4μs 409±20μs 0.94 mpas_ocean.PointInPolygon.time_face_search_lonlat('120km')
405±6μs 409±10μs 1.01 mpas_ocean.PointInPolygon.time_face_search_lonlat('480km')
396±7μs 391±10μs 0.99 mpas_ocean.PointInPolygon.time_face_search_xyz('120km')
372±10μs 367±10μs 0.99 mpas_ocean.PointInPolygon.time_face_search_xyz('480km')
132±0.3ms 130±0.8ms 0.98 mpas_ocean.RemapDownsample.time_bilinear_remapping
17.5±0.1ms 17.4±0.2ms 0.99 mpas_ocean.RemapDownsample.time_inverse_distance_weighted_remapping
15.6±0.1ms 15.9±0.1ms 1.02 mpas_ocean.RemapDownsample.time_nearest_neighbor_remapping
1.43±0s 1.42±0.01s 0.99 mpas_ocean.RemapUpsample.time_bilinear_remapping
27.0±0.8ms 26.3±0.2ms 0.97 mpas_ocean.RemapUpsample.time_inverse_distance_weighted_remapping
12.6±0.1ms 12.4±0.08ms 0.98 mpas_ocean.RemapUpsample.time_nearest_neighbor_remapping
8.60±0.2ms 8.41±0.1ms 0.98 mpas_ocean.ZonalAverage.time_zonal_average('120km')
5.20±0.1ms 5.20±0.07ms 1.00 mpas_ocean.ZonalAverage.time_zonal_average('480km')
354M 347M 0.98 mpas_ocean.ZonalAveragePeakMem.track_peakmem_zonal_average('120km')
354M 330M 0.93 mpas_ocean.ZonalAveragePeakMem.track_peakmem_zonal_average('480km')
7.04±0.03ms 7.02±0.02ms 1.00 quad_hexagon.QuadHexagon.time_open_dataset
6.04±0.04ms 6.00±0.08ms 0.99 quad_hexagon.QuadHexagon.time_open_grid
408 408 1.00 quad_hexagon.QuadHexagon.track_nbytes_open_dataset
392 392 1.00 quad_hexagon.QuadHexagon.track_nbytes_open_grid
73k 73.6k 1.01 quad_hexagon.QuadHexagon.track_peakmem_open_dataset
72.1k 72.7k 1.01 quad_hexagon.QuadHexagon.track_peakmem_open_grid

dask="parallelized" on the threaded scheduler can only overlap work that
releases the GIL, and only 2 of the package's 140 @njit sites passed
nogil=True. Adds it to 19 more: the kernels that Python invokes once per
array, which is exactly the set dask will call once per chunk.

Measured on _construct_edge_node_distances (2M edges, best of 3), the
same kernel jitted with and without nogil:

    threads     GIL held        nogil   speedup
          1        96.9ms        95.7ms    1.01x
          2       192.8ms       103.8ms    1.86x
          4       401.4ms       105.3ms    3.81x
          8       780.7ms       119.5ms    6.53x

The GIL-held column scales linearly with thread count -- that is the
signature of zero parallelism.

Deliberately NOT a blanket sweep. nogil is not free and not universally
inert:

  * It only takes effect at the Python->native boundary. An njit->njit
    call never touches the GIL, so on the 106 kernels reachable only
    from other jitted code the flag is dead weight. That includes every
    inline="always" primitive in utils/computing.py, which by
    construction is inlined into its caller and has no boundary at all.

  * At that boundary it costs a PyEval_SaveThread/RestoreThread pair,
    measured here at ~40ns per call. Against a whole-array kernel that
    is free; against a scalar kernel called from a Python loop it is a
    20-30% regression. An earlier revision of this commit did apply
    nogil everywhere and asv caught exactly that -- two_sum 1.26x,
    two_prod 1.21x, diff_of_products ~1.25x, acc_sqrt_re 1.37x,
    orient3d_on_sphere 1.17x, all of them sub-microsecond kernels.

So the rule is per-call work, not per-decorator uniformity. Kernels left
alone for that reason include gca_const_lat_intersection and
get_number_of_intersections (per-edge, from the Python loops in
zonal.py and integrate.py), _compute_band_overlap_area (per-face,
zonal.py:327), _barycentric_coordinates (per-candidate, neighbors.py:954)
and the EFT primitives above. Several of these are slated to become
gufuncs; they can take nogil when they stop being called per element.

Two further sites stay as they are:

  * the guvectorize kernel in grid/neighbors.py -- numba rejects nogil
    as a @guvectorize option, and the gufunc machinery already drops the
    GIL around the loop.
  * the 15 parallel=True kernels keep parallel=True. Nested under dask's
    threadpool it oversubscribes, but dropping it belongs with the work
    that gives dask the chunk loop.

_build_node_edge_connectivity was the package's only bare @njit and so
also its only kernel without cache=True; normalized to
@njit(cache=True, nogil=True) while adding the flag.

Test suite: 953 passed, 1 skipped. test_plot_with_features fails
identically before and after (matplotlib figure size, unrelated).

Co-Authored-By: Claude Opus 5 <[email protected]>
cmdupuis3 and others added 2 commits September 14, 2026 12:35
The previous commit's flag placement is not self-evident from reading a
decorator, and nothing in the repo could catch it drifting. Both failure
directions are silent:

  * dropping nogil from an entry point serializes it under dask's
    threaded scheduler with no error, and no timing in the asv suite
    moves, because every benchmark in it drives uxarray from one thread;

  * adding nogil to a kernel only jitted code calls does nothing useful
    -- its caller already released the GIL for the whole call tree -- but
    still pays ~40ns per direct Python call, which is how the earlier
    blanket sweep turned into six asv regressions.

test_nogil_policy.py states the rule as four assertions over an AST scan
of the package: Python-reachable njit kernels must release the GIL;
jit-only ones must not set the flag; inline="always" kernels must not
(they have no call boundary to release at, and the plan commits to
keeping them inlined); and gufuncs must not (numba rejects the option).

"Reachable" counts a bare reference, not just a call. The kernels that
matter most are passed to xr.apply_ufunc rather than called -- notably
_build_n_nodes_per_face at connectivity.py:136 -- and a call-only scan
would classify exactly those as jit-only, which is backwards.

Seven kernels are exempt, each with its reason recorded; a further test
fails if an exemption stops being live, so the list cannot rot as these
become gufuncs. Each assertion was mutation-tested: reverting the flag on
an entry point, adding it to a jit-only kernel, to an inline kernel, and
to an exempt kernel are all caught.

nogil_scaling.py measures wall(2 threads)/wall(1 thread) on
_construct_edge_node_distances, alongside the same kernel recompiled
without the flag as a control:

    track_gil_scaling          1.07   (overlaps)
    track_gil_scaling_control  1.97   (serializes)

This benchmarks the flag rather than a user-facing operation, which is a
fair criticism -- but no user-facing path reaches a kernel from two
threads until core/ grows the chunked call sites, so there is nothing
else to measure yet. Its docstring says to retire it for a real chunked
workload once one exists.

Test suite: 965 passed, 1 skipped.

Co-Authored-By: Claude Opus 5 <[email protected]>
test_nogil_policy.py parsed every module with ``path.read_text()`` and no
encoding, so it decoded with the locale's. On Linux and macOS that is
UTF-8 and the scan passed; on Windows it is cp1252, which has no mapping
for byte 0x90.

Four modules carry a U+2010 HYPHEN (core/dataarray.py, core/dataset.py,
cross_sections/dataarray_accessor.py, grid/point_in_face.py), encoded
E2 80 90 -- and that 0x90 is undefined in cp1252:

    UnicodeDecodeError: 'charmap' codec can't decode byte 0x90 in
    position 82386: character maps to <undefined>

The scan runs at module scope, so the failure aborted collection and
errored all 12 tests on every Windows job in the matrix. Python 3.11
through 3.14 all default to the locale encoding there, which matches the
failure appearing on the Windows builds alone.

Pass encoding="utf-8" explicitly. Verified with -X warn_default_encoding
and EncodingWarning promoted to an error, so no implicit-encoding read
remains in the file.

Test suite: 965 passed, 1 skipped.

Co-Authored-By: Claude Opus 5 <[email protected]>
@cmdupuis3
cmdupuis3 requested a review from Sevans711 September 21, 2026 22:42
Drops test_nogil_policy.py and the control arm of the scaling benchmark,
leaving the branch at the nogil=True decorator lines plus a single
track_gil_scaling number.

The policy test encoded, as four AST assertions and seven documented
exemptions, a rule that applies to 20 decorator lines. That is a lot of
machinery for the size of the change, and the scan carried its own
maintenance cost -- it already needed one fix for reading package
sources with the locale encoding, which broke the Windows matrix.

What is lost is the guard against the flag spreading back onto kernels
that do not want it. The regression it mattered most for is still
covered: dropping nogil from an entry point moves track_gil_scaling from
~1.07 toward ~2.0, which asv flags. Nothing now catches the other
direction -- a blanket re-application -- except asv noticing the
sub-microsecond kernels regress, which is how it was caught the first
time.

track_gil_scaling_control was the same kernel recompiled without the
flag, there so a broken harness could not read as a good result. Without
it the absolute value carries less weight; the regression signal does
not depend on it.

Test suite: 953 passed, 1 skipped.

Co-Authored-By: Claude Opus 5 <[email protected]>
@cmdupuis3
cmdupuis3 marked this pull request as ready for review September 21, 2026 22:59

@Sevans711 Sevans711 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks ready to merge! Simple changes which should help with future scalability work, and shouldn't hurt in the meantime (no regressions in benchmarks). The new benchmark is a good spot check to help prevent regression, and demonstrates clearly that the nogil=True flag can cause improvements. Approving as-is!

@cmdupuis3
cmdupuis3 requested a review from rajeeja September 22, 2026 15:35
@Sevans711

Copy link
Copy Markdown
Collaborator

(When I glanced at it earlier today there were no regressions in benchmarks; though now I do see tiny apparent regressions for sub-millisecond connectivity benchmarks time_face_node('120km') and time_n_nodes_per_face('120km'). I suppose that means this is probably just noise, rather than any real performance degradation. Keeping this marked as approved!)

@rajeeja

rajeeja commented Sep 23, 2026

Copy link
Copy Markdown
Contributor

This looks good overall. One small suggestion before merging: could we make the title a little more precise, maybe “Release the GIL in Python-entry-point Numba kernels”? The current title says “all Numba kernels,” but the PR intentionally (and correctly) leaves out small internal kernels, gufuncs, and places where nogil=True would add overhead or is not supported.

Could we also open a follow-up issue for the remaining end-to-end Dask work? It would be useful to track a real user-facing chunked workflow, measure scaling on representative datasets, and look at how Dask threads interact with Numba’s parallel=True kernels—especially possible oversubscription and whether we need thread-limit or scheduler guidance. Once that workflow exists, we could supplement or replace the synthetic GIL benchmark with an end-to-end benchmark.

The benchmark here is a good regression check for whether the GIL is actually released. The follow-up would tell us how much that helps real UXarray workflows and what the next bottlenecks are. None of this needs to block the implementation itself.

@rajeeja rajeeja left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just change title and later the issue as in previous comment

@cmdupuis3 cmdupuis3 changed the title Release the GIL in all numba kernels (nogil=True) Release the GIL in Python-entry-point Numba kernels Sep 23, 2026
@cmdupuis3

Copy link
Copy Markdown
Collaborator Author

@rajeeja Yeah, I'll make an overarching issue for the end-to-end chunked work and add this one as a sub-issue

@cmdupuis3
cmdupuis3 merged commit 59774e1 into UXARRAY:main Sep 23, 2026
18 checks passed
@cmdupuis3
cmdupuis3 deleted the cmd/nogil branch October 28, 2026 15:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

run-benchmark Run ASV benchmark workflow scalability Related to scalability & performance efforts

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GIL blocks parallelism for many kernels

3 participants