EpetraExt Package Browser (Single Doxygen Collection) Development
test/Zoltan/cxx_main.cpp
Go to the documentation of this file.
00001 //@HEADER
00002 // ***********************************************************************
00003 // 
00004 //     EpetraExt: Epetra Extended - Linear Algebra Services Package
00005 //                 Copyright (2001) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 // 
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 
00025 // 
00026 // ***********************************************************************
00027 //@HEADER
00028 
00029 // AMD Test routine
00030 #include <EpetraExt_ConfigDefs.h>
00031 #include "EpetraExt_Version.h"
00032 
00033 #ifdef HAVE_MPI
00034 #include "Epetra_MpiComm.h"
00035 #include <mpi.h>
00036 #endif
00037 #include "Epetra_SerialComm.h"
00038 #include "Epetra_Time.h"
00039 #include "Epetra_BlockMap.h"
00040 #include "Epetra_CrsGraph.h"
00041 #include "Epetra_CrsMatrix.h"
00042 #include "Epetra_Vector.h"
00043 #include "Epetra_LinearProblem.h"
00044 
00045 #include "Trilinos_Util.h"
00046 
00047 #include "EpetraExt_Zoltan_CrsGraph.h"
00048 #include "EpetraExt_LPTrans_From_GraphTrans.h"
00049 #include "../epetra_test_err.h"
00050 
00051 #ifdef HAVE_EXPERIMENTAL
00052 #include "EpetraExt_AMD_CrsGraph.h"
00053 #endif
00054 
00055 #include "Teuchos_RCP.hpp"
00056 
00057 #define perror(str) { fprintf(stderr,"%s\n",str);  exit(-1); }
00058 #define perror1(str,ierr) { fprintf(stderr,"%s %d\n",str,ierr);  exit(ierr); }
00059 
00060 int main(int argc, char *argv[]) {
00061 
00062   int i, ierr=0, returnierr=0;
00063 
00064 #ifdef HAVE_MPI
00065 
00066   // Initialize MPI
00067 
00068   MPI_Init(&argc,&argv);
00069   int size, rank; // Number of MPI processes, My process ID
00070 
00071   MPI_Comm_size(MPI_COMM_WORLD, &size);
00072   MPI_Comm_rank(MPI_COMM_WORLD, &rank);
00073 
00074 #else
00075 
00076   int size = 1; // Serial case (not using MPI)
00077   int rank = 0;
00078 
00079 #endif
00080 
00081   bool verbose = true;
00082   bool failure = false;
00083 
00084 #ifdef HAVE_MPI
00085   Epetra_MpiComm Comm(MPI_COMM_WORLD);
00086 #else
00087   Epetra_SerialComm Comm;
00088 #endif
00089   
00090   int MyPID = Comm.MyPID();
00091   int NumProc = Comm.NumProc();
00092 
00093   bool verbose_all = verbose;
00094 
00095   if (verbose) verbose = (MyPID==0);
00096 
00097   if (verbose) cout << EpetraExt::EpetraExt_Version() << endl << endl;
00098 
00099   //Read in Matrix File and distribute
00100   int NumGlobalEqs;
00101   int NumLocalEqs;
00102   int NumNZs;
00103   int *NumRowNZs;
00104   double *Values;
00105   double *Xprime;
00106   double *B;
00107   double *X;
00108   int *Bindx;
00109   int NUpdate;
00110   int *Update;
00111 
00112   Trilinos_Util_read_hb(argv[1], Comm.MyPID(), &NumGlobalEqs, &NumNZs, &Values, &Bindx, &Xprime, &B, &X );
00113   Trilinos_Util_distrib_msr_matrix( Comm, &NumGlobalEqs, &NumNZs, &NUpdate, &Update, &Values, &Bindx, &Xprime, &B, &X );
00114 
00115   NumLocalEqs = NUpdate;
00116   NumRowNZs = new int[NumLocalEqs];
00117   for( int i = 0; i < NumLocalEqs; ++i ) NumRowNZs[i] = Bindx[i+1]-Bindx[i]+1;
00118 
00119   Epetra_Map Map(NumGlobalEqs,NumLocalEqs,Update,0,Comm);
00120 
00121   if( verbose ) cout << endl << "Building Epetra_CrsMatrix" << endl;
00122 
00123   Epetra_CrsMatrix A(Copy, Map, NumRowNZs );
00124 
00125   //Add individual rows
00126   double *RowVals;
00127   int *ColInds;
00128   for( int i = 0; i < NumLocalEqs; ++i )
00129   {
00130     RowVals = Values + Bindx[i];
00131     ColInds = Bindx + Bindx[i];
00132     int NumEntries = Bindx[i+1] - Bindx[i];
00133     ierr = A.InsertGlobalValues( Update[i], NumEntries, RowVals, ColInds );
00134     if( ierr ) { printf("Row %d:", Update[i] ); perror1("Error Putting Row: ",ierr); }
00135     ierr = A.InsertGlobalValues( Update[i], 1, Values+i, Update+i);
00136     if( ierr ) { perror1("Error Putting Diag: ",ierr); }
00137   }
00138 
00139   ierr = A.FillComplete();
00140   if( ierr ) perror1("Error in FillComplete",ierr);
00141 
00142   Epetra_Vector XX(Copy,Map,X);
00143   Epetra_Vector BB(Copy,Map,B);
00144 
00145   Epetra_LinearProblem Prob(&A,&XX,&BB);
00146 
00147   // Generate Zoltan Load Balanced Version of Linear Problem
00148   if( verbose ) cout << "Creating Zoltan Partitioning Transform!\n";
00149 
00150   Teuchos::RCP<EpetraExt::Zoltan_CrsGraph> ZoltanTrans = Teuchos::rcp( new EpetraExt::Zoltan_CrsGraph() );
00151   if (ZoltanTrans==Teuchos::null) failure = true;
00152 
00153   Teuchos::RCP<EpetraExt::LinearProblem_GraphTrans> ZoltanLPTrans =
00154     Teuchos::rcp( new EpetraExt::LinearProblem_GraphTrans( 
00155          *(dynamic_cast<EpetraExt::StructuralSameTypeTransform<Epetra_CrsGraph>*>(&*ZoltanTrans)) ) );
00156 
00157   if( verbose ) cout << "Creating Load Balanced Linear Problem\n";
00158   Epetra_LinearProblem &BalancedProb = (*ZoltanLPTrans)(Prob);
00159 
00160   // Running this transform fwd() and rvs()
00161   failure = failure || !ZoltanLPTrans->fwd();
00162   failure = failure || !ZoltanLPTrans->rvs();
00163 
00164 #ifdef HAVE_EXPERIMENTAL
00165 
00166   Teuchos::RCP<EpetraExt::CrsGraph_AMD> AMDTrans = Teuchos::rcp( new EpetraExt::CrsGraph_AMD() );
00167   if (AMDTrans==Teuchos::null) failure = true;
00168 
00169   Teuchos::RCP<EpetraExt::LinearProblem_GraphTrans> AMDLPTrans =
00170     Teuchos::rcp( new EpetraExt::LinearProblem_GraphTrans( 
00171          *(dynamic_cast<EpetraExt::StructuralSameTypeTransform<Epetra_CrsGraph>*>(&*AMDTrans)) ) );
00172 
00173   if( verbose ) cout << "Creating AMD Linear Problem\n";
00174   Epetra_LinearProblem &AMDProb = (*AMDLPTrans)(BalancedProb);
00175 
00176   // Running this transform fwd() and rvs()
00177   failure = failure || !AMDLPTrans->fwd();
00178   failure = failure || !AMDLPTrans->rvs();
00179 
00180 #endif
00181  
00182   // Clean up
00183   if (NumRowNZs) delete [] NumRowNZs;
00184   if (Values) free(Values);
00185   if (Bindx) free(Bindx);
00186   if (Xprime) free(Xprime);
00187   if (B) free(B);
00188   if (X) free(X);
00189   if (Update) free(Update);
00190 
00191   Comm.Barrier();
00192 
00193   if ( ierr != 0 || failure ) {
00194     if (verbose)
00195       std::cout << "End Result: TEST FAILED" << std::endl;
00196     return -1;
00197   }
00198   //
00199   // Default return value
00200   //
00201   if (verbose)
00202     std::cout << "End Result: TEST PASSED" << std::endl;
00203   return 0;
00204 
00205 #ifdef HAVE_MPI
00206   MPI_Finalize();
00207 #endif
00208 }
00209 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines