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 2011 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 
00104   if(Amap.GlobalIndicesInt())
00105   {
00106 #ifndef EPETRA_NO_32BIT_GLOBAL_INDICES
00107     int* rows = Amap.MyGlobalElements();
00108 
00109     Epetra_Util util;
00110 
00111     for(int i=0; i<numRows; ++i) {
00112       int row = rows[i];
00113       int rowLen = A.NumGlobalEntries(row);
00114       if (rowLen != B.NumGlobalEntries(row)) {
00115         return(false);
00116       }
00117 
00118       int* indices = new int[rowLen*2];
00119       int* Bindices = indices+rowLen;
00120 
00121       double* values = new double[rowLen*2];
00122       double* Bvalues = values+rowLen;
00123 
00124       A.ExtractGlobalRowCopy(row, rowLen, rowLen, values, indices);
00125       B.ExtractGlobalRowCopy(row, rowLen, rowLen, Bvalues, Bindices);
00126 
00127       util.Sort(true, rowLen, indices, 1, &values, 0, 0, 0, 0);
00128       util.Sort(true, rowLen, Bindices, 1, &Bvalues, 0, 0, 0, 0);
00129 
00130       bool same = true;
00131       for(int j=0; j<rowLen; ++j) {
00132         if (indices[j] != Bindices[j]) {
00133           same = false; break;
00134         }
00135         if (values[j] != Bvalues[j]) {
00136           same = false; break;
00137         }
00138       }
00139 
00140       delete [] indices;
00141       delete [] values;
00142 
00143       if (!same) {
00144         return(false);
00145       }
00146     }
00147 #else
00148     throw "compare_matrices: GlobalIndices int but not API for it.";
00149 #endif
00150   }
00151   else if(Amap.GlobalIndicesLongLong()) {
00152 #ifndef EPETRA_NO_64BIT_GLOBAL_INDICES
00153 
00154     long long* rows = Amap.MyGlobalElements64();
00155 
00156     Epetra_Util util;
00157 
00158     for(int i=0; i<numRows; ++i) {
00159       long long row = rows[i];
00160       int rowLen = A.NumGlobalEntries(row);
00161       if (rowLen != B.NumGlobalEntries(row)) {
00162         return(false);
00163       }
00164 
00165       long long* indices = new long long[rowLen*2];
00166       long long* Bindices = indices+rowLen;
00167 
00168       double* values = new double[rowLen*2];
00169       double* Bvalues = values+rowLen;
00170 
00171       A.ExtractGlobalRowCopy(row, rowLen, rowLen, values, indices);
00172       B.ExtractGlobalRowCopy(row, rowLen, rowLen, Bvalues, Bindices);
00173 
00174       util.Sort(true, rowLen, indices, 1, &values, 0, 0, 0, 0);
00175       util.Sort(true, rowLen, Bindices, 1, &Bvalues, 0, 0, 0, 0);
00176 
00177       bool same = true;
00178       for(int j=0; j<rowLen; ++j) {
00179         if (indices[j] != Bindices[j]) {
00180           same = false; break;
00181         }
00182         if (values[j] != Bvalues[j]) {
00183           same = false; break;
00184         }
00185       }
00186 
00187       delete [] indices;
00188       delete [] values;
00189 
00190       if (!same) {
00191         return(false);
00192       }
00193     }
00194 #else
00195     throw "compare_matrices: GlobalIndices long long but not API for it.";
00196 #endif
00197   }
00198   else {
00199     return(false);
00200   }
00201 
00202 
00203   return(true);
00204 }
00205 
00206 bool compare_matrices_LL(const Epetra_CrsMatrix& A, const Epetra_CrsMatrix& B)
00207 {
00208   const Epetra_Map& Amap = A.RowMap();
00209   const Epetra_Map& Bmap = B.RowMap();
00210 
00211   if (!Amap.PointSameAs(Bmap)) {
00212     return(false);
00213   }
00214 
00215   // FIXME (mfh 18 Apr 2014) Does the commented-out line below have
00216   // any side effects?  In any case, it was causing a build warning
00217   // due to numRows never being used.  btw, shouldn't a function that
00218   // claims to compare matrices actually look at the matrices???
00219 
00220   //int numRows = Amap.NumMyElements();
00221   (void) Amap.NumMyElements();
00222   return true;
00223 }
00224 
00225 }//namespace epetra_test
00226 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines