简体   繁体   中英

How to map a list of Numpy matrices to a vector of Eigen matrices in Cython

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!

The problem is, that FlattenedMapWithOrder is more than just Eigen::Map - it is Eigen::Map plus pointer to the numpy-array:

class FlattenedMap: public MapBase<EigencyDenseBase<Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>, _MapOptions, Eigen::Stride<_StrideOuter, _StrideInner> >  {
public:
    ...
    operator Base() const {
        return static_cast<Base>(*this);
    }
...
private:
    PyArrayObject * const object_;
};

While it is possible to use implicit cast from FlattenedMapWithOrder to Eigen::Map due to operator Base (which by the way is a little bit "dangereous" as the new Eigen::Map will have dangling pointers, once the original FlattenedMapWithOrder object gets destroyed), it is not possible to automatically cast std::vector<FlattenedMapWithOrder> to std::vector<Eigen::Map> - it is just not allowed in C++ .

So you have to perform the conversion manually.

First, it doesn't cut it to use static_cast (as it is done for FlattenedMaptWithOrder to Eigen::Map ) these classes have different memory layouts (additional object_ member) and it would work only for vectors of lenght 1.

That means we need to create an temporary vector of right type, which could be done as follows:

cdef extern from "Header.h":
    """
       void MyCppFunction(std::vector<FlattenedMapWithOrder<Eigen::Matrix,float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>& vec){
           // convert:
           std::vector<Eigen::Map<Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>> mat_vec(vec.begin(), vec.end());
           // use converted vector:
           MyCppFunction(mat_vec);
    }
    """
    cdef void _MyCppFunction "MyCppFunction"(vector[FlattenedMapWithOrder[Matrix, float, Dynamic, Dynamic, RowMajor]] &)

so basically we introduce a proxy, which would be called from Cython and would create a temporary vector of correct type and then call the original function.

Thanks to @ead I found a solution.

FlattenedMapWithOrder has implementation so it can be assinged to an Eigen::Matrix . However, std::vector does not have such functionality and since std::vector<FlattenedMapWithOrder> and std::vector<Eigen::Matrix> are of a different type, they cannot be assigned to one another. More about this here . The implementation in FlattenedMapWithOrder mentioned above is here .

To solve this, the function in the C++ code called from Cython need to simply have as input argument the matching type: std::vector<FlattenedMapWithOrder> . To do this, the C++ code needs to know the definition of type FlattenedMapWithOrder .

To do this, you need to #include "eigency_cpp.h" . Unfortunately, this header is not self contained. Therefore, (credits to @ead) I added these lines:

#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"

Using this, I was able to declare this function in my C++ code:

void MyCppFunctionVector(
    const std::vector<eigency::FlattenedMap<Eigen::Matrix, float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>>& inputMatrixList,
    std::vector<eigency::FlattenedMap<Eigen::Matrix, float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>>& outputMatrixList
    );

My *.pxy file looks like:

cdef extern from "source_cpp_cython/cpp_source_cpp.h":
    cdef void _MyCppFunctionVector "MyCppFunctionVector"(
                vector[FlattenedMapWithOrder[Matrix, float, Dynamic, Dynamic, RowMajor]] &,
                vector[FlattenedMapWithOrder[Matrix, float, Dynamic, Dynamic, RowMajor]] &
                )
                
                
def my_python_function_vector(
        list my_matrix_list_input,
        list my_matrix_list_output
    ):
    cdef vector[FlattenedMapWithOrder[Matrix, float, Dynamic, Dynamic, RowMajor]] matrix_map_vec_input
    cdef FlattenedMapWithOrder[Matrix, float, Dynamic, Dynamic, RowMajor] my_matrix_input_cpp
    cdef vector[FlattenedMapWithOrder[Matrix, float, Dynamic, Dynamic, RowMajor]] matrix_map_vec_output
    cdef FlattenedMapWithOrder[Matrix, float, Dynamic, Dynamic, RowMajor] my_matrix_output_cpp

    # Convert input matrix to C++ type.
    for my_matrix_input in my_matrix_list_input:
        my_matrix_input_cpp = FlattenedMapWithOrder[Matrix, float, Dynamic, Dynamic, RowMajor](my_matrix_input)
        matrix_map_vec_input.push_back(my_matrix_input_cpp)

    for my_matrix_output in my_matrix_list_output:
        my_matrix_output_cpp = FlattenedMapWithOrder[Matrix, float, Dynamic, Dynamic, RowMajor](my_matrix_output)
        matrix_map_vec_output.push_back(my_matrix_output_cpp)

    # Call the C++ function.
    _MyCppFunctionVector(matrix_map_vec_input, matrix_map_vec_output)
    return my_matrix_list_output                
    

That was it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM