Amesos Package Browser (Single Doxygen Collection) Development
Test_UMFPACK/cxx_main.cpp
Go to the documentation of this file.
00001 #include "Amesos_ConfigDefs.h"
00002 
00003 #ifdef HAVE_MPI
00004 #include "mpi.h"
00005 #include "Epetra_MpiComm.h"
00006 #else
00007 #include "Epetra_SerialComm.h"
00008 #endif
00009 #include "Epetra_Map.h"
00010 #include "Epetra_Vector.h"
00011 #include "Epetra_Util.h"
00012 #include "Epetra_CrsMatrix.h"
00013 #include "Amesos_Umfpack.h"
00014 #include "Amesos_TestRowMatrix.h"
00015 #include "Teuchos_ParameterList.hpp"
00016 
00017 //=============================================================================
00018 bool CheckError(const Epetra_RowMatrix& A,
00019     const Epetra_MultiVector& x,
00020     const Epetra_MultiVector& b,
00021     const Epetra_MultiVector& x_exact)
00022 {
00023   std::vector<double> Norm;
00024   int NumVectors = x.NumVectors();
00025   Norm.resize(NumVectors);
00026   Epetra_MultiVector Ax(x);
00027   A.Multiply(false,x,Ax);
00028   Ax.Update(1.0,b,-1.0);
00029   Ax.Norm2(&Norm[0]);
00030   bool TestPassed = false;
00031   double TotalNorm = 0.0;
00032   for (int i = 0 ; i < NumVectors ; ++i) {
00033     TotalNorm += Norm[i];
00034   }
00035   if (A.Comm().MyPID() == 0)
00036     std::cout << "||Ax - b||  = " << TotalNorm << std::endl;
00037   if (TotalNorm < 1e-5 )
00038     TestPassed = true;
00039   else
00040     TestPassed = false;
00041 
00042   Ax.Update (1.0,x,-1.0,x_exact,0.0);
00043   Ax.Norm2(&Norm[0]);
00044   for (int i = 0 ; i < NumVectors ; ++i) {
00045     TotalNorm += Norm[i];
00046   }
00047   if (A.Comm().MyPID() == 0)
00048     std::cout << "||Ax - b||  = " << TotalNorm << std::endl;
00049   if (TotalNorm < 1e-5 )
00050     TestPassed = true;
00051   else
00052     TestPassed = false;
00053 
00054   return(TestPassed);
00055 }
00056 
00057 //=============================================================================
00058 int main(int argc, char *argv[]) {
00059 
00060 #ifdef HAVE_MPI
00061   MPI_Init(&argc, &argv);
00062   Epetra_MpiComm Comm(MPI_COMM_WORLD);
00063 #else
00064   Epetra_SerialComm Comm;
00065 #endif
00066 
00067   int NumGlobalElements = 1000;
00068   int NumVectors = 7;
00069   
00070   // =================== //
00071   // create a random map //
00072   // =================== //
00073  
00074   int* part = new int[NumGlobalElements];
00075 
00076   if (Comm.MyPID() == 0) 
00077   {
00078     Epetra_Util Util;
00079 
00080     for( int i=0 ; i<NumGlobalElements ; ++i ) 
00081     {
00082       unsigned int r = Util.RandomInt();  
00083       part[i] = r%(Comm.NumProc());
00084     }
00085   }
00086 
00087   Comm.Broadcast(part,NumGlobalElements,0);
00088 
00089   // count the elements assigned to this proc
00090   int NumMyElements = 0;
00091   for (int i = 0 ; i < NumGlobalElements ; ++i) 
00092   {
00093     if (part[i] == Comm.MyPID()) 
00094       NumMyElements++;
00095   }
00096 
00097   // get the loc2global list
00098   int* MyGlobalElements = new int[NumMyElements];
00099   int count = 0;
00100   for (int i = 0 ; i < NumGlobalElements ; ++i) 
00101   {
00102     if (part[i] == Comm.MyPID() ) 
00103       MyGlobalElements[count++] = i;
00104   }
00105 
00106   Epetra_Map Map(NumGlobalElements,NumMyElements,MyGlobalElements,
00107      0,Comm);
00108 
00109   delete [] part;
00110 
00111   // ===================== //
00112   // Create a dense matrix //
00113   // ===================== //
00114  
00115   Epetra_CrsMatrix Matrix(Copy,Map,NumGlobalElements);
00116 
00117   int* Indices = new int[NumGlobalElements];
00118   double* Values = new double[NumGlobalElements];
00119 
00120   for (int i = 0 ; i < NumGlobalElements ; ++i) 
00121     Indices[i] = i;
00122 
00123   for (int i = 0 ; i < NumMyElements ; ++i) {
00124     int iGlobal = MyGlobalElements[i];
00125     for (int jGlobal = 0 ; jGlobal < NumGlobalElements ; ++jGlobal) {
00126       if (iGlobal == jGlobal) 
00127   Values[jGlobal] = 1.0 * (NumGlobalElements + 1 ) *
00128     (NumGlobalElements + 1);
00129       else if (iGlobal > jGlobal)
00130   Values[jGlobal] = -1.0*(jGlobal+1);
00131       else
00132   Values[jGlobal] = 1.0*(iGlobal+1);
00133     }
00134     Matrix.InsertGlobalValues(MyGlobalElements[i],
00135                               NumGlobalElements, Values, Indices);
00136 
00137   }
00138 
00139   Matrix.FillComplete();
00140 
00141   delete[] MyGlobalElements;
00142   delete[] Indices;
00143   delete[] Values;
00144  
00145   // ======================== //
00146   // other data for this test //
00147   // ======================== //
00148 
00149   Amesos_TestRowMatrix A(&Matrix);
00150   Epetra_MultiVector x(Map,NumVectors);
00151   Epetra_MultiVector x_exact(Map,NumVectors);
00152   Epetra_MultiVector b(Map,NumVectors);
00153   x_exact.Random();
00154   A.Multiply(false,x_exact,b);
00155 
00156   // =========== //
00157   // AMESOS PART //
00158   // =========== //
00159 
00160   Epetra_LinearProblem Problem(&A, &x, &b);
00161   Amesos_Umfpack Solver(Problem);
00162 
00163   AMESOS_CHK_ERR(Solver.SymbolicFactorization());
00164   AMESOS_CHK_ERR(Solver.NumericFactorization());
00165   AMESOS_CHK_ERR(Solver.Solve());
00166 
00167   if (!CheckError(A,x,b,x_exact))
00168     AMESOS_CHK_ERR(-1);
00169 
00170 #ifdef HAVE_MPI
00171   MPI_Finalize();
00172 #endif
00173   return(EXIT_SUCCESS);
00174 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines