Evaluating expressions consisting of elementwise m

2020-02-15 04:11发布

问题:

I would like to use Thrust to evaluate expressions consisting of elementwise matrix operations. To make it clear, let us consider an expression like:

D=A*B+3*sin(C)

where A, B, C and D are matrices, of course of the same size.

The Thrust Quick Start Guide provides the saxpy example for which y is used both as input and as output, while in my case the output argument is different from the input ones which, by the way, are more than two. At Element-by-element vector multiplication with CUDA, the case of output different than the input, but of only two inputs, is considered.

Could anyone provide some suggestions (and possibly the rationale behind) on how using Thrust to implement the expression above (output matrix different from the inputs and more than two inputs)? Thanks.

回答1:

Here's how to implement that computation with Newton, which is the library mentioned in talonmies' comment:

#include <newton/newton.hpp>

int main()
{
  float a[4] = {1.0, 1.0, 1.0, 1.0};
  float b[4] = {2.0, 2.0, 2.0, 2.0};
  float c[4] = {3.0, 3.0, 3.0, 3.0};
  float d[4] = {4.0, 4.0, 4.0, 4.0};

  newton::numeric_vector<float> A = a;
  newton::numeric_vector<float> B = b;
  newton::numeric_vector<float> C = c;
  newton::numeric_vector<float> D = d;

  D = A * B + 3.f * sin(C);

  return 0;
}

The library is built using thrust::zip_iterator and thrust::transform_iterator to implement expressions with an arbitrary number of inputs. You can refer to the implementation for the details.



标签: cuda thrust