File size: 7,823 Bytes
8ae5fc5 | 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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 | #include <unittest/unittest.h>
#include <thrust/copy.h>
#include <thrust/sequence.h>
#include <thrust/execution_policy.h>
template<typename T>
struct is_even
{
__host__ __device__
bool operator()(T x) { return (static_cast<unsigned int>(x) & 1) == 0; }
};
template<typename T>
struct mod_3
{
__host__ __device__
unsigned int operator()(T x) { return static_cast<unsigned int>(x) % 3; }
};
#ifdef THRUST_TEST_DEVICE_SIDE
template<typename ExecutionPolicy, typename Iterator1, typename Iterator2, typename Predicate, typename Iterator3>
__global__ void copy_if_kernel(ExecutionPolicy exec, Iterator1 first, Iterator1 last, Iterator2 result1, Predicate pred, Iterator3 result2)
{
*result2 = thrust::copy_if(exec, first, last, result1, pred);
}
template<typename ExecutionPolicy>
void TestCopyIfDevice(ExecutionPolicy exec)
{
size_t n = 1000;
thrust::host_vector<int> h_data = unittest::random_integers<int>(n);
thrust::device_vector<int> d_data = h_data;
typename thrust::host_vector<int>::iterator h_new_end;
typename thrust::device_vector<int>::iterator d_new_end;
thrust::device_vector<
typename thrust::device_vector<int>::iterator
> d_new_end_vec(1);
// test with Predicate that returns a bool
{
thrust::host_vector<int> h_result(n);
thrust::device_vector<int> d_result(n);
h_new_end = thrust::copy_if(h_data.begin(), h_data.end(), h_result.begin(), is_even<int>());
copy_if_kernel<<<1,1>>>(exec, d_data.begin(), d_data.end(), d_result.begin(), is_even<int>(), d_new_end_vec.begin());
cudaError_t const err = cudaDeviceSynchronize();
ASSERT_EQUAL(cudaSuccess, err);
d_new_end = d_new_end_vec[0];
h_result.resize(h_new_end - h_result.begin());
d_result.resize(d_new_end - d_result.begin());
ASSERT_EQUAL(h_result, d_result);
}
// test with Predicate that returns a non-bool
{
thrust::host_vector<int> h_result(n);
thrust::device_vector<int> d_result(n);
h_new_end = thrust::copy_if(h_data.begin(), h_data.end(), h_result.begin(), mod_3<int>());
copy_if_kernel<<<1,1>>>(exec, d_data.begin(), d_data.end(), d_result.begin(), mod_3<int>(), d_new_end_vec.begin());
cudaError_t const err = cudaDeviceSynchronize();
ASSERT_EQUAL(cudaSuccess, err);
d_new_end = d_new_end_vec[0];
h_result.resize(h_new_end - h_result.begin());
d_result.resize(d_new_end - d_result.begin());
ASSERT_EQUAL(h_result, d_result);
}
}
void TestCopyIfDeviceSeq()
{
TestCopyIfDevice(thrust::seq);
}
DECLARE_UNITTEST(TestCopyIfDeviceSeq);
void TestCopyIfDeviceDevice()
{
TestCopyIfDevice(thrust::device);
}
DECLARE_UNITTEST(TestCopyIfDeviceDevice);
void TestCopyIfDeviceNoSync()
{
TestCopyIfDevice(thrust::cuda::par_nosync);
}
DECLARE_UNITTEST(TestCopyIfDeviceNoSync);
#endif
template<typename ExecutionPolicy>
void TestCopyIfCudaStreams(ExecutionPolicy policy)
{
typedef thrust::device_vector<int> Vector;
Vector data(5);
data[0] = 1;
data[1] = 2;
data[2] = 1;
data[3] = 3;
data[4] = 2;
Vector result(5);
cudaStream_t s;
cudaStreamCreate(&s);
Vector::iterator end = thrust::copy_if(policy.on(s),
data.begin(),
data.end(),
result.begin(),
is_even<int>());
ASSERT_EQUAL(end - result.begin(), 2);
ASSERT_EQUAL(result[0], 2);
ASSERT_EQUAL(result[1], 2);
cudaStreamDestroy(s);
}
void TestCopyIfCudaStreamsSync(){
TestCopyIfCudaStreams(thrust::cuda::par);
}
DECLARE_UNITTEST(TestCopyIfCudaStreamsSync);
void TestCopyIfCudaStreamsNoSync(){
TestCopyIfCudaStreams(thrust::cuda::par_nosync);
}
DECLARE_UNITTEST(TestCopyIfCudaStreamsNoSync);
#ifdef THRUST_TEST_DEVICE_SIDE
template<typename ExecutionPolicy, typename Iterator1, typename Iterator2, typename Iterator3, typename Predicate, typename Iterator4>
__global__ void copy_if_kernel(ExecutionPolicy exec, Iterator1 first, Iterator1 last, Iterator2 stencil_first, Iterator3 result1, Predicate pred, Iterator4 result2)
{
*result2 = thrust::copy_if(exec, first, last, stencil_first, result1, pred);
}
template<typename ExecutionPolicy>
void TestCopyIfStencilDevice(ExecutionPolicy exec)
{
size_t n = 1000;
thrust::host_vector<int> h_data(n); thrust::sequence(h_data.begin(), h_data.end());
thrust::device_vector<int> d_data(n); thrust::sequence(d_data.begin(), d_data.end());
thrust::host_vector<int> h_stencil = unittest::random_integers<int>(n);
thrust::device_vector<int> d_stencil = unittest::random_integers<int>(n);
typename thrust::host_vector<int>::iterator h_new_end;
typename thrust::device_vector<int>::iterator d_new_end;
thrust::device_vector<
typename thrust::device_vector<int>::iterator
> d_new_end_vec(1);
// test with Predicate that returns a bool
{
thrust::host_vector<int> h_result(n);
thrust::device_vector<int> d_result(n);
h_new_end = thrust::copy_if(h_data.begin(), h_data.end(), h_result.begin(), is_even<int>());
copy_if_kernel<<<1,1>>>(exec, d_data.begin(), d_data.end(), d_result.begin(), is_even<int>(), d_new_end_vec.begin());
cudaError_t const err = cudaDeviceSynchronize();
ASSERT_EQUAL(cudaSuccess, err);
d_new_end = d_new_end_vec[0];
h_result.resize(h_new_end - h_result.begin());
d_result.resize(d_new_end - d_result.begin());
ASSERT_EQUAL(h_result, d_result);
}
// test with Predicate that returns a non-bool
{
thrust::host_vector<int> h_result(n);
thrust::device_vector<int> d_result(n);
h_new_end = thrust::copy_if(h_data.begin(), h_data.end(), h_result.begin(), mod_3<int>());
copy_if_kernel<<<1,1>>>(exec, d_data.begin(), d_data.end(), d_result.begin(), mod_3<int>(), d_new_end_vec.begin());
cudaError_t const err = cudaDeviceSynchronize();
ASSERT_EQUAL(cudaSuccess, err);
d_new_end = d_new_end_vec[0];
h_result.resize(h_new_end - h_result.begin());
d_result.resize(d_new_end - d_result.begin());
ASSERT_EQUAL(h_result, d_result);
}
}
void TestCopyIfStencilDeviceSeq()
{
TestCopyIfStencilDevice(thrust::seq);
}
DECLARE_UNITTEST(TestCopyIfStencilDeviceSeq);
void TestCopyIfStencilDeviceDevice()
{
TestCopyIfStencilDevice(thrust::device);
}
DECLARE_UNITTEST(TestCopyIfStencilDeviceDevice);
void TestCopyIfStencilDeviceNoSync()
{
TestCopyIfStencilDevice(thrust::cuda::par_nosync);
}
DECLARE_UNITTEST(TestCopyIfStencilDeviceNoSync);
#endif
template<typename ExecutionPolicy>
void TestCopyIfStencilCudaStreams(ExecutionPolicy policy)
{
typedef thrust::device_vector<int> Vector;
typedef Vector::value_type T;
Vector data(5);
data[0] = 1;
data[1] = 2;
data[2] = 1;
data[3] = 3;
data[4] = 2;
Vector result(5);
Vector stencil(5);
stencil[0] = 0;
stencil[1] = 1;
stencil[2] = 0;
stencil[3] = 0;
stencil[4] = 1;
cudaStream_t s;
cudaStreamCreate(&s);
Vector::iterator end = thrust::copy_if(policy.on(s),
data.begin(),
data.end(),
stencil.begin(),
result.begin(),
thrust::identity<T>());
ASSERT_EQUAL(end - result.begin(), 2);
ASSERT_EQUAL(result[0], 2);
ASSERT_EQUAL(result[1], 2);
cudaStreamDestroy(s);
}
void TestCopyIfStencilCudaStreamsSync()
{
TestCopyIfStencilCudaStreams(thrust::cuda::par);
}
DECLARE_UNITTEST(TestCopyIfStencilCudaStreamsSync);
void TestCopyIfStencilCudaStreamsNoSync()
{
TestCopyIfStencilCudaStreams(thrust::cuda::par_nosync);
}
DECLARE_UNITTEST(TestCopyIfStencilCudaStreamsNoSync);
|