File size: 7,013 Bytes
28958dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include <unittest/unittest.h>
#include <thrust/functional.h>
#include <thrust/transform.h>
#include <thrust/iterator/constant_iterator.h>

#define BINARY_FUNCTIONAL_PLACEHOLDERS_TEST(name, op, reference_functor, type_list) \
template<typename Vector> \
  struct TestFunctionalPlaceholders##name \
{ \
  void operator()(const size_t) \
  { \
    const size_t num_samples = 10000; \
    typedef typename Vector::value_type T; \
    Vector lhs = unittest::random_samples<T>(num_samples); \
    Vector rhs = unittest::random_samples<T>(num_samples); \
    thrust::replace(rhs.begin(), rhs.end(), T(0), T(1)); \
\
    Vector lhs_reference = lhs; \
    Vector reference(lhs.size()); \
    Vector result(lhs_reference.size()); \
    using namespace thrust::placeholders; \
\
    thrust::transform(lhs_reference.begin(), lhs_reference.end(), rhs.begin(), reference.begin(), reference_functor<T>()); \
    thrust::transform(lhs.begin(), lhs.end(), rhs.begin(), result.begin(), _1 op _2); \
    ASSERT_ALMOST_EQUAL(reference, result); \
    ASSERT_ALMOST_EQUAL(lhs_reference, lhs); \
\
    thrust::transform(lhs_reference.begin(), lhs_reference.end(), thrust::make_constant_iterator<T>(1), reference.begin(), reference_functor<T>()); \
    thrust::transform(lhs.begin(), lhs.end(), rhs.begin(), result.begin(), _1 op T(1)); \
    ASSERT_ALMOST_EQUAL(reference, result); \
    ASSERT_ALMOST_EQUAL(lhs_reference, lhs); \
  } \
}; \
VectorUnitTest<TestFunctionalPlaceholders##name, type_list, thrust::device_vector, thrust::device_allocator> TestFunctionalPlaceholders##name##DeviceInstance; \
VectorUnitTest<TestFunctionalPlaceholders##name, type_list, thrust::host_vector, std::allocator> TestFunctionalPlaceholders##name##HostInstance;

template<typename T>
  struct plus_equal_reference
{
  __host__ __device__ T& operator()(T &lhs, const T &rhs) const { return lhs += rhs; }
};

template<typename T>
  struct minus_equal_reference
{
  __host__ __device__ T& operator()(T &lhs, const T &rhs) const { return lhs -= rhs; }
};

template<typename T>
  struct multiplies_equal_reference
{
  __host__ __device__ T& operator()(T &lhs, const T &rhs) const { return lhs *= rhs; }
};

template<typename T>
  struct divides_equal_reference
{
  __host__ __device__ T& operator()(T &lhs, const T &rhs) const { return lhs /= rhs; }
};

template<typename T>
  struct modulus_equal_reference
{
  __host__ __device__ T& operator()(T &lhs, const T &rhs) const { return lhs %= rhs; }
};

BINARY_FUNCTIONAL_PLACEHOLDERS_TEST(PlusEqual,       +=, plus_equal_reference,       ThirtyTwoBitTypes);
BINARY_FUNCTIONAL_PLACEHOLDERS_TEST(MinusEqual,      -=, minus_equal_reference,      ThirtyTwoBitTypes);
BINARY_FUNCTIONAL_PLACEHOLDERS_TEST(MultipliesEqual, *=, multiplies_equal_reference, ThirtyTwoBitTypes);
BINARY_FUNCTIONAL_PLACEHOLDERS_TEST(DividesEqual,    /=, divides_equal_reference,    ThirtyTwoBitTypes);
BINARY_FUNCTIONAL_PLACEHOLDERS_TEST(ModulusEqual,    %=, modulus_equal_reference,    SmallIntegralTypes);

template<typename T>
  struct bit_and_equal_reference
{
  __host__ __device__ T& operator()(T &lhs, const T &rhs) const { return lhs &= rhs; }
};

template<typename T>
  struct bit_or_equal_reference
{
  __host__ __device__ T& operator()(T &lhs, const T &rhs) const { return lhs |= rhs; }
};

template<typename T>
  struct bit_xor_equal_reference
{
  __host__ __device__ T& operator()(T &lhs, const T &rhs) const { return lhs ^= rhs; }
};

template<typename T>
  struct bit_lshift_equal_reference
{
  __host__ __device__ T& operator()(T &lhs, const T &rhs) const { return lhs <<= rhs; }
};

template<typename T>
  struct bit_rshift_equal_reference
{
  __host__ __device__ T& operator()(T &lhs, const T &rhs) const { return lhs >>= rhs; }
};

BINARY_FUNCTIONAL_PLACEHOLDERS_TEST(BitAndEqual,    &=,  bit_and_equal_reference,    SmallIntegralTypes);
BINARY_FUNCTIONAL_PLACEHOLDERS_TEST(BitOrEqual,     |=,  bit_or_equal_reference,     SmallIntegralTypes);
BINARY_FUNCTIONAL_PLACEHOLDERS_TEST(BitXorEqual,    ^=,  bit_xor_equal_reference,    SmallIntegralTypes);

// XXX ptxas produces an error
void TestFunctionalPlaceholdersBitLshiftEqualDevice(void)
{
  KNOWN_FAILURE;
}
// XXX KNOWN_FAILURE this until the above works
void TestFunctionalPlaceholdersBitLshiftEqualHost(void)
{
  KNOWN_FAILURE;
}
//BINARY_FUNCTIONAL_PLACEHOLDERS_TEST(BitLshiftEqual, <<=, bit_lshift_equal_reference, SmallIntegralTypes);

BINARY_FUNCTIONAL_PLACEHOLDERS_TEST(BitRshiftEqual, >>=, bit_rshift_equal_reference, SmallIntegralTypes);

template<typename T>
  struct prefix_increment_reference
{
  __host__ __device__ T& operator()(T &x) const { return ++x; }
};

template<typename T>
  struct suffix_increment_reference
{
  __host__ __device__ T operator()(T &x) const { return x++; }
};

template<typename T>
  struct prefix_decrement_reference
{
  __host__ __device__ T& operator()(T &x) const { return --x; }
};

template<typename T>
  struct suffix_decrement_reference
{
  __host__ __device__ T operator()(T &x) const { return x--; }
};

#define PREFIX_FUNCTIONAL_PLACEHOLDERS_TEST(name, reference_operator, functor) \
template<typename Vector> \
  void TestFunctionalPlaceholdersPrefix##name(void) \
{ \
  const size_t num_samples = 10000; \
  typedef typename Vector::value_type T; \
  Vector input = unittest::random_samples<T>(num_samples); \
\
  Vector input_reference = input; \
  Vector reference(input.size()); \
  thrust::transform(input.begin(), input.end(), reference.begin(), functor<T>()); \
\
  using namespace thrust::placeholders; \
  Vector result(input_reference.size()); \
  thrust::transform(input_reference.begin(), input_reference.end(), result.begin(), reference_operator _1); \
\
  ASSERT_ALMOST_EQUAL(input_reference, input); \
  ASSERT_ALMOST_EQUAL(reference, result); \
} \
DECLARE_INTEGRAL_VECTOR_UNITTEST(TestFunctionalPlaceholdersPrefix##name);

PREFIX_FUNCTIONAL_PLACEHOLDERS_TEST(Increment,  ++,  prefix_increment_reference);
PREFIX_FUNCTIONAL_PLACEHOLDERS_TEST(Decrement,  --,  prefix_decrement_reference);

#define SUFFIX_FUNCTIONAL_PLACEHOLDERS_TEST(name, reference_operator, functor) \
template<typename Vector> \
  void TestFunctionalPlaceholdersSuffix##name(void) \
{ \
  const size_t num_samples = 10000; \
  typedef typename Vector::value_type T; \
  Vector input = unittest::random_samples<T>(num_samples); \
\
  Vector input_reference = input; \
  Vector reference(input.size()); \
  thrust::transform(input.begin(), input.end(), reference.begin(), functor<T>()); \
\
  using namespace thrust::placeholders; \
  Vector result(input_reference.size()); \
  thrust::transform(input_reference.begin(), input_reference.end(), result.begin(), _1 reference_operator); \
\
  ASSERT_ALMOST_EQUAL(input_reference, input); \
  ASSERT_ALMOST_EQUAL(reference, result); \
} \
DECLARE_INTEGRAL_VECTOR_UNITTEST(TestFunctionalPlaceholdersSuffix##name);

SUFFIX_FUNCTIONAL_PLACEHOLDERS_TEST(Increment,  ++,  suffix_increment_reference);
SUFFIX_FUNCTIONAL_PLACEHOLDERS_TEST(Decrement,  --,  suffix_decrement_reference);