我有一个矩阵,我想查找大于阈值的索引和值,那么如何使用CUDA执行此操作?或者这是将矩阵复制到内存并让cpu完成工作的更好方法?在CUDA中查找大于阈值的索引和值
-3
A
回答
0
您可以使用Thrust非常轻松地实现此功能,该功能为您提供了所需的基本构建模块。以下代码首先找到满足条件的索引(value > threshold
),然后提取相应的值。如果你不需要索引,你可以一步完成所有这些。
#include <thrust/gather.h>
#include <thrust/iterator/counting_iterator.h>
#include <thrust/iterator/permutation_iterator.h>
#include <thrust/functional.h>
#include <thrust/copy.h>
#include <thrust/device_vector.h>
#include <iostream>
#include <thrust/sequence.h>
int main()
{
const int N = 100;
int threshold = 90;
thrust::device_vector<int> data(N);
// fill with demo data
thrust::sequence(data.begin(), data.end());
// find out the indices
thrust::device_vector<int> indices(N);
thrust::device_vector<int>::iterator end = thrust::copy_if(thrust::make_counting_iterator(0),
thrust::make_counting_iterator(N),
data.begin(),
indices.begin(),
thrust::placeholders::_1 > threshold);
int size = end-indices.begin();
indices.resize(size);
// fetch corresponding values
thrust::device_vector<int> values(size);
thrust::copy(thrust::make_permutation_iterator(data.begin(), indices.begin()),
thrust::make_permutation_iterator(data.end(), indices.end()),
values.begin());
std::cout << "indices: ";
thrust::copy(indices.begin(), indices.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
std::cout << "values: ";
thrust::copy(values.begin(), values.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
return 0;
}
这个演示程序的输出是:
indices: 91 92 93 94 95 96 97 98 99
values: 91 92 93 94 95 96 97 98 99
+0
您能否解释一下这一行:'thrust :: device_vector
+0
@JithinPavithran什么不清楚?正如我在答案中写的那样,这找到满足条件'(值>阈值)'的指标 –
相关问题
- 1. 在最大值列表中查找最大值和索引?
- 2. MATLAB - 我怎么找到的第一个索引,其中值大于阈值
- 3. 在CUDA中查找最大值
- 4. 在Python中,如何在排序列表中找到大于阈值的第一个值的索引?
- 5. 选择具有大于阈值的数组的索引
- 6. 在矩阵中查找最大值的行和列索引
- 7. 查找数组中的索引和值
- 8. MATLAB:如何找到长度大于阈值的单元的索引?
- 9. 找到最大值和最小值元素高于阈值在容器
- 10. 在python列表中查找最大值和索引?
- 11. 的Python:找到NA值高于阈值
- 12. 在Qlistview中查找值的索引
- 13. matlab - 当值超过阈值时找到索引n次数
- 14. 如何使用索引来查找所有的值大于
- 15. CUDA:如何在子矩阵中找到极值的索引?
- 16. 使用索引在List中查找值?
- 17. 熊猫:在列中查找最大字符串值的索引
- 18. 按值查找索引
- 19. XMLListCollection查找索引值
- 20. php array_search查找索引值
- 21. 在CUDA中查找矩阵的最大值
- 22. 查找数组中值的索引
- 23. 查找ArrayList中重复值的索引
- 24. 在稀疏lil_matrix(Scipy/Python)中查找最大值及其索引
- 25. numpy获取列索引,其中所有元素都大于阈值
- 26. CUDA:获取数组中的最大值及其索引
- 27. 使用CUDA在数组及其索引中查找最小值__shfl_down函数
- 28. 索引匹配,其中查找值在查找数组中
- 29. 查找列表中索引的起始和大于X的项目的索引
- 30. 在Java中查找num值和最小值/最大值值
你有*一下你会怎么做,你需要帮助,或者你只是寻找一个准备使用的解决方案的任何*的想法要放在银盘上? – talonmies
我想知道是否有更好的方式,我还没有弄清楚 –
比什么更好的方法?你没有解释任何你正在使用或正在尝试做的事情。 – talonmies