Spaces:
Runtime error
Runtime error
File size: 8,486 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 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 288 289 290 291 292 293 294 295 296 |
#include <unittest/unittest.h>
#include <thrust/replace.h>
#include <thrust/execution_policy.h>
template <typename T>
struct less_than_five
{
__host__ __device__ bool operator()(const T &val) const {return val < 5;}
};
template<typename ExecutionPolicy, typename Iterator, typename T1, typename T2>
__global__
void replace_kernel(ExecutionPolicy exec, Iterator first, Iterator last, T1 old_value, T2 new_value)
{
thrust::replace(exec, first, last, old_value, new_value);
}
template<typename T, typename ExecutionPolicy>
void TestReplaceDevice(ExecutionPolicy exec, const size_t n)
{
thrust::host_vector<T> h_data = unittest::random_samples<T>(n);
thrust::device_vector<T> d_data = h_data;
T old_value = 0;
T new_value = 1;
thrust::replace(h_data.begin(), h_data.end(), old_value, new_value);
replace_kernel<<<1,1>>>(exec, d_data.begin(), d_data.end(), old_value, new_value);
cudaError_t const err = cudaDeviceSynchronize();
ASSERT_EQUAL(cudaSuccess, err);
ASSERT_ALMOST_EQUAL(h_data, d_data);
}
template<typename T>
void TestReplaceDeviceSeq(const size_t n)
{
TestReplaceDevice<T>(thrust::seq, n);
}
DECLARE_VARIABLE_UNITTEST(TestReplaceDeviceSeq);
template<typename T>
void TestReplaceDeviceDevice(const size_t n)
{
TestReplaceDevice<T>(thrust::device, n);
}
DECLARE_VARIABLE_UNITTEST(TestReplaceDeviceDevice);
template<typename ExecutionPolicy, typename Iterator1, typename Iterator2, typename T1, typename T2>
__global__
void replace_copy_kernel(ExecutionPolicy exec, Iterator1 first, Iterator1 last, Iterator2 result, T1 old_value, T2 new_value)
{
thrust::replace_copy(exec, first, last, result, old_value, new_value);
}
template<typename ExecutionPolicy>
void TestReplaceCopyDevice(ExecutionPolicy exec)
{
size_t n = 1000;
thrust::host_vector<int> h_data = unittest::random_samples<int>(n);
thrust::device_vector<int> d_data = h_data;
int old_value = 0;
int new_value = 1;
thrust::host_vector<int> h_dest(n);
thrust::device_vector<int> d_dest(n);
thrust::replace_copy(h_data.begin(), h_data.end(), h_dest.begin(), old_value, new_value);
replace_copy_kernel<<<1,1>>>(exec, d_data.begin(), d_data.end(), d_dest.begin(), old_value, new_value);
cudaError_t const err = cudaDeviceSynchronize();
ASSERT_EQUAL(cudaSuccess, err);
ASSERT_ALMOST_EQUAL(h_data, d_data);
ASSERT_ALMOST_EQUAL(h_dest, d_dest);
}
void TestReplaceCopyDeviceSeq()
{
TestReplaceCopyDevice(thrust::seq);
}
DECLARE_UNITTEST(TestReplaceCopyDeviceSeq);
void TestReplaceCopyDeviceDevice()
{
TestReplaceCopyDevice(thrust::device);
}
DECLARE_UNITTEST(TestReplaceCopyDeviceDevice);
template<typename ExecutionPolicy, typename Iterator, typename Predicate, typename T>
__global__
void replace_if_kernel(ExecutionPolicy exec, Iterator first, Iterator last, Predicate pred, T new_value)
{
thrust::replace_if(exec, first, last, pred, new_value);
}
template<typename ExecutionPolicy>
void TestReplaceIfDevice(ExecutionPolicy exec)
{
size_t n = 1000;
thrust::host_vector<int> h_data = unittest::random_samples<int>(n);
thrust::device_vector<int> d_data = h_data;
thrust::replace_if(h_data.begin(), h_data.end(), less_than_five<int>(), 0);
replace_if_kernel<<<1,1>>>(exec, d_data.begin(), d_data.end(), less_than_five<int>(), 0);
cudaError_t const err = cudaDeviceSynchronize();
ASSERT_EQUAL(cudaSuccess, err);
ASSERT_ALMOST_EQUAL(h_data, d_data);
}
void TestReplaceIfDeviceSeq()
{
TestReplaceIfDevice(thrust::seq);
}
DECLARE_UNITTEST(TestReplaceIfDeviceSeq);
void TestReplaceIfDeviceDevice()
{
TestReplaceIfDevice(thrust::device);
}
DECLARE_UNITTEST(TestReplaceIfDeviceDevice);
template<typename ExecutionPolicy, typename Iterator1, typename Iterator2, typename Predicate, typename T>
__global__
void replace_if_kernel(ExecutionPolicy exec, Iterator1 first, Iterator1 last, Iterator2 stencil_first, Predicate pred, T new_value)
{
thrust::replace_if(exec, first, last, stencil_first, pred, new_value);
}
template<typename ExecutionPolicy>
void TestReplaceIfStencilDevice(ExecutionPolicy exec)
{
size_t n = 1000;
thrust::host_vector<int> h_data = unittest::random_samples<int>(n);
thrust::device_vector<int> d_data = h_data;
thrust::host_vector<int> h_stencil = unittest::random_samples<int>(n);
thrust::device_vector<int> d_stencil = h_stencil;
thrust::replace_if(h_data.begin(), h_data.end(), h_stencil.begin(), less_than_five<int>(), 0);
replace_if_kernel<<<1,1>>>(exec, d_data.begin(), d_data.end(), d_stencil.begin(), less_than_five<int>(), 0);
cudaError_t const err = cudaDeviceSynchronize();
ASSERT_EQUAL(cudaSuccess, err);
ASSERT_ALMOST_EQUAL(h_data, d_data);
}
void TestReplaceIfStencilDeviceSeq()
{
TestReplaceIfStencilDevice(thrust::seq);
}
DECLARE_UNITTEST(TestReplaceIfStencilDeviceSeq);
void TestReplaceIfStencilDeviceDevice()
{
TestReplaceIfStencilDevice(thrust::device);
}
DECLARE_UNITTEST(TestReplaceIfStencilDeviceDevice);
template<typename ExecutionPolicy, typename Iterator1, typename Iterator2, typename Predicate, typename T>
__global__
void replace_copy_if_kernel(ExecutionPolicy exec, Iterator1 first, Iterator1 last, Iterator2 result, Predicate pred, T new_value)
{
thrust::replace_copy_if(exec, first, last, result, pred, new_value);
}
template<typename ExecutionPolicy>
void TestReplaceCopyIfDevice(ExecutionPolicy exec)
{
size_t n = 1000;
thrust::host_vector<int> h_data = unittest::random_samples<int>(n);
thrust::device_vector<int> d_data = h_data;
thrust::host_vector<int> h_dest(n);
thrust::device_vector<int> d_dest(n);
thrust::replace_copy_if(h_data.begin(), h_data.end(), h_dest.begin(), less_than_five<int>(), 0);
replace_copy_if_kernel<<<1,1>>>(exec, d_data.begin(), d_data.end(), d_dest.begin(), less_than_five<int>(), 0);
cudaError_t const err = cudaDeviceSynchronize();
ASSERT_EQUAL(cudaSuccess, err);
ASSERT_ALMOST_EQUAL(h_data, d_data);
ASSERT_ALMOST_EQUAL(h_dest, d_dest);
}
void TestReplaceCopyIfDeviceSeq()
{
TestReplaceCopyIfDevice(thrust::seq);
}
DECLARE_UNITTEST(TestReplaceCopyIfDeviceSeq);
void TestReplaceCopyIfDeviceDevice()
{
TestReplaceCopyIfDevice(thrust::device);
}
DECLARE_UNITTEST(TestReplaceCopyIfDeviceDevice);
template<typename ExecutionPolicy, typename Iterator1, typename Iterator2, typename Iterator3, typename Predicate, typename T>
__global__
void replace_copy_if_kernel(ExecutionPolicy exec, Iterator1 first, Iterator1 last, Iterator2 stencil_first, Iterator3 result, Predicate pred, T new_value)
{
thrust::replace_copy_if(exec, first, last, stencil_first, result, pred, new_value);
}
template<typename ExecutionPolicy>
void TestReplaceCopyIfStencilDevice(ExecutionPolicy exec)
{
size_t n = 1000;
thrust::host_vector<int> h_data = unittest::random_samples<int>(n);
thrust::device_vector<int> d_data = h_data;
thrust::host_vector<int> h_stencil = unittest::random_samples<int>(n);
thrust::device_vector<int> d_stencil = h_stencil;
thrust::host_vector<int> h_dest(n);
thrust::device_vector<int> d_dest(n);
thrust::replace_copy_if(h_data.begin(), h_data.end(), h_stencil.begin(), h_dest.begin(), less_than_five<int>(), 0);
replace_copy_if_kernel<<<1,1>>>(exec, d_data.begin(), d_data.end(), d_stencil.begin(), d_dest.begin(), less_than_five<int>(), 0);
cudaError_t const err = cudaDeviceSynchronize();
ASSERT_EQUAL(cudaSuccess, err);
ASSERT_ALMOST_EQUAL(h_data, d_data);
ASSERT_ALMOST_EQUAL(h_dest, d_dest);
}
void TestReplaceCopyIfStencilDeviceSeq()
{
TestReplaceCopyIfStencilDevice(thrust::seq);
}
DECLARE_UNITTEST(TestReplaceCopyIfStencilDeviceSeq);
void TestReplaceCopyIfStencilDeviceDevice()
{
TestReplaceCopyIfStencilDevice(thrust::device);
}
DECLARE_UNITTEST(TestReplaceCopyIfStencilDeviceDevice);
void TestReplaceCudaStreams()
{
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;
cudaStream_t s;
cudaStreamCreate(&s);
thrust::replace(thrust::cuda::par.on(s), data.begin(), data.end(), (T) 1, (T) 4);
thrust::replace(thrust::cuda::par.on(s), data.begin(), data.end(), (T) 2, (T) 5);
cudaStreamSynchronize(s);
Vector result(5);
result[0] = 4;
result[1] = 5;
result[2] = 4;
result[3] = 3;
result[4] = 5;
ASSERT_EQUAL(data, result);
cudaStreamDestroy(s);
}
DECLARE_UNITTEST(TestReplaceCudaStreams);
|