Back to index

salome-med  6.5.0
MEDPARTITIONER_ScotchGraph.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 
00020 #include "MEDPARTITIONER_Graph.hxx"
00021 #include "MEDPARTITIONER_ScotchGraph.hxx"
00022 #include "MEDPARTITIONER_Utils.hxx"
00023 
00024 #include <cstdio>
00025 
00026 #ifdef MED_ENABLE_SCOTCH
00027 extern "C"
00028 {
00029 #define restrict
00030 #include "scotch.h"
00031 }
00032 #endif
00033 
00034 using namespace MEDPARTITIONER;
00035   
00036 SCOTCHGraph::SCOTCHGraph():Graph()
00037 {
00038 }
00039 
00040 SCOTCHGraph::SCOTCHGraph(MEDPARTITIONER::SkyLineArray* graph, int* edgeweight):Graph(graph,edgeweight)
00041 {
00042 }
00043 
00044 SCOTCHGraph::~SCOTCHGraph()
00045 {
00046 }
00047 
00048 void SCOTCHGraph::partGraph(int ndomain, const std::string& options_string, ParaDomainSelector* sel)
00049 {
00050   if (MyGlobals::_Verbose>10)
00051     std::cout << "proc " << MyGlobals::_Rank << " : SCOTCHGraph::partGraph" << std::endl;
00052   
00053   //number of graph vertices
00054   int n = _graph->getNumberOf();
00055   //graph
00056   int * xadj=const_cast<int*>(_graph->getIndex());
00057   int * adjncy=const_cast<int*>(_graph->getValue());
00058   //ndomain
00059   int nparts=ndomain;
00060 
00061 #if !defined(MED_ENABLE_SCOTCH)
00062   throw INTERP_KERNEL::Exception("SCOTCHGraph::partGraph : SCOTCH is not available. Check your products, please.");
00063 #else
00064   //output parameters
00065   int* partition = new int[n+1];
00066   
00067   SCOTCH_Graph scotch_graph;
00068   SCOTCH_graphInit(&scotch_graph);
00069   SCOTCH_graphBuild(&scotch_graph,
00070                     0, //base first indice 0
00071                     n, //nb of graph nodes
00072                     xadj,
00073                     0,
00074                     _cell_weight, //graph vertices loads
00075                     0,
00076                     xadj[n], //number of edges
00077                     adjncy,
00078                     _edge_weight);
00079   SCOTCH_Strat scotch_strategy;
00080   SCOTCH_stratInit(&scotch_strategy);
00081   
00083   if (options_string!="")
00084     SCOTCH_stratGraphMap(&scotch_strategy,options_string.c_str());
00085 
00086   if (nparts>1)
00087     {
00088       if (MyGlobals::_Verbose>10) std::cout << "SCOTCHGraph::graphPart SCOTCH_graphPart" << std::endl;
00089       SCOTCH_graphPart(&scotch_graph,nparts,&scotch_strategy,partition);
00090     }
00091   else  //partition for 1 subdomain
00092     {
00093     for (int i=0; i<n+1; i++)
00094       partition[i]=0;
00095     }
00096   
00097   SCOTCH_stratExit(&scotch_strategy);
00098   SCOTCH_graphExit(&scotch_graph);
00099 
00100   std::vector<int> index(n+1);
00101   std::vector<int> value(n);
00102   index[0]=0;
00103   for (int i=0; i<n; i++)
00104     {
00105       index[i+1]=index[i]+1;
00106       value[i]=partition[i];
00107     }
00108   delete [] partition;
00109   
00110   //creating a skylinearray with no copy of the index and partition array
00111   //the fifth argument true specifies that only the pointers are passed 
00112   //to the object
00113   _partition = new MEDPARTITIONER::SkyLineArray(index,value);
00114 #endif
00115 }