#include #include #include #include #include #include #include #include THRUST_DISABLE_MSVC_POSSIBLE_LOSS_OF_DATA_WARNING_BEGIN template class mark_present_for_each { public: T * ptr; __host__ __device__ void operator()(T x){ ptr[(int) x] = 1; } }; template void TestForEachSimple(void) { typedef typename Vector::value_type T; Vector input(5); Vector output(7, (T) 0); input[0] = 3; input[1] = 2; input[2] = 3; input[3] = 4; input[4] = 6; mark_present_for_each f; f.ptr = thrust::raw_pointer_cast(output.data()); typename Vector::iterator result = thrust::for_each(input.begin(), input.end(), f); ASSERT_EQUAL(output[0], 0); ASSERT_EQUAL(output[1], 0); ASSERT_EQUAL(output[2], 1); ASSERT_EQUAL(output[3], 1); ASSERT_EQUAL(output[4], 1); ASSERT_EQUAL(output[5], 0); ASSERT_EQUAL(output[6], 1); ASSERT_EQUAL_QUIET(result, input.end()); } DECLARE_INTEGRAL_VECTOR_UNITTEST(TestForEachSimple); template InputIterator for_each(my_system &system, InputIterator first, InputIterator, Function) { system.validate_dispatch(); return first; } void TestForEachDispatchExplicit() { thrust::device_vector vec(1); my_system sys(0); thrust::for_each(sys, vec.begin(), vec.end(), 0); ASSERT_EQUAL(true, sys.is_valid()); } DECLARE_UNITTEST(TestForEachDispatchExplicit); template InputIterator for_each(my_tag, InputIterator first, InputIterator, Function) { *first = 13; return first; } void TestForEachDispatchImplicit() { thrust::device_vector vec(1); thrust::for_each(thrust::retag(vec.begin()), thrust::retag(vec.end()), 0); ASSERT_EQUAL(13, vec.front()); } DECLARE_UNITTEST(TestForEachDispatchImplicit); template void TestForEachNSimple(void) { typedef typename Vector::value_type T; Vector input(5); Vector output(7, (T) 0); input[0] = 3; input[1] = 2; input[2] = 3; input[3] = 4; input[4] = 6; mark_present_for_each f; f.ptr = thrust::raw_pointer_cast(output.data()); typename Vector::iterator result = thrust::for_each_n(input.begin(), input.size(), f); ASSERT_EQUAL(output[0], 0); ASSERT_EQUAL(output[1], 0); ASSERT_EQUAL(output[2], 1); ASSERT_EQUAL(output[3], 1); ASSERT_EQUAL(output[4], 1); ASSERT_EQUAL(output[5], 0); ASSERT_EQUAL(output[6], 1); ASSERT_EQUAL_QUIET(result, input.end()); } DECLARE_INTEGRAL_VECTOR_UNITTEST(TestForEachNSimple); template InputIterator for_each_n(my_system &system, InputIterator first, Size, Function) { system.validate_dispatch(); return first; } void TestForEachNDispatchExplicit() { thrust::device_vector vec(1); my_system sys(0); thrust::for_each_n(sys, vec.begin(), vec.size(), 0); ASSERT_EQUAL(true, sys.is_valid()); } DECLARE_UNITTEST(TestForEachNDispatchExplicit); template InputIterator for_each_n(my_tag, InputIterator first, Size, Function) { *first = 13; return first; } void TestForEachNDispatchImplicit() { thrust::device_vector vec(1); thrust::for_each_n(thrust::retag(vec.begin()), vec.size(), 0); ASSERT_EQUAL(13, vec.front()); } DECLARE_UNITTEST(TestForEachNDispatchImplicit); void TestForEachSimpleAnySystem(void) { thrust::device_vector output(7, 0); mark_present_for_each f; f.ptr = thrust::raw_pointer_cast(output.data()); thrust::counting_iterator result = thrust::for_each(thrust::make_counting_iterator(0), thrust::make_counting_iterator(5), f); ASSERT_EQUAL(output[0], 1); ASSERT_EQUAL(output[1], 1); ASSERT_EQUAL(output[2], 1); ASSERT_EQUAL(output[3], 1); ASSERT_EQUAL(output[4], 1); ASSERT_EQUAL(output[5], 0); ASSERT_EQUAL(output[6], 0); ASSERT_EQUAL_QUIET(result, thrust::make_counting_iterator(5)); } DECLARE_UNITTEST(TestForEachSimpleAnySystem); void TestForEachNSimpleAnySystem(void) { thrust::device_vector output(7, 0); mark_present_for_each f; f.ptr = thrust::raw_pointer_cast(output.data()); thrust::counting_iterator result = thrust::for_each_n(thrust::make_counting_iterator(0), 5, f); ASSERT_EQUAL(output[0], 1); ASSERT_EQUAL(output[1], 1); ASSERT_EQUAL(output[2], 1); ASSERT_EQUAL(output[3], 1); ASSERT_EQUAL(output[4], 1); ASSERT_EQUAL(output[5], 0); ASSERT_EQUAL(output[6], 0); ASSERT_EQUAL_QUIET(result, thrust::make_counting_iterator(5)); } DECLARE_UNITTEST(TestForEachNSimpleAnySystem); template void TestForEach(const size_t n) { const size_t output_size = std::min((size_t) 10, 2 * n); thrust::host_vector h_input = unittest::random_integers(n); for(size_t i = 0; i < n; i++) h_input[i] = ((size_t) h_input[i]) % output_size; thrust::device_vector d_input = h_input; thrust::host_vector h_output(output_size, (T) 0); thrust::device_vector d_output(output_size, (T) 0); mark_present_for_each h_f; mark_present_for_each d_f; h_f.ptr = &h_output[0]; d_f.ptr = (&d_output[0]).get(); typename thrust::host_vector::iterator h_result = thrust::for_each(h_input.begin(), h_input.end(), h_f); typename thrust::device_vector::iterator d_result = thrust::for_each(d_input.begin(), d_input.end(), d_f); ASSERT_EQUAL(h_output, d_output); ASSERT_EQUAL_QUIET(h_result, h_input.end()); ASSERT_EQUAL_QUIET(d_result, d_input.end()); } DECLARE_VARIABLE_UNITTEST(TestForEach); template void TestForEachN(const size_t n) { const size_t output_size = std::min((size_t) 10, 2 * n); thrust::host_vector h_input = unittest::random_integers(n); for(size_t i = 0; i < n; i++) h_input[i] = ((size_t) h_input[i]) % output_size; thrust::device_vector d_input = h_input; thrust::host_vector h_output(output_size, (T) 0); thrust::device_vector d_output(output_size, (T) 0); mark_present_for_each h_f; mark_present_for_each d_f; h_f.ptr = &h_output[0]; d_f.ptr = (&d_output[0]).get(); typename thrust::host_vector::iterator h_result = thrust::for_each_n(h_input.begin(), h_input.size(), h_f); typename thrust::device_vector::iterator d_result = thrust::for_each_n(d_input.begin(), d_input.size(), d_f); ASSERT_EQUAL(h_output, d_output); ASSERT_EQUAL_QUIET(h_result, h_input.end()); ASSERT_EQUAL_QUIET(d_result, d_input.end()); } DECLARE_VARIABLE_UNITTEST(TestForEachN); template struct SetFixedVectorToConstant { FixedVector exemplar; SetFixedVectorToConstant(T scalar) : exemplar(scalar) {} __host__ __device__ void operator()(FixedVector& t) { t = exemplar; } }; template void _TestForEachWithLargeTypes(void) { size_t n = (64 * 1024) / sizeof(FixedVector); thrust::host_vector< FixedVector > h_data(n); for(size_t i = 0; i < h_data.size(); i++) h_data[i] = FixedVector(i); thrust::device_vector< FixedVector > d_data = h_data; SetFixedVectorToConstant func(123); thrust::for_each(h_data.begin(), h_data.end(), func); thrust::for_each(d_data.begin(), d_data.end(), func); ASSERT_EQUAL_QUIET(h_data, d_data); } void TestForEachWithLargeTypes(void) { _TestForEachWithLargeTypes(); _TestForEachWithLargeTypes(); _TestForEachWithLargeTypes(); _TestForEachWithLargeTypes(); _TestForEachWithLargeTypes(); _TestForEachWithLargeTypes(); // fails on Linux 32 w/ gcc 4.1 _TestForEachWithLargeTypes(); _TestForEachWithLargeTypes(); _TestForEachWithLargeTypes(); _TestForEachWithLargeTypes(); // XXX parallel_for doens't support large types // _TestForEachWithLargeTypes(); // fails on Vista 64 w/ VS2008 } DECLARE_UNITTEST(TestForEachWithLargeTypes); template void _TestForEachNWithLargeTypes(void) { size_t n = (64 * 1024) / sizeof(FixedVector); thrust::host_vector< FixedVector > h_data(n); for(size_t i = 0; i < h_data.size(); i++) h_data[i] = FixedVector(i); thrust::device_vector< FixedVector > d_data = h_data; SetFixedVectorToConstant func(123); thrust::for_each_n(h_data.begin(), h_data.size(), func); thrust::for_each_n(d_data.begin(), d_data.size(), func); ASSERT_EQUAL_QUIET(h_data, d_data); } void TestForEachNWithLargeTypes(void) { _TestForEachNWithLargeTypes(); _TestForEachNWithLargeTypes(); _TestForEachNWithLargeTypes(); _TestForEachNWithLargeTypes(); _TestForEachNWithLargeTypes(); _TestForEachNWithLargeTypes(); // fails on Linux 32 w/ gcc 4.1 _TestForEachNWithLargeTypes(); _TestForEachNWithLargeTypes(); _TestForEachNWithLargeTypes(); _TestForEachNWithLargeTypes(); // XXX parallel_for doens't support large types // _TestForEachNWithLargeTypes(); // fails on Vista 64 w/ VS2008 } DECLARE_UNITTEST(TestForEachNWithLargeTypes); THRUST_DISABLE_MSVC_POSSIBLE_LOSS_OF_DATA_WARNING_END struct only_set_when_expected { unsigned long long expected; bool * flag; __device__ void operator()(unsigned long long x) { if (x == expected) { *flag = true; } } }; void TestForEachWithBigIndexesHelper(int magnitude) { thrust::counting_iterator begin(0); thrust::counting_iterator end = begin + (1ull << magnitude); ASSERT_EQUAL(thrust::distance(begin, end), 1ll << magnitude); thrust::device_ptr has_executed = thrust::device_malloc(1); *has_executed = false; only_set_when_expected fn = { (1ull << magnitude) - 1, thrust::raw_pointer_cast(has_executed) }; thrust::for_each(thrust::device, begin, end, fn); bool has_executed_h = *has_executed; thrust::device_free(has_executed); ASSERT_EQUAL(has_executed_h, true); } void TestForEachWithBigIndexes() { TestForEachWithBigIndexesHelper(30); TestForEachWithBigIndexesHelper(31); TestForEachWithBigIndexesHelper(32); TestForEachWithBigIndexesHelper(33); } DECLARE_UNITTEST(TestForEachWithBigIndexes);