有没有一种自动将CUDA数学函数包装在一个函数对象中的方法,以便可以应用
示例代码:
thrust :: transform
而无需手动编写函数对象?就像(我所了解的)std :: function
提供的功能一样?
thrust :: placeholders
似乎不喜欢数学函数。
std :: function
似乎不可用。示例代码:
#include <thrust/transform.h>
#include <thrust/device_vector.h>
#include <iostream>
#include <functional>
#include <math.h>
struct myfunc{
__device__
double operator()(double x,double y){
return hypot(x,y);
}
};
int main(){
double x0[10] = {3.,0.,1.,2.,3.,4.,5.,6.,7.,8.};
double y0[10] = {4.,0.,1.,2.,3.,4.,5.,6.,7.,8.};
thrust::device_vector<double> x(x0,x0+10);
thrust::device_vector<double> y(y0,y0+10);
thrust::device_vector<double> r(10);
for (int i=0;i<10;i++) std::cout << x0[i] <<" "; std::cout<<std::endl;
for (int i=0;i<10;i++) std::cout << y0[i] <<" "; std::cout<<std::endl;
// this works:
thrust::transform(x.begin(),x.end(),y.begin(),r.begin(), myfunc());
// this doesn't compile:
using namespace thrust::placeholders;
thrust::transform(x.begin(),x.end(),y.begin(),r.begin(), hypot(_1,_2));
// nor does this:
thrust::transform(x.begin(),x.end(),y.begin(),r.begin(), std::function<double(double,double)>(hypot));
for (int i=0;i<10;i++) std::cout << r[i] <<" "; std::cout<<std::endl;
}
std::bind
的东西。然后,您需要使用bind
来定义所有感兴趣的数学函数(例如hypot
)的重载。 - Jared Hoberock--expt-extended-lambda
功能,并编写auto h = [] __device__(double x, double y){return hypot(x,y);}; thrust::transform(x.begin(),x.end(),y.begin(),r.begin(), h);
。 - m.s.