File size: 3,460 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
#include <unittest/unittest.h>
#include <thrust/generate.h>
#include <thrust/execution_policy.h>


template<typename ExecutionPolicy, typename Iterator, typename Function>
__global__
void generate_kernel(ExecutionPolicy exec, Iterator first, Iterator last, Function f)
{
  thrust::generate(exec, first, last, f);
}


template<typename T>
struct return_value
{
  T val;
  
  return_value(void){}
  return_value(T v):val(v){}
  
  __host__ __device__
  T operator()(void){ return val; }
};


template<typename T, typename ExecutionPolicy>
void TestGenerateDevice(ExecutionPolicy exec, const size_t n)
{
  thrust::host_vector<T> h_result(n);
  thrust::device_vector<T> d_result(n);
  
  T value = 13;
  return_value<T> f(value);
  
  thrust::generate(h_result.begin(), h_result.end(), f);

  generate_kernel<<<1,1>>>(exec, d_result.begin(), d_result.end(), f);
  {
    cudaError_t const err = cudaDeviceSynchronize();
    ASSERT_EQUAL(cudaSuccess, err);
  }
  
  ASSERT_EQUAL(h_result, d_result);
}


template<typename T>
void TestGenerateDeviceSeq(const size_t n)
{
  TestGenerateDevice<T>(thrust::seq, n);
}
DECLARE_VARIABLE_UNITTEST(TestGenerateDeviceSeq);


template<typename T>
void TestGenerateDeviceDevice(const size_t n)
{
  TestGenerateDevice<T>(thrust::device, n);
}
DECLARE_VARIABLE_UNITTEST(TestGenerateDeviceDevice);


void TestGenerateCudaStreams()
{
  thrust::device_vector<int> result(5);
  
  int value = 13;
  
  return_value<int> f(value);

  cudaStream_t s;
  cudaStreamCreate(&s);
  
  thrust::generate(thrust::cuda::par.on(s), result.begin(), result.end(), f);
  cudaStreamSynchronize(s);
  
  ASSERT_EQUAL(result[0], value);
  ASSERT_EQUAL(result[1], value);
  ASSERT_EQUAL(result[2], value);
  ASSERT_EQUAL(result[3], value);
  ASSERT_EQUAL(result[4], value);

  cudaStreamDestroy(s);
}
DECLARE_UNITTEST(TestGenerateCudaStreams);


template<typename ExecutionPolicy, typename Iterator, typename Size, typename Function>
__global__
void generate_n_kernel(ExecutionPolicy exec, Iterator first, Size n, Function f)
{
  thrust::generate_n(exec, first, n, f);
}


template<typename T, typename ExecutionPolicy>
void TestGenerateNDevice(ExecutionPolicy exec, const size_t n)
{
  thrust::host_vector<T> h_result(n);
  thrust::device_vector<T> d_result(n);
  
  T value = 13;
  return_value<T> f(value);
  
  thrust::generate_n(h_result.begin(), h_result.size(), f);

  generate_n_kernel<<<1,1>>>(exec, d_result.begin(), d_result.size(), f);
  {
    cudaError_t const err = cudaDeviceSynchronize();
    ASSERT_EQUAL(cudaSuccess, err);
  }
  
  ASSERT_EQUAL(h_result, d_result);
}


template<typename T>
void TestGenerateNDeviceSeq(const size_t n)
{
  TestGenerateNDevice<T>(thrust::seq, n);
}
DECLARE_VARIABLE_UNITTEST(TestGenerateNDeviceSeq);


template<typename T>
void TestGenerateNDeviceDevice(const size_t n)
{
  TestGenerateNDevice<T>(thrust::device, n);
}
DECLARE_VARIABLE_UNITTEST(TestGenerateNDeviceDevice);


void TestGenerateNCudaStreams()
{
  thrust::device_vector<int> result(5);
  
  int value = 13;
  
  return_value<int> f(value);

  cudaStream_t s;
  cudaStreamCreate(&s);
  
  thrust::generate_n(thrust::cuda::par.on(s), result.begin(), result.size(), f);
  cudaStreamSynchronize(s);
  
  ASSERT_EQUAL(result[0], value);
  ASSERT_EQUAL(result[1], value);
  ASSERT_EQUAL(result[2], value);
  ASSERT_EQUAL(result[3], value);
  ASSERT_EQUAL(result[4], value);

  cudaStreamDestroy(s);
}
DECLARE_UNITTEST(TestGenerateNCudaStreams);