ginkgo
ginkgo copied to clipboard
Code sample to fill `gko::matrix::Csr<double, int>` from `Eigen::SparseMatrix`?
Hello,
do you have some examples for filling a gko::matrix::Csr<double, int>
from an Eigen::SparseMatrix
?
Most of the examples load the matrices and vectors from files (in data folder).
Thank you.
Best regards.
BP
We don't really provide an interface for Eigen right now, and the Eigen SparseMatrix format does not map ideally to Ginkgo's matrix types, unless it is compressed explicitly. We need to add an example for that anyways, but what you basically do is use the constructor of Csr taking existing arrays as input and pass array views to the Eigen pointers (untested code!):
gko::Csr<>::create(exec,
gko::dim<2>(mtx.rows(), mtx.cols()),
gko::make_array_view(exec, mtx.nonZeros(), mtx.valuePtr()),
gko::make_array_view(exec, mtx.nonZeros(), mtx.innerIndexPtr()),
gko::make_array_view(exec, mtx.rows() + 1, mtx.outerIndexPtr()));
I'm guessing here at the purpose of innerIndexPtr and outerIndexPtr, since supposedly, SparseMatrix is like CSR, and thus outerIndexPtr does not actually store indices, but offsets.
#include <ginkgo/ginkgo.hpp>
#include <ginkgo/core/base/executor.hpp>
#include <ginkgo/core/base/range.hpp>
#include <ginkgo/core/matrix/csr.hpp>
#include <ginkgo/core/matrix/dense.hpp>
auto mat_A = gko::matrix::Csr<double, int>::create((exec), gko::dim<2>{A.rows(), A.cols()},
gko::make_array_view((exec), A.nonZeros(), A.valuePtr()),
gko::make_array_view((exec), A.nonZeros(), A.innerIndexPtr()),
gko::make_array_view((exec), A.rows() + 1, A.outerIndexPtr())
);
error: no member named 'make_array_view' in namespace 'gko'
gko::make_array_view((exec), A.nonZeros(), A.valuePtr()),
~~~~~^
Sorry, I'm always talking about the latest Ginkgo version. In 1.4.0, you need to specify the type explicitly:
auto mat_A = gko::matrix::Csr<double, int>::create((exec), gko::dim<2>{A.rows(), A.cols()},
gko::Array<int>::view(exec, A.nonZeros(), A.valuePtr()),
gko::Array<int>::view(exec, A.nonZeros(), A.innerIndexPtr()),
gko::Array<double>::view(exec, A.rows() + 1, A.outerIndexPtr())
);
Is there a new release planned soon?
one minor change:
auto mat_A = gko::matrix::Csr<double, int>::create((exec), gko::dim<2>{A.rows(), A.cols()},
gko::Array<double>::view(exec, A.nonZeros(), A.valuePtr()),
gko::Array<int>::view(exec, A.nonZeros(), A.innerIndexPtr()),
gko::Array<int>::view(exec, A.rows() + 1, A.outerIndexPtr())
);
A new release is planned in the next maybe 1-2 months, but since it involves a lot of new features, we want to make sure we got everything right before committing to an interface that cannot change until the next major release.