Skip to content

Refactor abstract layer for matmul_veclib() and enable blas on Windows#835

Merged
yungyuc merged 1 commit into
solvcon:masterfrom
ThreeMonth03:optimize-matmul-fast
May 29, 2026
Merged

Refactor abstract layer for matmul_veclib() and enable blas on Windows#835
yungyuc merged 1 commit into
solvcon:masterfrom
ThreeMonth03:optimize-matmul-fast

Conversation

@ThreeMonth03
Copy link
Copy Markdown
Collaborator

@ThreeMonth03 ThreeMonth03 commented May 26, 2026

Installs openblas on the Windows platform, includes cblas when implementing matmul_veclib(), and refactors testcases.

Implement abstract layer math/blas_compat.hpp. Move the existing abstraction layer linalg/lapack_compat.hpp to linalg/math_compat.hpp.

For issue #830

@ThreeMonth03 ThreeMonth03 marked this pull request as draft May 26, 2026 14:37
@ThreeMonth03 ThreeMonth03 force-pushed the optimize-matmul-fast branch 4 times, most recently from 998822d to b6febde Compare May 27, 2026 14:11
@ThreeMonth03 ThreeMonth03 marked this pull request as ready for review May 27, 2026 14:41
Copy link
Copy Markdown
Collaborator Author

@ThreeMonth03 ThreeMonth03 left a comment

Choose a reason for hiding this comment

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

@yungyuc Please take a look.

Comment thread .github/workflows/devbuild.yml Outdated
Comment on lines +285 to +294
# Reference: https://www.openmathlib.org/OpenBLAS/docs/install/
- name: install OpenBLAS
run: |
vcpkg install openblas:x64-windows
$openblas_bin_path = @(
"C:\vcpkg\installed\x64-windows\bin",
"C:\vcpkg\installed\x64-windows\debug\bin"
) -join ";"
echo "$openblas_bin_path" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This library doesn't includes lapack, so I think we need another pull request to write the script about source build on Windows.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Openblas includes LAPACK. Why this openblas you install using vcpkg does not?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Remove the check of cblas because we assume that every machine has installed cblas now.

Comment thread tests/test_matrix.py Outdated
Comment on lines +85 to +102
def assert_matmul(self, lhs, rhs, expected):
result = lhs.matmul(rhs)

self.assertEqual(list(result.shape), list(expected.shape))
np.testing.assert_array_almost_equal(result.ndarray, expected)
return result

def assert_matmul_fast(self, lhs, rhs, expected, matmul_result):
fast_result = lhs.matmul_fast(rhs)

self.assertEqual(list(fast_result.shape), list(expected.shape))
np.testing.assert_array_almost_equal(fast_result.ndarray, expected)
np.testing.assert_array_almost_equal(fast_result.ndarray,
matmul_result.ndarray)
return fast_result

def assert_matmul_veclib(self, lhs, rhs, expected, matmul_result):
veclib_unavailable = (
"SimpleArray::matmul_veclib(): "
"Accelerate/CBLAS matmul is only "
"available on Apple Silicon"
)

try:
veclib_result = lhs.matmul_veclib(rhs)
except RuntimeError as exc:
# FIXME: Split veclib backend coverage into dedicated tests and
# mark unsupported platforms as expected failures once a follow-up
# issue is filed.
self.assertEqual(str(exc), veclib_unavailable)
return
veclib_result = lhs.matmul_veclib(rhs)
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Add helper function for checking same patterns.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I'm not sure the abstract layer should be put in folder /math.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It's OK to keep lapack_compact.hpp here under math/ for now.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Abstract layer of blas.

Comment thread cpp/modmesh/math/blas_compat.hpp Outdated
namespace modmesh
{

namespace blas
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I think these api should be wrapped in something like namespace blas, but I barely see another namespace in namespace modmesh, except namespace detail;.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

No, we do not use an additional namespace for them. Please remove the namespace blas.

Comment thread cpp/modmesh/math/blas_compat.hpp Outdated
Complex<double> const * matrix,
Complex<double> const * vector,
Complex<double> * result,
MatrixOperation operation = MatrixOperation::none);
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

These functions are used in matmul_vec_vec_veclib();, matmul_vec_mat_veclib(); and return matmul_mat_vec_veclib();.

Comment thread cpp/modmesh/buffer/matmul.cpp Outdated
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Remove abstract layer here.

Copy link
Copy Markdown
Member

@yungyuc yungyuc left a comment

Choose a reason for hiding this comment

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

  • Clarify: why vcpkg openblas does not have lapack.
  • Remove the namespace blas.
  • Name the wrapper to vendor code as *_blas().

Comment thread .github/workflows/devbuild.yml Outdated
Comment on lines +285 to +294
# Reference: https://www.openmathlib.org/OpenBLAS/docs/install/
- name: install OpenBLAS
run: |
vcpkg install openblas:x64-windows
$openblas_bin_path = @(
"C:\vcpkg\installed\x64-windows\bin",
"C:\vcpkg\installed\x64-windows\debug\bin"
) -join ";"
echo "$openblas_bin_path" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Openblas includes LAPACK. Why this openblas you install using vcpkg does not?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It's OK to keep lapack_compact.hpp here under math/ for now.

Comment thread cpp/modmesh/math/blas_compat.hpp Outdated
#pragma once

/*
* Copyright (c) 2026, modmesh contributors.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You name.

Comment thread cpp/modmesh/math/blas_compat.hpp Outdated
namespace modmesh
{

namespace blas
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

No, we do not use an additional namespace for them. Please remove the namespace blas.

*/

#include <modmesh/buffer/small_vector.hpp>
#include <modmesh/math/Complex.hpp>
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Good catch.

Comment thread cpp/modmesh/math/blas_compat.cpp Outdated
@@ -0,0 +1,402 @@
/*
* Copyright (c) 2026, modmesh contributors.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Your name.

Comment thread cpp/modmesh/buffer/matmul.hpp Outdated
void check_inner(size_t lhs_idx, size_t rhs_idx) const;
void check_tiles() const;
A matmul_vec_vec();
A matmul_vec_vec_veclib();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Name the wrapper to vendor code as *_blas(). veclib is for Apple accelerate/veclib, not all vendor library.

veclib was used elsewhere for vendor lib, because initially the only vendor lib was only accelerate/veclib. A separate issue should be created to refactor.

@yungyuc yungyuc changed the title Support matmul_veclib() with cblas on Linux and Windows platform Refactor abstract layer for matmul_veclib() and enable blas on Windows May 28, 2026
@yungyuc yungyuc added performance Profiling, runtime, and memory consumption array Multi-dimensional array implementation labels May 28, 2026
@yungyuc yungyuc moved this from Todo to In Progress in tensor operations May 28, 2026
@ThreeMonth03 ThreeMonth03 force-pushed the optimize-matmul-fast branch 2 times, most recently from fcc2654 to 9fcfa1b Compare May 28, 2026 14:14
Copy link
Copy Markdown
Collaborator Author

@ThreeMonth03 ThreeMonth03 left a comment

Choose a reason for hiding this comment

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

  • Clarify: why vcpkg openblas does not have lapack.
  • Remove the namespace blas.
  • Name the wrapper to vendor code as *_blas().

I think there are some existing problem in CI now. @yungyuc, please review this pull request after CI works as usual.

Comment thread .github/workflows/devbuild.yml Outdated
Comment on lines +285 to +297
# vcpkg's openblas:x64-windows provides CBLAS but not LAPACK,
# since the vcpkg port explicitly builds openblas without LAPACK.
# References:
# - https://github.com/microsoft/vcpkg/blob/9733af874ef1d4c357ae0d7b4c0fc669e5a5e012/ports/openblas/portfile.cmake#L48-L56
- name: install OpenBLAS
run: |
vcpkg install openblas:x64-windows
$openblas_bin_path = @(
"C:\vcpkg\installed\x64-windows\bin",
"C:\vcpkg\installed\x64-windows\debug\bin"
) -join ";"
echo "$openblas_bin_path" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append

Copy link
Copy Markdown
Collaborator Author

@ThreeMonth03 ThreeMonth03 May 28, 2026

Choose a reason for hiding this comment

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

In the reference, vcpkg port builds openblas without turning on the flag -DBUILD_WITHOUT_LAPACK=ON.
It means that vcpkg install openblas:x64-windows doesn't install lapack.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please also clarify why vcpkg openblas turns off lapack.

modmesh uses both BLAS and LAPACK. The Linux installation has both. If Windows installation of openblas does not include both, please provide a way to supplement LAPACK.

We can discuss more.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

vcpkg also affords lapack port for windows, so lapack could be installed by the following command:

vcpkg install lapack:x64-windows

vcpkg's developers mentioned that they prefer dividing LAPACK and OpenBLAS to two ports, so vcpkg openblas turns off lapack. However, they don't explain the motivation furthermore.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

In that case, we should install both blas and lapack so that the dependency is aligned across macos, linux, and windows.

Comment on lines +38 to +45
float dot_blas(size_t size, float const * lhs, float const * rhs);
double dot_blas(size_t size, double const * lhs, double const * rhs);
Complex<float> dot_blas(size_t size,
Complex<float> const * lhs,
Complex<float> const * rhs);
Complex<double> dot_blas(size_t size,
Complex<double> const * lhs,
Complex<double> const * rhs);
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Remove namespace blas; and add suffix _blas for function names.

@ThreeMonth03 ThreeMonth03 requested a review from yungyuc May 28, 2026 14:31
@ThreeMonth03 ThreeMonth03 force-pushed the optimize-matmul-fast branch from 9fcfa1b to dd3dcaf Compare May 28, 2026 16:33
Copy link
Copy Markdown
Member

@yungyuc yungyuc left a comment

Choose a reason for hiding this comment

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

Remaining points:

  • Clarify: why vcpkg openblas does not have lapack.
  • *_veclib() should also be renamed to *_blas when they are on the same call stack of the wrappers already renamed.

@yungyuc, please review this pull request after CI works as usual.

It's OK. The passing CI jobs provided sufficient confidence.

Comment thread .github/workflows/devbuild.yml Outdated
Comment on lines +285 to +297
# vcpkg's openblas:x64-windows provides CBLAS but not LAPACK,
# since the vcpkg port explicitly builds openblas without LAPACK.
# References:
# - https://github.com/microsoft/vcpkg/blob/9733af874ef1d4c357ae0d7b4c0fc669e5a5e012/ports/openblas/portfile.cmake#L48-L56
- name: install OpenBLAS
run: |
vcpkg install openblas:x64-windows
$openblas_bin_path = @(
"C:\vcpkg\installed\x64-windows\bin",
"C:\vcpkg\installed\x64-windows\debug\bin"
) -join ";"
echo "$openblas_bin_path" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please also clarify why vcpkg openblas turns off lapack.

modmesh uses both BLAS and LAPACK. The Linux installation has both. If Windows installation of openblas does not include both, please provide a way to supplement LAPACK.

We can discuss more.

Comment thread cpp/modmesh/buffer/SimpleArray.hpp Outdated
* Perform matrix multiplication using vendor BLAS when available.
*/
template <typename A, typename T>
A SimpleArrayMixinCalculators<A, T>::matmul_veclib(A const & other) const
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

SimpleArrayMixinClaculator::matmul_veclib() should also be renamed to matmul_blas like that in SimpleArrayMatmulHelper, because they are on the same call stack.

@ThreeMonth03 ThreeMonth03 force-pushed the optimize-matmul-fast branch from dd3dcaf to 30c3008 Compare May 29, 2026 03:26
Copy link
Copy Markdown
Collaborator Author

@ThreeMonth03 ThreeMonth03 left a comment

Choose a reason for hiding this comment

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

  • Clarify: why vcpkg openblas does not have lapack.
  • *_veclib() should also be renamed to *_blas() when they are on the same call stack of the wrappers already renamed.
    I missed to modify the public api name, and I fix it now. @yungyuc , please see the discussion.

Comment thread .github/workflows/devbuild.yml Outdated
Comment on lines +285 to +297
# vcpkg's openblas:x64-windows provides CBLAS but not LAPACK,
# since the vcpkg port explicitly builds openblas without LAPACK.
# References:
# - https://github.com/microsoft/vcpkg/blob/9733af874ef1d4c357ae0d7b4c0fc669e5a5e012/ports/openblas/portfile.cmake#L48-L56
- name: install OpenBLAS
run: |
vcpkg install openblas:x64-windows
$openblas_bin_path = @(
"C:\vcpkg\installed\x64-windows\bin",
"C:\vcpkg\installed\x64-windows\debug\bin"
) -join ";"
echo "$openblas_bin_path" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

vcpkg also affords lapack port for windows, so lapack could be installed by the following command:

vcpkg install lapack:x64-windows

vcpkg's developers mentioned that they prefer dividing LAPACK and OpenBLAS to two ports, so vcpkg openblas turns off lapack. However, they don't explain the motivation furthermore.

Comment on lines 382 to +438
@@ -434,8 +434,8 @@ class MODMESH_PYTHON_WRAPPER_VISIBILITY WrapSimpleArray
{ self.idiv(scalar); })
.def("imatmul", [](wrapped_type & self, wrapped_type const & other)
{ self.imatmul(other); })
.def("imatmul_veclib", [](wrapped_type & self, wrapped_type const & other)
{ self.imatmul_veclib(other); })
.def("imatmul_blas", [](wrapped_type & self, wrapped_type const & other)
{ self.imatmul_blas(other); })
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Rename suffix *_veclib() to *_blas().

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Beautiful.

Copy link
Copy Markdown
Member

@yungyuc yungyuc left a comment

Choose a reason for hiding this comment

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

  • Install both blas and lapack on windows.

Comment thread .github/workflows/devbuild.yml Outdated
Comment on lines +285 to +297
# vcpkg's openblas:x64-windows provides CBLAS but not LAPACK,
# since the vcpkg port explicitly builds openblas without LAPACK.
# References:
# - https://github.com/microsoft/vcpkg/blob/9733af874ef1d4c357ae0d7b4c0fc669e5a5e012/ports/openblas/portfile.cmake#L48-L56
- name: install OpenBLAS
run: |
vcpkg install openblas:x64-windows
$openblas_bin_path = @(
"C:\vcpkg\installed\x64-windows\bin",
"C:\vcpkg\installed\x64-windows\debug\bin"
) -join ";"
echo "$openblas_bin_path" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

In that case, we should install both blas and lapack so that the dependency is aligned across macos, linux, and windows.

Comment on lines 382 to +438
@@ -434,8 +434,8 @@ class MODMESH_PYTHON_WRAPPER_VISIBILITY WrapSimpleArray
{ self.idiv(scalar); })
.def("imatmul", [](wrapped_type & self, wrapped_type const & other)
{ self.imatmul(other); })
.def("imatmul_veclib", [](wrapped_type & self, wrapped_type const & other)
{ self.imatmul_veclib(other); })
.def("imatmul_blas", [](wrapped_type & self, wrapped_type const & other)
{ self.imatmul_blas(other); })
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Beautiful.

@ThreeMonth03 ThreeMonth03 force-pushed the optimize-matmul-fast branch 2 times, most recently from 445efd0 to a43243c Compare May 29, 2026 07:06
@ThreeMonth03 ThreeMonth03 force-pushed the optimize-matmul-fast branch from a43243c to a4a77cb Compare May 29, 2026 08:17
Copy link
Copy Markdown
Collaborator Author

@ThreeMonth03 ThreeMonth03 left a comment

Choose a reason for hiding this comment

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

  • Install both blas and lapack on windows.

@yungyuc I install LAPACK on windows runner. Please review it.

Comment on lines +285 to 294
- name: install BLAS and LAPACK
run: |
vcpkg install openblas:x64-windows lapack:x64-windows
$vcpkg_bin_path = @(
"C:\vcpkg\installed\x64-windows\bin",
"C:\vcpkg\installed\x64-windows\debug\bin"
) -join ";"
echo "$vcpkg_bin_path" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append

- name: dependency by pip
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Install LAPACK and BLAS in windows runner.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

Comment thread Makefile
Comment on lines +45 to +52
# Let CMake find vcpkg-provided OpenBLAS/LAPACK headers, import libraries, and
# package metadata during configure and link on Windows.
ifeq ($(OS),Windows_NT)
CMAKE_TOOLCHAIN_FILE ?= C:/vcpkg/scripts/buildsystems/vcpkg.cmake
CMAKE_ARGS += -DCMAKE_TOOLCHAIN_FILE=$(CMAKE_TOOLCHAIN_FILE)
CMAKE_ARGS += -DVCPKG_TARGET_TRIPLET=x64-windows
endif

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

To ensure makefile works as usual on Windows, additional flags are added in makefile.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Good catch.

@ThreeMonth03 ThreeMonth03 requested a review from yungyuc May 29, 2026 08:26
Copy link
Copy Markdown
Member

@yungyuc yungyuc left a comment

Choose a reason for hiding this comment

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

LGTM

Comment on lines +285 to 294
- name: install BLAS and LAPACK
run: |
vcpkg install openblas:x64-windows lapack:x64-windows
$vcpkg_bin_path = @(
"C:\vcpkg\installed\x64-windows\bin",
"C:\vcpkg\installed\x64-windows\debug\bin"
) -join ";"
echo "$vcpkg_bin_path" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append

- name: dependency by pip
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

Comment thread Makefile
Comment on lines +45 to +52
# Let CMake find vcpkg-provided OpenBLAS/LAPACK headers, import libraries, and
# package metadata during configure and link on Windows.
ifeq ($(OS),Windows_NT)
CMAKE_TOOLCHAIN_FILE ?= C:/vcpkg/scripts/buildsystems/vcpkg.cmake
CMAKE_ARGS += -DCMAKE_TOOLCHAIN_FILE=$(CMAKE_TOOLCHAIN_FILE)
CMAKE_ARGS += -DVCPKG_TARGET_TRIPLET=x64-windows
endif

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Good catch.

@yungyuc yungyuc merged commit 3ae6690 into solvcon:master May 29, 2026
16 of 17 checks passed
@github-project-automation github-project-automation Bot moved this from In Progress to Done in tensor operations May 29, 2026
@yungyuc yungyuc linked an issue May 29, 2026 that may be closed by this pull request
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

array Multi-dimensional array implementation performance Profiling, runtime, and memory consumption

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Enable SimpleArray::matmul_veclib() on Linux and Windows using openblas

2 participants