When working with Julia, you may come across situations where you need to switch from the mkl (Math Kernel Library) to openblas (Open Basic Linear Algebra Subprograms) library. This article will explore three different ways to solve this problem and provide a recommendation on the best option.
Option 1: Manually switching the library
The first option is to manually switch the library by recompiling Julia with openblas. This involves downloading the openblas library, configuring Julia to use openblas, and recompiling the Julia binaries. Here are the steps to follow:
# Step 1: Download openblas
git clone https://github.com/xianyi/OpenBLAS.git
cd OpenBLAS
# Step 2: Configure Julia to use openblas
export USE_SYSTEM_BLAS=1
export USE_SYSTEM_LAPACK=1
export OPENBLAS_DYNAMIC_ARCH=1
export OPENBLAS_USE_THREAD=0
# Step 3: Recompile Julia
make -j4
This option gives you full control over the library switch, but it requires manual intervention and can be time-consuming. Additionally, it may not be suitable for users who are not comfortable with compiling software from source.
Option 2: Using the BinaryBuilder package
The second option is to use the BinaryBuilder package, which provides pre-built binaries for various libraries, including openblas. This option simplifies the process by automating the compilation and installation of openblas. Here are the steps to follow:
# Step 1: Install BinaryBuilder
using Pkg
Pkg.add("BinaryBuilder")
# Step 2: Build openblas
using BinaryBuilder
openblas = Library("openblas")
build(openblas)
This option is more user-friendly and reduces the manual effort required. However, it may not always provide the latest version of openblas, and you may need to wait for the BinaryBuilder package to be updated with the latest release.
Option 3: Using the MKL.jl package
The third option is to use the MKL.jl package, which provides a wrapper around the mkl library. This option allows you to switch between mkl and openblas without recompiling Julia. Here are the steps to follow:
# Step 1: Install MKL.jl
using Pkg
Pkg.add("MKL")
# Step 2: Switch to openblas
using MKL
MKL.set_num_threads(1)
This option is the easiest to implement as it does not require any manual compilation or installation. However, it relies on the MKL.jl package, which may not provide the same level of performance as using openblas directly.
After exploring these three options, the recommended approach would be Option 2: Using the BinaryBuilder package. It strikes a balance between ease of use and control over the library switch. Additionally, it ensures that you have access to the latest pre-built binaries without the need for manual compilation.