File: remove_points2d.cu

package info (click to toggle)
cccl 2.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 39,248 kB
  • sloc: cpp: 264,457; python: 6,421; sh: 2,762; perl: 460; makefile: 114; xml: 13
file content (78 lines) | stat: -rw-r--r-- 2,094 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <thrust/host_vector.h>
#include <thrust/random.h>
#include <thrust/remove.h>

#include "include/host_device.h"

// This example generates random points in the
// unit square [0,1)x[0,1) and then removes all
// points where x^2 + y^2 > 1
//
// The x and y coordinates are stored in separate arrays
// and a zip_iterator is used to combine them together

template <typename T>
struct is_outside_circle
{
  template <typename Tuple>
  inline __host__ __device__ bool operator()(const Tuple& tuple) const
  {
    // unpack the tuple into x and y coordinates
    const T x = thrust::get<0>(tuple);
    const T y = thrust::get<1>(tuple);

    if (x * x + y * y > 1)
    {
      return true;
    }
    else
    {
      return false;
    }
  }
};

int main()
{
  const size_t N = 20;

  // generate random points in the unit square on the host
  thrust::default_random_engine rng;
  thrust::uniform_real_distribution<float> u01(0.0f, 1.0f);
  thrust::host_vector<float> x(N);
  thrust::host_vector<float> y(N);
  for (size_t i = 0; i < N; i++)
  {
    x[i] = u01(rng);
    y[i] = u01(rng);
  }

  // print the initial points
  std::cout << std::fixed;
  std::cout << "Generated " << N << " points" << std::endl;
  for (size_t i = 0; i < N; i++)
  {
    std::cout << "(" << x[i] << "," << y[i] << ")" << std::endl;
  }
  std::cout << std::endl;

  // remove points where x^2 + y^2 > 1 and determine new array sizes
  size_t new_size =
    thrust::remove_if(thrust::make_zip_iterator(thrust::make_tuple(x.begin(), y.begin())),
                      thrust::make_zip_iterator(thrust::make_tuple(x.end(), y.end())),
                      is_outside_circle<float>())
    - thrust::make_zip_iterator(thrust::make_tuple(x.begin(), y.begin()));

  // resize the vectors (note: this does not free any memory)
  x.resize(new_size);
  y.resize(new_size);

  // print the filtered points
  std::cout << "After stream compaction, " << new_size << " points remain" << std::endl;
  for (size_t i = 0; i < new_size; i++)
  {
    std::cout << "(" << x[i] << "," << y[i] << ")" << std::endl;
  }

  return 0;
}