我有一個矩陣,我想查找大於閾值的索引和值,那麼如何使用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