我想计算
对于矢量
和
,哪里
表示矢量的大小
.由于这涉及取两个向量的每个相应分量之间的差的平方和的平方根,因此它应该是高度可并行化的任务.我在Windows 10上使用Cuda和Thrust,通过Cygwin .Cuda和Thrust都在工作.
下面的代码编译并运行(使用nvcc),但仅仅因为我已经在底部注释了三行main,我认为每行都应该工作但不会.func::operator()(tup t)认为我传递的论据实际上不是tup类型.
为了使其更有可能至少编译,我还注释了运算符的实际主体.运算符应该找到输入tup的元素之间的平方差异.unary_op从transform_reduce(在这种情况下func())的减少将添加这些,给出我的矢量差异的范数平方.
#include <iostream>
#include <stdlib.h>
#include <thrust/device_vector.h>
#include <thrust/transform.h>
#include <thrust/tuple.h>
#include <thrust/transform_reduce.h>
#include <thrust/iterator/zip_iterator.h>
typedef thrust::device_vector<float> dvec;
typedef dvec::iterator iter;
typedef thrust::tuple<iter, iter> tup;
struct func: public thrust::unary_function<tup, float>
{
__device__ float operator()(tup t) //difsq
{
// I've commented out these two lines for testing purposes:
// float f = thrust::get<0>(t) - thrust::get<1>(t);
// return f*f;
return 3.14;
} …Run Code Online (Sandbox Code Playgroud)