I have a C++ function which I want to run from Python. For this I use Cython. My C++ function relies heavily on Eigen matrices which I map to Python's Numpy matrices using Eigency.
I cannot get this to work for the case where I have a list of Numpy matrices.
What does works (mapping a plain Numpy matrix to an Eigen matrix):
I have a C++ function which in the header (Header.h) looks like:
float MyCppFunction(Eigen::Map<Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>> &inputMatrix);
In my CythonFile.pyx file I have (and create the maps using Eigency as explained here):
cdef extern from "Header.h":
cdef void _MyCppFunction "MyCppFunction"(FlattenedMapWithOrder[Matrix, float, Dynamic, Dynamic, RowMajor] &)
and
def my_python_function(np.ndarray[ndim=2, dtype=np.float32_t] my_matrix)
return _MyCppFunction(FlattenedMapWithOrder[Matrix, float, Dynamic, Dynamic, RowMajor](my_matrix))
I can build this module using Cython and call my_python_function successfully from Python.
What does not work (mapping a list of Numpy matrices to a vector of Eigen matrices):
Now I try to do the same thing, but for a list of matrices. I cannot get this to work. What I have:
The C++ function in the header (Header.h) looks like:
float MyCppFunction(std::vector<Eigen::Map<Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>>> &inputMatrixList);
In my CythonFile.pyx file I have:
cdef extern from "Header.h":
cdef void _MyCppFunction "MyCppFunction"(vector[FlattenedMapWithOrder[Matrix, float, Dynamic, Dynamic, RowMajor]] &)
and
def my_python_function(list my_matrix_list)
cdef vector[FlattenedMapWithOrder[Matrix, float, Dynamic, Dynamic, RowMajor]] map
for matrix in my_matrix_list:
map.push_back(FlattenedMapWithOrder[Matrix, float, Dynamic, Dynamic, RowMajor](matrix))
return _MyCppFunction(map)
This won't compile unfortunately.
This concept does compile and run when I, for example, simply use a list of int which I want to map to a std::vector<int>. It does not work however, when I map a list of Numpy-matrices to a vector of Eigen matrices (which is the case I have denoted above).
The Error I get:
The error I get during compilation:
error C2664: 'float MyCppFunction(std::vector<Eigen::Map<Eigen::Matrix<float,-1,-1,1,-1,-1>,0,Eigen::Stride<0,0>>,std::allocator<Eigen::Map<Eigen::Matrix<float,-1,-1,1,-1,-1>,0,Eigen::Stride<0,0>>>> &)': cannot convert argument 1 from 'std::vector<eigency::FlattenedMap<Eigen::Matrix,float,-1,-1,1,0,0,0,-1,-1>,std::allocator<eigency::FlattenedMap<Eigen::Matrix,float,-1,-1,1,0,0,0,-1,-1>>>' to 'std::vector<Eigen::Map<Eigen::Matrix<float,-1,-1,1,-1,-1>,0,Eigen::Stride<0,0>>,std::allocator<Eigen::Map<Eigen::Matrix<float,-1,-1,1,-1,-1>,0,Eigen::Stride<0,0>>>> &' ./Header.h(21): note: see declaration of 'MyCppFunction'
My analysis so far:
This works as expected, so: I can assign a python list of int to a C++ std::vector<int>.
This works also as expected, so: I can assign a variable of type eigency::FlattenedMap<Eigen::Matrix,float,-1,-1,1,0,0,0,-1,-1> to a variable of type Eigen::Map<Eigen::Matrix<float,-1,-1,1,-1,-1>,0,Eigen::Stride<0,0>>.
When I wrap the latter two variables in a list/vector, then I cannot assign this variables:
std::vector<eigency::FlattenedMap<Eigen::Matrix,float,-1,-1,1,0,0,0,-1,-1>, std::allocator<eigency::FlattenedMap<Eigen::Matrix,float,-1,-1,1,0,0,0,-1,-1>>> to a variable of type std::vector<Eigen::Map<Eigen::Matrix<float,-1,-1,1,-1,-1>,0,Eigen::Stride<0,0>>, std::allocator<Eigen::Map<Eigen::Matrix<float,-1,-1,1,-1,-1>,0,Eigen::Stride<0,0>>>>.
Maybe it has to do with the allocator part but I don't know since I'm not really a C++ expert. Does anybody has a solution to map a list of Numpy matrices to a vector of Eigen matrices? Preferably, following the same patterns as above but other solutions are also welcome.
My source code to reproduce:
Below the source code I use to test. It has a function accepting a plain Eigen/Numpy matrix, and a function accepting a Vector/List of Eigen/Numpy matrices.
The code compiles if you comment out all passages of the vector variants. Else, I get a compile error. The (first) compile error I get when compiling is: .\source_cpp_cython/cpp_source_cpp.h(16): error C2065: 'FlattenedMapWithOrder': undeclared identifier.
I use the Microsoft Visual Studio Compiler (MSVC) 2019 on Windows. I also use Eigen version 3.4.0-rc1 in case it is relevant.
cython_source.pyx
# distutils: language = c++
# distutils: sources = source_cpp_cython/cpp_source_cpp.cpp
from eigency.core cimport * # Docs: https://pypi.org/project/eigency/1.4/
from libcpp.vector cimport vector
import numpy as np
cimport numpy
cdef extern from "source_cpp_cython/cpp_source_cpp.h":
cdef float _MyCppFunction "MyCppFunction"(
FlattenedMapWithOrder[Matrix, float, Dynamic, Dynamic, RowMajor] &
)
cdef float _MyCppFunctionVector "MyCppFunctionVector"(
vector[FlattenedMapWithOrder[Matrix, float, Dynamic, Dynamic, RowMajor]] &
)
def my_python_function(np.ndarray[ndim=2, dtype=np.float32_t] my_matrix):
cdef FlattenedMapWithOrder[Matrix, float, Dynamic, Dynamic, RowMajor] my_matrix_cpp
my_matrix_cpp = FlattenedMapWithOrder[Matrix, float, Dynamic, Dynamic, RowMajor](my_matrix)
return _MyCppFunction(my_matrix_cpp)
def my_python_function_vector(list my_matrix_list):
cdef vector[FlattenedMapWithOrder[Matrix, float, Dynamic, Dynamic, RowMajor]] matrix_map_vec
cdef FlattenedMapWithOrder[Matrix, float, Dynamic, Dynamic, RowMajor] my_matrix_cpp
for my_matrix in my_matrix_list:
my_matrix_cpp = FlattenedMapWithOrder[Matrix, float, Dynamic, Dynamic, RowMajor](my_matrix)
matrix_map_vec.push_back(my_matrix_cpp)
return _MyCppFunctionVector(matrix_map_vec)
cpp_source_cpp.h
#pragma once
#include <Eigen/Dense>
#include <Eigen/Core>
#include <vector>
#include <numpy/ndarraytypes.h>
#include <complex>
typedef ::std::complex< double > __pyx_t_double_complex;
typedef ::std::complex< float > __pyx_t_float_complex;
#include "eigency_cpp.h"
float MyCppFunction(
const Eigen::Map<Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>>& inputMatrix
);
float MyCppFunctionVector(
const std::vector<FlattenedMapWithOrder<Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>>>& inputMatrixList
);
cpp_source_cpp.cpp
#include "cpp_source_cpp.h"
float MyCppFunction(
const Eigen::Map<Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>>& inputMatrix
)
{
// std::vector<FlattenedMapWithOrder<Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>>> test;
return 5.0;
}
float MyCppFunctionVector(
const std::vector<FlattenedMapWithOrder<Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>>>& inputMatrixList
)
{
//Convert FlattenedMap to Eigen-Map.
std::vector<Eigen::Map<Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>> convertedMatrixList(
inputMatrixList.begin(), inputMatrixList.end() );
return 6.0;
}
The relevant passages from my cython setup file, setup_cython_module.py are:
# Some constants
SOURCE_FOLDER_NAME = "source_cpp_cython"
OUTPUT_FOLDER_NAME = "cython_module"
# Build extensions list
extensions = [
Extension(f"{OUTPUT_FOLDER_NAME}.{OUTPUT_FOLDER_NAME}",
[f"{SOURCE_FOLDER_NAME}/cython_source.pyx"],
include_dirs=["."] + [f"{SOURCE_FOLDER_NAME}"]
+ [f"{SOURCE_FOLDER_NAME}\Eigen"] + eigency.get_includes(include_eigen=False)
+ [numpy.get_include()],
language='c++',
# extra_compile_args=['/MT'], # To let the Microsoft compiler use a specific lib for threading required by OpenCV.
)
]
# Build cython package
dist = setup(
name=f"{OUTPUT_FOLDER_NAME}",
version="1.0",
ext_modules=cythonize(extensions, language_level="3"), # , gdb_debug=True),
packages=[f"{OUTPUT_FOLDER_NAME}"]
)
The full log output from Cython:
Created output directory: D:\Default_Folders\Documents\Development\RepoStefan\CythonTest\cython_module
running build_ext
building 'cython_module.cython_module' extension
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30037\bin\HostX86\x64\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -IC:\ProgramData\Miniconda3\envs\cenv38rl\lib\site-packages\eigency -I. -Isource_cpp_cython -Isource_cpp_cython\Eigen -IC:\ProgramData\Miniconda3\envs\cenv38rl\lib\site-packages\numpy\core\include -IC:\ProgramData\Miniconda3\envs\cenv38rl\include -IC:\ProgramData\Miniconda3\envs\cenv38rl\include "-IC:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30037\ATLMFC\include" "-IC:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30037\include" "-IC:\Program Files (x86)\Windows Kits\NETFXSDK\4.8\include\um" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\shared" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\um" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\winrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\cppwinrt" /EHsc /Tpsource_cpp_cython/cython_source.cpp /Fobuild\temp.win-amd64-3.8\Release\source_cpp_cython/cython_source.obj /MT
cl : Command line warning D9025 : overriding '/MD' with '/MT'
cython_source.cpp
C:\ProgramData\Miniconda3\envs\cenv38rl\lib\site-packages\numpy\core\include\numpy\npy_1_7_deprecated_api.h(14) : Warning Msg: Using deprecated NumPy API, disable it with #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
.\source_cpp_cython/cpp_source_cpp.h(17): error C2065: 'FlattenedMapWithOrder': undeclared identifier
.\source_cpp_cython/cpp_source_cpp.h(17): error C2275: 'Eigen::Matrix<float,-1,-1,1,-1,-1>': illegal use of this type as an expression
.\source_cpp_cython/cpp_source_cpp.h(17): note: see declaration of 'Eigen::Matrix<float,-1,-1,1,-1,-1>'
.\source_cpp_cython/cpp_source_cpp.h(17): error C2974: 'std::vector': invalid template argument for '_Ty', type expected
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30037\include\vector(443): note: see declaration of 'std::vector'
.\source_cpp_cython/cpp_source_cpp.h(17): error C2976: 'std::vector': too few template arguments
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30037\include\vector(443): note: see declaration of 'std::vector'
.\source_cpp_cython/cpp_source_cpp.h(17): error C2143: syntax error: missing ')' before '>'
.\source_cpp_cython/cpp_source_cpp.h(17): error C2059: syntax error: '>'
.\source_cpp_cython/cpp_source_cpp.h(18): error C2059: syntax error: ')'
source_cpp_cython/cython_source.cpp(1964): error C2664: 'float MyCppFunctionVector(const std::vector)': cannot convert argument 1 from 'std::vector<eigency::FlattenedMap<Eigen::Matrix,float,-1,-1,1,0,0,0,-1,-1>,std::allocator<eigency::FlattenedMap<Eigen::Matrix,float,-1,-1,1,0,0,0,-1,-1>>>' to 'const std::vector'
source_cpp_cython/cython_source.cpp(1964): note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
.\source_cpp_cython/cpp_source_cpp.h(16): note: see declaration of 'MyCppFunctionVector'
error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Tools\\MSVC\\14.29.30037\\bin\\HostX86\\x64\\cl.exe' failed with exit status 2
Process finished with exit code 1
Thank you very much!