From 272ee141d29e7e840c72bb1b9fe7b58a689573b8 Mon Sep 17 00:00:00 2001 From: Alexey Zarubin Date: Thu, 19 Nov 2026 12:39:58 +0300 Subject: [PATCH] Fix CUDA MxM when output aliases an input --- cubool/sources/cuda/cuda_matrix_multiply.cu | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/cubool/sources/cuda/cuda_matrix_multiply.cu b/cubool/sources/cuda/cuda_matrix_multiply.cu index 5401108..ec54cd0 100644 --- a/cubool/sources/cuda/cuda_matrix_multiply.cu +++ b/cubool/sources/cuda/cuda_matrix_multiply.cu @@ -40,6 +40,14 @@ namespace cubool { assert(this->getNrows() == M); assert(this->getNcols() == N); + if (!accumulate && (this == a || this == b)) { + // Keep both operands intact until the product is ready. + CudaMatrix temporary(M, N, mInstance); + temporary.multiply(aBase, bBase, false, checkTime); + mMatrixImpl = std::move(temporary.mMatrixImpl); + return; + } + if (!accumulate) { // Clear all values this->clearAndResizeStorageToDim(); @@ -63,4 +71,4 @@ namespace cubool { this->mMatrixImpl = std::move(result); } -}// namespace cubool \ No newline at end of file +}// namespace cubool