2015-04-24 42 views
-3

我有一个矩阵,我想查找大于阈值的索引和值,那么如何使用CUDA执行此操作?或者这是将矩阵复制到内存并让cpu完成工作的更好方法?在CUDA中查找大于阈值的索引和值

+1

你有*一下你会怎么做,你需要帮助,或者你只是寻找一个准备使用的解决方案的任何*的想法要放在银盘上? – talonmies

+0

我想知道是否有更好的方式,我还没有弄清楚 –

+2

比什么更好的方法?你没有解释任何你正在使用或正在尝试做的事情。 – talonmies

回答

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 :: iterator end ...'? –

+0

@JithinPavithran什么不清楚?正如我在答案中写的那样,这找到满足条件'(值>阈值)'的指标 –

相关问题