Skip to content

Slow oneMKL BLAS calls on the CPU device when NumPy in the same env uses MKL BLAS #3199

Todo

Description

@antonwolfy

Summary

On the CPU device (opencl:cpu), dpnp.matmul and other calls that go to oneMKL BLAS run 6-16x slower (median) when NumPy's BLAS in the same env is MKL (libblas=*_intelmkl); single calls take up to 35x longer. Both documented conda install commands (README "Conda" and "Installing the bleeding edge") give such an env. With MKL_THREADING_LAYER=TBB, or with NumPy on OpenBLAS, the same calls run at full speed.

Environment

  • Release: conda create -n t -c https://software.repos.intel.com/python/conda/ -c conda-forge --override-channels python=3.13 dpnp gives dpnp 0.21.0, dpctl 0.23.0, numpy 2.5.5 (conda-forge), and libblas 3.2026.2 0_intelmkl + mkl 2026.2.1 (Intel channel).
  • Bleeding edge: the same with -c dppy/label/dev first gives dpnp 0.22.0dev3, dpctl 0.24.0dev0, and the same libblas and mkl.
  • Control: the bleeding-edge env with "libblas=*=*openblas" added.
  • Linux (Ubuntu 24.04 and 26.04), OpenCL CPU runtime 2026.2 from the env, on an Intel Xeon Platinum 8480+ (112 cores) and on a 16-core Intel Panther Lake laptop CPU.

Reproducer

import os, statistics, time
import numpy, dpctl, dpnp

maps = open("/proc/self/maps").read()
print("MKL_THREADING_LAYER =", os.environ.get("MKL_THREADING_LAYER"))
print("loaded:", sorted({n for n in ("mkl_rt", "mkl_intel_thread", "mkl_tbb_thread", "iomp5", "openblas") if n in maps}))

q = dpctl.SyclQueue("cpu")
a = dpnp.asarray(numpy.random.default_rng(0).standard_normal((2048, 2048), dtype=numpy.float32), sycl_queue=q)
for _ in range(2):
    dpnp.matmul(a, a)
q.wait()
ts = []
for _ in range(15):
    t0 = time.perf_counter()
    dpnp.matmul(a, a)
    q.wait()
    ts.append(1e3 * (time.perf_counter() - t0))
print(f"dpnp.matmul 2048x2048 float32 on {q.sycl_device.name}: median {statistics.median(ts):.1f} ms (min {min(ts):.1f}, max {max(ts):.1f})")

Results

Median of 15 calls, in two separate processes per row:

Env Setting Xeon 8480+ (112 cores) Panther Lake (16 cores)
0.21.0, MKL libblas default 35-94 ms (single calls up to 200 ms) 231-241 ms
0.21.0, MKL libblas MKL_THREADING_LAYER=TBB 5.9 ms 24-31 ms
0.21.0, MKL libblas KMP_BLOCKTIME=0 5.4-5.6 ms 19-39 ms
0.22.0dev3, MKL libblas default 34-36 ms 221-235 ms
0.22.0dev3, MKL libblas MKL_THREADING_LAYER=TBB 5.7-5.8 ms 30-31 ms
0.22.0dev3, OpenBLAS libblas default 5.8-5.9 ms 21-22 ms

The loaded: line shows the difference:

  • MKL libblas, default: import dpnp (which imports NumPy) already loads libmkl_rt, libmkl_intel_thread and libiomp5, next to dpnp's libmkl_tbb_thread.
  • With MKL_THREADING_LAYER=TBB: only libmkl_rt and libmkl_tbb_thread.
  • OpenBLAS NumPy: no libmkl_rt at all.

Other calls:

  • The oneMKL strided trsm_batch that dpnp uses for batched dpnp.scipy.linalg.cho_solve behaves the same. For float32, n = 4096 and 64 right-hand sides, one call takes 250-290 ms on the Xeon by default, against 2.2 ms with MKL_THREADING_LAYER=TBB (2.1 ms with KMP_BLOCKTIME=0). On the Panther Lake CPU it is 5.4-6.3 ms against 2.0-2.9 ms.
  • dpnp.linalg.solve (2048 x 2048, 64 right-hand sides) was not affected: 23-27 ms on the Xeon in every env.

Likely cause

NumPy's MKL BLAS goes through libmkl_rt. When MKL_THREADING_LAYER is unset, libmkl_rt picks the Intel OpenMP threading layer, and MKL then runs dpnp's oneMKL CPU calls on that layer too, although dpnp is built against TBB threading (MKL_THREADING "tbb_thread" in CMakeLists.txt). The OpenMP threads keep spinning after each call (KMP_BLOCKTIME defaults to 200 ms) while the OpenCL CPU runtime runs the next kernels on its own TBB threads. That KMP_BLOCKTIME=0 removes most of the slowdown points that way. This is a reading of the observations above, not confirmed in MKL.

Who is affected

  • Both documented conda install commands give libblas=*_intelmkl (checked on 2026-11-24).
  • The GHA test jobs install with mamba and currently get conda-forge's OpenBLAS libblas (e.g. the Linux py3.15 job of the latest master conda-package.yml run), so CI doesn't show this.
  • In a venv with the pip install from Intel's index, dpctl.SyclQueue("cpu") fails (no CPU device), so it can't show this either.

Possible fixes

  • Set MKL_THREADING_LAYER=TBB in an activation script of dpnp's conda package, when the user hasn't set it. Setting it inside import dpnp would likely come too late when NumPy was imported first, since libmkl_rt loads its threading layer at import (see the loaded: line).
  • Document the workaround (MKL_THREADING_LAYER=TBB) in the install or performance notes.

Activity

  1. self-assigned this
    on Nov 24, 2026
  2. antonwolfy commented on Nov 25, 2026

    @antonwolfy
    ContributorAuthor

    A second way in: oneMKL VM on the CPU device, also with OpenBLAS NumPy

    The OpenBLAS control above keeps libmkl_rt out only until the first elementwise call that dpnp runs through oneMKL VM on the CPU device. At that call, libmkl_sycl_vm.so.6 dlopens libmkl_rt.so.3, and libmkl_rt loads libmkl_intel_thread and libiomp5 (seen with LD_DEBUG=files). A float32 a - a on an 8 x 8 array is enough. The same calls on the GPU don't load libmkl_rt.

    import os
    import dpctl, dpnp
    
    def loaded():
        maps = open("/proc/self/maps").read()
        return sorted({n for n in ("mkl_rt", "mkl_intel_thread", "mkl_tbb_thread", "iomp5") if f"lib{n}.so" in maps})
    
    print("MKL_THREADING_LAYER =", os.environ.get("MKL_THREADING_LAYER"))
    q = dpctl.SyclQueue("cpu")
    a = dpnp.ones((8, 8), dtype="f4", sycl_queue=q)
    print("before a - a:", loaded())
    (a - a)
    q.wait()
    print("after a - a: ", loaded())

    With dpnp 0.22.0dev3 built from source, NumPy 2.5.5 with libblas 3.11.0 *_openblas, and the OpenCL CPU runtime 2026.2, on Linux:

    MKL_THREADING_LAYER = None
    before a - a: ['mkl_tbb_thread']
    after a - a:  ['iomp5', 'mkl_intel_thread', 'mkl_rt', 'mkl_tbb_thread']
    MKL_THREADING_LAYER = TBB
    before a - a: ['mkl_tbb_thread']
    after a - a:  ['mkl_rt', 'mkl_tbb_thread']
    

    The effect on speed was small, and it doesn't clearly come from the OpenMP layer. Each line below is one process, timing a float32 dpnp.scipy.linalg.solve_triangular from #3200 (one oneMKL trsm_batch call, n = 4096, 64 right-hand sides, check_finite=False) on the CPU device, median before and after one a - a:

    CPU default MKL_THREADING_LAYER=TBB
    Panther Lake, 16 cores 2.9 ms → 3.9-4.1 ms 3.1 ms → 4.6-6.8 ms
    Xeon Platinum 8480+ 5.7 ms → 5.9-6.2 ms 4.1 ms → 3.7-3.9 ms

    So the activation script proposed above would also keep the OpenMP layer out in this case, but it may not remove what slows the Panther Lake runs after the first VM call. Benchmarks on the CPU device should also keep in mind that a device-side residual such as dpnp.linalg.norm(a @ x - b) makes this load, which changes the timings that follow it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions