E_1996
E_1996

Reputation: 89

How to convert between C++ ArrayFire and C++ Eigen matrices?

I am new to Eigen and looking to learn ArrayFire for GPU acceleration. I would like to implement ArrayFire within my existing Eigen code rather than having to re-code all of my functions.

Is there a way to convert from an ArrayFire matrix to an Eigen matrix, and vice-versa?

Thanks!

Upvotes: 1

Views: 173

Answers (1)

Homer512
Homer512

Reputation: 13295

Untested since I'm just looking at the ArrayFire documentation.

  1. Copy Eigen matrix into a new AF array using this constructor:
Eigen::MatrixXf hostside = ...;
af::array deviceside(hostside.rows(), hostside.cols(), hostside.data());

It's important that a plain matrix or 2D array is used on Eigen's side (not an Eigen::Ref or other type of sub-matrix block) since neither the constructor nor any of the copy functions accept a column stride.

  1. Copy host data into an existing device-side array
deviceside.write(hostside.data(), hostside.size() * sizeof(float));
  1. Copy device-side data into an Eigen matrix
Eigen::MatrixXf hostside(deviceside.dims(0), deviceside.dims(1));
deviceside.host(hostside.data());

Upvotes: 2

Related Questions