@@ -22,36 +22,36 @@ void TestAdjacentDifferenceDevice(ExecutionPolicy exec, const size_t n)
2222{
2323 thrust::host_vector<T> h_input = unittest::random_samples<T>(n);
2424 thrust::device_vector<T> d_input = h_input;
25-
25+
2626 thrust::host_vector<T> h_output (n);
2727 thrust::device_vector<T> d_output (n);
28-
28+
2929 thrust::adjacent_difference (h_input.begin (), h_input.end (), h_output.begin ());
3030 adjacent_difference_kernel<<<1 ,1 >>> (exec, d_input.begin (), d_input.end (), d_output.begin ());
3131 {
3232 cudaError_t const err = cudaDeviceSynchronize ();
3333 ASSERT_EQUAL (cudaSuccess, err);
3434 }
35-
35+
3636 ASSERT_EQUAL (h_output, d_output);
37-
37+
3838 thrust::adjacent_difference (h_input.begin (), h_input.end (), h_output.begin (), thrust::plus<T>());
3939 adjacent_difference_kernel<<<1 ,1 >>> (exec, d_input.begin (), d_input.end (), d_output.begin (), thrust::plus<T>());
4040 {
4141 cudaError_t const err = cudaDeviceSynchronize ();
4242 ASSERT_EQUAL (cudaSuccess, err);
4343 }
44-
44+
4545 ASSERT_EQUAL (h_output, d_output);
46-
46+
4747 // in-place operation
4848 thrust::adjacent_difference (h_input.begin (), h_input.end (), h_input.begin (), thrust::plus<T>());
4949 adjacent_difference_kernel<<<1 ,1 >>> (exec, d_input.begin (), d_input.end (), d_input.begin (), thrust::plus<T>());
5050 {
5151 cudaError_t const err = cudaDeviceSynchronize ();
5252 ASSERT_EQUAL (cudaSuccess, err);
5353 }
54-
54+
5555 ASSERT_EQUAL (h_input, h_output); // computed previously
5656 ASSERT_EQUAL (d_input, d_output); // computed previously
5757}
@@ -77,15 +77,15 @@ void TestAdjacentDifferenceCudaStreams()
7777{
7878 cudaStream_t s;
7979 cudaStreamCreate (&s);
80-
80+
8181 thrust::device_vector<int > input (3 );
8282 thrust::device_vector<int > output (3 );
8383 input[0 ] = 1 ; input[1 ] = 4 ; input[2 ] = 6 ;
84-
84+
8585 thrust::adjacent_difference (thrust::cuda::par.on (s), input.begin (), input.end (), output.begin ());
8686
8787 cudaStreamSynchronize (s);
88-
88+
8989 ASSERT_EQUAL (output[0 ], 1 );
9090 ASSERT_EQUAL (output[1 ], 3 );
9191 ASSERT_EQUAL (output[2 ], 2 );
@@ -94,3 +94,51 @@ void TestAdjacentDifferenceCudaStreams()
9494}
9595DECLARE_UNITTEST (TestAdjacentDifferenceCudaStreams);
9696
97+ struct detect_wrong_difference
98+ {
99+ bool * flag;
100+
101+ __host__ __device__ detect_wrong_difference operator ++() const { return *this ; }
102+ __host__ __device__ detect_wrong_difference operator *() const { return *this ; }
103+ template <typename Difference>
104+ __host__ __device__ detect_wrong_difference operator +(Difference) const { return *this ; }
105+ template <typename Index>
106+ __host__ __device__ detect_wrong_difference operator [](Index) const { return *this ; }
107+
108+ __device__
109+ void operator =(long long difference) const
110+ {
111+ if (difference != 1 )
112+ {
113+ *flag = false ;
114+ }
115+ }
116+ };
117+
118+ void TestAdjacentDifferenceWithBigIndexesHelper (int magnitude)
119+ {
120+ thrust::counting_iterator<long long > begin (1 );
121+ thrust::counting_iterator<long long > end = begin + (1ll << magnitude);
122+ ASSERT_EQUAL (thrust::distance (begin, end), 1ll << magnitude);
123+
124+ thrust::device_ptr<bool > all_differences_correct = thrust::device_malloc<bool >(1 );
125+ *all_differences_correct = true ;
126+
127+ detect_wrong_difference out = { thrust::raw_pointer_cast (all_differences_correct) };
128+
129+ thrust::adjacent_difference (thrust::device, begin, end, out);
130+
131+ bool all_differences_correct_h = *all_differences_correct;
132+ thrust::device_free (all_differences_correct);
133+
134+ ASSERT_EQUAL (all_differences_correct_h, true );
135+ }
136+
137+ void TestAdjacentDifferenceWithBigIndexes ()
138+ {
139+ TestAdjacentDifferenceWithBigIndexesHelper (30 );
140+ TestAdjacentDifferenceWithBigIndexesHelper (31 );
141+ TestAdjacentDifferenceWithBigIndexesHelper (32 );
142+ TestAdjacentDifferenceWithBigIndexesHelper (33 );
143+ }
144+ DECLARE_UNITTEST (TestAdjacentDifferenceWithBigIndexes);
0 commit comments