推力:我怎么能用索引和范围按范围填充数组
Thrust : how could i fill an array by range with index and range
我尝试使用索引和值按范围填充数组
thrust::device_vector<int> vec(12)
vec[1] = 2;
vec[6] = 3;
vec[10] = 1;
结果应该是
vec { 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0}
我有
vec { 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0}
有人能告诉我哪里错了吗?
谢谢
这是我的代码:
#include <thrust/device_vector.h>
#include <thrust/scan.h>
#include <iterator>
template<class T>
struct FillRange
{
__host__ __device__ T operator()(const T& res, const T& dat)
{
return dat >= T(0) ? dat : 1;
}
};
int main()
{
thrust::device_vector<int> vec(12);
vec[1] = 2;
vec[6] = 3;
vec[10] = 2;
thrust::inclusive_scan(
vec.rbegin(), vec.rend(),
vec.rbegin(),
FillRange<int>());
thrust::copy(
vec.begin(), vec.end(),
std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
}
这里是预期结果的代码。
数组给出索引和要设置的标志数:
所以
thrust::device_vector<int> vec(12)
vec[1] = 2;
vec[6] = 3;
vec[10] = 1;
结果会是
vec { 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0}
#include <thrust/device_vector.h>
#include <thrust/scan.h>
#include <iterator>
struct clean_
{
__host__ __device__
int operator()(const int &p1) const
{
return (p1>0)?1:p1;
}
};
template<class T>
struct FillRange
{
__host__ __device__ T operator()(const T& res, const T& dat)
{
return ((res-1)>0)? res-1 : dat ;
}
};
int main()
{
thrust::device_vector<int> vec(12);
vec[1] = 2;
vec[6] = 3;
vec[11] = 2;
thrust::inclusive_scan(
vec.begin(), vec.end(),
vec.begin(),
FillRange<int>());
thrust::transform(
vec.begin(),
vec.end(),
vec.begin(),
clean_());
thrust::copy(
vec.begin(), vec.end(),
std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
}
我尝试使用索引和值按范围填充数组
thrust::device_vector<int> vec(12)
vec[1] = 2;
vec[6] = 3;
vec[10] = 1;
结果应该是
vec { 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0}
我有
vec { 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0}
有人能告诉我哪里错了吗? 谢谢
这是我的代码:
#include <thrust/device_vector.h>
#include <thrust/scan.h>
#include <iterator>
template<class T>
struct FillRange
{
__host__ __device__ T operator()(const T& res, const T& dat)
{
return dat >= T(0) ? dat : 1;
}
};
int main()
{
thrust::device_vector<int> vec(12);
vec[1] = 2;
vec[6] = 3;
vec[10] = 2;
thrust::inclusive_scan(
vec.rbegin(), vec.rend(),
vec.rbegin(),
FillRange<int>());
thrust::copy(
vec.begin(), vec.end(),
std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
}
这里是预期结果的代码。
数组给出索引和要设置的标志数: 所以
thrust::device_vector<int> vec(12)
vec[1] = 2;
vec[6] = 3;
vec[10] = 1;
结果会是
vec { 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0}
#include <thrust/device_vector.h>
#include <thrust/scan.h>
#include <iterator>
struct clean_
{
__host__ __device__
int operator()(const int &p1) const
{
return (p1>0)?1:p1;
}
};
template<class T>
struct FillRange
{
__host__ __device__ T operator()(const T& res, const T& dat)
{
return ((res-1)>0)? res-1 : dat ;
}
};
int main()
{
thrust::device_vector<int> vec(12);
vec[1] = 2;
vec[6] = 3;
vec[11] = 2;
thrust::inclusive_scan(
vec.begin(), vec.end(),
vec.begin(),
FillRange<int>());
thrust::transform(
vec.begin(),
vec.end(),
vec.begin(),
clean_());
thrust::copy(
vec.begin(), vec.end(),
std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
}