- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我想计算 用于载体 和 , 其中表示 vector 的大小 .由于这涉及对两个 vector 的每个对应分量之间的差的平方和求平方根,因此它应该是一个高度可并行化的任务。我在 Windows 10 上通过 Cygwin 使用 Cuda 和 Thrust。Cuda 和 Thrust 都正常工作。
下面的代码编译并运行(使用 nvcc),但这只是因为我在 main
的底部注释掉了三行,我认为每一行都应该工作但没有。 func::operator()(tup t)
认为我传递给它的参数实际上不是 tup 类型。
为了使其更有可能至少编译,我还注释掉了运算符的实际主体。运算符应该找到输入 tup 的元素之间的平方差。 transform_reduce
(在本例中为 func()
)的归约 unary_op
将添加这些,得到差值的范数平方 vector 。
#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;
}
};
int main()
{
dvec a(40, 4.f);
dvec b(40, 3.f);
auto begin = thrust::make_zip_iterator(thrust::make_tuple(a.begin(), b.begin()));
auto end = thrust::make_zip_iterator(thrust::make_tuple(a.end(), b.end()));
//these two lines work
thrust::get<0>(begin[0]);
std::cout << thrust::get<0>(begin[0]) - thrust::get<1>(begin[0]);
//these three lines do not
//thrust::transform_reduce(begin, end, func(), 0.0f, thrust::plus<float>());
//func()(begin[0]);
//thrust::transform(begin, end, begin, func());
std::cout << "done" << std::endl;
return 0;
}
我收到此错误:(我的程序名为 sandbox.cu
)
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5\bin/../include\thrust/detail/tuple.inl(310): error: no instance of constructor "thrust::detail::normal_iterator<Pointer>::normal_iterator [with Pointer=thrust::device_ptr<float>]" matches the argument list
argument types are: (const thrust::device_reference<float>)
detected during:
instantiation of "thrust::detail::cons<HT, TT>::cons(const thrust::detail::cons<HT2, TT2> &) [with HT=iter, TT=thrust::detail::cons<iter, thrust::null_type>, HT2=thrust::device_reference<float>, TT2=thrust::detail::cons<thrust::device_reference<float>, thrust::null_type>]"
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5\bin/../include\thrust/tuple.h(361): here
instantiation of "thrust::tuple<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9>::tuple(const thrust::detail::cons<U1, U2> &) [with T0=iter, T1=iter, T2=thrust::null_type, T3=thrust::null_type, T4=thrust::null_type, T5=thrust::null_type, T6=thrust::null_type, T7=thrust::null_type, T8=thrust::null_type, T9=thrust::null_type, U1=thrust::device_reference<float>, U2=thrust::detail::cons<thrust::device_reference<float>, thrust::null_type>]"
sandbox.cu(37): here
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5\bin/../include\thrust/detail/tuple.inl(411): error: no instance of constructor "thrust::detail::normal_iterator<Pointer>::normal_iterator [with Pointer=thrust::device_ptr<float>]" matches the argument list
argument types are: (const thrust::device_reference<float>)
detected during:
instantiation of "thrust::detail::cons<HT, thrust::null_type>::cons(const thrust::detail::cons<HT2, thrust::null_type> &) [with HT=iter, HT2=thrust::device_reference<float>]"
(310): here
instantiation of "thrust::detail::cons<HT, TT>::cons(const thrust::detail::cons<HT2, TT2> &) [with HT=iter, TT=thrust::detail::cons<iter, thrust::null_type>, HT2=thrust::device_reference<float>, TT2=thrust::detail::cons<thrust::device_reference<float>, thrust::null_type>]"
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5\bin/../include\thrust/tuple.h(361): here
instantiation of "thrust::tuple<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9>::tuple(const thrust::detail::cons<U1, U2> &) [with T0=iter, T1=iter, T2=thrust::null_type, T3=thrust::null_type, T4=thrust::null_type, T5=thrust::null_type, T6=thrust::null_type, T7=thrust::null_type, T8=thrust::null_type, T9=thrust::null_type, U1=thrust::device_reference<float>, U2=thrust::detail::cons<thrust::device_reference<float>, thrust::null_type>]"
sandbox.cu(37): here
2 errors detected in the compilation of "C:/cygwin64/tmp/tmpxft_00001a90_00000000-10_sandbox.cpp1.ii".
最佳答案
解决了! tup
应该是 thrust::tuple<float, float>
, 不是 thrust::tuple<iter, iter>
.完整解决方案:
#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 thrust::tuple<float, float> tup;
struct func
{
__device__ float operator()(tup t) //difsq
{
float f = thrust::get<0>(t) - thrust::get<1>(t);
return f*f;
}
};
int main()
{
dvec a(4, 3.f);
dvec b(4, 2.f);
auto begin = thrust::make_zip_iterator(thrust::make_tuple(a.begin(), b.begin()));
auto end = thrust::make_zip_iterator(thrust::make_tuple(a.end(), b.end()));
std::cout << thrust::transform_reduce(begin, end, func(), 0.0f, thrust::plus<float>()) << std::endl;
std::cout << "done" << std::endl;
return 0;
}
关于c++ - CUDA 推力 zip_iterator 元组 transform_reduce,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36436432/
我不知道如何用 zip_iterator 调用 lower_bound。 这不会编译: #include #include #include void main() { typedef
问题 我正在使用 sort_by_key使用 zip_iterator 传递值.此 sort_by_key被多次调用,经过一定的迭代后变为 10x 较慢 !这是什么原因引起的性能下降 ? 症状 我正在
在以下代码的 main() 函数内的 for 循环中,我可以更改变量 ab 内的变量,即使 const auto& 在循环中使用。有什么办法可以避免这种情况吗? #include #include
我想计算 用于载体 和 , 其中表示 vector 的大小 .由于这涉及对两个 vector 的每个对应分量之间的差的平方和求平方根,因此它应该是一个高度可并行化的任务。我在 Windows 10 上
我在这里查看了很多问题以寻找类似的问题,并且有很多问题,尽管有一个小改动。我正在尝试使用 zip_iterator 作为复合键对值进行排序。 具体来说,我有以下功能: void thrustSort(
我想做的是通过 thrust::reduce_by_key 按键获取平均值.我先sort_by_key这对于 reduce_by_key 的连续键分组工作得很好.我用了this帮助我走到这一步。但是,
以下代码无法为我编译: #include #include #include typedef boost::tuple EntryTuple; struct zip_func : publi
考虑以下代码: #include #include #include #include #include typedef boost::range_detail::any_iterator,
我有两个 vector ,我想以 CSV 格式写入文件。我可以使用 for 循环“手动”执行此操作,但我第一次尝试使用 boost zip_iterator。这就是我能做到的程度。 ( online
使用 std::get<>() 有哪些选择?和 std::tie<>()与 boost 结构一起? 例子:我想使用基于范围的 for 循环对多个容器进行迭代。我可以实现 zip函数,它使用 boost
我是一名优秀的程序员,十分优秀!