File: adjacent_difference.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 (66 lines) | stat: -rw-r--r-- 1,797 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
#include <thrust/adjacent_difference.h>
#include <thrust/device_free.h>
#include <thrust/device_malloc.h>
#include <thrust/execution_policy.h>

#include <unittest/unittest.h>

struct detect_wrong_difference
{
  bool* flag;

  _CCCL_HOST_DEVICE detect_wrong_difference operator++() const
  {
    return *this;
  }
  _CCCL_HOST_DEVICE detect_wrong_difference operator*() const
  {
    return *this;
  }
  template <typename Difference>
  _CCCL_HOST_DEVICE detect_wrong_difference operator+(Difference) const
  {
    return *this;
  }
  template <typename Index>
  _CCCL_HOST_DEVICE detect_wrong_difference operator[](Index) const
  {
    return *this;
  }

  _CCCL_DEVICE void operator=(long long difference) const
  {
    if (difference != 1)
    {
      *flag = false;
    }
  }
};

void TestAdjacentDifferenceWithBigIndexesHelper(int magnitude)
{
  thrust::counting_iterator<long long> begin(1);
  thrust::counting_iterator<long long> end = begin + (1ll << magnitude);
  ASSERT_EQUAL(thrust::distance(begin, end), 1ll << magnitude);

  thrust::device_ptr<bool> all_differences_correct = thrust::device_malloc<bool>(1);
  *all_differences_correct                         = true;

  detect_wrong_difference out = {thrust::raw_pointer_cast(all_differences_correct)};

  thrust::adjacent_difference(thrust::device, begin, end, out);

  bool all_differences_correct_h = *all_differences_correct;
  thrust::device_free(all_differences_correct);

  ASSERT_EQUAL(all_differences_correct_h, true);
}

void TestAdjacentDifferenceWithBigIndexes()
{
  TestAdjacentDifferenceWithBigIndexesHelper(30);
  TestAdjacentDifferenceWithBigIndexesHelper(31);
  TestAdjacentDifferenceWithBigIndexesHelper(32);
  TestAdjacentDifferenceWithBigIndexesHelper(33);
}
DECLARE_UNITTEST(TestAdjacentDifferenceWithBigIndexes);