/[escript]/branches/diaplayground/cusplibrary/testing/blas.cu
ViewVC logotype

Contents of /branches/diaplayground/cusplibrary/testing/blas.cu

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4955 - (show annotations)
Tue May 20 04:33:15 2014 UTC (5 years, 4 months ago) by caltinay
File size: 10542 byte(s)
added pristine copy of cusplibrary (apache license) to be used by ripley.

1 #include <unittest/unittest.h>
2
3 #include <cusp/blas.h>
4
5
6 template <class MemorySpace>
7 void TestAxpy(void)
8 {
9 typedef typename cusp::array1d<float, MemorySpace> Array;
10 typedef typename cusp::array1d<float, MemorySpace>::view View;
11
12 Array x(4);
13 Array y(4);
14
15
16 x[0] = 7.0f; y[0] = 0.0f;
17 x[1] = 5.0f; y[1] = -2.0f;
18 x[2] = 4.0f; y[2] = 0.0f;
19 x[3] = -3.0f; y[3] = 5.0f;
20
21 cusp::blas::axpy(x, y, 2.0f);
22
23 ASSERT_EQUAL(y[0], 14.0);
24 ASSERT_EQUAL(y[1], 8.0);
25 ASSERT_EQUAL(y[2], 8.0);
26 ASSERT_EQUAL(y[3], -1.0);
27
28 cusp::blas::axpy(View(x), View(y), 2.0f);
29
30 ASSERT_EQUAL(y[0], 28.0);
31 ASSERT_EQUAL(y[1], 18.0);
32 ASSERT_EQUAL(y[2], 16.0);
33 ASSERT_EQUAL(y[3], -7.0);
34
35 // test size checking
36 Array w(3);
37 ASSERT_THROWS(cusp::blas::axpy(x, w, 1.0f), cusp::invalid_input_exception);
38 }
39 DECLARE_HOST_DEVICE_UNITTEST(TestAxpy);
40
41
42 template <class MemorySpace>
43 void TestAxpby(void)
44 {
45 typedef typename cusp::array1d<float, MemorySpace> Array;
46 typedef typename cusp::array1d<float, MemorySpace>::view View;
47
48 Array x(4);
49 Array y(4);
50 Array z(4,0);
51
52 x[0] = 7.0f; y[0] = 0.0f;
53 x[1] = 5.0f; y[1] = -2.0f;
54 x[2] = 4.0f; y[2] = 0.0f;
55 x[3] = -3.0f; y[3] = 5.0f;
56
57
58 cusp::blas::axpby(x, y, z, 2.0f, 1.0f);
59
60 ASSERT_EQUAL(z[0], 14.0);
61 ASSERT_EQUAL(z[1], 8.0);
62 ASSERT_EQUAL(z[2], 8.0);
63 ASSERT_EQUAL(z[3], -1.0);
64
65 z[0] = 0.0f;
66 z[1] = 0.0f;
67 z[2] = 0.0f;
68 z[3] = 0.0f;
69
70 cusp::blas::axpby(View(x), View(y), View(z), 2.0f, 1.0f);
71
72 ASSERT_EQUAL(z[0], 14.0);
73 ASSERT_EQUAL(z[1], 8.0);
74 ASSERT_EQUAL(z[2], 8.0);
75 ASSERT_EQUAL(z[3], -1.0);
76
77 // test size checking
78 Array w(3);
79 ASSERT_THROWS(cusp::blas::axpby(x, y, w, 2.0f, 1.0f), cusp::invalid_input_exception);
80 }
81 DECLARE_HOST_DEVICE_UNITTEST(TestAxpby);
82
83
84 template <class MemorySpace>
85 void TestAxpbypcz(void)
86 {
87 typedef typename cusp::array1d<float, MemorySpace> Array;
88 typedef typename cusp::array1d<float, MemorySpace>::view View;
89
90 Array x(4);
91 Array y(4);
92 Array z(4);
93 Array w(4,0);
94
95 x[0] = 7.0f; y[0] = 0.0f; z[0] = 1.0f;
96 x[1] = 5.0f; y[1] = -2.0f; z[1] = 0.0f;
97 x[2] = 4.0f; y[2] = 0.0f; z[2] = 3.0f;
98 x[3] = -3.0f; y[3] = 5.0f; z[3] = -2.0f;
99
100
101 cusp::blas::axpbypcz(x, y, z, w, 2.0f, 1.0f, 3.0f);
102
103 ASSERT_EQUAL(w[0], 17.0);
104 ASSERT_EQUAL(w[1], 8.0);
105 ASSERT_EQUAL(w[2], 17.0);
106 ASSERT_EQUAL(w[3], -7.0);
107
108 w[0] = 0.0f;
109 w[1] = 0.0f;
110 w[2] = 0.0f;
111 w[3] = 0.0f;
112
113 cusp::blas::axpbypcz(View(x), View(y), View(z), View(w), 2.0f, 1.0f, 3.0f);
114
115 ASSERT_EQUAL(w[0], 17.0);
116 ASSERT_EQUAL(w[1], 8.0);
117 ASSERT_EQUAL(w[2], 17.0);
118 ASSERT_EQUAL(w[3], -7.0);
119
120 // test size checking
121 Array output(3);
122 ASSERT_THROWS(cusp::blas::axpbypcz(x, y, z, output, 2.0f, 1.0f, 3.0f), cusp::invalid_input_exception);
123 }
124 DECLARE_HOST_DEVICE_UNITTEST(TestAxpbypcz);
125
126
127 template <class MemorySpace>
128 void TestXmy(void)
129 {
130 typedef typename cusp::array1d<float, MemorySpace> Array;
131 typedef typename cusp::array1d<float, MemorySpace>::view View;
132
133 Array x(4);
134 Array y(4);
135 Array z(4,0);
136
137 x[0] = 7.0f; y[0] = 0.0f;
138 x[1] = 5.0f; y[1] = -2.0f;
139 x[2] = 4.0f; y[2] = 0.0f;
140 x[3] = -3.0f; y[3] = 5.0f;
141
142
143 cusp::blas::xmy(x, y, z);
144
145 ASSERT_EQUAL(z[0], 0.0f);
146 ASSERT_EQUAL(z[1], -10.0f);
147 ASSERT_EQUAL(z[2], 0.0f);
148 ASSERT_EQUAL(z[3], -15.0f);
149
150 z[0] = 0.0f;
151 z[1] = 0.0f;
152 z[2] = 0.0f;
153 z[3] = 0.0f;
154
155 cusp::blas::xmy(View(x), View(y), View(z));
156
157 ASSERT_EQUAL(z[0], 0.0f);
158 ASSERT_EQUAL(z[1], -10.0f);
159 ASSERT_EQUAL(z[2], 0.0f);
160 ASSERT_EQUAL(z[3], -15.0f);
161
162 // test size checking
163 Array output(3);
164 ASSERT_THROWS(cusp::blas::xmy(x, y, output), cusp::invalid_input_exception);
165 }
166 DECLARE_HOST_DEVICE_UNITTEST(TestXmy);
167
168
169 template <class MemorySpace>
170 void TestCopy(void)
171 {
172 typedef typename cusp::array1d<float, MemorySpace> Array;
173 typedef typename cusp::array1d<float, MemorySpace>::view View;
174
175 Array x(4);
176
177 x[0] = 7.0f;
178 x[1] = 5.0f;
179 x[2] = 4.0f;
180 x[3] = -3.0f;
181
182 {
183 Array y(4, -1);
184 cusp::blas::copy(x, y);
185 ASSERT_EQUAL(x, y);
186 }
187
188 {
189 Array y(4, -1);
190 cusp::blas::copy(View(x), View(y));
191 ASSERT_EQUAL(x, y);
192 }
193
194 // test size checking
195 cusp::array1d<float, MemorySpace> w(3);
196 ASSERT_THROWS(cusp::blas::copy(w, x), cusp::invalid_input_exception);
197 }
198 DECLARE_HOST_DEVICE_UNITTEST(TestCopy);
199
200
201 template <class MemorySpace>
202 void TestDot(void)
203 {
204 typedef typename cusp::array1d<float, MemorySpace> Array;
205 typedef typename cusp::array1d<float, MemorySpace>::view View;
206
207 Array x(6);
208 Array y(6);
209
210 x[0] = 7.0f; y[0] = 0.0f;
211 x[1] = 5.0f; y[1] = -2.0f;
212 x[2] = 4.0f; y[2] = 0.0f;
213 x[3] = -3.0f; y[3] = 5.0f;
214 x[4] = 0.0f; y[4] = 6.0f;
215 x[5] = 4.0f; y[5] = 1.0f;
216
217 ASSERT_EQUAL(cusp::blas::dot(x, y), -21.0f);
218
219 ASSERT_EQUAL(cusp::blas::dot(View(x), View(y)), -21.0f);
220
221 // test size checking
222 cusp::array1d<float, MemorySpace> w(3);
223 ASSERT_THROWS(cusp::blas::dot(x, w), cusp::invalid_input_exception);
224 }
225 DECLARE_HOST_DEVICE_UNITTEST(TestDot);
226
227
228 template <class MemorySpace>
229 void TestDotc(void)
230 {
231 typedef typename cusp::array1d<float, MemorySpace> Array;
232 typedef typename cusp::array1d<float, MemorySpace>::view View;
233
234 // TODO test complex types
235 Array x(6);
236 Array y(6);
237
238 x[0] = 7.0f; y[0] = 0.0f;
239 x[1] = 5.0f; y[1] = -2.0f;
240 x[2] = 4.0f; y[2] = 0.0f;
241 x[3] = -3.0f; y[3] = 5.0f;
242 x[4] = 0.0f; y[4] = 6.0f;
243 x[5] = 4.0f; y[5] = 1.0f;
244
245 ASSERT_EQUAL(cusp::blas::dotc(x, y), -21.0f);
246
247 ASSERT_EQUAL(cusp::blas::dotc(View(x), View(y)), -21.0f);
248
249 // test size checking
250 Array w(3);
251 ASSERT_THROWS(cusp::blas::dotc(x, w), cusp::invalid_input_exception);
252 }
253 DECLARE_HOST_DEVICE_UNITTEST(TestDotc);
254
255
256 template <class MemorySpace>
257 void TestFill(void)
258 {
259 typedef typename cusp::array1d<float, MemorySpace> Array;
260 typedef typename cusp::array1d<float, MemorySpace>::view View;
261
262 Array x(4);
263
264 x[0] = 7.0f;
265 x[1] = 5.0f;
266 x[2] = 4.0f;
267 x[3] = -3.0f;
268
269 cusp::blas::fill(x, 2.0f);
270
271 ASSERT_EQUAL(x[0], 2.0);
272 ASSERT_EQUAL(x[1], 2.0);
273 ASSERT_EQUAL(x[2], 2.0);
274 ASSERT_EQUAL(x[3], 2.0);
275
276 cusp::blas::fill(View(x), 1.0f);
277
278 ASSERT_EQUAL(x[0], 1.0);
279 ASSERT_EQUAL(x[1], 1.0);
280 ASSERT_EQUAL(x[2], 1.0);
281 ASSERT_EQUAL(x[3], 1.0);
282 }
283 DECLARE_HOST_DEVICE_UNITTEST(TestFill);
284
285
286 template <class MemorySpace>
287 void TestNrm1(void)
288 {
289 typedef typename cusp::array1d<float, MemorySpace> Array;
290 typedef typename cusp::array1d<float, MemorySpace>::view View;
291
292 Array x(6);
293
294 x[0] = 7.0f;
295 x[1] = 5.0f;
296 x[2] = 4.0f;
297 x[3] = -3.0f;
298 x[4] = 0.0f;
299 x[5] = 1.0f;
300
301 ASSERT_EQUAL(cusp::blas::nrm1(x), 20.0f);
302
303 ASSERT_EQUAL(cusp::blas::nrm1(View(x)), 20.0f);
304 }
305 DECLARE_HOST_DEVICE_UNITTEST(TestNrm1);
306
307 template <class MemorySpace>
308 void TestComplexNrm1(void)
309 {
310 typedef typename cusp::array1d<cusp::complex<float>, MemorySpace> Array;
311 typedef typename cusp::array1d<cusp::complex<float>, MemorySpace>::view View;
312
313 Array x(6);
314
315 x[0] = 7.0f;
316 x[1] = 5.0f;
317 x[2] = 4.0f;
318 x[3] = -3.0f;
319 x[4] = 0.0f;
320 x[5] = 1.0f;
321
322 ASSERT_EQUAL(cusp::blas::nrm1(x), 20.0f);
323
324 ASSERT_EQUAL(cusp::blas::nrm1(View(x)), 20.0f);
325 }
326 DECLARE_HOST_DEVICE_UNITTEST(TestComplexNrm1);
327
328
329 template <class MemorySpace>
330 void TestNrm2(void)
331 {
332 typedef typename cusp::array1d<float, MemorySpace> Array;
333 typedef typename cusp::array1d<float, MemorySpace>::view View;
334
335 Array x(6);
336
337 x[0] = 7.0f;
338 x[1] = 5.0f;
339 x[2] = 4.0f;
340 x[3] = -3.0f;
341 x[4] = 0.0f;
342 x[5] = 1.0f;
343
344 ASSERT_EQUAL(cusp::blas::nrm2(x), 10.0f);
345
346 ASSERT_EQUAL(cusp::blas::nrm2(View(x)), 10.0f);
347 }
348 DECLARE_HOST_DEVICE_UNITTEST(TestNrm2);
349
350 template <class MemorySpace>
351 void TestComplexNrm2(void)
352 {
353 typedef typename cusp::array1d<cusp::complex<float>, MemorySpace> Array;
354 typedef typename cusp::array1d<cusp::complex<float>, MemorySpace>::view View;
355
356 Array x(6);
357
358 x[0] = 7.0f;
359 x[1] = 5.0f;
360 x[2] = 4.0f;
361 x[3] = -3.0f;
362 x[4] = 0.0f;
363 x[5] = 1.0f;
364
365 ASSERT_EQUAL(cusp::blas::nrm2(x), 10.0f);
366
367 ASSERT_EQUAL(cusp::blas::nrm2(View(x)), 10.0f);
368 }
369 DECLARE_HOST_DEVICE_UNITTEST(TestComplexNrm2);
370
371
372 template <class MemorySpace>
373 void TestNrmmax(void)
374 {
375 typedef typename cusp::array1d<float, MemorySpace> Array;
376 typedef typename cusp::array1d<float, MemorySpace>::view View;
377
378 Array x(6);
379 View view_x(x);
380
381 x[0] = 7.0f;
382 x[1] = -5.0f;
383 x[2] = 4.0f;
384 x[3] = -3.0f;
385 x[4] = 0.0f;
386 x[5] = 1.0f;
387
388 ASSERT_EQUAL(cusp::blas::nrmmax(x), 7.0f);
389
390 ASSERT_EQUAL(cusp::blas::nrmmax(View(x)), 7.0f);
391 }
392 DECLARE_HOST_DEVICE_UNITTEST(TestNrmmax);
393
394 template <class MemorySpace>
395 void TestComplexNrmmax(void)
396 {
397 typedef typename cusp::array1d<cusp::complex<float>, MemorySpace> Array;
398 typedef typename cusp::array1d<cusp::complex<float>, MemorySpace>::view View;
399
400 Array x(6);
401 View view_x(x);
402
403 x[0] = 7.0f;
404 x[1] = -5.0f;
405 x[2] = 4.0f;
406 x[3] = -3.0f;
407 x[4] = 0.0f;
408 x[5] = 1.0f;
409
410 ASSERT_EQUAL(cusp::blas::nrmmax(x), 7.0f);
411
412 ASSERT_EQUAL(cusp::blas::nrmmax(View(x)), 7.0f);
413 }
414 DECLARE_HOST_DEVICE_UNITTEST(TestComplexNrmmax);
415
416
417 template <class MemorySpace>
418 void TestScal(void)
419 {
420 typedef typename cusp::array1d<float, MemorySpace> Array;
421 typedef typename cusp::array1d<float, MemorySpace>::view View;
422
423 Array x(6);
424
425 x[0] = 7.0f;
426 x[1] = 5.0f;
427 x[2] = 4.0f;
428 x[3] = -3.0f;
429 x[4] = 0.0f;
430 x[5] = 4.0f;
431
432 cusp::blas::scal(x, 4.0f);
433
434 ASSERT_EQUAL(x[0], 28.0);
435 ASSERT_EQUAL(x[1], 20.0);
436 ASSERT_EQUAL(x[2], 16.0);
437 ASSERT_EQUAL(x[3], -12.0);
438 ASSERT_EQUAL(x[4], 0.0);
439 ASSERT_EQUAL(x[5], 16.0);
440
441 cusp::blas::scal(View(x), 2.0f);
442
443 ASSERT_EQUAL(x[0], 56.0);
444 ASSERT_EQUAL(x[1], 40.0);
445 ASSERT_EQUAL(x[2], 32.0);
446 ASSERT_EQUAL(x[3], -24.0);
447 ASSERT_EQUAL(x[4], 0.0);
448 ASSERT_EQUAL(x[5], 32.0);
449 }
450 DECLARE_HOST_DEVICE_UNITTEST(TestScal);
451

  ViewVC Help
Powered by ViewVC 1.1.26