Debian Codegraph @

packages/belos/kokkos/test/BelosKokkosMVOPTester.hpp

raw
1 // @HEADER
2 // *****************************************************************************
3 // Belos: Block Linear Solvers Package
4 //
5 // Copyright 2004-2016 NTESS and the Belos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 //
10 #ifndef BELOS_KOKKOS_MVOPTESTER_HPP
11 #define BELOS_KOKKOS_MVOPTESTER_HPP
12
13 // Assumptions that I have made:
14 // * I assume/verify that a multivector must have at least one std::vector. This seems
15 // to be consistent with Epetra_MultiVec.
16 // * I do not assume that an operator is deterministic; I do assume that the
17 // operator, applied to 0, will return 0.
18
19 /*! \file BelosKokkosMVOPTester.hpp
20 \brief Test routines for Kokkos MultiVecTraits and OperatorTraits conformity.
21 Note: This tester file is a direct copy of the BelosMVOPTester.hpp,
22 except that the tests for accessing non-contiguous columns of data have
23 been removed for the testing of the Kokkos adapter.
24 */
25
26 #include "BelosConfigDefs.hpp"
27 #include "BelosTypes.hpp"
28
29 #include "BelosMultiVecTraits.hpp"
30 #include "BelosOperatorTraits.hpp"
31 #include "BelosOutputManager.hpp"
32
33 #include "Teuchos_RCP.hpp"
34 #include "Teuchos_SetScientific.hpp"
35 #include "Teuchos_SerialDenseHelpers.hpp"
36
37 namespace Belos {
38
39 /// \brief Test correctness of a MultiVecTraits specialization and
40 /// multivector implementation.
41 ///
42 /// \tparam ScalarType The type of the entries in the multivectors;
43 /// the first template parameter of MultiVecTraits.
44 /// \tparam MV The multivector type; the second template parameter
45 /// of MultiVecTraits.
46 ///
47 /// \param om [in/out] A valid OutputManager, for displaying test results.
48 ///
49 /// \param A [in] An initial multivector, to use for making new
50 /// multivectors. (Belos doesn't currently have a "vector space"
51 /// abstraction; making a new multivector requires a valid input
52 /// multivector to clone.)
53 ///
54 /// \return Test result: true if all tests passed, else false.
55 template< class ScalarType, class MV >
56 bool
57 TestKokkosMultiVecTraits (const Teuchos::RCP<OutputManager<ScalarType> > &om,
58 const Teuchos::RCP<const MV> &A)
59 {
60 using Teuchos::SetScientific;
61 using std::endl;
62 typedef MultiVecTraits<ScalarType, MV> MVT;
63 typedef Teuchos::ScalarTraits<ScalarType> STS;
64 typedef typename STS::magnitudeType MagType;
65
66 // Make sure that all floating-point numbers are printed with the
67 // right precision.
68 SetScientific<ScalarType> sci (om->stream (Warnings));
69
70 // FIXME (mfh 09 Jan 2013) Added an arbitrary tolerance in case
71 // norms are not computed deterministically (which is possible
72 // even with MPI only, and more likely with threads).
73 const MagType tol = Teuchos::as<MagType> (100) * STS::eps ();
74
75 /* MVT Contract:
76
77 Clone(MV,int)
78 CloneCopy(MV)
79 CloneCopy(MV,vector<int>)
80 USER: will request positive number of vectors
81 MV: will return a multivector with exactly the number of
82 requested vectors.
83 vectors are the same dimension as the cloned MV
84
85
86 CloneView(MV,vector<int>) [const and non-const]
87 USER: There is no assumed communication between creation and
88 destruction of a view. I.e., after a view is created, changes to the
89 source multivector are not reflected in the view. Likewise, until
90 destruction of the view, changes in the view are not reflected in the
91 source multivector.
92
93 GetGlobalLength
94 MV: will always be positive (MV cannot have zero vectors)
95
96 GetNumberVecs
97 MV: will always be positive (MV cannot have zero vectors)
98
99 MvAddMv
100 USER: multivecs will be of the same dimension and same number of vecs
101 MV: input vectors will not be modified
102 performing C=0*A+1*B will assign B to C exactly
103
104 MvTimesMatAddMv
105 USER: multivecs and serialdensematrix will be of the proper shape
106 MV: input arguments will not be modified
107 following arithmetic relations hold exactly:
108 A*I = A
109 0*B = B
110 1*B = B
111
112 MvTransMv
113 USER: SerialDenseMatrix will be large enough to hold results.
114 MV: SerialDenseMatrix will not be resized.
115 Inner products will satisfy |a'*b| <= |a|*|b|
116 alpha == 0 => SerialDenseMatrix == 0
117
118 MvDot
119 USER: Results vector will be large enough for results.
120 Both multivectors will have the same number of vectors.
121 (Epetra crashes, otherwise.)
122 MV: Inner products will satisfy |a'*b| <= |a|*|b|
123 Results vector will not be resized.
124
125 MvNorm
126 MV: vector norm is always non-negative, and zero
127 only for zero vectors.
128 results vector should not be resized
129
130 SetBlock
131 USER: indices will be distinct
132 MV: assigns copies of the vectors to the specified
133 locations, leaving the other vectors untouched.
134
135 MvRandom
136 MV: Generate zero vector with "zero" probability
137 Don't gen the same vectors twice.
138
139 MvInit
140 MV: Init(alpha) sets all elements to alpha
141
142 MvScale (two versions)
143 MV: scales multivector values
144
145 MvPrint
146 MV: routine does not modify vectors (not tested here)
147 *********************************************************************/
148
149 const ScalarType one = STS::one();
150 const ScalarType zero = STS::zero();
151 const MagType zero_mag = Teuchos::ScalarTraits<MagType>::zero();
152
153 // Don't change these two without checking the initialization of ind below
154 const int numvecs = 10;
155 const int numvecs_2 = 5;
156
157 std::vector<int> ind(numvecs_2);
158 std::vector<int> indContig(numvecs_2);
159
160 /* Initialize indices for selected copies/views
161 The MVT specialization should not assume that
162 these are ordered or even distinct.
163 Also retrieve the edges.
164
165 However, to spice things up, grab the first std::vector,
166 last std::vector, and choose the others randomly.
167 */
168 TEUCHOS_TEST_FOR_EXCEPT(numvecs_2 != 5);
169 ind[0] = 0;
170 ind[1] = 5;
171 ind[2] = 2;
172 ind[3] = 2;
173 ind[4] = 9;
174
175 indContig[0] = 2;
176 indContig[1] = 3;
177 indContig[2] = 4;
178 indContig[3] = 5;
179 indContig[4] = 6;
180
181 /*********** GetNumberVecs() *****************************************
182 Verify:
183 1) This number should be strictly positive
184 *********************************************************************/
185 if ( MVT::GetNumberVecs(*A) <= 0 ) {
186 om->stream(Warnings)
187 << "*** ERROR *** MultiVectorTraits::GetNumberVecs()." << endl
188 << "Returned <= 0." << endl;
189 return false;
190 }
191
192
193 /*********** GetGlobalLength() ***************************************
194 Verify:
195 1) This number should be strictly positive
196 *********************************************************************/
197 if ( MVT::GetGlobalLength(*A) <= 0 ) {
198 om->stream(Warnings)
199 << "*** ERROR *** MultiVectorTraitsExt::GetGlobalLength()" << endl
200 << "Returned <= 0." << endl;
201 return false;
202 }
203
204
205 /*********** Clone() and MvNorm() ************************************
206 Verify:
207 1) Clone() allows us to specify the number of vectors
208 2) Clone() returns a multivector of the same dimension
209 3) Vector norms shouldn't be negative
210 4) MvNorm result std::vector should not be resized
211 *********************************************************************/
212 {
213 Teuchos::RCP<MV> B = MVT::Clone(*A,numvecs);
214 std::vector<MagType> norms(2*numvecs);
215 bool ResizeWarning = false;
216 if ( MVT::GetNumberVecs(*B) != numvecs ) {
217 om->stream(Warnings)
218 << "*** ERROR *** MultiVecTraits::Clone()." << endl
219 << "Did not allocate requested number of vectors." << endl;
220 return false;
221 }
222 if ( MVT::GetGlobalLength(*B) != MVT::GetGlobalLength(*A) ) {
223 om->stream(Warnings)
224 << "*** ERROR *** MultiVecTraits::Clone()." << endl
225 << "Did not allocate requested number of vectors." << endl;
226 return false;
227 }
228 MVT::MvNorm(*B, norms);
229 if ( norms.size() != 2*numvecs && ResizeWarning==false ) {
230 om->stream(Warnings)
231 << "*** WARNING *** MultiVecTraits::MvNorm()." << endl
232 << "Method resized the output vector." << endl;
233 ResizeWarning = true;
234 }
235 for (int i=0; i<numvecs; i++) {
236 if ( norms[i] < zero_mag ) {
237 om->stream(Warnings)
238 << "*** ERROR *** MultiVecTraits::Clone()." << endl
239 << "Vector had negative norm." << endl;
240 return false;
241 }
242 }
243 }
244
245
246 /*********** MvRandom() and MvNorm() and MvInit() ********************
247 Verify:
248 1) Set vectors to zero
249 2) Check that norm is zero
250 3) Perform MvRandom.
251 4) Verify that vectors aren't zero anymore
252 5) Perform MvRandom again.
253 6) Verify that std::vector norms are different than before
254
255 Without knowing something about the random distribution,
256 this is about the best that we can do, to make sure that MvRandom
257 did at least *something*.
258
259 Also, make sure std::vector norms aren't negative.
260 *********************************************************************/
261 {
262 Teuchos::RCP<MV> B = MVT::Clone(*A,numvecs);
263 std::vector<MagType> norms(numvecs), norms2(numvecs);
264
265 MVT::MvInit(*B);
266 MVT::MvNorm(*B, norms);
267 for (int i=0; i<numvecs; i++) {
268 if ( norms[i] != zero_mag ) {
269 om->stream(Warnings)
270 << "*** ERROR *** MultiVecTraits::MvInit() "
271 << "and MultiVecTraits::MvNorm()" << endl
272 << "Supposedly zero vector has non-zero norm." << endl;
273 return false;
274 }
275 }
276 MVT::MvRandom(*B);
277 MVT::MvNorm(*B, norms);
278 MVT::MvRandom(*B);
279 MVT::MvNorm(*B, norms2);
280 for (int i=0; i<numvecs; i++) {
281 if ( norms[i] == zero_mag || norms2[i] == zero_mag ) {
282 om->stream(Warnings)
283 << "*** ERROR *** MultiVecTraits::MvRandom()." << endl
284 << "Random vector was empty (very unlikely)." << endl;
285 return false;
286 }
287 else if ( norms[i] < zero_mag || norms2[i] < zero_mag ) {
288 om->stream(Warnings)
289 << "*** ERROR *** MultiVecTraits::MvRandom()." << endl
290 << "Vector had negative norm." << endl;
291 return false;
292 }
293 else if ( norms[i] == norms2[i] ) {
294 om->stream(Warnings)
295 << "*** ERROR *** MutliVecTraits::MvRandom()." << endl
296 << "Vectors not random enough." << endl;
297 return false;
298 }
299 }
300 }
301
302
303 /*********** MvRandom() and MvNorm() and MvScale() *******************
304 Verify:
305 1) Perform MvRandom.
306 2) Verify that vectors aren't zero
307 3) Set vectors to zero via MvScale
308 4) Check that norm is zero
309 *********************************************************************/
310 {
311 Teuchos::RCP<MV> B = MVT::Clone(*A,numvecs);
312 std::vector<MagType> norms(numvecs);
313
314 MVT::MvRandom(*B);
315 MVT::MvScale(*B,STS::zero());
316 MVT::MvNorm(*B, norms);
317 for (int i=0; i<numvecs; i++) {
318 if ( norms[i] != zero_mag ) {
319 om->stream(Warnings)
320 << "*** ERROR *** MultiVecTraits::MvScale(alpha) "
321 << "Supposedly zero vector has non-zero norm." << endl;
322 return false;
323 }
324 }
325
326 MVT::MvRandom(*B);
327 std::vector<ScalarType> zeros(numvecs,STS::zero());
328 MVT::MvScale(*B,zeros);
329 MVT::MvNorm(*B, norms);
330 for (int i=0; i<numvecs; i++) {
331 if ( norms[i] != zero_mag ) {
332 om->stream(Warnings)
333 << "*** ERROR *** MultiVecTraits::MvScale(alphas) "
334 << "Supposedly zero vector has non-zero norm." << endl;
335 return false;
336 }
337 }
338 }
339
340
341 /*********** MvInit() and MvNorm() ***********************************
342 A std::vector of ones of dimension n should have norm std::sqrt(n)
343 1) Init vectors to all ones
344 2) Verify that norm is std::sqrt(n)
345 3) Verify that norms aren't negative
346
347 Note: I'm not sure that we can expect this to hold in practice.
348 Maybe something like std::abs(norm-std::sqrt(n)) < STS::eps() ???
349 The sum of 1^2==1 should be n, but what about std::sqrt(n)?
350 They may be using a different square root than ScalartTraits
351 On my iBook G4 and on jeter, this test works.
352 Right now, this has been demoted to a warning.
353 *********************************************************************/
354 {
355 Teuchos::RCP<MV> B = MVT::Clone(*A,numvecs);
356 std::vector<MagType> norms(numvecs);
357
358 MVT::MvInit(*B,one);
359 MVT::MvNorm(*B, norms);
360 bool BadNormWarning = false;
361 for (int i=0; i<numvecs; i++) {
362 if ( norms[i] < zero_mag ) {
363 om->stream(Warnings)
364 << "*** ERROR *** MultiVecTraits::MvRandom()." << endl
365 << "Vector had negative norm." << endl;
366 return false;
367 }
368 else if ( norms[i] != STS::squareroot(MVT::GetGlobalLength(*B)) && !BadNormWarning ) {
369 om->stream(Warnings)
370 << endl
371 << "Warning testing MultiVecTraits::MvInit()." << endl
372 << "Ones std::vector should have norm std::sqrt(dim)." << endl
373 << "norms[i]: " << norms[i] << "\tdim: " << MVT::GetGlobalLength(*B) << endl << endl;
374 BadNormWarning = true;
375 }
376 }
377 }
378
379
380 /*********** MvInit() and MvNorm() ***********************************
381 A std::vector of zeros of dimension n should have norm 0
382 1) Verify that norms aren't negative
383 2) Verify that norms are zero
384
385 We must know this works before the next tests.
386 *********************************************************************/
387 {
388 Teuchos::RCP<MV> B = MVT::Clone(*A,numvecs);
389 std::vector<MagType> norms(numvecs);
390 MVT::MvInit(*B, zero_mag);
391 MVT::MvNorm(*B, norms);
392 for (int i=0; i<numvecs; i++) {
393 if ( norms[i] < zero_mag ) {
394 om->stream(Warnings)
395 << "*** ERROR *** MultiVecTraits::MvInit()." << endl
396 << "Vector had negative norm." << endl;
397 return false;
398 }
399 else if ( norms[i] != zero_mag ) {
400 om->stream(Warnings)
401 << "*** ERROR *** MultiVecTraits::MvInit()." << endl
402 << "Zero std::vector should have norm zero." << endl;
403 return false;
404 }
405 }
406 }
407
408
409 /*********** CloneCopy(MV,std::vector<int>) and MvNorm ********************
410 1) Check quantity/length of vectors
411 2) Check std::vector norms for agreement
412 3) Zero out B and make sure that C norms are not affected
413 *********************************************************************/
414 {
415 Teuchos::RCP<MV> B, C;
416 std::vector<MagType> norms(numvecs), norms2(numvecs);
417
418 B = MVT::Clone(*A,numvecs);
419 MVT::MvRandom(*B);
420 MVT::MvNorm(*B, norms);
421 C = MVT::CloneCopy(*B,ind);
422 MVT::MvNorm(*C, norms2);
423 if ( MVT::GetNumberVecs(*C) != numvecs_2 ) {
424 om->stream(Warnings)
425 << "*** ERROR *** MultiVecTraits::CloneCopy(ind)." << endl
426 << "Wrong number of vectors." << endl;
427 return false;
428 }
429 if ( MVT::GetGlobalLength(*C) != MVT::GetGlobalLength(*B) ) {
430 om->stream(Warnings)
431 << "*** ERROR *** MultiVecTraits::CloneCopy(ind)." << endl
432 << "Vector lengths don't match." << endl;
433 return false;
434 }
435 for (int i=0; i<numvecs_2; i++) {
436 if (STS::magnitude (norms2[i] - norms[ind[i]]) > tol) {
437 om->stream(Warnings)
438 << "*** ERROR *** MultiVecTraits::CloneCopy(ind)." << endl
439 << "Copied vectors do not agree: "
440 << norms2[i] << " != " << norms[ind[i]] << endl
441 << "Difference " << STS::magnitude (norms2[i] - norms[ind[i]])
442 << " exceeds the tolerance 100*eps = " << tol << endl;
443 return false;
444 }
445 }
446 MVT::MvInit(*B,zero);
447 MVT::MvNorm(*C, norms);
448 for (int i=0; i<numvecs_2; i++) {
449 //if ( norms2[i] != norms[i] ) {
450 if (STS::magnitude (norms2[i] - norms[i]) > tol) {
451 om->stream(Warnings)
452 << "*** ERROR *** MultiVecTraits::CloneCopy(ind)." << endl
453 << "Copied vectors were not independent." << endl
454 << norms2[i] << " != " << norms[i] << endl
455 << "Difference " << STS::magnitude (norms2[i] - norms[i])
456 << " exceeds the tolerance 100*eps = " << tol << endl;
457 return false;
458 }
459 }
460 }
461
462 /*********** CloneCopy(MV) and MvNorm ********************************
463 1) Check quantity
464 2) Check value of norms
465 3) Zero out B and make sure that C is still okay
466 *********************************************************************/
467 {
468 Teuchos::RCP<MV> B, C;
469 std::vector<MagType> norms(numvecs), norms2(numvecs);
470
471 B = MVT::Clone(*A,numvecs);
472 MVT::MvRandom(*B);
473 MVT::MvNorm(*B, norms);
474 C = MVT::CloneCopy(*B);
475 MVT::MvNorm(*C, norms2);
476 if ( MVT::GetNumberVecs(*C) != numvecs ) {
477 om->stream(Warnings)
478 << "*** ERROR *** MultiVecTraits::CloneCopy()." << endl
479 << "Wrong number of vectors." << endl;
480 return false;
481 }
482 for (int i=0; i<numvecs; i++) {
483 if (STS::magnitude (norms2[i] - norms[i]) > tol) {
484 om->stream(Warnings)
485 << "*** ERROR *** MultiVecTraits::CloneCopy()." << endl
486 << "Copied vectors do not agree: "
487 << norms2[i] << " != " << norms[i] << endl
488 << "Difference " << STS::magnitude (norms2[i] - norms[i])
489 << " exceeds the tolerance 100*eps = " << tol << endl;
490 return false;
491 }
492 }
493 MVT::MvInit(*B,zero);
494 MVT::MvNorm(*C, norms);
495 for (int i=0; i<numvecs; i++) {
496 //if ( norms2[i] != norms[i] ) {
497 if (STS::magnitude (norms2[i] - norms[i]) > tol) {
498 om->stream(Warnings)
499 << "*** ERROR *** MultiVecTraits::CloneCopy()." << endl
500 << "Copied vectors were not independent." << endl
501 << norms2[i] << " != " << norms[i] << endl
502 << "Difference " << STS::magnitude (norms2[i] - norms[i])
503 << " exceeds the tolerance 100*eps = " << tol << endl;
504 return false;
505 }
506 }
507 }
508
509
510 /*********** CloneView(MV,std::vector<int>) and MvNorm ********************
511 Check that we have a view of the selected vectors
512 1) Check quantity
513 2) Check value of norms
514 3) Zero out B and make sure that C is zero as well
515 *********************************************************************/
516 {
517 Teuchos::RCP<MV> B, C;
518 std::vector<MagType> norms(numvecs), norms2(numvecs);
519
520 B = MVT::Clone(*A,numvecs);
521 MVT::MvRandom(*B);
522 MVT::MvNorm(*B, norms);
523 C = MVT::CloneViewNonConst(*B,indContig);
524 MVT::MvNorm(*C, norms2);
525 if ( MVT::GetNumberVecs(*C) != numvecs_2 ) {
526 om->stream(Warnings)
527 << "*** ERROR *** MultiVecTraits::CloneView(indContig)." << endl
528 << "Wrong number of vectors." << endl;
529 return false;
530 }
531 for (int i=0; i<numvecs_2; i++) {
532 //if ( norms2[i] != norms[indContig[i]] ) {
533 if (STS::magnitude (norms2[i] - norms[indContig[i]]) > tol) {
534 om->stream(Warnings)
535 << "*** ERROR *** MultiVecTraits::CloneView(indContig)." << endl
536 << "Viewed vectors do not agree." << endl;
537 return false;
538 }
539 }
540 }
541
542
543 /*********** CloneView(const MV,std::vector<int>) and MvNorm() ************
544 Check that we have a view of the selected vectors.
545 1) Check quantity
546 2) Check value of norms for agreement
547 3) Zero out B and make sure that C is zerod as well
548 *********************************************************************/
549 {
550 Teuchos::RCP<MV> B;
551 Teuchos::RCP<const MV> C;
552 std::vector<MagType> normsB(numvecs), normsC(numvecs_2);
553 std::vector<int> allind(numvecs);
554 for (int i=0; i<numvecs; i++) {
555 allind[i] = i;
556 }
557
558 B = MVT::Clone(*A,numvecs);
559 MVT::MvRandom( *B );
560 MVT::MvNorm(*B, normsB);
561 C = MVT::CloneView(*B,indContig);
562 MVT::MvNorm(*C, normsC);
563 if ( MVT::GetNumberVecs(*C) != numvecs_2 ) {
564 om->stream(Warnings)
565 << "*** ERROR *** const MultiVecTraits::CloneView(indContig)." << endl
566 << "Wrong number of vectors." << endl;
567 return false;
568 }
569 for (int i=0; i<numvecs_2; i++) {
570 //if ( normsC[i] != normsB[ind[i]] ) {
571 if (STS::magnitude (normsC[i] - normsB[indContig[i]]) > tol) {
572 om->stream(Warnings)
573 << "*** ERROR *** const MultiVecTraits::CloneView(indContig)." << endl
574 << "Viewed vectors do not agree." << endl;
575 return false;
576 }
577 }
578 }
579
580 /*********** SetBlock() and MvNorm() *********************************
581 SetBlock() will copy the vectors from C into B
582 1) Verify that the specified vectors were copied
583 2) Verify that the other vectors were not modified
584 3) Verify that C was not modified
585 4) Change C and then check B to make sure it was not modified
586
587 Use a different index set than has been used so far (distinct entries).
588 This is because duplicate entries will cause the std::vector to be
589 overwritten, making it more difficult to test.
590 *********************************************************************/
591 {
592 Teuchos::RCP<MV> B, C; //numvecs = 10, numvecs_2 = 5
593 std::vector<MagType> normsB1(numvecs), normsB2(numvecs),
594 normsC1(numvecs_2), normsC2(numvecs_2);
595
596 B = MVT::Clone(*A,numvecs);
597 C = MVT::Clone(*A,numvecs_2);
598 // Just do every other one, interleaving the vectors of C into B
599 ind.resize(numvecs_2);
600 for (int i=0; i<numvecs_2; i++) {
601 ind[i] = 2*i; // 0,2,4,6,..
602 }
603 MVT::MvRandom(*B);
604 MVT::MvRandom(*C);
605
606 MVT::MvNorm(*B,normsB1);
607 MVT::MvNorm(*C,normsC1);
608 MVT::SetBlock(*C,ind,*B);// Copy vectors of C to vectors specified by ind in B.
609 MVT::MvNorm(*B,normsB2);
610 MVT::MvNorm(*C,normsC2);
611
612 // check that C was not changed by SetBlock
613 for (int i=0; i<numvecs_2; i++) {
614 //if ( normsC1[i] != normsC2[i] ) {
615 if (STS::magnitude (normsC1[i] - normsC2[i]) > tol) {
616 om->stream(Warnings)
617 << "*** ERROR *** MultiVecTraits::SetBlock()." << endl
618 << "Operation modified source vectors." << endl;
619 return false;
620 }
621 }
622 // check that the correct vectors of B were modified
623 // and the others were not
624 for (int i=0; i<numvecs; i++) {
625 if (i % 2 == 0) {
626 // should be a vector from C
627 if (STS::magnitude (normsB2[i] - normsC1[i/2]) > tol) {
628 om->stream(Warnings)
629 << "*** ERROR *** MultiVecTraits::SetBlock()." << endl
630 << "Copied vectors do not agree: " << endl
631 << normsB2[i] << " != " << normsC1[i/2] << endl
632 << "Difference " << STS::magnitude (normsB2[i] - normsC1[i/2])
633 << " exceeds the tolerance 100*eps = " << tol << endl;
634 return false;
635 }
636 }
637 else {
638 // should be an original vector
639 if (STS::magnitude (normsB1[i] - normsB2[i]) > tol) {
640 om->stream(Warnings)
641 << "*** ERROR *** MultiVecTraits::SetBlock()." << endl
642 << "Incorrect vectors were modified." << endl
643 << normsB1[i] << " != " << normsB2[i] << endl
644 << "Difference " << STS::magnitude (normsB2[i] - normsB2[i])
645 << " exceeds the tolerance 100*eps = " << tol << endl;
646 return false;
647 }
648 }
649 }
650 MVT::MvInit(*C,zero);
651 MVT::MvNorm(*B,normsB1);
652 // verify that we copied and didn't reference
653 for (int i=0; i<numvecs; i++) {
654 //if ( normsB1[i] != normsB2[i] ) {
655 if (STS::magnitude (normsB1[i] - normsB2[i]) > tol) {
656 om->stream(Warnings)
657 << "*** ERROR *** MultiVecTraits::SetBlock()." << endl
658 << "Copied vectors were not independent." << endl
659 << normsB1[i] << " != " << normsB2[i] << endl
660 << "Difference " << STS::magnitude (normsB1[i] - normsB2[i])
661 << " exceeds the tolerance 100*eps = " << tol << endl;
662 return false;
663 }
664 }
665 }
666
667
668 /*********** SetBlock() and MvNorm() *********************************
669 SetBlock() will copy the vectors from C into B
670 1) Verify that the specified vectors were copied
671 2) Verify that the other vectors were not modified
672 3) Verify that C was not modified
673 4) Change C and then check B to make sure it was not modified
674
675 Use a different index set than has been used so far (distinct entries).
676 This is because duplicate entries will cause the std::vector to be
677 overwritten, making it more difficult to test.
678
679 These tests are the same as the ones above, except that the
680 number of indices (to be copied into B) is less than the number
681 of vectors in C, so that not all of C is put into B.
682 *********************************************************************/
683 {
684 Teuchos::RCP<MV> B, C;
685 // set these: we assume below that setSize*2=BSize
686 const int CSize = 6,
687 setSize = 5,
688 BSize = 2*setSize;
689 std::vector<MagType> normsB1(BSize), normsB2(BSize),
690 normsC1(CSize), normsC2(CSize);
691
692 B = MVT::Clone(*A,BSize);
693 C = MVT::Clone(*A,CSize);
694 // Just do every other one, interleaving the vectors of C into B
695 ind.resize(setSize);
696 for (int i=0; i<setSize; i++) {
697 ind[i] = 2*i;
698 }
699 MVT::MvRandom(*B);
700 MVT::MvRandom(*C);
701
702 MVT::MvNorm(*B,normsB1);
703 MVT::MvNorm(*C,normsC1);
704 MVT::SetBlock(*C,ind,*B);
705 MVT::MvNorm(*B,normsB2);
706 MVT::MvNorm(*C,normsC2);
707
708 // check that C was not changed by SetBlock
709 for (int i=0; i<CSize; i++) {
710 //if ( normsC1[i] != normsC2[i] ) {
711 if (STS::magnitude (normsC1[i] - normsC2[i]) > tol) {
712 om->stream(Warnings)
713 << "*** ERROR *** MultiVecTraits::SetBlock()." << endl
714 << "Operation modified source vectors." << endl;
715 return false;
716 }
717 }
718 // check that the correct vectors of B were modified
719 // and the others were not
720 for (int i=0; i<BSize; i++) {
721 if (i % 2 == 0) {
722 // should be a vector from C
723 const MagType diff = STS::magnitude (normsB2[i] - normsC1[i/2]);
724 if (diff > tol) {
725 om->stream(Warnings)
726 << "*** ERROR *** MultiVecTraits::SetBlock()." << endl
727 << "Copied vectors do not agree: " << endl
728 << normsB2[i] << " != " << normsC1[i/2] << endl
729 << "Difference " << diff << " exceeds the tolerance 100*eps = "
730 << tol << endl;
731 return false;
732 }
733 }
734 else {
735 // should be an original vector
736 const MagType diff = STS::magnitude (normsB1[i] - normsB2[i]);
737 //if ( normsB1[i] != normsB2[i] ) {
738 if (diff > tol) {
739 om->stream(Warnings)
740 << "*** ERROR *** MultiVecTraits::SetBlock()." << endl
741 << "Incorrect vectors were modified." << endl
742 << normsB1[i] << " != " << normsB2[i] << endl
743 << "Difference " << diff << " exceeds the tolerance 100*eps = "
744 << tol << endl;
745 return false;
746 }
747 }
748 }
749 MVT::MvInit(*C,zero);
750 MVT::MvNorm(*B,normsB1);
751 // verify that we copied and didn't reference
752 for (int i=0; i<numvecs; i++) {
753 //if ( normsB1[i] != normsB2[i] ) {
754 if (STS::magnitude (normsB1[i] - normsB2[i]) > tol) {
755 om->stream(Warnings)
756 << "*** ERROR *** MultiVecTraits::SetBlock()." << endl
757 << "Copied vectors were not independent." << endl
758 << normsB1[i] << " != " << normsB2[i] << endl
759 << "Difference " << STS::magnitude (normsB1[i] - normsB2[i])
760 << " exceeds the tolerance 100*eps = " << tol << endl;
761 return false;
762 }
763 }
764 }
765
766
767 /*********** MvTransMv() *********************************************
768 Performs C = alpha * A^H * B, where
769 alpha is type ScalarType
770 A,B are type MV with p and q vectors, respectively
771 C is a SerialDenseMatrix<int,ScalarType> ALREADY sized to p by q
772
773 Verify:
774 1) C is not resized by the routine
775 3) Check that zero*(A^H B) == zero
776 3) Check inner product inequality:
777 [ |a1|*|b1| ... |ap|*|b1| ]
778 [a1 ... ap]^H [b1 ... bq] <= [ ... |ai|*|bj| ... ]
779 [ |ap|*|b1| ... |ap|*|bq| ]
780 4) Zero B and check that C is zero
781 5) Zero A and check that C is zero
782
783 Note: Should we really require that C is correctly sized already?
784 Epetra does (and crashes if it isn't.)
785 *********************************************************************/
786 {
787 const int p = 7;
788 const int q = 9;
789 Teuchos::RCP<MV> B, C;
790 std::vector<MagType> normsB(p), normsC(q);
791 Teuchos::SerialDenseMatrix<int,ScalarType> SDM(p,q);
792
793 B = MVT::Clone(*A,p);
794 C = MVT::Clone(*A,q);
795
796 // randomize the multivectors
797 MVT::MvRandom(*B);
798 MVT::MvNorm(*B,normsB);
799 MVT::MvRandom(*C);
800 MVT::MvNorm(*C,normsC);
801
802 // perform SDM = zero() * B^H * C
803 MVT::MvTransMv( zero, *B, *C, SDM );
804
805 // check the sizes: not allowed to have shrunk
806 if ( SDM.numRows() != p || SDM.numCols() != q ) {
807 om->stream(Warnings)
808 << "*** ERROR *** MultiVecTraits::MvTransMv()." << endl
809 << "Routine resized SerialDenseMatrix." << endl;
810 return false;
811 }
812
813 // check that zero**A^H*B == zero
814 if ( SDM.normOne() != zero ) {
815 om->stream(Warnings)
816 << "*** ERROR *** MultiVecTraits::MvTransMv()." << endl
817 << "Scalar argument processed incorrectly." << endl;
818 return false;
819 }
820
821 // perform SDM = one * B^H * C
822 MVT::MvTransMv( one, *B, *C, SDM );
823
824 // check the norms: a^H b = |a| |b| cos(theta) <= |a| |b|
825 // with equality only when a and b are colinear
826 for (int i=0; i<p; i++) {
827 for (int j=0; j<q; j++) {
828 if ( STS::magnitude(SDM(i,j))
829 > STS::magnitude(normsB[i]*normsC[j]) ) {
830 om->stream(Warnings)
831 << "*** ERROR *** MultiVecTraits::MvTransMv()." << endl
832 << "Triangle inequality did not hold: "
833 << STS::magnitude(SDM(i,j))
834 << " > "
835 << STS::magnitude(normsB[i]*normsC[j])
836 << endl;
837 return false;
838 }
839 }
840 }
841 MVT::MvInit(*C);
842 MVT::MvRandom(*B);
843 MVT::MvTransMv( one, *B, *C, SDM );
844 for (int i=0; i<p; i++) {
845 for (int j=0; j<q; j++) {
846 if ( SDM(i,j) != zero ) {
847 om->stream(Warnings)
848 << "*** ERROR *** MultiVecTraits::MvTransMv()." << endl
849 << "Inner products not zero for C==0." << endl;
850 return false;
851 }
852 }
853 }
854 MVT::MvInit(*B);
855 MVT::MvRandom(*C);
856 MVT::MvTransMv( one, *B, *C, SDM );
857 for (int i=0; i<p; i++) {
858 for (int j=0; j<q; j++) {
859 if ( SDM(i,j) != zero ) {
860 om->stream(Warnings)
861 << "*** ERROR *** MultiVecTraits::MvTransMv()." << endl
862 << "Inner products not zero for B==0." << endl;
863 return false;
864 }
865 }
866 }
867 }
868
869
870 /*********** MvDot() *************************************************
871 Verify:
872 1) Results std::vector not resized
873 2) Inner product inequalities are satisfied
874 3) Zero vectors give zero inner products
875 *********************************************************************/
876 {
877 const int p = 7;
878 const int q = 9;
879 Teuchos::RCP<MV> B, C;
880 std::vector<ScalarType> iprods(p+q);
881 std::vector<MagType> normsB(numvecs), normsC(numvecs);
882
883 B = MVT::Clone(*A,p);
884 C = MVT::Clone(*A,p);
885
886 MVT::MvRandom(*B);
887 MVT::MvRandom(*C);
888 MVT::MvNorm(*B,normsB);
889 MVT::MvNorm(*C,normsC);
890 MVT::MvDot( *B, *C, iprods );
891 if ( iprods.size() != p+q ) {
892 om->stream(Warnings)
893 << "*** ERROR *** MultiVecTraits::MvDot." << endl
894 << "Routine resized results std::vector." << endl;
895 return false;
896 }
897 for (int i=0; i<BELOS_MIN(p,q); i++) {
898 if ( STS::magnitude(iprods[i])
899 > STS::magnitude(normsB[i]*normsC[i]) ) {
900 om->stream(Warnings)
901 << "*** ERROR *** MultiVecTraits::MvDot()." << endl
902 << "Inner products not valid." << endl;
903 return false;
904 }
905 }
906 MVT::MvInit(*B);
907 MVT::MvRandom(*C);
908 MVT::MvDot( *B, *C, iprods );
909 for (int i=0; i<p; i++) {
910 if ( iprods[i] != zero ) {
911 om->stream(Warnings)
912 << "*** ERROR *** MultiVecTraits::MvDot()." << endl
913 << "Inner products not zero for B==0." << endl;
914 return false;
915 }
916 }
917 MVT::MvInit(*C);
918 MVT::MvRandom(*B);
919 MVT::MvDot( *B, *C, iprods );
920 for (int i=0; i<p; i++) {
921 if ( iprods[i] != zero ) {
922 om->stream(Warnings)
923 << "*** ERROR *** MultiVecTraits::MvDot()." << endl
924 << "Inner products not zero for C==0." << endl;
925 return false;
926 }
927 }
928 }
929
930
931 /*********** MvAddMv() ***********************************************
932 D = alpha*B + beta*C
933 1) Use alpha==0,beta==1 and check that D == C
934 2) Use alpha==1,beta==0 and check that D == B
935 3) Use D==0 and D!=0 and check that result is the same
936 4) Check that input arguments are not modified
937 *********************************************************************/
938 {
939 const int p = 7;
940 Teuchos::RCP<MV> B, C, D;
941 std::vector<MagType> normsB1(p), normsB2(p),
942 normsC1(p), normsC2(p),
943 normsD1(p), normsD2(p);
944
945 Teuchos::SerialDenseMatrix<int,ScalarType> Alpha(1,1), Beta(1,1);
946 Teuchos::randomSyncedMatrix( Alpha );
947 Teuchos::randomSyncedMatrix( Beta );
948 ScalarType alpha = Alpha(0,0),
949 beta = Beta(0,0);
950
951 B = MVT::Clone(*A,p);
952 C = MVT::Clone(*A,p);
953 D = MVT::Clone(*A,p);
954
955 MVT::MvRandom(*B);
956 MVT::MvRandom(*C);
957 MVT::MvNorm(*B,normsB1);
958 MVT::MvNorm(*C,normsC1);
959
960 // check that 0*B+1*C == C
961 MVT::MvAddMv(zero,*B,one,*C,*D);
962 MVT::MvNorm(*B,normsB2);
963 MVT::MvNorm(*C,normsC2);
964 MVT::MvNorm(*D,normsD1);
965 for (int i=0; i<p; i++) {
966 if (STS::magnitude (normsB1[i] - normsB2[i]) > tol) {
967 om->stream(Warnings)
968 << "*** ERROR *** MultiVecTraits::MvAddMv()." << endl
969 << "Input arguments were modified." << endl;
970 return false;
971 }
972 else if (STS::magnitude (normsC1[i] - normsC2[i]) > tol) {
973 om->stream(Warnings)
974 << "*** ERROR *** MultiVecTraits::MvAddMv()." << endl
975 << "Input arguments were modified." << endl;
976 return false;
977 }
978 else if (STS::magnitude (normsC1[i] - normsD1[i]) > tol) {
979 om->stream(Warnings)
980 << "*** ERROR *** MultiVecTraits::MvAddMv()." << endl
981 << "Assignment did not work." << endl;
982 return false;
983 }
984 }
985
986 // check that 1*B+0*C == B
987 MVT::MvAddMv(one,*B,zero,*C,*D);
988 MVT::MvNorm(*B,normsB2);
989 MVT::MvNorm(*C,normsC2);
990 MVT::MvNorm(*D,normsD1);
991 for (int i=0; i<p; i++) {
992 if (STS::magnitude (normsB1[i] - normsB2[i]) > tol) {
993 om->stream(Warnings)
994 << "*** ERROR *** MultiVecTraits::MvAddMv()." << endl
995 << "Input arguments were modified." << endl;
996 return false;
997 }
998 else if (STS::magnitude (normsC1[i] - normsC2[i]) > tol) {
999 om->stream(Warnings)
1000 << "*** ERROR *** MultiVecTraits::MvAddMv()." << endl
1001 << "Input arguments were modified." << endl;
1002 return false;
1003 }
1004 else if (STS::magnitude (normsB1[i] - normsD1[i]) > tol) {
1005 om->stream(Warnings)
1006 << "*** ERROR *** MultiVecTraits::MvAddMv()." << endl
1007 << "Assignment did not work." << endl;
1008 return false;
1009 }
1010 }
1011
1012 // check that alpha*B+beta*C -> D is invariant under initial D
1013 // first, try random D
1014 MVT::MvRandom(*D);
1015 MVT::MvAddMv(alpha,*B,beta,*C,*D);
1016 MVT::MvNorm(*B,normsB2);
1017 MVT::MvNorm(*C,normsC2);
1018 MVT::MvNorm(*D,normsD1);
1019 // check that input args are not modified
1020 for (int i=0; i<p; i++) {
1021 if (STS::magnitude (normsB1[i] - normsB2[i]) > tol) {
1022 om->stream(Warnings)
1023 << "*** ERROR *** MultiVecTraits::MvAddMv()." << endl
1024 << "Input arguments were modified." << endl;
1025 return false;
1026 }
1027 else if (STS::magnitude (normsC1[i] - normsC2[i]) > tol) {
1028 om->stream(Warnings)
1029 << "*** ERROR *** MultiVecTraits::MvAddMv()." << endl
1030 << "Input arguments were modified." << endl;
1031 return false;
1032 }
1033 }
1034 // next, try zero D
1035 MVT::MvInit(*D);
1036 MVT::MvAddMv(alpha,*B,beta,*C,*D);
1037 MVT::MvNorm(*B,normsB2);
1038 MVT::MvNorm(*C,normsC2);
1039 MVT::MvNorm(*D,normsD2);
1040 // check that input args are not modified and that D is the same
1041 // as the above test
1042 for (int i=0; i<p; i++) {
1043 if (STS::magnitude (normsB1[i] - normsB2[i]) > tol) {
1044 om->stream(Warnings)
1045 << "*** ERROR *** MultiVecTraits::MvAddMv()." << endl
1046 << "Input arguments were modified." << endl;
1047 return false;
1048 }
1049 else if (STS::magnitude (normsC1[i] - normsC2[i]) > tol) {
1050 om->stream(Warnings)
1051 << "*** ERROR *** MultiVecTraits::MvAddMv()." << endl
1052 << "Input arguments were modified." << endl;
1053 return false;
1054 }
1055 else if (STS::magnitude (normsD1[i] - normsD2[i]) > tol) {
1056 om->stream(Warnings)
1057 << "*** ERROR *** MultiVecTraits::MvAddMv()." << endl
1058 << "Results varies depending on initial state of dest vectors." << endl;
1059 return false;
1060 }
1061 }
1062 }
1063
1064 /*********** MvAddMv() ***********************************************
1065 Similar to above, but where B or C are potentially the same
1066 object as D. This case is commonly used, for example, to affect
1067 A <- alpha*A
1068 via
1069 MvAddMv(alpha,A,zero,A,A)
1070 ** OR **
1071 MvAddMv(zero,A,alpha,A,A)
1072
1073 The result is that the operation has to be "atomic". That is,
1074 B and C are no longer reliable after D is modified, so that
1075 the assignment to D must be the last thing to occur.
1076
1077 D = alpha*B + beta*C
1078
1079 1) Use alpha==0,beta==1 and check that D == C
1080 2) Use alpha==1,beta==0 and check that D == B
1081 *********************************************************************/
1082 {
1083 const int p = 7;
1084 Teuchos::RCP<MV> B, D;
1085 Teuchos::RCP<const MV> C;
1086 std::vector<MagType> normsB(p),
1087 normsD(p);
1088 std::vector<int> lclindex(p);
1089 for (int i=0; i<p; i++) lclindex[i] = i;
1090
1091 B = MVT::Clone(*A,p);
1092 C = MVT::CloneView(*B,lclindex);
1093 D = MVT::CloneViewNonConst(*B,lclindex);
1094
1095 MVT::MvRandom(*B);
1096 MVT::MvNorm(*B,normsB);
1097
1098 // check that 0*B+1*C == C
1099 MVT::MvAddMv(zero,*B,one,*C,*D);
1100 MVT::MvNorm(*D,normsD);
1101 for (int i=0; i<p; i++) {
1102 if (STS::magnitude (normsB[i] - normsD[i]) > tol) {
1103 om->stream(Warnings)
1104 << "*** ERROR *** MultiVecTraits::MvAddMv() #2" << endl
1105 << "Assignment did not work." << endl;
1106 return false;
1107 }
1108 }
1109
1110 // check that 1*B+0*C == B
1111 MVT::MvAddMv(one,*B,zero,*C,*D);
1112 MVT::MvNorm(*D,normsD);
1113 for (int i=0; i<p; i++) {
1114 if (STS::magnitude (normsB[i] - normsD[i]) > tol) {
1115 om->stream(Warnings)
1116 << "*** ERROR *** MultiVecTraits::MvAddMv() #2" << endl
1117 << "Assignment did not work." << endl;
1118 return false;
1119 }
1120 }
1121
1122 }
1123
1124 /*********** MvTimesMatAddMv() 7 by 5 ********************************
1125 C = alpha*B*SDM + beta*C
1126 1) Use alpha==0, SDM!=0, beta==1 and check that C is unchanged
1127 2) Use alpha==0, SDM!=0, beta==0 and check that C is set to zero
1128 3) Use alpha==1, SDM==I, beta==0 and check that C is set to B
1129 4) Use alpha==1, SDM==0, beta==1 and check that C is unchanged
1130 5) Test with non-square matrices
1131 6) Always check that input arguments are not modified
1132 *********************************************************************/
1133 {
1134 const int p = 7, q = 5;
1135 Teuchos::RCP<MV> B, C;
1136 Teuchos::SerialDenseMatrix<int,ScalarType> SDM(p,q);
1137 std::vector<MagType> normsC1(q), normsC2(q),
1138 normsB1(p), normsB2(p);
1139
1140 B = MVT::Clone(*A,p);
1141 C = MVT::Clone(*A,q);
1142
1143 // Test 1: alpha==0, SDM!=0, beta==1 and check that C is unchanged
1144 MVT::MvRandom(*B);
1145 MVT::MvRandom(*C);
1146 MVT::MvNorm(*B,normsB1);
1147 MVT::MvNorm(*C,normsC1);
1148 Teuchos::randomSyncedMatrix(SDM);
1149 MVT::MvTimesMatAddMv(zero,*B,SDM,one,*C);
1150 MVT::MvNorm(*B,normsB2);
1151 MVT::MvNorm(*C,normsC2);
1152 for (int i=0; i<p; i++) {
1153 if (STS::magnitude (normsB1[i] - normsB2[i]) > tol) {
1154 om->stream(Warnings)
1155 << "*** ERROR *** MultiVecTraits::MvTimesMatAddMv()." << endl
1156 << "Input vectors were modified." << endl;
1157 return false;
1158 }
1159 }
1160 for (int i=0; i<q; i++) {
1161 if (STS::magnitude (normsC1[i] - normsC2[i]) > tol) {
1162 om->stream(Warnings)
1163 << "*** ERROR *** MultiVecTraits::MvTimesMatAddMv()." << endl
1164 << "Arithmetic test 1 failed." << endl;
1165 return false;
1166 }
1167 }
1168
1169 // Test 2: alpha==0, SDM!=0, beta==0 and check that C is set to zero
1170 MVT::MvRandom(*B);
1171 MVT::MvRandom(*C);
1172 MVT::MvNorm(*B,normsB1);
1173 MVT::MvNorm(*C,normsC1);
1174 Teuchos::randomSyncedMatrix(SDM);
1175 MVT::MvTimesMatAddMv(zero,*B,SDM,zero,*C);
1176 MVT::MvNorm(*B,normsB2);
1177 MVT::MvNorm(*C,normsC2);
1178 for (int i=0; i<p; i++) {
1179 if (STS::magnitude (normsB1[i] - normsB2[i]) > tol) {
1180 om->stream(Warnings)
1181 << "*** ERROR *** MultiVecTraits::MvTimesMatAddMv()." << endl
1182 << "Input vectors were modified." << endl;
1183 return false;
1184 }
1185 }
1186 for (int i=0; i<q; i++) {
1187 if ( normsC2[i] != zero ) {
1188 om->stream(Warnings)
1189 << "*** ERROR *** MultiVecTraits::MvTimesMatAddMv()." << endl
1190 << "Arithmetic test 2 failed: "
1191 << normsC2[i]
1192 << " != "
1193 << zero
1194 << endl;
1195 return false;
1196 }
1197 }
1198
1199 // Test 3: alpha==1, SDM==|I|, beta==0 and check that C is set to B
1200 // |0|
1201 MVT::MvRandom(*B);
1202 MVT::MvRandom(*C);
1203 MVT::MvNorm(*B,normsB1);
1204 MVT::MvNorm(*C,normsC1);
1205 SDM.scale(zero);
1206 for (int i=0; i<q; i++) {
1207 SDM(i,i) = one;
1208 }
1209 MVT::MvTimesMatAddMv(one,*B,SDM,zero,*C);
1210 MVT::MvNorm(*B,normsB2);
1211 MVT::MvNorm(*C,normsC2);
1212 for (int i=0; i<p; i++) {
1213 if (STS::magnitude (normsB1[i] - normsB2[i]) > tol) {
1214 om->stream(Warnings)
1215 << "*** ERROR *** MultiVecTraits::MvTimesMatAddMv()." << endl
1216 << "Input vectors were modified." << endl;
1217 return false;
1218 }
1219 }
1220 for (int i=0; i<q; i++) {
1221 if (STS::magnitude (normsB1[i] - normsC2[i]) > tol) {
1222 om->stream(Warnings)
1223 << "*** ERROR *** MultiVecTraits::MvTimesMatAddMv()." << endl
1224 << "Arithmetic test 3 failed: "
1225 << normsB1[i]
1226 << " != "
1227 << normsC2[i]
1228 << endl;
1229 return false;
1230 }
1231 }
1232
1233 // Test 4: alpha==1, SDM==0, beta==1 and check that C is unchanged
1234 MVT::MvRandom(*B);
1235 MVT::MvRandom(*C);
1236 MVT::MvNorm(*B,normsB1);
1237 MVT::MvNorm(*C,normsC1);
1238 SDM.scale(zero);
1239 MVT::MvTimesMatAddMv(one,*B,SDM,one,*C);
1240 MVT::MvNorm(*B,normsB2);
1241 MVT::MvNorm(*C,normsC2);
1242 for (int i=0; i<p; i++) {
1243 if (STS::magnitude (normsB1[i] - normsB2[i]) > tol) {
1244 om->stream(Warnings)
1245 << "*** ERROR *** MultiVecTraits::MvTimesMatAddMv()." << endl
1246 << "Input vectors were modified." << endl;
1247 return false;
1248 }
1249 }
1250 for (int i=0; i<q; i++) {
1251 if (STS::magnitude (normsC1[i] - normsC2[i]) > tol) {
1252 om->stream(Warnings)
1253 << "*** ERROR *** MultiVecTraits::MvTimesMatAddMv()." << endl
1254 << "Arithmetic test 4 failed." << endl;
1255 return false;
1256 }
1257 }
1258 }
1259
1260 /*********** MvTimesMatAddMv() 5 by 7 ********************************
1261 C = alpha*B*SDM + beta*C
1262 1) Use alpha==0, SDM!=0, beta==1 and check that C is unchanged
1263 2) Use alpha==0, SDM!=0, beta==0 and check that C is set to zero
1264 3) Use alpha==1, SDM==I, beta==0 and check that C is set to B
1265 4) Use alpha==1, SDM==0, beta==1 and check that C is unchanged
1266 5) Test with non-square matrices
1267 6) Always check that input arguments are not modified
1268 *********************************************************************/
1269 {
1270 const int p = 5, q = 7;
1271 Teuchos::RCP<MV> B, C;
1272 Teuchos::SerialDenseMatrix<int,ScalarType> SDM(p,q);
1273 std::vector<MagType> normsC1(q), normsC2(q),
1274 normsB1(p), normsB2(p);
1275
1276 B = MVT::Clone(*A,p);
1277 C = MVT::Clone(*A,q);
1278
1279 // Test 5: alpha==0, SDM!=0, beta==1 and check that C is unchanged
1280 MVT::MvRandom(*B);
1281 MVT::MvRandom(*C);
1282 MVT::MvNorm(*B,normsB1);
1283 MVT::MvNorm(*C,normsC1);
1284 Teuchos::randomSyncedMatrix(SDM);
1285 MVT::MvTimesMatAddMv(zero,*B,SDM,one,*C);
1286 MVT::MvNorm(*B,normsB2);
1287 MVT::MvNorm(*C,normsC2);
1288 for (int i=0; i<p; i++) {
1289 if (STS::magnitude (normsB1[i] - normsB2[i]) > tol) {
1290 om->stream(Warnings)
1291 << "*** ERROR *** MultiVecTraits::MvTimesMatAddMv()." << endl
1292 << "Input vectors were modified." << endl;
1293 return false;
1294 }
1295 }
1296 for (int i=0; i<q; i++) {
1297 if (STS::magnitude (normsC1[i] - normsC2[i]) > tol) {
1298 om->stream(Warnings)
1299 << "*** ERROR *** MultiVecTraits::MvTimesMatAddMv()." << endl
1300 << "Arithmetic test 5 failed." << endl;
1301 return false;
1302 }
1303 }
1304
1305 // Test 6: alpha==0, SDM!=0, beta==0 and check that C is set to zero
1306 MVT::MvRandom(*B);
1307 MVT::MvRandom(*C);
1308 MVT::MvNorm(*B,normsB1);
1309 MVT::MvNorm(*C,normsC1);
1310 Teuchos::randomSyncedMatrix(SDM);
1311 MVT::MvTimesMatAddMv(zero,*B,SDM,zero,*C);
1312 MVT::MvNorm(*B,normsB2);
1313 MVT::MvNorm(*C,normsC2);
1314 for (int i=0; i<p; i++) {
1315 if (STS::magnitude (normsB1[i] - normsB2[i]) > tol) {
1316 om->stream(Warnings)
1317 << "*** ERROR *** MultiVecTraits::MvTimesMatAddMv()." << endl
1318 << "Input vectors were modified." << endl;
1319 return false;
1320 }
1321 }
1322 for (int i=0; i<q; i++) {
1323 if ( normsC2[i] != zero ) {
1324 om->stream(Warnings)
1325 << "*** ERROR *** MultiVecTraits::MvTimesMatAddMv()." << endl
1326 << "Arithmetic test 6 failed: "
1327 << normsC2[i]
1328 << " != "
1329 << zero
1330 << endl;
1331 return false;
1332 }
1333 }
1334
1335 // Test 7: alpha==1, SDM==[I 0], beta==0 and check that C is set to B
1336 MVT::MvRandom(*B);
1337 MVT::MvRandom(*C);
1338 MVT::MvNorm(*B,normsB1);
1339 MVT::MvNorm(*C,normsC1);
1340 SDM.scale(zero);
1341 for (int i=0; i<p; i++) {
1342 SDM(i,i) = one;
1343 }
1344 MVT::MvTimesMatAddMv(one,*B,SDM,zero,*C);
1345 MVT::MvNorm(*B,normsB2);
1346 MVT::MvNorm(*C,normsC2);
1347 for (int i=0; i<p; i++) {
1348 if (STS::magnitude (normsB1[i] - normsB2[i]) > tol) {
1349 om->stream(Warnings)
1350 << "*** ERROR *** MultiVecTraits::MvTimesMatAddMv()." << endl
1351 << "Input vectors were modified." << endl;
1352 return false;
1353 }
1354 }
1355 for (int i=0; i<p; i++) {
1356 if (STS::magnitude (normsB1[i] - normsC2[i]) > tol) {
1357 om->stream(Warnings)
1358 << "*** ERROR *** MultiVecTraits::MvTimesMatAddMv()." << endl
1359 << "Arithmetic test 7 failed." << endl;
1360 return false;
1361 }
1362 }
1363 for (int i=p; i<q; i++) {
1364 if ( normsC2[i] != zero ) {
1365 om->stream(Warnings)
1366 << "*** ERROR *** MultiVecTraits::MvTimesMatAddMv()." << endl
1367 << "Arithmetic test 7 failed." << endl;
1368 return false;
1369 }
1370 }
1371
1372 // Test 8: alpha==1, SDM==0, beta==1 and check that C is unchanged
1373 MVT::MvRandom(*B);
1374 MVT::MvRandom(*C);
1375 MVT::MvNorm(*B,normsB1);
1376 MVT::MvNorm(*C,normsC1);
1377 SDM.scale(zero);
1378 MVT::MvTimesMatAddMv(one,*B,SDM,one,*C);
1379 MVT::MvNorm(*B,normsB2);
1380 MVT::MvNorm(*C,normsC2);
1381 for (int i=0; i<p; i++) {
1382 if (STS::magnitude (normsB1[i] - normsB2[i]) > tol) {
1383 om->stream(Warnings)
1384 << "*** ERROR *** MultiVecTraits::MvTimesMatAddMv()." << endl
1385 << "Input vectors were modified." << endl;
1386 return false;
1387 }
1388 }
1389 for (int i=0; i<q; i++) {
1390 if (STS::magnitude (normsC1[i] - normsC2[i]) > tol) {
1391 om->stream(Warnings)
1392 << "*** ERROR *** MultiVecTraits::MvTimesMatAddMv()." << endl
1393 << "Arithmetic test 8 failed." << endl;
1394 return false;
1395 }
1396 }
1397 }
1398
1399 return true;
1400
1401 }
1402
1403
1404
1405 /// \brief Test correctness of OperatorTraits specialization and its
1406 /// operator implementation.
1407 ///
1408 /// \tparam ScalarType The type of the entries in the multivectors;
1409 /// the first template parameter of MultiVecTraits and
1410 /// OperatorTraits.
1411 /// \tparam MV The multivector type; the second template parameter
1412 /// of MultiVecTraits and OperatorTraits.
1413 /// \tparam OP The operator type; the third template parameter
1414 /// of OperatorTraits.
1415 ///
1416 /// \param om [in/out] A valid OutputManager, for displaying test results.
1417 ///
1418 /// \param A [in] An initial multivector, to use for making new
1419 /// multivectors. (Belos doesn't currently have a "vector space"
1420 /// abstraction; making a new multivector requires a valid input
1421 /// multivector to clone.)
1422 ///
1423 /// \param M [in] The operator to test.
1424 ///
1425 /// \return Test result: true if all tests passed, else false.
1426 template< class ScalarType, class MV, class OP>
1427 bool
1428 TestKokkosOperatorTraits (const Teuchos::RCP<OutputManager<ScalarType> > &om,
1429 const Teuchos::RCP<const MV> &A,
1430 const Teuchos::RCP<const OP> &M)
1431 {
1432 using Teuchos::SetScientific;
1433 using std::endl;
1434 typedef MultiVecTraits<ScalarType, MV> MVT;
1435 typedef Teuchos::ScalarTraits<ScalarType> STS;
1436 typedef typename STS::magnitudeType MagType;
1437
1438 // Make sure that all floating-point numbers are printed with the
1439 // right precision.
1440 SetScientific<ScalarType> sci (om->stream (Warnings));
1441
1442 // FIXME (mfh 09 Jan 2013) Added an arbitrary tolerance in case
1443 // norms are not computed deterministically (which is possible
1444 // even with MPI only, and more likely with threads).
1445 const MagType tol = Teuchos::as<MagType> (100) * STS::eps ();
1446
1447 /* OPT Contract:
1448 Apply()
1449 MV: OP*zero == zero
1450 Warn if OP is not deterministic (OP*A != OP*A)
1451 Does not modify input arguments
1452 *********************************************************************/
1453
1454 typedef MultiVecTraits<ScalarType, MV> MVT;
1455 typedef Teuchos::ScalarTraits<ScalarType> STS;
1456 typedef OperatorTraits<ScalarType, MV, OP> OPT;
1457 typedef typename STS::magnitudeType MagType;
1458
1459 const int numvecs = 10;
1460
1461 Teuchos::RCP<MV> B = MVT::Clone(*A,numvecs),
1462 C = MVT::Clone(*A,numvecs);
1463
1464 std::vector<MagType> normsB1(numvecs), normsB2(numvecs),
1465 normsC1(numvecs), normsC2(numvecs);
1466 bool NonDeterministicWarning;
1467
1468 /*********** Apply() *************************************************
1469 Verify:
1470 1) OP*B == OP*B; OP is deterministic (just warn on this)
1471 2) OP*zero == 0
1472 3) OP*B doesn't modify B
1473 4) OP*B is invariant under initial state of destination vectors
1474 *********************************************************************/
1475 MVT::MvInit(*B);
1476 MVT::MvRandom(*C);
1477 MVT::MvNorm(*B,normsB1);
1478 OPT::Apply(*M,*B,*C);
1479 MVT::MvNorm(*B,normsB2);
1480 MVT::MvNorm(*C,normsC2);
1481 for (int i=0; i<numvecs; i++) {
1482 if (STS::magnitude (normsB2[i] - normsB1[i]) > tol) {
1483 om->stream(Warnings)
1484 << "*** ERROR *** OperatorTraits::Apply() [1]" << endl
1485 << "Apply() modified the input vectors." << endl
1486 << "Original: " << normsB1[i] << "; After: " << normsB2[i] << endl
1487 << "Difference " << STS::magnitude (normsB2[i] - normsB1[i])
1488 << " exceeds the tolerance 100*eps = " << tol << endl;
1489 return false;
1490 }
1491 if (normsC2[i] != STS::zero()) {
1492 om->stream(Warnings)
1493 << "*** ERROR *** OperatorTraits::Apply() [1]" << endl
1494 << "Operator applied to zero did not return zero." << endl;
1495 return false;
1496 }
1497 }
1498
1499 // If we send in a random matrix, we should not get a zero return
1500 MVT::MvRandom(*B);
1501 MVT::MvNorm(*B,normsB1);
1502 OPT::Apply(*M,*B,*C);
1503 MVT::MvNorm(*B,normsB2);
1504 MVT::MvNorm(*C,normsC2);
1505 bool ZeroWarning = false;
1506 for (int i=0; i<numvecs; i++) {
1507 if (STS::magnitude (normsB2[i] - normsB1[i]) > tol) {
1508 om->stream(Warnings)
1509 << "*** ERROR *** OperatorTraits::Apply() [2]" << endl
1510 << "Apply() modified the input vectors." << endl
1511 << "Original: " << normsB1[i] << "; After: " << normsB2[i] << endl
1512 << "Difference " << STS::magnitude (normsB2[i] - normsB1[i])
1513 << " exceeds the tolerance 100*eps = " << tol << endl;
1514 return false;
1515 }
1516 if (normsC2[i] == STS::zero() && ZeroWarning==false ) {
1517 om->stream(Warnings)
1518 << "*** ERROR *** OperatorTraits::Apply() [2]" << endl
1519 << "Operator applied to random vectors returned zero." << endl;
1520 ZeroWarning = true;
1521 }
1522 }
1523
1524 // Apply operator with C init'd to zero
1525 MVT::MvRandom(*B);
1526 MVT::MvNorm(*B,normsB1);
1527 MVT::MvInit(*C);
1528 OPT::Apply(*M,*B,*C);
1529 MVT::MvNorm(*B,normsB2);
1530 MVT::MvNorm(*C,normsC1);
1531 for (int i=0; i<numvecs; i++) {
1532 if (STS::magnitude (normsB2[i] - normsB1[i]) > tol) {
1533 om->stream(Warnings)
1534 << "*** ERROR *** OperatorTraits::Apply() [3]" << endl
1535 << "Apply() modified the input vectors." << endl
1536 << "Original: " << normsB1[i] << "; After: " << normsB2[i] << endl
1537 << "Difference " << STS::magnitude (normsB2[i] - normsB1[i])
1538 << " exceeds the tolerance 100*eps = " << tol << endl;
1539 return false;
1540 }
1541 }
1542
1543 // Apply operator with C init'd to random
1544 // Check that result is the same as before; warn if not.
1545 // This could be a result of a bug, or a stochastic
1546 // operator. We do not want to prejudice against a
1547 // stochastic operator.
1548 MVT::MvRandom(*C);
1549 OPT::Apply(*M,*B,*C);
1550 MVT::MvNorm(*B,normsB2);
1551 MVT::MvNorm(*C,normsC2);
1552 NonDeterministicWarning = false;
1553 for (int i=0; i<numvecs; i++) {
1554 if (STS::magnitude (normsB2[i] - normsB1[i]) > tol) {
1555 om->stream(Warnings)
1556 << "*** ERROR *** OperatorTraits::Apply() [4]" << endl
1557 << "Apply() modified the input vectors." << endl
1558 << "Original: " << normsB1[i] << "; After: " << normsB2[i] << endl
1559 << "Difference " << STS::magnitude (normsB2[i] - normsB1[i])
1560 << " exceeds the tolerance 100*eps = " << tol << endl;
1561 return false;
1562 }
1563 if (normsC1[i] != normsC2[i] && !NonDeterministicWarning) {
1564 om->stream(Warnings)
1565 << endl
1566 << "*** WARNING *** OperatorTraits::Apply() [4]" << endl
1567 << "Apply() returned two different results." << endl << endl;
1568 NonDeterministicWarning = true;
1569 }
1570 }
1571
1572 return true;
1573
1574 }
1575
1576 }
1577
1578 #endif