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