How to perform a DeviceSelect on indexes? #1650
Unanswered
dennisorlando
asked this question in
Q&A
Replies: 2 comments 6 replies
-
Beta Was this translation helpful? Give feedback.
1 reply
-
#include <cub/device/device_select.cuh>
#include <thrust/device_vector.h>
#include <thrust/host_vector.h>
thrust::device_vector<int> create_flags(int num_elements) {
thrust::device_vector<int> vec(num_elements);
for(int i = 0; i < num_elements; ++i) {
vec[i] = !(i % 2); // even indexes
}
return vec;
}
int main()
{
thrust::device_vector<int> d_in = {13,4,7,8,9,10,11};
int num_items = d_in.size();
thrust::device_vector<int> d_flags = create_flags(num_items);
thrust::device_vector<int> d_out(num_items / 2 + 1); // output cannot be more than half the input size + 1
thrust::device_vector<int> d_num_selected_out(num_items);
// Determine temporary device storage requirements
void* d_temp_storage = nullptr;
size_t temp_storage_bytes = 0;
cub::DeviceSelect::Flagged(
d_temp_storage,
temp_storage_bytes,
d_in.begin(),
d_flags.begin(),
d_out.begin(),
d_num_selected_out.data(),
num_items);
cudaMalloc(&d_temp_storage, temp_storage_bytes);
cub::DeviceSelect::Flagged(
d_temp_storage,
temp_storage_bytes,
d_in.begin(),
d_flags.begin(),
d_out.begin(),
d_num_selected_out.data(),
num_items);
thrust::host_vector<int> h_vec = d_out;
for(int i = 0; i < h_vec.size(); ++i) {
printf("%d ", h_vec[i]);
}
} |
Beta Was this translation helpful? Give feedback.
5 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi, I want to perform a DeviceSelect on an array based on the index and not the value of every element but I have no idea how.
E.g. I would like to define an "is_even" function such that given
int A[] = {13,4,7,8,9,10, 11}
produces[13, 7,9, 11]
(even indexes) and not[4,7,10]
(even numbers)Beta Was this translation helpful? Give feedback.
All reactions