File size: 2,265 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
#include <unittest/unittest.h>
#include <thrust/copy.h>
#include <thrust/execution_policy.h>


template<typename ExecutionPolicy, typename Iterator1, typename Iterator2>
__global__
void copy_kernel(ExecutionPolicy exec, Iterator1 first, Iterator1 last, Iterator2 result)
{
  thrust::copy(exec, first, last, result);
}


template<typename T, typename ExecutionPolicy>
void TestCopyDevice(ExecutionPolicy exec, size_t n)
{
  thrust::host_vector<T>   h_src = unittest::random_integers<T>(n);
  thrust::host_vector<T>   h_dst(n);

  thrust::device_vector<T> d_src = h_src;
  thrust::device_vector<T> d_dst(n);
  
  thrust::copy(h_src.begin(), h_src.end(), h_dst.begin());
  copy_kernel<<<1,1>>>(exec, d_src.begin(), d_src.end(), d_dst.begin());
  {
    cudaError_t const err = cudaDeviceSynchronize();
    ASSERT_EQUAL(cudaSuccess, err);
  }
  
  ASSERT_EQUAL(h_dst, d_dst);
}


template<typename T>
void TestCopyDeviceSeq(size_t n)
{
  TestCopyDevice<T>(thrust::seq, n);
}
DECLARE_VARIABLE_UNITTEST(TestCopyDeviceSeq);


template<typename T>
void TestCopyDeviceDevice(size_t n)
{
  TestCopyDevice<T>(thrust::device, n);
}
DECLARE_VARIABLE_UNITTEST(TestCopyDeviceDevice);


template<typename ExecutionPolicy, typename Iterator1, typename Size, typename Iterator2>
__global__
void copy_n_kernel(ExecutionPolicy exec, Iterator1 first, Size n, Iterator2 result)
{
  thrust::copy_n(exec, first, n, result);
}


template<typename T, typename ExecutionPolicy>
void TestCopyNDevice(ExecutionPolicy exec, size_t n)
{
  thrust::host_vector<T>   h_src = unittest::random_integers<T>(n);
  thrust::host_vector<T>   h_dst(n);

  thrust::device_vector<T> d_src = h_src;
  thrust::device_vector<T> d_dst(n);
  
  thrust::copy_n(h_src.begin(), h_src.size(), h_dst.begin());
  copy_n_kernel<<<1,1>>>(exec, d_src.begin(), d_src.size(), d_dst.begin());
  {
    cudaError_t const err = cudaDeviceSynchronize();
    ASSERT_EQUAL(cudaSuccess, err);
  }
  
  ASSERT_EQUAL(h_dst, d_dst);
}


template<typename T>
void TestCopyNDeviceSeq(size_t n)
{
  TestCopyNDevice<T>(thrust::seq, n);
}
DECLARE_VARIABLE_UNITTEST(TestCopyNDeviceSeq);


template<typename T>
void TestCopyNDeviceDevice(size_t n)
{
  TestCopyNDevice<T>(thrust::device, n);
}
DECLARE_VARIABLE_UNITTEST(TestCopyNDeviceDevice);