#include #include #include #include template __global__ void sort_kernel(ExecutionPolicy exec, Iterator first, Iterator last, Compare comp, Iterator2 is_supported) { #if (__CUDA_ARCH__ >= 200) *is_supported = true; thrust::sort(exec, first, last, comp); #else *is_supported = false; #endif } template struct my_less { __host__ __device__ bool operator()(const T& lhs, const T& rhs) const { return lhs < rhs; } }; template void TestComparisonSortDevice(ExecutionPolicy exec, const size_t n, Compare comp) { thrust::host_vector h_data = unittest::random_integers(n); thrust::device_vector d_data = h_data; thrust::device_vector is_supported(1); sort_kernel<<<1,1>>>(exec, d_data.begin(), d_data.end(), comp, is_supported.begin()); cudaError_t const err = cudaDeviceSynchronize(); ASSERT_EQUAL(cudaSuccess, err); if(is_supported[0]) { thrust::sort(h_data.begin(), h_data.end(), comp); ASSERT_EQUAL(h_data, d_data); } }; template struct TestComparisonSortDeviceSeq { void operator()(const size_t n) { TestComparisonSortDevice(thrust::seq, n, my_less()); } }; VariableUnitTest< TestComparisonSortDeviceSeq, unittest::type_list > TestComparisonSortDeviceSeqInstance; template struct TestComparisonSortDeviceDevice { void operator()(const size_t n) { TestComparisonSortDevice(thrust::device, n, my_less()); } }; VariableUnitTest< TestComparisonSortDeviceDevice, unittest::type_list > TestComparisonSortDeviceDeviceDeviceInstance; template void TestSortDevice(ExecutionPolicy exec, const size_t n) { TestComparisonSortDevice(exec, n, thrust::less()); }; template struct TestSortDeviceSeq { void operator()(const size_t n) { TestSortDevice(thrust::seq, n); } }; VariableUnitTest< TestSortDeviceSeq, unittest::type_list > TestSortDeviceSeqInstance; template struct TestSortDeviceDevice { void operator()(const size_t n) { TestSortDevice(thrust::device, n); } }; VariableUnitTest< TestSortDeviceDevice, unittest::type_list > TestSortDeviceDeviceInstance; void TestSortCudaStreams() { thrust::device_vector keys(10); keys[0] = 9; keys[1] = 3; keys[2] = 2; keys[3] = 0; keys[4] = 4; keys[5] = 7; keys[6] = 8; keys[7] = 1; keys[8] = 5; keys[9] = 6; cudaStream_t s; cudaStreamCreate(&s); thrust::sort(thrust::cuda::par.on(s), keys.begin(), keys.end()); cudaStreamSynchronize(s); ASSERT_EQUAL(true, thrust::is_sorted(keys.begin(), keys.end())); cudaStreamDestroy(s); } DECLARE_UNITTEST(TestSortCudaStreams); void TestComparisonSortCudaStreams() { thrust::device_vector keys(10); keys[0] = 9; keys[1] = 3; keys[2] = 2; keys[3] = 0; keys[4] = 4; keys[5] = 7; keys[6] = 8; keys[7] = 1; keys[8] = 5; keys[9] = 6; cudaStream_t s; cudaStreamCreate(&s); thrust::sort(thrust::cuda::par.on(s), keys.begin(), keys.end(), my_less()); cudaStreamSynchronize(s); ASSERT_EQUAL(true, thrust::is_sorted(keys.begin(), keys.end(), my_less())); cudaStreamDestroy(s); } DECLARE_UNITTEST(TestComparisonSortCudaStreams);