Skip to content

Instantly share code, notes, and snippets.

@eglaser77
Last active September 18, 2015 15:47
Show Gist options
  • Save eglaser77/756e5a9234cf0f08a3fb to your computer and use it in GitHub Desktop.
Save eglaser77/756e5a9234cf0f08a3fb to your computer and use it in GitHub Desktop.
#include <thrust/version.h>
#include <thrust/device_vector.h>
#include <thrust/host_vector.h>
#include <thrust/binary_search.h>
#include <thrust/copy.h>
#define STENCIL_SIZE 8000000
#define STENCIL_TRUE_LOCATIONS 32
struct is_true
{
__host__ __device__
bool operator()(const bool x)
{
return x;
}
};
int main()
{
//allocate stencil
thrust::host_vector<bool> hv(STENCIL_SIZE,false);
size_t stencil_locations[STENCIL_TRUE_LOCATIONS] = {467508,
1326441,
1541662,
1679866,
2234773,
2387355,
2653762,
3159732,
3226888,
3828014,
3887644,
3909417,
3924245,
4042273,
4150580,
4233776,
4425058,
4446033,
4484984,
4565655,
4629464,
4703190,
4836990,
4903165,
4910365,
5328271,
5483482,
5755194,
5781566,
5966710,
5987753,
7870669};
for (size_t i=0;i<STENCIL_TRUE_LOCATIONS;i++)
hv[stencil_locations[i]] = true;
//copy stencil to GPU
thrust::device_vector<bool> dv = hv;
//FIRST METHOD: inclusive_scan / upper_bound
thrust::device_vector<size_t> dscanned(STENCIL_SIZE);
thrust::inclusive_scan(dv.begin(), dv.end(), dscanned.begin());
thrust::counting_iterator<size_t> count_it(0);
//dindices will have the locations of the 'true' values of the stencil
thrust::device_vector<size_t> dindices1(STENCIL_TRUE_LOCATIONS);
thrust::upper_bound(dscanned.begin(),dscanned.end(),count_it,count_it+STENCIL_TRUE_LOCATIONS,dindices1.begin());
//copy back to host
thrust::host_vector<size_t> hindices1 = dindices1;
//SECOND METHOD: copy_if
thrust::device_vector<size_t> dindices2(STENCIL_TRUE_LOCATIONS);
thrust::copy_if(count_it, count_it+STENCIL_SIZE, dv.begin(), dindices2.begin(), is_true());
//copy back to host
thrust::host_vector<size_t> hindices2 = dindices2;
for (size_t i=0;i<STENCIL_TRUE_LOCATIONS;i++)
{
printf("i: %2u stencil_location: %8u hindices1: %8u hindices2: %8u\n",i,stencil_locations[i],hindices1[i],hindices2[i]);
}
printf("done\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment