在 CUDA 中查找大于阈值的索引和值
Find the indices and values that larger than a threshold in CUDA
我有一个矩阵,我想找到大于阈值的索引和值,那么用CUDA怎么办?或者是将矩阵复制到内存并让 cpu 完成工作的更好方法?
您可以使用 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
实际上有一个内置函数可以直接执行此操作:
它return是第一个元素的迭代器,可以在其中插入值,同时保持列表的顺序。因此,如果 value 是您的阈值,它将 return 指向阈值 >= i 的第一个元素 i 的迭代器。
感谢投反对票,我可能不会再尝试回答问题。
我有一个矩阵,我想找到大于阈值的索引和值,那么用CUDA怎么办?或者是将矩阵复制到内存并让 cpu 完成工作的更好方法?
您可以使用 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
实际上有一个内置函数可以直接执行此操作:
它return是第一个元素的迭代器,可以在其中插入值,同时保持列表的顺序。因此,如果 value 是您的阈值,它将 return 指向阈值 >= i 的第一个元素 i 的迭代器。
感谢投反对票,我可能不会再尝试回答问题。