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