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