2015-04-24 43 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什麼不清楚?正如我在答案中寫的那樣,這找到滿足條件'(值>閾值)'的指標 –

相關問題