#include #include #include #include #include #include #include template void TestTransformInputOutputIterator(void) { typedef typename Vector::value_type T; typedef thrust::negate InputFunction; typedef thrust::square OutputFunction; typedef typename Vector::iterator Iterator; Vector input(4); Vector squared(4); Vector negated(4); // initialize input thrust::sequence(input.begin(), input.end(), 1); // construct transform_iterator thrust::transform_input_output_iterator transform_iter(squared.begin(), InputFunction(), OutputFunction()); // 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; 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; ASSERT_EQUAL(negated, gold_negated); } DECLARE_VECTOR_UNITTEST(TestTransformInputOutputIterator); template void TestMakeTransformInputOutputIterator(void) { typedef typename Vector::value_type T; typedef thrust::negate InputFunction; typedef thrust::square OutputFunction; Vector input(4); Vector negated(4); Vector squared(4); // initialize input thrust::sequence(input.begin(), input.end(), 1); // negated value read from transform iterator thrust::copy_n(thrust::make_transform_input_output_iterator(input.begin(), InputFunction(), OutputFunction()), input.size(), negated.begin()); Vector gold_negated(4); gold_negated[0] = -1; gold_negated[1] = -2; gold_negated[2] = -3; gold_negated[3] = -4; ASSERT_EQUAL(negated, gold_negated); // squared value writen by transform iterator thrust::copy(negated.begin(), 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; ASSERT_EQUAL(squared, gold_squared); } DECLARE_VECTOR_UNITTEST(TestMakeTransformInputOutputIterator); template struct TestTransformInputOutputIteratorScan { void operator()(const size_t n) { thrust::host_vector h_data = unittest::random_samples(n); thrust::device_vector d_data = h_data; thrust::host_vector h_result(n); thrust::device_vector d_result(n); // run on host (uses forward iterator negate) thrust::inclusive_scan(thrust::make_transform_input_output_iterator(h_data.begin(), thrust::negate(), thrust::identity()), thrust::make_transform_input_output_iterator(h_data.end(), thrust::negate(), thrust::identity()), h_result.begin()); // run on device (uses reverse iterator negate) thrust::inclusive_scan(d_data.begin(), d_data.end(), thrust::make_transform_input_output_iterator( d_result.begin(), thrust::square(), thrust::negate())); ASSERT_EQUAL(h_result, d_result); } }; VariableUnitTest TestTransformInputOutputIteratorScanInstance;