External Issue: Incorrect result with dot(mat, mat[.., 1]) when using BLAS

18118, "redhatturtle", "Incorrect result with dot(mat, mat[.., 1]) when using BLAS", "2021-07-27T01:40:35Z"

Summary of Problem

Matrix multiplication with the Linear Algebra module gives a different, and arguably incorrect result, when using the BLAS implementation for the multiplication of a matrix by a column subdomain of a matrix. When compiling with BLAS the subdomain is always taken as a line subdomain. Using a .T method to transpose the sub-matrix "fixes" the result but obliviously isn't a reasonable behavior either.

It's my understanding that when multiplying matrices and vectors with .dot the vector will be assumed as a line when it's in the first argument and a column when it's in the second. And this should not depend on on the shape of the parent array. Furthermore transposing a vector shouldn't affect the results in any way. All the results I got without using BLAS were as I expected.

Obs: The operation seems to work fine with integer arrays, possibly because it never relies on a BLAS subroutine for integers?

Steps to Reproduce

Source Code:

use LinearAlgebra;

var matrix : [1..2, 1..2] real = reshape([1, 2, 3, 4], {1..2,1..2});

writeln("Matrix:");
writeln(matrix);

// Works fine. Output is [5, 11] = [1*1+2*2, 3*1+4*2]
writeln();
writeln("dot(Matrix, Matrix[1,..])");
writeln(dot(matrix, matrix[1,..]));

// Transposing the vector makes no difference, as expected.
writeln();
writeln("dot(Matrix, Matrix[1,..].T)");
writeln(dot(matrix, matrix[1,..].T));

// Different behavior depending on Linear Algebra implementations.
// Without BLAS [7,15] = [1*1+2*3, 3*1+4*3]
// With BLAS [5,11] = [1*1+2*2, 1*3+2*4] ???
writeln();
writeln("dot(Matrix, Matrix[..,1])");
writeln(dot(matrix, matrix[..,1]));

// Here transposing the vector "fixes" the BLAS version but has no effect on the other version.
writeln();
writeln("dot(Matrix, Matrix[..,1].T)");
writeln(dot(matrix, matrix[..,1].T));

Compile commands:
Without BLAS
chpl --set blasImpl=off test.chpl
and with BLAS
chpl -I/usr/include -L/usr/lib64 -lcblas -I/usr/include -L/usr/lib64 -lgfortran -L/usr/lib64 -llapacke -llapack -lcblas test.chpl

Execution command:
./a.out

Configuration Information

  • Output of chpl --version:
chpl version 1.24.1
Copyright 2020-2021 Hewlett Packard Enterprise Development LP
Copyright 2004-2019 Cray Inc.
(See LICENSE file for more details)
  • Output of $CHPL_HOME/util/printchplenv --anonymize:
CHPL_TARGET_PLATFORM: linux64
CHPL_TARGET_COMPILER: gnu
CHPL_TARGET_ARCH: x86_64
CHPL_TARGET_CPU: native
CHPL_LOCALE_MODEL: flat
CHPL_COMM: none
CHPL_TASKS: qthreads
CHPL_LAUNCHER: none
CHPL_TIMERS: generic
CHPL_UNWIND: none
CHPL_MEM: jemalloc
CHPL_ATOMICS: cstdlib
CHPL_GMP: bundled
CHPL_HWLOC: bundled
CHPL_REGEXP: re2 *
CHPL_LLVM: none
CHPL_AUX_FILESYS: none
  • Back-end compiler and version, e.g. gcc --version:
gcc (SUSE Linux) 11.1.1 20210625 [revision 62bbb113ae68a7e724255e17143520735bcb9ec9]
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.