Introduction
This guide provides detailed instructions for integrating Intel® oneAPI Math Kernel Library (oneMKL) with Julia, enhancing performance for linear algebra operations. It covers installation, configuration, and conditional loading of oneMKL in Julia packages, leveraging the MKL.jl package and best practices for package development.
Installation
Installing MKL.jl
To use oneMKL with Julia, you need to install the MKL.jl package, which provides an interface to Intel's oneMKL.
- Install MKL.jl: Use Julia's package manager to install MKL.jl.
using Pkg Pkg.add("MKL") - Switch to oneMKL: After installation, switch Julia's BLAS and LAPACK to use oneMKL.
using MKL
Configuration
Verifying oneMKL Usage
To ensure that Julia is using oneMKL for linear algebra operations, you can verify the BLAS vendor:
using LinearAlgebra
BLAS.get_config()
This should return oneMKL library name if oneMKL is correctly configured.
Using oneMKL in Julia Packages
Extensions (Julia 1.9+)
Julia 1.9 introduced a new Extensions system, which provides a more robust and efficient way to conditionally load code compared to the older Requires.jl approach. This is the recommended method for new packages.
- Define Your Extension: Create an extension module for oneMKL-specific functionality.
module MyPackage using LinearAlgebra function my_function() # Default implementation end # Extension point declaration module MKLExt end end # module - Create Extension File: Create an extension file in an `ext` directory for oneMKL-specific code:
module MKLExt using MyPackage using MKL # Override functions with oneMKL-specific implementations function MyPackage.my_function() # oneMKL-specific implementation end end # module - Update Project.toml: Add an `[extensions]` section to your Project.toml:
[extensions] MKLExt = "MKL" [weakdeps] MKL = "33e6dc65-8f57-5167-99aa-e5a354878fb2"
Conditional Loading with Requires.jl (Pre-1.9)
For Julia versions prior to 1.9, you can use Requires.jl:
- Add Requires.jl: Include Requires.jl in your package dependencies.
using Pkg Pkg.add("Requires") - Conditional Code: Use the `@require` macro to conditionally load oneMKL-specific code.
module MyPackage using LinearAlgebra using Requires function __init__() @require MKL="33e6dc65-8f57-5167-99aa-e5a354878fb2" begin function my_function() # oneMKL-specific implementation end end end function my_function() # Default implementation end end
Example Package Setup
Using Extensions (Julia 1.9+)
-
Package Structure: Set up your package structure using Julia's package generator.
```bash julia --project=@. -e 'using Pkg; Pkg.generate("MyPackage")' mkdir -p MyPackage/ext ``` - Edit Project.toml: Add LinearAlgebra and oneMKL as dependencies in Project.toml.
[deps] LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" [weakdeps] MKL = "33e6dc65-8f57-5167-99aa-e5a354878fb2" [extensions] MKLExt = "MKL" - Main Package Code: Create your main package code with extension points.
module MyPackage using LinearAlgebra function matrix_multiply(A, B) return A * B # Default implementation using standard BLAS end # Define extension module module MKLExt end end # module - Extension Implementation: Implement oneMKL-specific functionality.
module MKLExt using MyPackage using MKL # Override the matrix multiplication with oneMKL-optimized version function MyPackage.matrix_multiply(A, B) # This will use oneMKL's optimized BLAS automatically # You could also add oneMKL-specific optimizations here return A * B end end # module
Additional Resources
- [Julia Package Documentation on Extensions](https://pkgdocs.julialang.org/v1/creating-packages/#Conditional-loading-of-code-in-packages-(Extensions))
- [MKL.jl GitHub Repository](https://github.com/JuliaLinearAlgebra/MKL.jl)
- [Julia's LinearAlgebra Documentation](https://docs.julialang.org/en/v1/stdlib/LinearAlgebra/)