Skip to content

Commit

Permalink
almost there
Browse files Browse the repository at this point in the history
  • Loading branch information
gonidelis committed Sep 18, 2024
1 parent 44dc8d5 commit 706688f
Show file tree
Hide file tree
Showing 14 changed files with 145 additions and 465 deletions.
31 changes: 7 additions & 24 deletions thrust/testing/swap_ranges.cu
Original file line number Diff line number Diff line change
Expand Up @@ -44,33 +44,16 @@ DECLARE_UNITTEST(TestSwapRangesDispatchImplicit);
template <class Vector>
void TestSwapRangesSimple()
{
Vector v1(5);
v1[0] = 0;
v1[1] = 1;
v1[2] = 2;
v1[3] = 3;
v1[4] = 4;

Vector v2(5);
v2[0] = 5;
v2[1] = 6;
v2[2] = 7;
v2[3] = 8;
v2[4] = 9;
Vector v1{0, 1, 2, 3, 4};
Vector v2{5, 6, 7, 8, 9};

thrust::swap_ranges(v1.begin(), v1.end(), v2.begin());

ASSERT_EQUAL(v1[0], 5);
ASSERT_EQUAL(v1[1], 6);
ASSERT_EQUAL(v1[2], 7);
ASSERT_EQUAL(v1[3], 8);
ASSERT_EQUAL(v1[4], 9);

ASSERT_EQUAL(v2[0], 0);
ASSERT_EQUAL(v2[1], 1);
ASSERT_EQUAL(v2[2], 2);
ASSERT_EQUAL(v2[3], 3);
ASSERT_EQUAL(v2[4], 4);
Vector ref1{5, 6, 7, 8, 9};
ASSERT_EQUAL(v1, ref1);

Vector ref2{0, 1, 2, 3, 4};
ASSERT_EQUAL(v2, ref2);
}
DECLARE_VECTOR_UNITTEST(TestSwapRangesSimple);

Expand Down
21 changes: 6 additions & 15 deletions thrust/testing/tabulate.cu
Original file line number Diff line number Diff line change
Expand Up @@ -48,27 +48,18 @@ void TestTabulateSimple()

thrust::tabulate(v.begin(), v.end(), thrust::identity<T>());

ASSERT_EQUAL(v[0], 0);
ASSERT_EQUAL(v[1], 1);
ASSERT_EQUAL(v[2], 2);
ASSERT_EQUAL(v[3], 3);
ASSERT_EQUAL(v[4], 4);
Vector ref{0, 1, 2, 3, 4};
ASSERT_EQUAL(v, ref);

thrust::tabulate(v.begin(), v.end(), -_1);

ASSERT_EQUAL(v[0], 0);
ASSERT_EQUAL(v[1], -1);
ASSERT_EQUAL(v[2], -2);
ASSERT_EQUAL(v[3], -3);
ASSERT_EQUAL(v[4], -4);
ref = {0, -1, -2, -3, -4};
ASSERT_EQUAL(v, ref);

thrust::tabulate(v.begin(), v.end(), _1 * _1 * _1);

ASSERT_EQUAL(v[0], 0);
ASSERT_EQUAL(v[1], 1);
ASSERT_EQUAL(v[2], 8);
ASSERT_EQUAL(v[3], 27);
ASSERT_EQUAL(v[4], 64);
ref = {0, 1, 8, 27, 64};
ASSERT_EQUAL(v, ref);
}
DECLARE_VECTOR_UNITTEST(TestTabulateSimple);

Expand Down
18 changes: 6 additions & 12 deletions thrust/testing/tabulate_output_iterator.cu
Original file line number Diff line number Diff line change
Expand Up @@ -129,22 +129,16 @@ void TestTabulateOutputIterator()
thrust::tabulate_output_iterator<op_t> tabulate_out_it{op_t{out.begin()}};

tabulate_out_it[1] = 2;
ASSERT_EQUAL(out[0], 42);
ASSERT_EQUAL(out[1], 2);
ASSERT_EQUAL(out[2], 42);
ASSERT_EQUAL(out[3], 42);
vector_t ref{42, 2, 42, 42};
ASSERT_EQUAL(out, ref);

tabulate_out_it[3] = 0;
ASSERT_EQUAL(out[0], 42);
ASSERT_EQUAL(out[1], 2);
ASSERT_EQUAL(out[2], 42);
ASSERT_EQUAL(out[3], 0);
ref = {42, 2, 42, 0};
ASSERT_EQUAL(out, ref);

tabulate_out_it[1] = 4;
ASSERT_EQUAL(out[0], 42);
ASSERT_EQUAL(out[1], 4);
ASSERT_EQUAL(out[2], 42);
ASSERT_EQUAL(out[3], 0);
ref = {42, 4, 42, 0};
ASSERT_EQUAL(out, ref);
}

DECLARE_UNITTEST(TestTabulateOutputIterator);
100 changes: 23 additions & 77 deletions thrust/testing/transform.cu
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,9 @@ THRUST_DISABLE_BROKEN_GCC_VECTORIZER void TestTransformUnarySimple()

typename Vector::iterator iter;

Vector input(3);
Vector input{1, -2, 3};
Vector output(3);
Vector result(3);
input[0] = 1;
input[1] = -2;
input[2] = 3;
result[0] = -1;
result[1] = 2;
result[2] = -3;
Vector result{-1, 2, -3};

iter = thrust::transform(input.begin(), input.end(), output.begin(), thrust::negate<T>());

Expand Down Expand Up @@ -83,15 +77,9 @@ THRUST_DISABLE_BROKEN_GCC_VECTORIZER void TestTransformIfUnaryNoStencilSimple()

typename Vector::iterator iter;

Vector input(3);
Vector output(3);
Vector result(3);

// clang-format off
input[0] = 0; input[1] = -2; input[2] = 0;
output[0] = -1; output[1] = -2; output[2] = -3;
result[0] = -1; result[1] = 2; result[2] = -3;
// clang-format on
Vector input{0, -2, 0};
Vector output{-1, -2, -3};
Vector result{-1, 2, -3};

iter = thrust::transform_if(input.begin(), input.end(), output.begin(), thrust::negate<T>(), thrust::identity<T>());

Expand Down Expand Up @@ -148,17 +136,10 @@ THRUST_DISABLE_BROKEN_GCC_VECTORIZER void TestTransformIfUnarySimple()

typename Vector::iterator iter;

Vector input(3);
Vector stencil(3);
Vector output(3);
Vector result(3);

// clang-format off
input[0] = 1; input[1] = -2; input[2] = 3;
output[0] = 1; output[1] = 2; output[2] = 3;
stencil[0] = 1; stencil[1] = 0; stencil[2] = 1;
result[0] = -1; result[1] = 2; result[2] = -3;
// clang-format on
Vector input{1, -2, 3};
Vector stencil{1, 0, 1};
Vector output{1, 2, 3};
Vector result{-1, 2, -3};

iter = thrust::transform_if(
input.begin(), input.end(), stencil.begin(), output.begin(), thrust::negate<T>(), thrust::identity<T>());
Expand Down Expand Up @@ -223,16 +204,10 @@ THRUST_DISABLE_BROKEN_GCC_VECTORIZER void TestTransformBinarySimple()
// There is a strange gcc bug here where it belives we would write out of bounds.
// It seems to go away if we add one more element that we leave untouched. Luckily 0 - 0 = 0 so all is fine.
// Note that we still write the element, so it does not hide a functional thrust bug
Vector input1(4);
Vector input2(4);
Vector output(4);
Vector result(4);

// clang-format off
input1[0] = 1; input1[1] = -2; input1[2] = 3;
input2[0] = -4; input2[1] = 5; input2[2] = 6;
result[0] = 5; result[1] = -7; result[2] = -3;
// clang-format on
Vector input1{1, -2, 3};
Vector input2{-4, 5, 6};
Vector output(3);
Vector result{5, -7, -3};

iter = thrust::transform(input1.begin(), input1.end(), input2.begin(), output.begin(), thrust::minus<T>());

Expand Down Expand Up @@ -289,19 +264,11 @@ THRUST_DISABLE_BROKEN_GCC_VECTORIZER void TestTransformIfBinarySimple()

typename Vector::iterator iter;

Vector input1(3);
Vector input2(3);
Vector stencil(3);
Vector output(3);
Vector result(3);

// clang-format off
input1[0] = 1; input1[1] = -2; input1[2] = 3;
input2[0] = -4; input2[1] = 5; input2[2] = 6;
stencil[0] = 0; stencil[1] = 1; stencil[2] = 0;
output[0] = 1; output[1] = 2; output[2] = 3;
result[0] = 5; result[1] = 2; result[2] = -3;
// clang-format on
Vector input1{1, -2, 3};
Vector input2{-4, 5, 6};
Vector stencil{0, 1, 0};
Vector output{1, 2, 3};
Vector result{5, 2, -3};

thrust::identity<T> identity;

Expand Down Expand Up @@ -768,37 +735,16 @@ THRUST_DISABLE_BROKEN_GCC_VECTORIZER void TestTransformWithIndirection()
// add numbers modulo 3 with external lookup table
using T = typename Vector::value_type;

Vector input1(7);
Vector input2(7);
Vector input1{0, 1, 2, 1, 2, 0, 1};
Vector input2{2, 2, 2, 0, 2, 1, 0};
Vector output(7, 0);

// clang-format off
input1[0] = 0; input2[0] = 2;
input1[1] = 1; input2[1] = 2;
input1[2] = 2; input2[2] = 2;
input1[3] = 1; input2[3] = 0;
input1[4] = 2; input2[4] = 2;
input1[5] = 0; input2[5] = 1;
input1[6] = 1; input2[6] = 0;
// clang-format on

Vector table(6);
table[0] = 0;
table[1] = 1;
table[2] = 2;
table[3] = 0;
table[4] = 1;
table[5] = 2;
Vector table{0, 1, 2, 0, 1, 2};

thrust::transform(
input1.begin(), input1.end(), input2.begin(), output.begin(), plus_mod3<T>(thrust::raw_pointer_cast(&table[0])));

ASSERT_EQUAL(output[0], T(2));
ASSERT_EQUAL(output[1], T(0));
ASSERT_EQUAL(output[2], T(1));
ASSERT_EQUAL(output[3], T(1));
ASSERT_EQUAL(output[4], T(1));
ASSERT_EQUAL(output[5], T(1));
ASSERT_EQUAL(output[6], T(1));
Vector ref{2, 0, 1, 1, 1, 1, 1};
ASSERT_EQUAL(output, ref);
}
DECLARE_INTEGRAL_VECTOR_UNITTEST(TestTransformWithIndirection);
24 changes: 4 additions & 20 deletions thrust/testing/transform_input_output_iterator.cu
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,14 @@ void TestTransformInputOutputIterator()
// transform_iter writes squared value
thrust::copy(input.begin(), input.end(), transform_iter);

Vector gold_squared(4);
gold_squared[0] = 1;
gold_squared[1] = 4;
gold_squared[2] = 9;
gold_squared[3] = 16;
Vector gold_squared{1, 4, 9, 16};

ASSERT_EQUAL(squared, gold_squared);

// negated value read from transform_iter
thrust::copy_n(transform_iter, squared.size(), negated.begin());

Vector gold_negated(4);
gold_negated[0] = -1;
gold_negated[1] = -4;
gold_negated[2] = -9;
gold_negated[3] = -16;
Vector gold_negated{-1, -4, -9, -16};

ASSERT_EQUAL(negated, gold_negated);
}
Expand All @@ -71,11 +63,7 @@ void TestMakeTransformInputOutputIterator()
input.size(),
negated.begin());

Vector gold_negated(4);
gold_negated[0] = -1;
gold_negated[1] = -2;
gold_negated[2] = -3;
gold_negated[3] = -4;
Vector gold_negated{-1, -2, -3, -4};

ASSERT_EQUAL(negated, gold_negated);

Expand All @@ -84,11 +72,7 @@ void TestMakeTransformInputOutputIterator()
negated.end(),
thrust::make_transform_input_output_iterator(squared.begin(), InputFunction(), OutputFunction()));

Vector gold_squared(4);
gold_squared[0] = 1;
gold_squared[1] = 4;
gold_squared[2] = 9;
gold_squared[3] = 16;
Vector gold_squared{1, 4, 9, 16};

ASSERT_EQUAL(squared, gold_squared);
}
Expand Down
12 changes: 4 additions & 8 deletions thrust/testing/transform_iterator.cu
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@ void TestTransformIterator()

thrust::copy(iter, iter + 4, output.begin());

ASSERT_EQUAL(output[0], -1);
ASSERT_EQUAL(output[1], -2);
ASSERT_EQUAL(output[2], -3);
ASSERT_EQUAL(output[3], -4);
Vector ref{-1, -2, -3, -4};
ASSERT_EQUAL(output, ref);
}
DECLARE_VECTOR_UNITTEST(TestTransformIterator);

Expand All @@ -57,10 +55,8 @@ void TestMakeTransformIterator()
thrust::make_transform_iterator(input.end(), UnaryFunction()),
output.begin());

ASSERT_EQUAL(output[0], -1);
ASSERT_EQUAL(output[1], -2);
ASSERT_EQUAL(output[2], -3);
ASSERT_EQUAL(output[3], -4);
Vector ref{-1, -2, -3, -4};
ASSERT_EQUAL(output, ref);
}
DECLARE_VECTOR_UNITTEST(TestMakeTransformIterator);

Expand Down
12 changes: 2 additions & 10 deletions thrust/testing/transform_output_iterator.cu
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,7 @@ void TestTransformOutputIterator()

thrust::copy(input.begin(), input.end(), output_iter);

Vector gold_output(4);
gold_output[0] = 1;
gold_output[1] = 4;
gold_output[2] = 9;
gold_output[3] = 16;
Vector gold_output{1, 4, 9, 16};

ASSERT_EQUAL(output, gold_output);
}
Expand All @@ -53,11 +49,7 @@ void TestMakeTransformOutputIterator()

thrust::copy(input.begin(), input.end(), thrust::make_transform_output_iterator(output.begin(), UnaryFunction()));

Vector gold_output(4);
gold_output[0] = 1;
gold_output[1] = 4;
gold_output[2] = 9;
gold_output[3] = 16;
Vector gold_output{1, 4, 9, 16};
ASSERT_EQUAL(output, gold_output);
}
DECLARE_VECTOR_UNITTEST(TestMakeTransformOutputIterator);
Expand Down
5 changes: 1 addition & 4 deletions thrust/testing/transform_reduce.cu
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,7 @@ void TestTransformReduceSimple()
{
using T = typename Vector::value_type;

Vector data(3);
data[0] = 1;
data[1] = -2;
data[2] = 3;
Vector data{1, -2, 3};

T init = 10;
T result = thrust::transform_reduce(data.begin(), data.end(), thrust::negate<T>(), init, thrust::plus<T>());
Expand Down
Loading

0 comments on commit 706688f

Please sign in to comment.