File size: 11,149 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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#include <unittest/unittest.h>
#include <thrust/host_vector.h>
#include <thrust/complex.h>
#include <thrust/transform.h>
#include <iostream>

#if THRUST_DEVICE_SYSTEM == THRUST_DEVICE_SYSTEM_CUDA
#include <unittest/cuda/testframework.h>
#endif

struct basic_arithmetic_functor
{
  template<typename T>
  __host__ __device__
  thrust::complex<T> operator()(const thrust::complex<T> &x,
				const thrust::complex<T> &y)
  {
    // exercise unary and binary arithmetic operators
    // Should return approximately 1
    return (+x + +y) + (x * y) / (y * x) + (-y + -x);
  } // end operator()()
}; // end make_pair_functor

struct complex_plane_functor
{
  template<typename T>
  __host__ __device__
  thrust::complex<T> operator()(const thrust::complex<T> &x)
  {
    // Should return a proximately 1
    return thrust::proj( (thrust::polar(abs(x),arg(x)) * conj(x))/norm(x));
  } // end operator()()
}; // end make_pair_functor

struct pow_functor
{
  template<typename T>
  __host__ __device__
  thrust::complex<T> operator()(const thrust::complex<T> &x,
				const thrust::complex<T> &y)
  {
    // exercise power functions
    return pow(x,y);
  } // end operator()()
}; // end make_pair_functor

struct sqrt_functor
{
  template<typename T>
  __host__ __device__
  thrust::complex<T> operator()(const thrust::complex<T> &x)
  {
    // exercise power functions
    return sqrt(x);
  } // end operator()()
}; // end make_pair_functor

struct log_functor
{
  template<typename T>
  __host__ __device__
  thrust::complex<T> operator()(const thrust::complex<T> &x)
  {
    return log(x);
  } // end operator()()
}; // end make_pair_functor

struct exp_functor
{
  template<typename T>
  __host__ __device__
  thrust::complex<T> operator()(const thrust::complex<T> &x)
  {
    return exp(x);
  } // end operator()()
}; // end make_pair_functor

struct log10_functor
{
  template<typename T>
  __host__ __device__
  thrust::complex<T> operator()(const thrust::complex<T> &x)
  {
    return log10(x);
  } // end operator()()
}; // end make_pair_functor


struct cos_functor
{
  template<typename T>
  __host__ __device__
  thrust::complex<T> operator()(const thrust::complex<T> &x)
  {
    return cos(x);
  } 
}; 

struct sin_functor
{
  template<typename T>
  __host__ __device__
  thrust::complex<T> operator()(const thrust::complex<T> &x)
  {
    return sin(x);
  } 
}; 

struct tan_functor
{
  template<typename T>
  __host__ __device__
  thrust::complex<T> operator()(const thrust::complex<T> &x)
  {
    return tan(x);
  } 
}; 



struct cosh_functor
{
  template<typename T>
  __host__ __device__
  thrust::complex<T> operator()(const thrust::complex<T> &x)
  {
    return cosh(x);
  } 
}; 

struct sinh_functor
{
  template<typename T>
  __host__ __device__
  thrust::complex<T> operator()(const thrust::complex<T> &x)
  {
    return sinh(x);
  } 
}; 

struct tanh_functor
{
  template<typename T>
  __host__ __device__
  thrust::complex<T> operator()(const thrust::complex<T> &x)
  {
    return tanh(x);
  } 
}; 


struct acos_functor
{
  template<typename T>
  __host__ __device__
  thrust::complex<T> operator()(const thrust::complex<T> &x)
  {
    return acos(x);
  } 
}; 

struct asin_functor
{
  template<typename T>
  __host__ __device__
  thrust::complex<T> operator()(const thrust::complex<T> &x)
  {
    return asin(x);
  } 
}; 

struct atan_functor
{
  template<typename T>
  __host__ __device__
  thrust::complex<T> operator()(const thrust::complex<T> &x)
  {
    return atan(x);
  } 
}; 


struct acosh_functor
{
  template<typename T>
  __host__ __device__
  thrust::complex<T> operator()(const thrust::complex<T> &x)
  {
    return acosh(x);
  } 
}; 

struct asinh_functor
{
  template<typename T>
  __host__ __device__
  thrust::complex<T> operator()(const thrust::complex<T> &x)
  {
    return asinh(x);
  } 
}; 

struct atanh_functor
{
  template<typename T>
  __host__ __device__
  thrust::complex<T> operator()(const thrust::complex<T> &x)
  {
    return atanh(x);
  } 
}; 


template <typename T>
thrust::host_vector<thrust::complex<T> > random_complex_samples(size_t n){
  thrust::host_vector<T> real = unittest::random_samples<T>(2*n);
  thrust::host_vector<thrust::complex<T> > h_p1(n);
  for(size_t i = 0; i<n; i++){
    h_p1[i].real(real[i]);
    h_p1[i].imag(real[2*i]);
  }
  return h_p1;
}

template <typename T>
struct TestComplexArithmeticTransform
{
  void operator()(const size_t n)
  {
    typedef thrust::complex<T> type;
    thrust::host_vector<type> h_p1 = random_complex_samples<T>(n);
    thrust::host_vector<type> h_p2 = random_complex_samples<T>(n);
    thrust::host_vector<type>   h_result(n);

    thrust::device_vector<type> d_p1 = h_p1;
    thrust::device_vector<type> d_p2 = h_p2;
    thrust::device_vector<type> d_result(n);

    thrust::transform(h_p1.begin(), h_p1.end(), h_p2.begin(), h_result.begin(), basic_arithmetic_functor());
    thrust::transform(d_p1.begin(), d_p1.end(), d_p2.begin(), d_result.begin(), basic_arithmetic_functor());    
    ASSERT_ALMOST_EQUAL(h_result, d_result);
  }
};
VariableUnitTest<TestComplexArithmeticTransform, FloatingPointTypes> TestComplexArithmeticTransformInstance;

template <typename T>
struct TestComplexPlaneTransform
{
  void operator()(const size_t n)
  {
    typedef thrust::complex<T> type;
    thrust::host_vector<type> h_p1 = random_complex_samples<T>(n);
    thrust::host_vector<type>   h_result(n);

    thrust::device_vector<type> d_p1 = h_p1;
    thrust::device_vector<type> d_result(n);

    thrust::transform(h_p1.begin(), h_p1.end(), h_result.begin(), complex_plane_functor());
    thrust::transform(d_p1.begin(), d_p1.end(), d_result.begin(), complex_plane_functor());    
    ASSERT_ALMOST_EQUAL(h_result, d_result);
  }
};
VariableUnitTest<TestComplexPlaneTransform, FloatingPointTypes> TestComplexPlaneTransformInstance;


template <typename T>
struct TestComplexPowerTransform
{
  void operator()(const size_t n)
  {
    typedef thrust::complex<T> type;
    thrust::host_vector<type> h_p1 = random_complex_samples<T>(n);
    thrust::host_vector<type> h_p2 = random_complex_samples<T>(n);
    thrust::host_vector<type>   h_result(n);

    thrust::device_vector<type> d_p1 = h_p1;
    thrust::device_vector<type> d_p2 = h_p2;
    thrust::device_vector<type> d_result(n);

    thrust::transform(h_p1.begin(), h_p1.end(), h_p2.begin(), h_result.begin(), pow_functor());
    thrust::transform(d_p1.begin(), d_p1.end(), d_p2.begin(), d_result.begin(), pow_functor());    
    // pow can be very innacurate there's no point trying to check for equality
    // Currently just checking for compilation
    //    ASSERT_ALMOST_EQUAL(h_result, d_result);

    thrust::transform(h_p1.begin(), h_p1.end(), h_result.begin(), sqrt_functor());
    thrust::transform(d_p1.begin(), d_p1.end(), d_result.begin(), sqrt_functor());    
    ASSERT_ALMOST_EQUAL(h_result, d_result);
  }
};
VariableUnitTest<TestComplexPowerTransform, FloatingPointTypes> TestComplexPowerTransformInstance;

template <typename T>
struct TestComplexExponentialTransform
{
  void operator()(const size_t n)
  {
    typedef thrust::complex<T> type;
    thrust::host_vector<type> h_p1 = random_complex_samples<T>(n);
    thrust::host_vector<type>   h_result(n);

    thrust::device_vector<type> d_p1 = h_p1;
    thrust::device_vector<type> d_result(n);

    thrust::transform(h_p1.begin(), h_p1.end(), h_result.begin(), exp_functor());
    thrust::transform(d_p1.begin(), d_p1.end(), d_result.begin(), exp_functor());    
    ASSERT_ALMOST_EQUAL(h_result, d_result);

    thrust::transform(h_p1.begin(), h_p1.end(), h_result.begin(), log_functor());
    thrust::transform(d_p1.begin(), d_p1.end(), d_result.begin(), log_functor());    
    ASSERT_ALMOST_EQUAL(h_result, d_result);

    thrust::transform(h_p1.begin(), h_p1.end(), h_result.begin(), log10_functor());
    thrust::transform(d_p1.begin(), d_p1.end(), d_result.begin(), log10_functor());    
    ASSERT_ALMOST_EQUAL(h_result, d_result);
  }
};
VariableUnitTest<TestComplexExponentialTransform, FloatingPointTypes> TestComplexExponentialTransformInstance;

template <typename T>
struct TestComplexTrigonometricTransform
{
  void operator()(const size_t n)
  {
    typedef thrust::complex<T> type;
    thrust::host_vector<type> h_p1 = random_complex_samples<T>(n);
    thrust::host_vector<type>   h_result(n);

    thrust::device_vector<type> d_p1 = h_p1;
    thrust::device_vector<type> d_result(n);


    thrust::transform(h_p1.begin(), h_p1.end(), h_result.begin(), sin_functor());
    thrust::transform(d_p1.begin(), d_p1.end(), d_result.begin(), sin_functor());    
    ASSERT_ALMOST_EQUAL(h_result, d_result);

    thrust::transform(h_p1.begin(), h_p1.end(), h_result.begin(), cos_functor());
    thrust::transform(d_p1.begin(), d_p1.end(), d_result.begin(), cos_functor());    
    ASSERT_ALMOST_EQUAL(h_result, d_result);

    thrust::transform(h_p1.begin(), h_p1.end(), h_result.begin(), tan_functor());
    thrust::transform(d_p1.begin(), d_p1.end(), d_result.begin(), tan_functor());    
    ASSERT_ALMOST_EQUAL(h_result, d_result);


    thrust::transform(h_p1.begin(), h_p1.end(), h_result.begin(), sinh_functor());
    thrust::transform(d_p1.begin(), d_p1.end(), d_result.begin(), sinh_functor());    
    ASSERT_ALMOST_EQUAL(h_result, d_result);

    thrust::transform(h_p1.begin(), h_p1.end(), h_result.begin(), cosh_functor());
    thrust::transform(d_p1.begin(), d_p1.end(), d_result.begin(), cosh_functor());    
    ASSERT_ALMOST_EQUAL(h_result, d_result);

    thrust::transform(h_p1.begin(), h_p1.end(), h_result.begin(), tanh_functor());
    thrust::transform(d_p1.begin(), d_p1.end(), d_result.begin(), tanh_functor());    
    ASSERT_ALMOST_EQUAL(h_result, d_result);


    thrust::transform(h_p1.begin(), h_p1.end(), h_result.begin(), asin_functor());
    thrust::transform(d_p1.begin(), d_p1.end(), d_result.begin(), asin_functor());    
    ASSERT_ALMOST_EQUAL(h_result, d_result);

    thrust::transform(h_p1.begin(), h_p1.end(), h_result.begin(), acos_functor());
    thrust::transform(d_p1.begin(), d_p1.end(), d_result.begin(), acos_functor());    
    ASSERT_ALMOST_EQUAL(h_result, d_result);

    thrust::transform(h_p1.begin(), h_p1.end(), h_result.begin(), atan_functor());
    thrust::transform(d_p1.begin(), d_p1.end(), d_result.begin(), atan_functor());    
    ASSERT_ALMOST_EQUAL(h_result, d_result);


    thrust::transform(h_p1.begin(), h_p1.end(), h_result.begin(), asinh_functor());
    thrust::transform(d_p1.begin(), d_p1.end(), d_result.begin(), asinh_functor());    
    ASSERT_ALMOST_EQUAL(h_result, d_result);

    thrust::transform(h_p1.begin(), h_p1.end(), h_result.begin(), acosh_functor());
    thrust::transform(d_p1.begin(), d_p1.end(), d_result.begin(), acosh_functor());    
    ASSERT_ALMOST_EQUAL(h_result, d_result);

    thrust::transform(h_p1.begin(), h_p1.end(), h_result.begin(), atanh_functor());
    thrust::transform(d_p1.begin(), d_p1.end(), d_result.begin(), atanh_functor());    
    ASSERT_ALMOST_EQUAL(h_result, d_result);

  }
};
VariableUnitTest<TestComplexTrigonometricTransform, FloatingPointTypes> TestComplexTrigonometricTransformInstance;