ginkgo icon indicating copy to clipboard operation
ginkgo copied to clipboard

Code sample to fill `gko::matrix::Csr<double, int>` from `Eigen::SparseMatrix`?

Open bp-intact-solutions opened this issue 2 years ago • 6 comments

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

bp-intact-solutions avatar Jun 04 '22 18:06 bp-intact-solutions

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.

upsj avatar Jun 04 '22 18:06 upsj

#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()),
        ~~~~~^

ghost avatar Jun 04 '22 19:06 ghost

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())
  );

upsj avatar Jun 04 '22 19:06 upsj

Is there a new release planned soon?

ghost avatar Jun 04 '22 19:06 ghost

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())
  );

ghost avatar Jun 04 '22 19:06 ghost

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.

upsj avatar Jun 04 '22 19:06 upsj