Epetra Package Browser (Single Doxygen Collection) Development
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 the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00009 // the U.S. Government retains certain rights in this software.
00010 //
00011 // Redistribution and use in source and binary forms, with or without
00012 // modification, are permitted provided that the following conditions are
00013 // met:
00014 //
00015 // 1. Redistributions of source code must retain the above copyright
00016 // notice, this list of conditions and the following disclaimer.
00017 //
00018 // 2. Redistributions in binary form must reproduce the above copyright
00019 // notice, this list of conditions and the following disclaimer in the
00020 // documentation and/or other materials provided with the distribution.
00021 //
00022 // 3. Neither the name of the Corporation nor the names of the
00023 // contributors may be used to endorse or promote products derived from
00024 // this software without specific prior written permission.
00025 //
00026 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00027 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00029 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00030 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00031 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00032 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00033 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00034 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00035 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00036 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037 //
00038 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 
00039 // 
00040 // ************************************************************************
00041 //@HEADER
00042 
00043 #include <Epetra_Map.h>
00044 #include <Epetra_CrsMatrix.h>
00045 #include <Epetra_SerialComm.h>
00046 #include <Epetra_Util.h>
00047 
00048 #ifdef EPETRA_MPI
00049 #include <Epetra_MpiComm.h>
00050 #endif
00051 
00052 namespace epetra_test {
00053 
00054 bool global_check_for_flag_on_proc_0(const char* flag,
00055                                      int numargs,
00056                                      char** strargs,
00057                                      const Epetra_Comm& comm)
00058 {
00059   int mypid = comm.MyPID();
00060   int numprocs = comm.NumProc();
00061 
00062   int flag_found = 0;
00063   if (mypid==0) {
00064     for(int i=0; i<numargs; ++i) {
00065       if (strargs[i]==0) continue;
00066 
00067       if (strcmp(flag, strargs[i]) == 0) {
00068         flag_found = 1; break;
00069       }
00070     }
00071   }
00072 
00073   if (numprocs > 1) {
00074     comm.Broadcast(&flag_found, 1, 0);
00075   }
00076 
00077   bool return_value = flag_found==1 ? true : false;
00078 
00079   return( return_value );
00080 }
00081 
00082 Epetra_Comm* create_comm(int argc, char** argv)
00083 {
00084 #ifdef EPETRA_MPI
00085   MPI_Init(&argc, &argv);
00086 
00087   return( new Epetra_MpiComm(MPI_COMM_WORLD) );
00088 #else
00089   return( new Epetra_SerialComm );
00090 #endif
00091 }
00092 
00093 bool compare_matrices(const Epetra_CrsMatrix& A, const Epetra_CrsMatrix& B)
00094 {
00095   const Epetra_Map& Amap = A.RowMap();
00096   const Epetra_Map& Bmap = B.RowMap();
00097 
00098   if (!Amap.PointSameAs(Bmap)) {
00099     return(false);
00100   }
00101 
00102   int numRows = Amap.NumMyElements();
00103   int* rows = Amap.MyGlobalElements();
00104 
00105   Epetra_Util util;
00106 
00107   for(int i=0; i<numRows; ++i) {
00108     int row = rows[i];
00109     int rowLen = A.NumGlobalEntries(row);
00110     if (rowLen != B.NumGlobalEntries(row)) {
00111       return(false);
00112     }
00113 
00114     int* indices = new int[rowLen*2];
00115     int* Bindices = indices+rowLen;
00116 
00117     double* values = new double[rowLen*2];
00118     double* Bvalues = values+rowLen;
00119 
00120     A.ExtractGlobalRowCopy(row, rowLen, rowLen, values, indices);
00121     B.ExtractGlobalRowCopy(row, rowLen, rowLen, Bvalues, Bindices);
00122 
00123     util.Sort(true, rowLen, indices, 1, &values, 0, 0);
00124     util.Sort(true, rowLen, Bindices, 1, &Bvalues, 0, 0);
00125 
00126     bool same = true;
00127     for(int j=0; j<rowLen; ++j) {
00128       if (indices[j] != Bindices[j]) {
00129         same = false; break;
00130       }
00131       if (values[j] != Bvalues[j]) {
00132         same = false; break;
00133       }
00134     }
00135 
00136     delete [] indices;
00137     delete [] values;
00138 
00139     if (!same) {
00140       return(false);
00141     }
00142   }
00143 
00144   return(true);
00145 }
00146 
00147 }//namespace epetra_test
00148 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines