Skip to content

SparseMatrix::Map breaks while trying to convert to SparseMatrix

Summary

When passing a MappedSparseMatrix to a template function as a const&, Eigen fails to convert that Map into a SparseMatrix. I don't expect/want Eigen to convert it at all.

Environment

  • Operating System : Ubuntu 20.04
  • Architecture : x64
  • Eigen Version : 3.3.7, 3.4.0, 3.4.90 (82dd3710) and master (0488b708)
  • Compiler Version : Gcc9.4
  • Compile Flags : -g
  • Vector Extension : doesn't matter

Minimal Example

# CMakeLists.txt

cmake_minimum_required(VERSION 3.15)
project(eigenbug)

set(CMAKE_CXX_STANDARD 17)

find_package(Eigen3 REQUIRED)

add_executable(mwe mwe.cpp)
target_link_libraries(mwe Eigen3::Eigen)
// mwe.cpp

#include <iostream>

// Ok: 3.3.7, 3.4.0, 3.4.90 (82dd3710d)
// Broken: master (0488b708b)
#include <Eigen/SparseCore>

using Eigen::Map, Eigen::SparseMatrix, Eigen::Index;
using StorageIndex = int;
using Scalar = double;
using Matrix = SparseMatrix<Scalar,Eigen::RowMajor,StorageIndex>;

using MatrixMapOk = Map<Matrix>;
using MatrixMapBroken = Matrix::Map;
using MatrixMap = MatrixMapBroken; // change me

template<typename T>
void print(T const& t) {
  std::cout << t;
}

int main() {
  printf("Eigen %d.%d.%d\n", EIGEN_WORLD_VERSION, EIGEN_MAJOR_VERSION, EIGEN_MINOR_VERSION);

  // Create small identity matrix
  Index rows=3, cols=3, nnz=3;
  StorageIndex outer[] = {0, 1, 2, 3, 4};
  StorageIndex inner[] = {0, 1, 2, 3};
  Scalar values[] = {1, 1, 1};

  // Wrap into Eigen
  auto X = MatrixMap(rows, cols, nnz, outer, inner, values);
  print(X);
}

Steps to reproduce

  1. Build as is
  2. Optional: Change line marked with // change me from ...Broken to ...Ok and build again

What is the current bug behavior?

Build system says "meh"

What is the expected correct behavior?

Build system says "ok"

Relevant logs

[1/2] Building CXX object CMakeFiles/mwe.dir/mwe.cpp.o
FAILED: CMakeFiles/mwe.dir/mwe.cpp.o 
/usr/bin/c++  -isystem /usr/include/eigen3 -g -std=gnu++17 -MD -MT CMakeFiles/mwe.dir/mwe.cpp.o -MF CMakeFiles/mwe.dir/mwe.cpp.o.d -o CMakeFiles/mwe.dir/mwe.cpp.o -c /home/jschulze/tmp/eigen/mwe.cpp
In file included from /home/jschulze/git/eigen/Eigen/Core:171,
                 from /home/jschulze/git/eigen/Eigen/SparseCore:11,
                 from /home/jschulze/tmp/eigen/mwe.cpp:6:
/home/jschulze/git/eigen/Eigen/src/SparseCore/SparseMatrix.h: In instantiation of ‘class Eigen::SparseMatrix<double, 1313, int>’:
/home/jschulze/tmp/eigen/mwe.cpp:33:10:   required from here
/home/jschulze/git/eigen/Eigen/src/SparseCore/SparseMatrix.h:1078:52: error: static assertion failed: INVALID_MATRIX_TEMPLATE_PARAMETERS
 1078 |   EIGEN_STATIC_ASSERT((Options&(ColMajor|RowMajor))==Options,INVALID_MATRIX_TEMPLATE_PARAMETERS)
      |                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~
/home/jschulze/git/eigen/Eigen/src/Core/util/StaticAssert.h:26:50: note: in definition of macro ‘EIGEN_STATIC_ASSERT’
   26 | #define EIGEN_STATIC_ASSERT(X,MSG) static_assert(X,#MSG);
      |                                                  ^
ninja: build stopped: subcommand failed.
Edited by Jonas Schulze