EpetraExt Development
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 (2011) Sandia Corporation
00006 //
00007 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00008 // the U.S. Government retains certain rights in this software.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00038 //
00039 // ***********************************************************************
00040 //@HEADER
00041 
00042 #include "EpetraExt_config.h"
00043 
00044 #include <EpetraExt_Zoltan_CrsGraph.h>
00045 
00046 #include <EpetraExt_Transpose_CrsGraph.h>
00047 
00048 #include <EpetraExt_ZoltanQuery.h>
00049 #include <Zoltan_LoadBalance.h>
00050 
00051 #include <Epetra_CrsGraph.h>
00052 #include <Epetra_Map.h>
00053 #include <Epetra_Import.h>
00054 
00055 #include <Epetra_MpiComm.h>
00056 
00057 #include <vector>
00058 #include <set>
00059 
00060 using std::vector;
00061 using std::set;
00062 
00063 namespace EpetraExt {
00064 
00065 EPETRAEXT_DEPRECATED
00066 Zoltan_CrsGraph::
00067 ~Zoltan_CrsGraph()
00068 {
00069   if( newObj_ ) delete newObj_;
00070 
00071   if( NewRowMap_ ) delete NewRowMap_;
00072 }
00073 
00074 EPETRAEXT_DEPRECATED
00075 Zoltan_CrsGraph::NewTypeRef
00076 Zoltan_CrsGraph::
00077 operator()( OriginalTypeRef orig )
00078 {
00079   origObj_ = &orig;
00080 
00081   int err;
00082 
00083   //Setup Load Balance Object
00084   float version;
00085   char * dummy = 0;
00086   Zoltan::LoadBalance LB( 0, &dummy, &version );
00087   err = LB.Create( dynamic_cast<const Epetra_MpiComm&>(orig.Comm()).Comm() );
00088   if( err == ZOLTAN_OK ) err = LB.Set_Param( "LB_METHOD", "GRAPH" );
00089 #ifdef HAVE_LIBPARMETIS
00090   if( err == ZOLTAN_OK ) err = LB.Set_Param( "GRAPH_PACKAGE", "PARMETIS" );
00091   if( err == ZOLTAN_OK ) err = LB.Set_Param( "PARMETIS_METHOD", partitionMethod_ );
00092 #endif
00093 
00094   //Setup Query Object
00095   CrsGraph_Transpose transposeTransform;
00096   Epetra_CrsGraph & TransGraph = transposeTransform( orig );
00097   ZoltanQuery Query( orig, &TransGraph );
00098   if( err == ZOLTAN_OK ) err = LB.Set_QueryObject( &Query );
00099 
00100   if( err != ZOLTAN_OK )
00101   { cout << "Setup of Zoltan Load Balancing Objects FAILED!\n"; exit(0); }
00102 
00103   //Generate Load Balance
00104   int changes;
00105   int num_gid_entries, num_lid_entries;
00106   int num_import;
00107   ZOLTAN_ID_PTR import_global_ids, import_local_ids;
00108   int * import_procs;
00109   int num_export;
00110   ZOLTAN_ID_PTR export_global_ids, export_local_ids;
00111   int * export_procs;
00112 
00113   orig.Comm().Barrier();
00114   err = LB.Balance( &changes,
00115                      &num_gid_entries, &num_lid_entries,
00116                      &num_import, &import_global_ids, &import_local_ids, &import_procs,
00117                      &num_export, &export_global_ids, &export_local_ids, &export_procs );
00118   LB.Evaluate( 1, 0, 0, 0, 0, 0, 0 );
00119   orig.Comm().Barrier();
00120 
00121   //Generate New Element List
00122   int numMyElements = orig.RowMap().NumMyElements();
00123   vector<int> elementList( numMyElements );
00124   orig.RowMap().MyGlobalElements( &elementList[0] );
00125 
00126   int newNumMyElements = numMyElements - num_export + num_import;
00127   vector<int> newElementList( newNumMyElements );
00128 
00129   set<int> gidSet;
00130   for( int i = 0; i < num_export; ++i )
00131     gidSet.insert( export_global_ids[i] );
00132 
00133   //Add unmoved indices to new list
00134   int loc = 0;
00135   for( int i = 0; i < numMyElements; ++i )
00136     if( !gidSet.count( elementList[i] ) )
00137       newElementList[loc++] = elementList[i];
00138   
00139   //Add imports to end of list
00140   for( int i = 0; i < num_import; ++i )
00141     newElementList[loc+i] = import_global_ids[i];
00142 
00143   //Free Zoltan Data
00144   if( err == ZOLTAN_OK )
00145     err = LB.Free_Data( &import_global_ids, &import_local_ids, &import_procs,
00146                          &export_global_ids, &export_local_ids, &export_procs );
00147 
00148   //Create Import Map
00149   NewRowMap_ = new Epetra_Map( orig.RowMap().NumGlobalElements(),
00150                                            newNumMyElements,
00151                                            &newElementList[0],
00152                                            orig.RowMap().IndexBase(),
00153                                            orig.RowMap().Comm() );
00154 
00155   //Create Importer
00156   Epetra_Import Importer( *NewRowMap_, orig.RowMap() );
00157 
00158   //Create New Graph
00159   Epetra_CrsGraph * NewGraph = new Epetra_CrsGraph( Copy, *NewRowMap_, 0 );
00160   NewGraph->Import( orig, Importer, Insert );
00161   NewGraph->FillComplete();
00162 
00163   Zoltan::LoadBalance LB2( 0, &dummy, &version );
00164   err = LB2.Create( dynamic_cast<const Epetra_MpiComm&>(orig.Comm()).Comm() );
00165   if( err == ZOLTAN_OK ) err = LB2.Set_Param( "LB_METHOD", "GRAPH" );
00166 #ifdef HAVE_LIBPARMETIS
00167   if( err == ZOLTAN_OK ) err = LB2.Set_Param( "GRAPH_PACKAGE", "PARMETIS" );
00168   if( err == ZOLTAN_OK ) err = LB2.Set_Param( "PARMETIS_METHOD", partitionMethod_ );
00169 #endif
00170   CrsGraph_Transpose transTrans;
00171   Epetra_CrsGraph & trans2 = transTrans( *NewGraph );
00172   ZoltanQuery query( *NewGraph, &trans2 );
00173   if( err == ZOLTAN_OK ) err = LB2.Set_QueryObject( &query );
00174   //err = LB2.Balance( &changes,
00175   //                   &num_gid_entries, &num_lid_entries,
00176   //                   &num_import, &import_global_ids, &import_local_ids, &import_procs,
00177   //                   &num_export, &export_global_ids, &export_local_ids, &export_procs );
00178   LB2.Evaluate( 1, 0, 0, 0, 0, 0, 0 );
00179 
00180   newObj_ = NewGraph;
00181 
00182   return *NewGraph;
00183 }
00184 
00185 } // namespace EpetraExt
00186 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines