使用CUDA推力的元件功率操作
有没有办法用使用CUDA推力的元件功率操作,cuda,thrust,Cuda,Thrust,有没有办法用pow功能转换推力矢量?换句话说,我想将向量的每个元素x转化为pow(x,a),其中a是一个常数。请参考《推力问答开始指南》中的内容,了解如何编写带有初始化参数的函子 struct saxpy_functor { const float a; saxpy_functor(float _a) : a(_a) {} __host__ __device__ float operator()(const float& x, const fl
pow
功能转换推力矢量?换句话说,我想将向量的每个元素x
转化为pow(x,a)
,其中a
是一个常数。请参考《推力问答开始指南》中的内容,了解如何编写带有初始化参数的函子
struct saxpy_functor
{
const float a;
saxpy_functor(float _a) : a(_a) {}
__host__ __device__
float operator()(const float& x, const float& y) const {
return a * x + y;
}
};
有关如何使用初始化参数编写函子的信息,请参阅《推力Quict启动指南》
struct saxpy_functor
{
const float a;
saxpy_functor(float _a) : a(_a) {}
__host__ __device__
float operator()(const float& x, const float& y) const {
return a * x + y;
}
};
这里有一个完整的例子。正如@Eric所提到的,所需要的只是定义自己的幂函子,并使用
推力::变换
#include <thrust/sequence.h>
#include <thrust/device_vector.h>
class power_functor {
double a;
public:
power_functor(double a_) { a = a_; }
__host__ __device__ double operator()(double x) const
{
return pow(x,a);
}
};
void main() {
int N = 20;
thrust::device_vector<double> d_n(N);
thrust::sequence(d_n.begin(), d_n.end());
thrust::transform(d_n.begin(),d_n.end(),d_n.begin(),power_functor(2.));
for (int i=0; i<N; i++) {
double val = d_n[i];
printf("Device vector element number %i equal to %f\n",i,val);
}
getchar();
}
#包括
#包括
类幂函数{
双a;
公众:
幂函数(双a_u){a=a_u;}
__主机设备双运算符()(双x)常量
{
返回功率(x,a);
}
};
void main(){
int N=20;
推力:装置矢量d(n);
顺序(d_n.begin(),d_n.end());
推力:变换(d_n.begin(),d_n.end(),d_n.begin(),power_函子(2.);
对于(int i=0;i这里是一个完整的例子。正如@Eric所提到的,所需要的只是定义自己的幂函子,并使用推力::变换
#include <thrust/sequence.h>
#include <thrust/device_vector.h>
class power_functor {
double a;
public:
power_functor(double a_) { a = a_; }
__host__ __device__ double operator()(double x) const
{
return pow(x,a);
}
};
void main() {
int N = 20;
thrust::device_vector<double> d_n(N);
thrust::sequence(d_n.begin(), d_n.end());
thrust::transform(d_n.begin(),d_n.end(),d_n.begin(),power_functor(2.));
for (int i=0; i<N; i++) {
double val = d_n[i];
printf("Device vector element number %i equal to %f\n",i,val);
}
getchar();
}
#包括
#包括
类幂函数{
双a;
公众:
幂函数(双a_u){a=a_u;}
__主机设备双运算符()(双x)常量
{
返回功率(x,a);
}
};
void main(){
int N=20;
推力:装置矢量d(n);
顺序(d_n.begin(),d_n.end());
推力:变换(d_n.begin(),d_n.end(),d_n.begin(),power_函子(2.);
对于(int i=0;i您可以编写自己的函子来执行此操作您可以编写自己的函子来执行此操作方法operator()
返回int
,而不是double
?@something这是一个错误。修复了。感谢您通知我。方法operator()是否故意返回int
返回int
而不是double
?@something这是个错误。已修复。感谢您通知我。