| 1 |
//@HEADER |
| 2 |
//************************************************************************ |
| 3 |
// |
| 4 |
// Isorropia: Partitioning and Load Balancing Package |
| 5 |
// Copyright (2006) Sandia Corporation |
| 6 |
// |
| 7 |
//Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive |
| 8 |
//license for use of this work by or on behalf of the U.S. Government. |
| 9 |
// |
| 10 |
// Redistribution and use in source and binary forms, with or without |
| 11 |
// modification, are permitted provided that the following conditions are |
| 12 |
// met: |
| 13 |
// |
| 14 |
// 1. Redistributions of source code must retain the above copyright |
| 15 |
// notice, this list of conditions and the following disclaimer. |
| 16 |
// |
| 17 |
// 2. Redistributions in binary form must reproduce the above copyright |
| 18 |
// notice, this list of conditions and the following disclaimer in the |
| 19 |
// documentation and/or other materials provided with the distribution. |
| 20 |
// |
| 21 |
// 3. Neither the name of the Corporation nor the names of the |
| 22 |
// contributors may be used to endorse or promote products derived from |
| 23 |
// this software without specific prior written permission. |
| 24 |
// |
| 25 |
// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY |
| 26 |
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 27 |
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 28 |
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE |
| 29 |
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 30 |
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 31 |
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 32 |
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 33 |
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 34 |
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 35 |
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 36 |
// |
| 37 |
//************************************************************************ |
| 38 |
//@HEADER |
| 39 |
|
| 40 |
#include <Isorropia_Zoltan_Repartition.hpp> |
| 41 |
|
| 42 |
#ifdef HAVE_ISORROPIA_ZOLTAN |
| 43 |
|
| 44 |
#ifndef HAVE_MPI |
| 45 |
#error "Isorropia_Zoltan requires MPI." |
| 46 |
#endif |
| 47 |
|
| 48 |
#include <Isorropia_Exception.hpp> |
| 49 |
|
| 50 |
#include <Teuchos_RefCountPtr.hpp> |
| 51 |
|
| 52 |
#ifdef HAVE_EPETRA |
| 53 |
#include <Epetra_Map.h> |
| 54 |
#include <Epetra_RowMatrix.h> |
| 55 |
#ifdef HAVE_MPI |
| 56 |
#include <Epetra_MpiComm.h> |
| 57 |
#endif |
| 58 |
#include <Epetra_CrsGraph.h> |
| 59 |
#endif |
| 60 |
|
| 61 |
#include <QueryObject.hpp> |
| 62 |
|
| 63 |
namespace Isorropia { |
| 64 |
|
| 65 |
namespace Epetra { |
| 66 |
|
| 67 |
namespace ZoltanLib { |
| 68 |
|
| 69 |
static int me = 0; |
| 70 |
|
| 71 |
//TODO do we need to add these functions for geometric partitioning? |
| 72 |
// Are these being used? |
| 73 |
|
| 74 |
int |
| 75 |
repartition(Teuchos::RefCountPtr<const Epetra_CrsGraph> input_graph, |
| 76 |
Teuchos::RefCountPtr<const Isorropia::Epetra::CostDescriber> costs, |
| 77 |
Teuchos::ParameterList& paramlist, |
| 78 |
std::vector<int>& myNewElements, |
| 79 |
std::map<int,int>& exports, |
| 80 |
std::map<int,int>& imports) |
| 81 |
{ |
| 82 |
int inputType = QueryObject::hgraph_input_; |
| 83 |
std::string lb_method_str("LB_METHOD"); |
| 84 |
if (paramlist.isParameter(lb_method_str)){ |
| 85 |
std::string lb_meth = paramlist.get(lb_method_str, "HYPERGRAPH"); |
| 86 |
if (lb_meth == "GRAPH"){ |
| 87 |
inputType = QueryObject::graph_input_; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
Teuchos::RefCountPtr<QueryObject> queryObject = |
| 92 |
Teuchos::rcp(new QueryObject(input_graph, costs, inputType)); |
| 93 |
|
| 94 |
const Epetra_Comm &ecomm = input_graph->RowMap().Comm(); |
| 95 |
#ifdef HAVE_MPI |
| 96 |
const Epetra_MpiComm &empicomm = dynamic_cast<const Epetra_MpiComm &>(ecomm); |
| 97 |
|
| 98 |
MPI_Comm mpicomm = empicomm.Comm(); |
| 99 |
|
| 100 |
return( load_balance(mpicomm, paramlist, *queryObject, |
| 101 |
myNewElements, exports, imports) ); |
| 102 |
#else |
| 103 |
return (1); |
| 104 |
#endif |
| 105 |
} |
| 106 |
|
| 107 |
int |
| 108 |
repartition(Teuchos::RefCountPtr<const Epetra_RowMatrix> input_matrix, |
| 109 |
Teuchos::RefCountPtr<const Isorropia::Epetra::CostDescriber> costs, |
| 110 |
Teuchos::ParameterList& paramlist, |
| 111 |
std::vector<int>& myNewElements, |
| 112 |
std::map<int,int>& exports, |
| 113 |
std::map<int,int>& imports) |
| 114 |
{ |
| 115 |
int inputType = QueryObject::hgraph_input_; |
| 116 |
std::string lb_method_str("LB_METHOD"); |
| 117 |
if (paramlist.isParameter(lb_method_str)){ |
| 118 |
std::string lb_meth = paramlist.get(lb_method_str, "HYPERGRAPH"); |
| 119 |
if (lb_meth == "GRAPH"){ |
| 120 |
inputType = QueryObject::graph_input_; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
Teuchos::RefCountPtr<QueryObject> queryObject = |
| 125 |
Teuchos::rcp(new QueryObject(input_matrix, costs, inputType)); |
| 126 |
|
| 127 |
const Epetra_Comm &ecomm = input_matrix->RowMatrixRowMap().Comm(); |
| 128 |
#ifdef HAVE_MPI |
| 129 |
const Epetra_MpiComm &empicomm = dynamic_cast<const Epetra_MpiComm &>(ecomm); |
| 130 |
|
| 131 |
MPI_Comm mpicomm = empicomm.Comm(); |
| 132 |
|
| 133 |
return( load_balance(mpicomm, paramlist, *queryObject, |
| 134 |
myNewElements, exports, imports) ); |
| 135 |
#else |
| 136 |
return (1); |
| 137 |
#endif |
| 138 |
} |
| 139 |
|
| 140 |
#ifdef HAVE_MPI |
| 141 |
int |
| 142 |
load_balance(MPI_Comm &comm, |
| 143 |
Teuchos::ParameterList& paramlist, |
| 144 |
QueryObject& queryObject, |
| 145 |
std::vector<int>& myNewElements, |
| 146 |
std::map<int,int>& exports, |
| 147 |
std::map<int,int>& imports) |
| 148 |
{ |
| 149 |
float version; |
| 150 |
int argcTmp=0; |
| 151 |
char *argvTmp[1]; |
| 152 |
|
| 153 |
// create a Zoltan problem |
| 154 |
|
| 155 |
argvTmp[0] = NULL; |
| 156 |
Zoltan_Initialize(argcTmp, argvTmp, &version); |
| 157 |
|
| 158 |
Zoltan *zz = new Zoltan(comm); |
| 159 |
|
| 160 |
if (zz == NULL){ |
| 161 |
throw Isorropia::Exception("Error creating Zoltan object"); |
| 162 |
return -1; |
| 163 |
} |
| 164 |
|
| 165 |
// set problem parameters (override Zoltan defaults) |
| 166 |
|
| 167 |
std::string dbg_level_str("DEBUG_LEVEL"); |
| 168 |
if (!paramlist.isParameter(dbg_level_str)) { |
| 169 |
paramlist.set(dbg_level_str, "0"); |
| 170 |
} |
| 171 |
|
| 172 |
// Make LB_APPROACH = PARTITION the default in Isorropia |
| 173 |
std::string lb_approach_str("LB_APPROACH"); |
| 174 |
if (!paramlist.isParameter(lb_approach_str)) { |
| 175 |
paramlist.set(lb_approach_str, "PARTITION"); |
| 176 |
} |
| 177 |
|
| 178 |
std::string lb_method_str("LB_METHOD"); |
| 179 |
std::string lb_meth = paramlist.get(lb_method_str, "HYPERGRAPH"); |
| 180 |
|
| 181 |
if (!paramlist.isParameter(lb_method_str)) { |
| 182 |
paramlist.set(lb_method_str, lb_meth); // set to HYPERGRAPH |
| 183 |
} |
| 184 |
|
| 185 |
// If no process set vertex weights, let Zoltan default to |
| 186 |
// unit weights for vertices |
| 187 |
|
| 188 |
if (queryObject.haveVertexWeights()) { |
| 189 |
if (!paramlist.isParameter("OBJ_WEIGHT_DIM")) { |
| 190 |
paramlist.set("OBJ_WEIGHT_DIM", "1"); |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
// If no process set graph or hypergraph edge weights, |
| 195 |
// let Zoltan default to unit weights for edges |
| 196 |
|
| 197 |
if (queryObject.haveGraphEdgeWeights() || |
| 198 |
queryObject.haveHypergraphEdgeWeights()) { |
| 199 |
if (!paramlist.isParameter("EDGE_WEIGHT_DIM")) { |
| 200 |
paramlist.set("EDGE_WEIGHT_DIM", "1"); |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
Teuchos::ParameterList::ConstIterator |
| 205 |
iter = paramlist.begin(), |
| 206 |
iter_end = paramlist.end(); |
| 207 |
|
| 208 |
for(; iter != iter_end; ++iter) { |
| 209 |
const std::string& name = iter->first; |
| 210 |
const std::string& value = Teuchos::getValue<std::string>(iter->second); |
| 211 |
zz->Set_Param(name, value); |
| 212 |
} |
| 213 |
|
| 214 |
// Set the query functions |
| 215 |
// M.M.W. do we need to support hierarchical partitioning here? |
| 216 |
|
| 217 |
zz->Set_Num_Obj_Fn(QueryObject::Number_Objects, (void *)&queryObject); |
| 218 |
zz->Set_Obj_List_Fn(QueryObject::Object_List, (void *)&queryObject); |
| 219 |
|
| 220 |
if (lb_meth == "HYPERGRAPH") |
| 221 |
{ |
| 222 |
zz->Set_HG_Size_CS_Fn(QueryObject::HG_Size_CS, (void *)&queryObject); |
| 223 |
zz->Set_HG_CS_Fn(QueryObject::HG_CS, (void *)&queryObject); |
| 224 |
zz->Set_HG_Size_Edge_Wts_Fn(QueryObject::HG_Size_Edge_Weights , |
| 225 |
(void *)&queryObject); |
| 226 |
zz->Set_HG_Edge_Wts_Fn(QueryObject::HG_Edge_Weights, (void *)&queryObject); |
| 227 |
} |
| 228 |
else |
| 229 |
{ |
| 230 |
zz->Set_Num_Edges_Multi_Fn(QueryObject::Number_Edges_Multi, (void *)&queryObject); |
| 231 |
zz->Set_Edge_List_Multi_Fn(QueryObject::Edge_List_Multi, (void *)&queryObject); |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
//Generate Load Balance |
| 237 |
int changes, num_gid_entries, num_lid_entries, num_import, num_export; |
| 238 |
ZOLTAN_ID_PTR import_global_ids, import_local_ids; |
| 239 |
ZOLTAN_ID_PTR export_global_ids, export_local_ids; |
| 240 |
int * import_procs, * export_procs; |
| 241 |
int *import_to_part, *export_to_part; |
| 242 |
|
| 243 |
int err = zz->LB_Partition(changes, num_gid_entries, num_lid_entries, |
| 244 |
num_import, import_global_ids, import_local_ids, import_procs, import_to_part, |
| 245 |
num_export, export_global_ids, export_local_ids, export_procs, export_to_part ); |
| 246 |
|
| 247 |
if (err != ZOLTAN_OK){ |
| 248 |
throw Isorropia::Exception("Error computing partitioning with Zoltan"); |
| 249 |
return -1; |
| 250 |
} |
| 251 |
|
| 252 |
//Generate New Element List |
| 253 |
int numMyElements = queryObject.RowMap().NumMyElements(); |
| 254 |
std::vector<int> elementList( numMyElements ); |
| 255 |
queryObject.RowMap().MyGlobalElements( &elementList[0] ); |
| 256 |
|
| 257 |
int newNumMyElements = numMyElements - num_export + num_import; |
| 258 |
myNewElements.resize( newNumMyElements ); |
| 259 |
|
| 260 |
for( int i = 0; i < num_export; ++i ) { |
| 261 |
exports[export_global_ids[i]] = export_procs[i]; |
| 262 |
} |
| 263 |
|
| 264 |
for( int i = 0; i < num_import; ++i ) { |
| 265 |
imports[import_global_ids[i]] = import_procs[i]; |
| 266 |
} |
| 267 |
|
| 268 |
//Add unmoved indices to new list |
| 269 |
int loc = 0; |
| 270 |
for( int i = 0; i < numMyElements; ++i ) { |
| 271 |
if( !exports.count( elementList[i] ) ) { |
| 272 |
myNewElements[loc++] = elementList[i]; |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
//Add imports to end of list |
| 277 |
for( int i = 0; i < num_import; ++i ) { |
| 278 |
myNewElements[loc+i] = import_global_ids[i]; |
| 279 |
} |
| 280 |
|
| 281 |
//Free Zoltan Data |
| 282 |
zz->LB_Free_Part(&import_global_ids, &import_local_ids, |
| 283 |
&import_procs, &import_to_part); |
| 284 |
zz->LB_Free_Part(&export_global_ids, &export_local_ids, |
| 285 |
&export_procs, &export_to_part); |
| 286 |
|
| 287 |
delete zz; |
| 288 |
|
| 289 |
return( 0 ); |
| 290 |
} |
| 291 |
#endif |
| 292 |
|
| 293 |
}//namespace ZoltanLib |
| 294 |
}//namespace Epetra |
| 295 |
}//namespace Isorropia |
| 296 |
|
| 297 |
#endif |
| 298 |
|