Evaluating expressions consisting of elementwise matrix operations in Thrust

两盒软妹~` 提交于 2020-01-10 05:49:05

问题


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.



来源:https://stackoverflow.com/questions/17487322/evaluating-expressions-consisting-of-elementwise-matrix-operations-in-thrust

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!