EpetraExt_Zoltan_CrsGraph.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 #include <EpetraExt_Zoltan_CrsGraph.h>
00030 
00031 #include <EpetraExt_Transpose_CrsGraph.h>
00032 
00033 #include <EpetraExt_ZoltanQuery.h>
00034 #include <Zoltan_LoadBalance.h>
00035 
00036 #include <Epetra_CrsGraph.h>
00037 #include <Epetra_Map.h>
00038 #include <Epetra_Import.h>
00039 
00040 #include <Epetra_MpiComm.h>
00041 
00042 #include <vector>
00043 #include <set>
00044 
00045 using std::vector;
00046 using std::set;
00047 
00048 namespace EpetraExt {
00049 
00050 Zoltan_CrsGraph::
00051 ~Zoltan_CrsGraph()
00052 {
00053   if( newObj_ ) delete newObj_;
00054 
00055   if( NewRowMap_ ) delete NewRowMap_;
00056 }
00057 
00058 Zoltan_CrsGraph::NewTypeRef
00059 Zoltan_CrsGraph::
00060 operator()( OriginalTypeRef orig )
00061 {
00062   origObj_ = &orig;
00063 
00064   int err;
00065 
00066   //Setup Load Balance Object
00067   float version;
00068   char * dummy = 0;
00069   Zoltan::LoadBalance LB( 0, &dummy, &version );
00070   err = LB.Create( dynamic_cast<const Epetra_MpiComm&>(orig.Comm()).Comm() );
00071   if( err == ZOLTAN_OK ) err = LB.Set_Param( "LB_METHOD", "PARMETIS" );
00072   if( err == ZOLTAN_OK ) err = LB.Set_Param( "PARMETIS_METHOD", partitionMethod_ );
00073 
00074   //Setup Query Object
00075   CrsGraph_Transpose transposeTransform;
00076   Epetra_CrsGraph & TransGraph = transposeTransform( orig );
00077   ZoltanQuery Query( orig, &TransGraph );
00078   if( err == ZOLTAN_OK ) err = LB.Set_QueryObject( &Query );
00079 
00080   if( err != ZOLTAN_OK )
00081   { cout << "Setup of Zoltan Load Balancing Objects FAILED!\n"; exit(0); }
00082 
00083   //Generate Load Balance
00084   int changes;
00085   int num_gid_entries, num_lid_entries;
00086   int num_import;
00087   ZOLTAN_ID_PTR import_global_ids, import_local_ids;
00088   int * import_procs;
00089   int num_export;
00090   ZOLTAN_ID_PTR export_global_ids, export_local_ids;
00091   int * export_procs;
00092 
00093 //  orig.Comm().Barrier();
00094 //  err = LB.Generate_Files( "zoltan_output" );
00095   orig.Comm().Barrier();
00096   err = LB.Balance( &changes,
00097                      &num_gid_entries, &num_lid_entries,
00098                      &num_import, &import_global_ids, &import_local_ids, &import_procs,
00099                      &num_export, &export_global_ids, &export_local_ids, &export_procs );
00100   LB.Evaluate( 1, 0, 0, 0, 0, 0, 0 );
00101   orig.Comm().Barrier();
00102 
00103   //Generate New Element List
00104   int numMyElements = orig.RowMap().NumMyElements();
00105   vector<int> elementList( numMyElements );
00106   orig.RowMap().MyGlobalElements( &elementList[0] );
00107 
00108   int newNumMyElements = numMyElements - num_export + num_import;
00109   vector<int> newElementList( newNumMyElements );
00110 
00111   set<int> gidSet;
00112   for( int i = 0; i < num_export; ++i )
00113     gidSet.insert( export_global_ids[i] );
00114 
00115   //Add unmoved indices to new list
00116   int loc = 0;
00117   for( int i = 0; i < numMyElements; ++i )
00118     if( !gidSet.count( elementList[i] ) )
00119       newElementList[loc++] = elementList[i];
00120   
00121   //Add imports to end of list
00122   for( int i = 0; i < num_import; ++i )
00123     newElementList[loc+i] = import_global_ids[i];
00124 
00125   //Free Zoltan Data
00126   if( err == ZOLTAN_OK )
00127     err = LB.Free_Data( &import_global_ids, &import_local_ids, &import_procs,
00128                          &export_global_ids, &export_local_ids, &export_procs );
00129 
00130   //Create Import Map
00131   NewRowMap_ = new Epetra_Map( orig.RowMap().NumGlobalElements(),
00132                                            newNumMyElements,
00133                                            &newElementList[0],
00134                                            orig.RowMap().IndexBase(),
00135                                            orig.RowMap().Comm() );
00136 
00137   //Create Importer
00138   Epetra_Import Importer( *NewRowMap_, orig.RowMap() );
00139 
00140   //Create New Graph
00141   Epetra_CrsGraph * NewGraph = new Epetra_CrsGraph( Copy, *NewRowMap_, 0 );
00142   NewGraph->Import( orig, Importer, Insert );
00143   NewGraph->FillComplete();
00144 
00145   Zoltan::LoadBalance LB2( 0, &dummy, &version );
00146   err = LB2.Create( dynamic_cast<const Epetra_MpiComm&>(orig.Comm()).Comm() );
00147   if( err == ZOLTAN_OK ) err = LB2.Set_Param( "LB_METHOD", "PARMETIS" );
00148   if( err == ZOLTAN_OK ) err = LB2.Set_Param( "PARMETIS_METHOD", partitionMethod_ );
00149   CrsGraph_Transpose transTrans;
00150   Epetra_CrsGraph & trans2 = transTrans( *NewGraph );
00151   ZoltanQuery query( *NewGraph, &trans2 );
00152   if( err == ZOLTAN_OK ) err = LB2.Set_QueryObject( &query );
00153   err = LB2.Balance( &changes,
00154                      &num_gid_entries, &num_lid_entries,
00155                      &num_import, &import_global_ids, &import_local_ids, &import_procs,
00156                      &num_export, &export_global_ids, &export_local_ids, &export_procs );
00157   LB2.Evaluate( 1, 0, 0, 0, 0, 0, 0 );
00158 
00159   newObj_ = NewGraph;
00160 
00161   return *NewGraph;
00162 }
00163 
00164 } // namespace EpetraExt
00165 

Generated on Thu Sep 18 12:31:44 2008 for EpetraExt by doxygen 1.3.9.1