Epetra_test_functions.cpp

Go to the documentation of this file.
00001 
00002 //@HEADER
00003 // ************************************************************************
00004 //
00005 //               Epetra: Linear Algebra Services Package
00006 //                 Copyright (2001) Sandia Corporation
00007 //
00008 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00009 // license for use of this work by or on behalf of the U.S. Government.
00010 //
00011 // This library is free software; you can redistribute it and/or modify
00012 // it under the terms of the GNU Lesser General Public License as
00013 // published by the Free Software Foundation; either version 2.1 of the
00014 // License, or (at your option) any later version.
00015 //
00016 // This library is distributed in the hope that it will be useful, but
00017 // WITHOUT ANY WARRANTY; without even the implied warranty of
00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019 // Lesser General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License along with this library; if not, write to the Free Software
00023 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00024 // USA
00025 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00026 //
00027 // ************************************************************************
00028 //@HEADER
00029 
00030 #include <Epetra_Map.h>
00031 #include <Epetra_CrsMatrix.h>
00032 #include <Epetra_SerialComm.h>
00033 #include <Epetra_Util.h>
00034 
00035 #ifdef EPETRA_MPI
00036 #include <Epetra_MpiComm.h>
00037 #endif
00038 
00039 namespace epetra_test {
00040 
00041 bool global_check_for_flag_on_proc_0(const char* flag,
00042                                      int numargs,
00043                                      char** strargs,
00044                                      const Epetra_Comm& comm)
00045 {
00046   int mypid = comm.MyPID();
00047   int numprocs = comm.NumProc();
00048 
00049   int flag_found = 0;
00050   if (mypid==0) {
00051     for(int i=0; i<numargs; ++i) {
00052       if (strargs[i]==0) continue;
00053 
00054       if (strcmp(flag, strargs[i]) == 0) {
00055         flag_found = 1; break;
00056       }
00057     }
00058   }
00059 
00060   if (numprocs > 1) {
00061     comm.Broadcast(&flag_found, 1, 0);
00062   }
00063 
00064   bool return_value = flag_found==1 ? true : false;
00065 
00066   return( return_value );
00067 }
00068 
00069 Epetra_Comm* create_comm(int argc, char** argv)
00070 {
00071 #ifdef EPETRA_MPI
00072   MPI_Init(&argc, &argv);
00073 
00074   return( new Epetra_MpiComm(MPI_COMM_WORLD) );
00075 #else
00076   return( new Epetra_SerialComm );
00077 #endif
00078 }
00079 
00080 bool compare_matrices(const Epetra_CrsMatrix& A, const Epetra_CrsMatrix& B)
00081 {
00082   const Epetra_Map& Amap = A.RowMap();
00083   const Epetra_Map& Bmap = B.RowMap();
00084 
00085   if (!Amap.PointSameAs(Bmap)) {
00086     return(false);
00087   }
00088 
00089   int numRows = Amap.NumMyElements();
00090   int* rows = Amap.MyGlobalElements();
00091 
00092   Epetra_Util util;
00093 
00094   for(int i=0; i<numRows; ++i) {
00095     int row = rows[i];
00096     int rowLen = A.NumGlobalEntries(row);
00097     if (rowLen != B.NumGlobalEntries(row)) {
00098       return(false);
00099     }
00100 
00101     int* indices = new int[rowLen*2];
00102     int* Bindices = indices+rowLen;
00103 
00104     double* values = new double[rowLen*2];
00105     double* Bvalues = values+rowLen;
00106 
00107     A.ExtractGlobalRowCopy(row, rowLen, rowLen, values, indices);
00108     B.ExtractGlobalRowCopy(row, rowLen, rowLen, Bvalues, Bindices);
00109 
00110     util.Sort(true, rowLen, indices, 1, &values, 0, 0);
00111     util.Sort(true, rowLen, Bindices, 1, &Bvalues, 0, 0);
00112 
00113     bool same = true;
00114     for(int j=0; j<rowLen; ++j) {
00115       if (indices[j] != Bindices[j]) {
00116         same = false; break;
00117       }
00118       if (values[j] != Bvalues[j]) {
00119         same = false; break;
00120       }
00121     }
00122 
00123     delete [] indices;
00124     delete [] values;
00125 
00126     if (!same) {
00127       return(false);
00128     }
00129   }
00130 
00131   return(true);
00132 }
00133 
00134 }//namespace epetra_test
00135 

Generated on Wed May 12 21:41:05 2010 for Epetra Package Browser (Single Doxygen Collection) by  doxygen 1.4.7