推力紧凑时如何获取映射数组
how to get mapping array when compact with thrust
我需要知道每个元素在推力紧凑时映射到什么位置。
例如:
arr: 5 2 -1 3 -1 6 -1 7
compacted: 5 2 3 6 7 --------------(remove all -1 element)
map Arr: 0 1 -1 2 -1 3 -1 4
映射数组,这里我指的是表示每个元素移动到哪个位置的数组,抱歉我想不出更好的名字来形容这个,我希望我说清楚了。
用推力压缩很容易,但我想知道我是否可以在推力压缩时获得映射数组。
以下推力调用序列可以生成所需的映射数组。
您的示例的输出是:
compacted: 5 2 3 6 7
map: 0 0 -1 0 -1 0 -1 0
map: 0 0 -1 -1 -2 -2 -3 -3
map: 0 1 1 2 2 3 3 4
map: 0 1 -1 2 -1 3 -1 4
#include <iostream>
#include <string>
#include <thrust/scan.h>
#include <thrust/scatter.h>
#include <thrust/remove.h>
#include <thrust/iterator/counting_iterator.h>
#include <thrust/iterator/constant_iterator.h>
void print(const std::string& name, int* begin, int* end)
{
std::cout << name << ": ";
thrust::copy(begin, end, std::ostream_iterator<int>(std::cout, " "));
std::cout <<std::endl;
}
struct is_marker
{
__host__ __device__
bool operator()(const int x) const
{
return (x == -1);
}
};
int main()
{
const int N = 8;
int arr[N] = {5,2,-1,3,-1,6,-1,7};
int compacted[N] = {0};
int* compacted_end = thrust::remove_copy(arr, arr+N, compacted, -1);
print("compacted", compacted, compacted_end);
int map[N] = {0};
thrust::scatter_if(thrust::make_constant_iterator(-1), thrust::make_constant_iterator(-1)+N, thrust::make_counting_iterator(0), arr, map, is_marker());
print("map", map, map+N);
thrust::inclusive_scan(map, map+N, map);
print("map", map, map+N);
thrust::transform(map, map+N, thrust::make_counting_iterator(0), map, thrust::plus<int>());
print("map", map, map+N);
thrust::scatter_if(thrust::make_constant_iterator(-1), thrust::make_constant_iterator(-1)+N, thrust::make_counting_iterator(0), arr, map, is_marker());
print("map", map, map+N);
return 0;
}
我需要知道每个元素在推力紧凑时映射到什么位置。
例如:
arr: 5 2 -1 3 -1 6 -1 7
compacted: 5 2 3 6 7 --------------(remove all -1 element)
map Arr: 0 1 -1 2 -1 3 -1 4
映射数组,这里我指的是表示每个元素移动到哪个位置的数组,抱歉我想不出更好的名字来形容这个,我希望我说清楚了。
用推力压缩很容易,但我想知道我是否可以在推力压缩时获得映射数组。
以下推力调用序列可以生成所需的映射数组。
您的示例的输出是:
compacted: 5 2 3 6 7
map: 0 0 -1 0 -1 0 -1 0
map: 0 0 -1 -1 -2 -2 -3 -3
map: 0 1 1 2 2 3 3 4
map: 0 1 -1 2 -1 3 -1 4
#include <iostream>
#include <string>
#include <thrust/scan.h>
#include <thrust/scatter.h>
#include <thrust/remove.h>
#include <thrust/iterator/counting_iterator.h>
#include <thrust/iterator/constant_iterator.h>
void print(const std::string& name, int* begin, int* end)
{
std::cout << name << ": ";
thrust::copy(begin, end, std::ostream_iterator<int>(std::cout, " "));
std::cout <<std::endl;
}
struct is_marker
{
__host__ __device__
bool operator()(const int x) const
{
return (x == -1);
}
};
int main()
{
const int N = 8;
int arr[N] = {5,2,-1,3,-1,6,-1,7};
int compacted[N] = {0};
int* compacted_end = thrust::remove_copy(arr, arr+N, compacted, -1);
print("compacted", compacted, compacted_end);
int map[N] = {0};
thrust::scatter_if(thrust::make_constant_iterator(-1), thrust::make_constant_iterator(-1)+N, thrust::make_counting_iterator(0), arr, map, is_marker());
print("map", map, map+N);
thrust::inclusive_scan(map, map+N, map);
print("map", map, map+N);
thrust::transform(map, map+N, thrust::make_counting_iterator(0), map, thrust::plus<int>());
print("map", map, map+N);
thrust::scatter_if(thrust::make_constant_iterator(-1), thrust::make_constant_iterator(-1)+N, thrust::make_counting_iterator(0), arr, map, is_marker());
print("map", map, map+N);
return 0;
}