- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
给定一些输入键和值,我试图计算存在多少个具有相同键的连续值。我将举一个例子来说明这一点。
输入键:{ 1, 4, 4, 4, 2, 2, 1 }
输入值:{ 9, 8, 7, 6, 5, 4, 3 }
预期输出键:{ 1, 4, 2, 1 }
预期输出值:{ 1, 3, 2, 1 }
我正在尝试使用 CUDA 在 GPU 上解决这个问题。 Thrust 库的缩减功能似乎是一个很好的解决方案,我得到了以下结果:
#include <thrust/reduce.h>
#include <thrust/functional.h>
struct count_functor : public thrust::binary_function<int, int, int>
{
__host__ __device__
int operator()(int input, int counter)
{
return counter + 1;
}
};
const int N = 7;
int A[N] = { 1, 4, 4, 4, 2, 2, 1 }; // input keys
int B[N] = { 9, 8, 7, 6, 5, 4, 3 }; // input values
int C[N]; // output keys
int D[N]; // output values
thrust::pair<int*, int*> new_end;
thrust::equal_to<int> binary_pred;
count_functor binary_op;
new_end = thrust::reduce_by_key(A, A + N, B, C, D, binary_pred, binary_op);
for (int i = 0; i < new_end.first - C; i++) {
std::cout << C[i] << " - " << D[i] << "\n";
}
此代码与 Thrust documentation 中的示例非常相似.但是,我尝试计算而不是 plus
操作。此代码的输出如下:
1 - 9
4 - 7
2 - 5
1 - 3
但是,我希望第二列包含值 1, 3, 2, 1
。我认为计数是关闭的,因为减少从它找到的第一个值开始并且在它具有第二个值之前不应用运算符,但我不确定情况是否如此。
我是不是忽略了可以解决这个问题的 reduce_by_key
函数,还是我应该使用完全不同的函数来实现我想要的?
最佳答案
对于您的用例,您不需要 B
的值,D
的值仅取决于 A
的值.
为了计算 A
中有多少个连续值,您可以提供一个 thrust::constant_iterator
作为输入值并应用 thrust::reduce_by_key
:
#include <thrust/reduce.h>
#include <thrust/functional.h>
#include <iostream>
#include <thrust/iterator/constant_iterator.h>
int main()
{
const int N = 7;
int A[N] = { 1, 4, 4, 4, 2, 2, 1 };
int C[N];
int D[N];
thrust::pair<int*, int*> new_end;
thrust::equal_to<int> binary_pred;
thrust::plus<int> binary_op;
new_end = thrust::reduce_by_key(A, A + N, thrust::make_constant_iterator(1), C, D, binary_pred, binary_op);
for (int i = 0; i < new_end.first - C; i++) {
std::cout << C[i] << " - " << D[i] << "\n";
}
return 0;
}
输出
1 - 1
4 - 3
2 - 2
1 - 1
关于c++ - 使用推力减少计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34250322/
我需要一些帮助来追踪 thrust::fill 给我的编译错误。 代码没有问题: line 9 #include // needed for other thrus
如何使用推力返回事件数组元素的索引,即返回数组元素等于 1 的索引向量? 对此进行扩展,在给定数组维度的多维索引的情况下,这将如何工作? 编辑:目前该功能看起来像这样 template void Vo
当尝试创建thrust::device_vector的struct时,我得到了Bus error (core dumped)。奇怪的是,下面的代码在我的笔记本电脑(Quadro P2000)上运行良好
我尝试将数据从主机复制到设备并返回,但不是使用 CUDA API,而是使用推力库。我在 thrust::host_vector 中分配了内存,并尝试将其复制到 thrust::device_vecto
我有一对大小相等的数组,我将它们称为键和值。 例如: K: V 1: 99 1: 100 1: 100 1: 100 1: 103 2: 103 2: 105 3: 45 3: 67 键被排序,与每个
我想知道是否可以使用 Thrust 库按键排序,而无需创建 Vector 来存储键(动态)。例如,我有以下两个 vector :键和值: vectorKeys: 0, 1, 2, 0,
假设我想做一个 thrust::reduce_by_key 但我不关心输出键是什么。有没有一种方法可以通过某种方式将空对象(可能是空指针)传递给该参数的算法,从而不会创建毫无意义的输出键列表,从而节省
我目前正在通过以下方式按键对值进行排序 thrust::sort_by_key(thrust::device_ptr(keys), thrust::device
这个问题在这里已经有了答案: is there a better and a faster way to copy from CPU memory to GPU using thrust? (1 个回
有没有办法在不实际分配 vector 的情况下声明推力 vector 指针?我需要将此指针用作类中的成员变量。因为我事先并不知道 vector 的大小,所以我不能将 vector 静态分配为成员变量。
我想知道如何 thrust::set_intersection有效,但从我的测试结果来看,我对这个函数的作用更加困惑。 举几个例子: const int size1 = 5; const int si
考虑以下数据集和质心。一共有7个人,两个均值有8个维度。它们按行主要顺序存储。 short dim = 8; float centroids[] = { 0.223, 0.002, 0.223
我有以下(可编译和可执行)代码,使用 CUDA Thrust 来执行 float2 数组的缩减。它工作正常 using namespace std; // includes, system #incl
我有一个使用 Thrust 目前在单个 GPU 上正常工作的 Cuda C++ 代码。我现在想为多 GPU 修改它。我有一个主机函数,其中包括许多对设备数组进行排序、复制、计算差异等的推力调用。我想使
我在 thrust::device_vector 中有一个矩阵(面向行) .有什么方法可以获取该 vector 的切片/ View (也属于 thrust::device_vector 类型)?我对复
我遇到了 thrust 库的 reduce_by_key 函数的问题。对我来说这看起来像是一个错误,但我想在报告之前确定一下。 首先,我的设置:CUDA 7.0、Windows 8、NIVIDA Ge
我有以下函数,用于用从 -time/2 到 time/2 的步长和步长 dt 填充 vector t: #define THRUST_PREC thrust::complex __host__ voi
在我现在正在编写的程序中,我想使用 GPU 或 CPU 进行计算(用于对彼此进行基准测试)。为此,我想要一些通用指针,我可以像这样使用 device_vector 或 host_vector 的实例对
我试图找到数组中的最小元素: thrust::device_ptr devPtr(d_ary); int minPos = thrust::min_element(devPtr.begin(),
我的计划是使用 Pearsons 相关性计算距离矩阵,并从距离矩阵中为每个节点 (q=ln(n)) 获取 q-最近邻,并将它们放入结果向量中。我在 C++ 中使用相关函数循环内的 STL 优先级队列来
我是一名优秀的程序员,十分优秀!