Array filtering in CUDA

Input:
1. A = array of bitmaps, for example, A = [100011, 001111, 000101, 111001, 001111]
2. Filter, for example, F = [111, 101]

Desired output:
In A, filtering out those bitmaps whose three most significant digits(MSD) is not in the filter and keep the ordering, continue with above example, those bitmaps 100011, 111001 would be filtered out, and the result should be [001111, 000101, 001111].

I have try to use copy_if in Thrust, but I have no idea how to pass the filter list to the API.

You could create a predicate struct which contains a device pointer to F and the number of elements in F. Then implement operator() to perform the check.

Something like this: (untested code)

struct MyFilter{
thrust::device_ptr<int> d_filter; int N;
MyFilter(auto d_filter, int N) :d_filter(d_filter), N(N){}
__device__
bool operator()(int bitmap){
    for(int i = 0; i < N; i++)
        if(check(bitmap, d_filter[i]))
           return true;
    return false;
}
};

auto end = thrust::copy_if(d_A.begin(), d_A.end(), d_dest.begin(), MyFilter{d_F.data(), d_F.size()});

d_dest.erase(end, d_dest.end());

See also: thrust: Stream Compaction

This works nicely, thank you!