- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试对 zip 迭代器进行最小缩减,但使用自定义运算符仅考虑元组中的第二个字段(第一个字段是键,而第二个字段是值)实际上与减少有关)
但是,我无法让它工作,目前正在计算 vector 中存在的结果
下面的代码重现了这个问题:
#include <thrust/device_vector.h>
#include <thrust/iterator/zip_iterator.h>
#include <thrust/tuple.h>
#include <thrust/sequence.h>
typedef thrust::tuple<unsigned int, unsigned int> DereferencedIteratorTuple;
struct tuple_snd_min{
__host__ __device__
bool operator()(const DereferencedIteratorTuple& lhs,
const DereferencedIteratorTuple& rhs){
return (thrust::get<1>(lhs) < thrust::get<1>(rhs));
}
};
void f(){
thrust::device_vector<unsigned int> X(10);
thrust::device_vector<unsigned int> Y(10);
thrust::sequence(X.begin(), X.end());
thrust::sequence(Y.begin(), Y.end());
X[0] = 5;
Y[0] = 5;
X[1] = 50;
// X: 5 50 2 3 4 5 6 7 8 9
// Y: 5 1 2 3 4 5 6 7 8 9
typedef thrust::device_vector<unsigned int>::iterator UIntIterator;
typedef thrust::tuple<UIntIterator, UIntIterator> IteratorTuple;
thrust::zip_iterator<IteratorTuple> first =
thrust::make_zip_iterator(thrust::make_tuple(X.begin(), Y.begin()));
thrust::tuple<unsigned int, unsigned int> init = first[0];
thrust::tuple<unsigned int, unsigned int> min =
thrust::reduce(first, first + 10, init, tuple_snd_min());
printf("(%d,%d)\n", thrust::get<0>(min), thrust::get<1>(min));
// should return (50,1)
// returns (0,0)
}
感谢 Jared Hoberock 的评论,我得以解决此问题。
typedef thrust::tuple<unsigned int, unsigned int> DereferencedIteratorTuple;
struct tuple_snd_min{
__host__ __device__
const DereferencedIteratorTuple& operator()(const DereferencedIteratorTuple& lhs, const DereferencedIteratorTuple& rhs)
{
if(thrust::get<1>(lhs) < thrust::get<1>(rhs)) return lhs;
else return rhs;
}
};
最佳答案
这似乎是由于对 reduce
调用中的仿函数必须实现哪个操作的误解造成的。根据 documentation ,仿函数必须是一个二元函数的模型,其输出必须可以转换为输入类型。这是你的仿函数失败的地方。而不是这个
struct tuple_snd_min{
__host__ __device__
bool operator()(const DereferencedIteratorTuple& lhs,
const DereferencedIteratorTuple& rhs){
return (thrust::get<1>(lhs) < thrust::get<1>(rhs));
}
};
你的仿函数需要像这样定义:
struct tuple_snd_min{
__host__ __device__
int operator()(const DereferencedIteratorTuple& lhs,
const DereferencedIteratorTuple& rhs){
return (thrust::get<1>(lhs) < thrust::get<1>(rhs)) ?
thrust::get<1>(lhs) : thrust::get<1>(rhs);
}
};
即该函数应该返回一个值而不是充当谓词。
[此答案是根据评论汇总并作为社区 wiki 条目发布的,以便将此问题从未回答的队列中删除]
关于c++ - thrust::tuple in reduction 的自定义最小运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24188761/
以下 CUDA Thrust 程序崩溃: #include #include int main(void) { thrust::device_vector vec; for (int i(
我使用 cuda 内核对推力 vector 执行 S 形激活: thrust::device_vector output = input; float * output_ptr = thrust::r
我一直在尝试实现一些需要在 thrust::complexes 上调用 reduce 的代码,编译器向我发出错误消息: cannot pass an argument with a user-prov
我是 CUDA 的新手,而且很吃力。当提供 counting_iterator 时,我似乎无法让 thrust::for_each 算法工作。这是我的简单仿函数: struct print_Funct
我实际上正在学习CUDA和thrust,我正在尝试用.cpp做一个项目,。 hpp 文件和 .cu, .cuh 文件。因此,我做了第一个小实现(见下面的代码),但是我有一个编译错误。这是 output
我想覆盖低级 CUDA 设备内存分配器(实现为 thrust::system::cuda::detail::malloc()),以便它使用自定义分配器而不是直接调用 cudaMalloc()在主机 (
当我在main函数中使用thrust::device_vector时,可以正确的传递给内核函数,代码如下: thrust::device_vector device_a(2); thrust::h
我在 CUDA 中使用这种 vector 方法的 vector 方法,因为我仍然习惯于 Matlab 和 Python 风格的编程环境。我能够从设备 vector 中的主机端提取数据,但现在我不确定如
我正在尝试使用 thrust::raw_pointer_cast 转换原始指针以捕获仿函数中的输出。我尝试了多种方法来将指针传递给 float ,但不断出现内存冲突和两个智能感知错误 thrust::
gather与scatter正好相反: scatter是顺序输入根据map确定撒点输出位置。 #include #include #include ... // mark even indice
我是 Thrust 的新手,有件事我不明白。 Thrust 是异步还是同步? 如果我编写以下代码,所花费的时间不是0。但在其他标签中,其他用户报告的结果为0。真相是什么? clock_t start,
我的编译器 (PGI) 不支持 #pragma once 但是我想包含的库(推力)使用它们。 这个问题有解决办法吗? 最佳答案 您可以使用guardonce将 #pragma Once 语句转换为标准
我的设备上有两个整数数组 dmap 和 dflag相同的长度我用推力设备指针 dmapt 和dflagt dmap 数组中有一些值为 -1 的元素。我想要删除这些 -1 和相应的值dflag 数组。
Thrust 能够对编码器隐藏各种细节,并且声称 Thrust 会根据系统规范在一定程度上设置参数。 Thrust 如何选择最佳参数化,以及如何处理不同机器上的各种代码? Thrust 实现这种通用库
我在当前项目中使用了 Thrust,所以我不必写 device_vector自己抽象或(分段)扫描内核。 到目前为止,我已经使用推力抽象完成了我的所有工作,但是对于简单的内核或不容易转换为 for_e
我想做这样的事情: BaseFunctor* f = new MyFunctor(); thrust::transform(it1,it2,MyFunctor); 目标是让用户能够传递不同的仿函数(具
当我尝试实现任何仿函数时,我得到了不好的结果。例如,我尝试了一个类似于 thrust::negate 的否定仿函数下面是一个示例代码,它使用内置的否定仿函数产生了良好的结果: int data[10]
我正在使用 OpenCV 加载一个 .png 文件,我想使用 thrust 库提取它的蓝色强度值。 我的代码是这样的: 使用 OpenCV IplImage 指针加载图像 将图像数据复制到thrust
我有一个奇怪的问题,我无法解决。它与 boost +推力代码相关联。 代码: #include #include #include #include #include #include #
是否可以使用 Thrust 创建一个 device_vectors 数组?我知道我不能创建一个 device_vector 的 device_vector,但是我将如何创建一个 device_vect
我是一名优秀的程序员,十分优秀!