00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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
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
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
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.Balance( &changes,
00095 &num_gid_entries, &num_lid_entries,
00096 &num_import, &import_global_ids, &import_local_ids, &import_procs,
00097 &num_export, &export_global_ids, &export_local_ids, &export_procs );
00098 LB.Evaluate( 1, 0, 0, 0, 0, 0, 0 );
00099 orig.Comm().Barrier();
00100
00101
00102 int numMyElements = orig.RowMap().NumMyElements();
00103 vector<int> elementList( numMyElements );
00104 orig.RowMap().MyGlobalElements( &elementList[0] );
00105
00106 int newNumMyElements = numMyElements - num_export + num_import;
00107 vector<int> newElementList( newNumMyElements );
00108
00109 set<int> gidSet;
00110 for( int i = 0; i < num_export; ++i )
00111 gidSet.insert( export_global_ids[i] );
00112
00113
00114 int loc = 0;
00115 for( int i = 0; i < numMyElements; ++i )
00116 if( !gidSet.count( elementList[i] ) )
00117 newElementList[loc++] = elementList[i];
00118
00119
00120 for( int i = 0; i < num_import; ++i )
00121 newElementList[loc+i] = import_global_ids[i];
00122
00123
00124 if( err == ZOLTAN_OK )
00125 err = LB.Free_Data( &import_global_ids, &import_local_ids, &import_procs,
00126 &export_global_ids, &export_local_ids, &export_procs );
00127
00128
00129 NewRowMap_ = new Epetra_Map( orig.RowMap().NumGlobalElements(),
00130 newNumMyElements,
00131 &newElementList[0],
00132 orig.RowMap().IndexBase(),
00133 orig.RowMap().Comm() );
00134
00135
00136 Epetra_Import Importer( *NewRowMap_, orig.RowMap() );
00137
00138
00139 Epetra_CrsGraph * NewGraph = new Epetra_CrsGraph( Copy, *NewRowMap_, 0 );
00140 NewGraph->Import( orig, Importer, Insert );
00141 NewGraph->FillComplete();
00142
00143 Zoltan::LoadBalance LB2( 0, &dummy, &version );
00144 err = LB2.Create( dynamic_cast<const Epetra_MpiComm&>(orig.Comm()).Comm() );
00145 if( err == ZOLTAN_OK ) err = LB2.Set_Param( "LB_METHOD", "PARMETIS" );
00146 if( err == ZOLTAN_OK ) err = LB2.Set_Param( "PARMETIS_METHOD", partitionMethod_ );
00147 CrsGraph_Transpose transTrans;
00148 Epetra_CrsGraph & trans2 = transTrans( *NewGraph );
00149 ZoltanQuery query( *NewGraph, &trans2 );
00150 if( err == ZOLTAN_OK ) err = LB2.Set_QueryObject( &query );
00151
00152
00153
00154
00155 LB2.Evaluate( 1, 0, 0, 0, 0, 0, 0 );
00156
00157 newObj_ = NewGraph;
00158
00159 return *NewGraph;
00160 }
00161
00162 }
00163