Spaces:
Runtime error
Runtime error
File size: 5,161 Bytes
be11144 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
#include <unittest/unittest.h>
#include <thrust/scatter.h>
#include <thrust/execution_policy.h>
#include <algorithm>
template<typename ExecutionPolicy, typename Iterator1, typename Iterator2, typename Iterator3>
__global__
void scatter_kernel(ExecutionPolicy exec, Iterator1 first, Iterator1 last, Iterator2 map_first, Iterator3 result)
{
thrust::scatter(exec, first, last, map_first, result);
}
template<typename ExecutionPolicy>
void TestScatterDevice(ExecutionPolicy exec)
{
size_t n = 1000;
const size_t output_size = std::min((size_t) 10, 2 * n);
thrust::host_vector<int> h_input(n, 1);
thrust::device_vector<int> d_input(n, 1);
thrust::host_vector<unsigned int> h_map = unittest::random_integers<unsigned int>(n);
for(size_t i = 0; i < n; i++)
{
h_map[i] = h_map[i] % output_size;
}
thrust::device_vector<unsigned int> d_map = h_map;
thrust::host_vector<int> h_output(output_size, 0);
thrust::device_vector<int> d_output(output_size, 0);
thrust::scatter(h_input.begin(), h_input.end(), h_map.begin(), h_output.begin());
scatter_kernel<<<1,1>>>(exec, d_input.begin(), d_input.end(), d_map.begin(), d_output.begin());
cudaError_t const err = cudaDeviceSynchronize();
ASSERT_EQUAL(cudaSuccess, err);
ASSERT_EQUAL(h_output, d_output);
}
void TestScatterDeviceSeq()
{
TestScatterDevice(thrust::seq);
}
DECLARE_UNITTEST(TestScatterDeviceSeq);
void TestScatterDeviceDevice()
{
TestScatterDevice(thrust::device);
}
DECLARE_UNITTEST(TestScatterDeviceDevice);
template<typename ExecutionPolicy, typename Iterator1, typename Iterator2, typename Iterator3, typename Iterator4, typename Function>
__global__
void scatter_if_kernel(ExecutionPolicy exec, Iterator1 first, Iterator1 last, Iterator2 map_first, Iterator3 stencil_first, Iterator4 result, Function f)
{
thrust::scatter_if(exec, first, last, map_first, stencil_first, result, f);
}
template<typename T>
struct is_even_scatter_if
{
__host__ __device__ bool operator()(const T i) const { return (i % 2) == 0; }
};
template<typename ExecutionPolicy>
void TestScatterIfDevice(ExecutionPolicy exec)
{
size_t n = 1000;
const size_t output_size = std::min((size_t) 10, 2 * n);
thrust::host_vector<int> h_input(n, 1);
thrust::device_vector<int> d_input(n, 1);
thrust::host_vector<unsigned int> h_map = unittest::random_integers<unsigned int>(n);
for(size_t i = 0; i < n; i++)
{
h_map[i] = h_map[i] % output_size;
}
thrust::device_vector<unsigned int> d_map = h_map;
thrust::host_vector<int> h_output(output_size, 0);
thrust::device_vector<int> d_output(output_size, 0);
thrust::scatter_if(h_input.begin(), h_input.end(), h_map.begin(), h_map.begin(), h_output.begin(), is_even_scatter_if<unsigned int>());
scatter_if_kernel<<<1,1>>>(exec, d_input.begin(), d_input.end(), d_map.begin(), d_map.begin(), d_output.begin(), is_even_scatter_if<unsigned int>());
cudaError_t const err = cudaDeviceSynchronize();
ASSERT_EQUAL(cudaSuccess, err);
ASSERT_EQUAL(h_output, d_output);
}
void TestScatterIfDeviceSeq()
{
TestScatterIfDevice(thrust::seq);
}
DECLARE_UNITTEST(TestScatterIfDeviceSeq);
void TestScatterIfDeviceDevice()
{
TestScatterIfDevice(thrust::device);
}
DECLARE_UNITTEST(TestScatterIfDeviceDevice);
void TestScatterCudaStreams()
{
typedef thrust::device_vector<int> Vector;
Vector map(5); // scatter indices
Vector src(5); // source vector
Vector dst(8); // destination vector
map[0] = 6; map[1] = 3; map[2] = 1; map[3] = 7; map[4] = 2;
src[0] = 0; src[1] = 1; src[2] = 2; src[3] = 3; src[4] = 4;
dst[0] = 0; dst[1] = 0; dst[2] = 0; dst[3] = 0; dst[4] = 0; dst[5] = 0; dst[6] = 0; dst[7] = 0;
cudaStream_t s;
cudaStreamCreate(&s);
thrust::scatter(thrust::cuda::par.on(s), src.begin(), src.end(), map.begin(), dst.begin());
cudaStreamSynchronize(s);
ASSERT_EQUAL(dst[0], 0);
ASSERT_EQUAL(dst[1], 2);
ASSERT_EQUAL(dst[2], 4);
ASSERT_EQUAL(dst[3], 1);
ASSERT_EQUAL(dst[4], 0);
ASSERT_EQUAL(dst[5], 0);
ASSERT_EQUAL(dst[6], 0);
ASSERT_EQUAL(dst[7], 3);
cudaStreamDestroy(s);
}
DECLARE_UNITTEST(TestScatterCudaStreams);
void TestScatterIfCudaStreams()
{
typedef thrust::device_vector<int> Vector;
Vector flg(5); // predicate array
Vector map(5); // scatter indices
Vector src(5); // source vector
Vector dst(8); // destination vector
flg[0] = 0; flg[1] = 1; flg[2] = 0; flg[3] = 1; flg[4] = 0;
map[0] = 6; map[1] = 3; map[2] = 1; map[3] = 7; map[4] = 2;
src[0] = 0; src[1] = 1; src[2] = 2; src[3] = 3; src[4] = 4;
dst[0] = 0; dst[1] = 0; dst[2] = 0; dst[3] = 0; dst[4] = 0; dst[5] = 0; dst[6] = 0; dst[7] = 0;
cudaStream_t s;
cudaStreamCreate(&s);
thrust::scatter_if(thrust::cuda::par.on(s), src.begin(), src.end(), map.begin(), flg.begin(), dst.begin());
cudaStreamSynchronize(s);
ASSERT_EQUAL(dst[0], 0);
ASSERT_EQUAL(dst[1], 0);
ASSERT_EQUAL(dst[2], 0);
ASSERT_EQUAL(dst[3], 1);
ASSERT_EQUAL(dst[4], 0);
ASSERT_EQUAL(dst[5], 0);
ASSERT_EQUAL(dst[6], 0);
ASSERT_EQUAL(dst[7], 3);
cudaStreamDestroy(s);
}
DECLARE_UNITTEST(TestScatterIfCudaStreams);
|