Skip to content

Commit

Permalink
Ensure that thrust fancy iterators are trivially_copy_constructible
Browse files Browse the repository at this point in the history
… when possible (#1368)

Our implementation provides user defined copy constructors, even if that is not necessary, as this was necessary prior to C++11.

Having dropped any support for older standard dialects, we can simply rely on defaulting or even better omitting those copy constructors.

This ensures that thrust fany iterators are trivially_copy_constructible when possible.

I did not rework transform_iterator, as that would require considerable effort due to potentially non copy assignable functors.

Addresses #1367
  • Loading branch information
miscco authored Feb 14, 2024
1 parent 57f449b commit f099227
Show file tree
Hide file tree
Showing 15 changed files with 132 additions and 196 deletions.
20 changes: 12 additions & 8 deletions thrust/testing/constant_iterator.cu
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <thrust/transform.h>
#include <thrust/reduce.h>

#include <cuda/std/type_traits>

void TestConstantIteratorConstructFromConvertibleSystem(void)
{
using namespace thrust;
Expand All @@ -30,21 +32,23 @@ void TestConstantIteratorIncrement(void)
lhs++;

ASSERT_EQUAL(1, lhs - rhs);

lhs++;
lhs++;

ASSERT_EQUAL(3, lhs - rhs);

lhs += 5;

ASSERT_EQUAL(8, lhs - rhs);

lhs -= 10;

ASSERT_EQUAL(-2, lhs - rhs);
}
DECLARE_UNITTEST(TestConstantIteratorIncrement);
static_assert(cuda::std::is_trivially_copy_constructible<thrust::constant_iterator<int>>::value, "");
static_assert(cuda::std::is_trivially_copyable<thrust::constant_iterator<int>>::value, "");

void TestConstantIteratorIncrementBig(void)
{
Expand All @@ -68,15 +72,15 @@ void TestConstantIteratorComparison(void)
ASSERT_EQUAL(true, iter1 == iter2);

iter1++;

ASSERT_EQUAL(1, iter1 - iter2);
ASSERT_EQUAL(false, iter1 == iter2);

iter2++;

ASSERT_EQUAL(0, iter1 - iter2);
ASSERT_EQUAL(true, iter1 == iter2);

iter1 += 100;
iter2 += 100;

Expand Down Expand Up @@ -146,7 +150,7 @@ void TestConstantIteratorTransform(void)
ASSERT_EQUAL(-7, result[1]);
ASSERT_EQUAL(-7, result[2]);
ASSERT_EQUAL(-7, result[3]);

thrust::transform(first1, last1, first2, result.begin(), thrust::plus<T>());

ASSERT_EQUAL(10, result[0]);
Expand Down
35 changes: 19 additions & 16 deletions thrust/testing/counting_iterator.cu
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <thrust/distance.h>
#include <thrust/detail/cstdint.h>

#include <cuda/std/type_traits>

THRUST_DISABLE_MSVC_POSSIBLE_LOSS_OF_DATA_WARNING_BEGIN

Expand Down Expand Up @@ -33,6 +34,8 @@ void TestCountingIteratorCopyConstructor(void)
ASSERT_EQUAL(*iter0, *d_iter);
}
DECLARE_UNITTEST(TestCountingIteratorCopyConstructor);
static_assert(cuda::std::is_trivially_copy_constructible<thrust::counting_iterator<int>>::value, "");
static_assert(cuda::std::is_trivially_copyable<thrust::counting_iterator<int>>::value, "");


void TestCountingIteratorIncrement(void)
Expand All @@ -44,18 +47,18 @@ void TestCountingIteratorIncrement(void)
iter++;

ASSERT_EQUAL(*iter, 1);

iter++;
iter++;

ASSERT_EQUAL(*iter, 3);

iter += 5;

ASSERT_EQUAL(*iter, 8);

iter -= 10;

ASSERT_EQUAL(*iter, -2);
}
DECLARE_UNITTEST(TestCountingIteratorIncrement);
Expand All @@ -70,15 +73,15 @@ void TestCountingIteratorComparison(void)
ASSERT_EQUAL(iter1 == iter2, true);

iter1++;

ASSERT_EQUAL(iter1 - iter2, 1);
ASSERT_EQUAL(iter1 == iter2, false);

iter2++;

ASSERT_EQUAL(iter1 - iter2, 0);
ASSERT_EQUAL(iter1 == iter2, true);

iter1 += 100;
iter2 += 100;

Expand All @@ -99,19 +102,19 @@ void TestCountingIteratorFloatComparison(void)
ASSERT_EQUAL(iter2 < iter1, false);

iter1++;

ASSERT_EQUAL(iter1 - iter2, 1);
ASSERT_EQUAL(iter1 == iter2, false);
ASSERT_EQUAL(iter2 < iter1, true);
ASSERT_EQUAL(iter1 < iter2, false);
ASSERT_EQUAL(iter2 < iter1, true);
ASSERT_EQUAL(iter1 < iter2, false);

iter2++;

ASSERT_EQUAL(iter1 - iter2, 0);
ASSERT_EQUAL(iter1 == iter2, true);
ASSERT_EQUAL(iter1 < iter2, false);
ASSERT_EQUAL(iter2 < iter1, false);

iter1 += 100;
iter2 += 100;

Expand All @@ -130,12 +133,12 @@ void TestCountingIteratorFloatComparison(void)
ASSERT_EQUAL(iter4 < iter3, false);

iter3++; // iter3 = 1.0, iter4 = 0.5

ASSERT_EQUAL(iter3 - iter4, 0);
ASSERT_EQUAL(iter3 == iter4, true);
ASSERT_EQUAL(iter3 < iter4, false);
ASSERT_EQUAL(iter4 < iter3, false);

iter4++; // iter3 = 1.0, iter4 = 1.5

ASSERT_EQUAL(iter3 - iter4, 0);
Expand All @@ -162,9 +165,9 @@ void TestCountingIteratorDistance(void)
ASSERT_EQUAL(thrust::distance(iter1, iter2), 5);

iter1++;

ASSERT_EQUAL(thrust::distance(iter1, iter2), 4);

iter2 += 100;

ASSERT_EQUAL(thrust::distance(iter1, iter2), 104);
Expand Down
20 changes: 12 additions & 8 deletions thrust/testing/discard_iterator.cu
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <unittest/unittest.h>
#include <thrust/iterator/discard_iterator.h>

#include <cuda/std/type_traits>

void TestDiscardIteratorIncrement(void)
{
thrust::discard_iterator<> lhs(0);
Expand All @@ -11,21 +13,23 @@ void TestDiscardIteratorIncrement(void)
lhs++;

ASSERT_EQUAL(1, lhs - rhs);

lhs++;
lhs++;

ASSERT_EQUAL(3, lhs - rhs);

lhs += 5;

ASSERT_EQUAL(8, lhs - rhs);

lhs -= 10;

ASSERT_EQUAL(-2, lhs - rhs);
}
DECLARE_UNITTEST(TestDiscardIteratorIncrement);
static_assert(cuda::std::is_trivially_copy_constructible<thrust::discard_iterator<>>::value, "");
static_assert(cuda::std::is_trivially_copyable<thrust::discard_iterator<>>::value, "");

void TestDiscardIteratorComparison(void)
{
Expand All @@ -36,15 +40,15 @@ void TestDiscardIteratorComparison(void)
ASSERT_EQUAL(true, iter1 == iter2);

iter1++;

ASSERT_EQUAL(1, iter1 - iter2);
ASSERT_EQUAL(false, iter1 == iter2);

iter2++;

ASSERT_EQUAL(0, iter1 - iter2);
ASSERT_EQUAL(true, iter1 == iter2);

iter1 += 100;
iter2 += 100;

Expand Down Expand Up @@ -82,7 +86,7 @@ void TestZippedDiscardIterator(void)
{
;
}

ASSERT_EQUAL(10, thrust::get<0>(z_iter1_first.get_iterator_tuple()) - thrust::make_discard_iterator());

typedef tuple<int *, discard_iterator<> > IteratorTuple2;
Expand Down
Loading

0 comments on commit f099227

Please sign in to comment.