xtensor-stack / xtensor-blas

BLAS extension to xtensor
BSD 3-Clause "New" or "Revised" License
157 stars 55 forks source link

Segmentation fault using dot for tensors with one empty dimension #132

Open dariusarnold opened 5 years ago

dariusarnold commented 5 years ago

I want to do matrix multiplication for two tensors with shape (1, 2, 2). Using a (2, 2) shape works, but using an array with an empty dimension (shape (1, 2, 2)) gives Process finished with exit code 139 (interrupted by signal 11: SIGSEGV). I am using xtensor-blas 0.16.1 from conda. I have the following blas library installed:

ii  libopenblas-base:amd64  0.3.5+ds-2  amd64  Optimized BLAS (linear algebra) library (shared library)
ii  libopenblas-dev:amd64   0.3.5+ds-2  amd64  Optimized BLAS (linear algebra) library (development files)

Code example:

#include <xtensor/xtensor.hpp>
#include <xtensor/xio.hpp>
#include "xtensor-blas/xlinalg.hpp"
template <typename T, size_t N>

std::ostream& operator<<(std::ostream& os, std::array<T, N> a) {
    os << "Shape(";
    for (auto& i : a) {
        os << i << " ";
    }
    os << ")";
    return os;
}

using complex = std::complex<double>;

void works() {
    using matrix_t = xt::xtensor<complex, 2>;
    matrix_t A{{std::cos(35), 0}, {0, 1}};
    matrix_t B{{1, -1}, {-1, 1}};
    std::cout << A << std::endl;
    std::cout << B << std::endl;
    std::cout << A.shape() << std::endl;
    std::cout << B.shape() << std::endl;
    auto G = xt::linalg::dot(A, B);
    std::cout << G << std::endl;
}

void fails() {
    using matrix_t = xt::xtensor<complex, 3>;
    matrix_t A{{{std::cos(35), 0}, {0, 1}}};
    matrix_t B{{{1, -1}, {-1, 1}}};
    std::cout << A << std::endl;
    std::cout << B << std::endl;
    std::cout << A.shape() << std::endl;
    std::cout << B.shape() << std::endl;
    auto G = xt::linalg::dot(A, B);
    std::cout << G << std::endl;
}

int main() {
    std::cout << "Working code: " << std::endl;
    works();
    std::cout << "\nSegmentation fault: " << std::endl;
    fails();
}

Output:

Working code: 
{{-0.903692+0.i,  0.      +0.i},
 { 0.      +0.i,  1.      +0.i}}
{{ 1.+0.i, -1.+0.i},
 {-1.+0.i,  1.+0.i}}
Shape(2 2 )
Shape(2 2 )
{{-0.903692+0.i,  0.903692+0.i},
 {-1.      +0.i,  1.      +0.i}}

Segmentation fault: 
{{{-0.903692+0.i,  0.      +0.i},
  { 0.      +0.i,  1.      +0.i}}}
{{{ 1.+0.i, -1.+0.i},
  {-1.+0.i,  1.+0.i}}}
Shape(1 2 2 )
Shape(1 2 2 )

EDIT: I know using xt::squeeze to remove empty dimensions works. I am still curious why the error occurs.

tdegeus commented 5 years ago

I cannot reproduce this segmentation fault. Can you tell us how you build (on which platform, with which compiler, ...)?

dariusarnold commented 5 years ago

I am using CMake 3.15.3 (bundled with CLion) on Debian 10. My minimal CMakeLists.txt:

cmake_minimum_required(VERSION 3.14)
project(doublebeam_cpp CXX)

set(CMAKE_CXX_STANDARD 17)

find_package(xtl REQUIRED)
find_package(xtensor REQUIRED)
find_package(xtensor-blas REQUIRED)
add_definitions(-DHAVE_CBLAS=1)
find_package(BLAS REQUIRED)
find_package(LAPACK REQUIRED)

add_executable(scratch_1 /home/darius/.CLion2019.2/config/scratches/scratch_1.cpp)
target_link_libraries(scratch_1 xtensor ${BLAS_LIBRARIES} ${LAPACK_LIBRARIES})
target_include_directories(scratch_1 PRIVATE ${xtensor_blas_INCLUDE_DIRS})

compiler

$ c++ -v
Using built-in specs.
COLLECT_GCC=c++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/8/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 8.3.0-6' --with-bugurl=file:///usr/share/doc/gcc-8/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++ --prefix=/usr --with-gcc-major-version-only --program-suffix=-8 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --enable-default-pie --with-system-zlib --with-target-system-zlib --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 8.3.0 (Debian 8.3.0-6) 

Is there any other information that would be helpful to you?