Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/med.git] / src / MEDPartitioner / MEDPARTITIONER_ScotchGraph.cxx
1 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "MEDPARTITIONER_Graph.hxx"
21 #include "MEDPARTITIONER_ScotchGraph.hxx"
22 #include "MEDPARTITIONER_Utils.hxx"
23
24 #include <cstdio>
25
26 #ifdef MED_ENABLE_SCOTCH
27 extern "C"
28 {
29 #define restrict
30 #include "scotch.h"
31 }
32 #endif
33
34 using namespace MEDPARTITIONER;
35   
36 SCOTCHGraph::SCOTCHGraph():Graph()
37 {
38 }
39
40 SCOTCHGraph::SCOTCHGraph(MEDPARTITIONER::SkyLineArray* graph, int* edgeweight):Graph(graph,edgeweight)
41 {
42 }
43
44 SCOTCHGraph::~SCOTCHGraph()
45 {
46 }
47
48 void SCOTCHGraph::partGraph(int ndomain, const std::string& options_string, ParaDomainSelector* sel)
49 {
50   if (MyGlobals::_Verbose>10)
51     std::cout << "proc " << MyGlobals::_Rank << " : SCOTCHGraph::partGraph" << std::endl;
52   
53   //number of graph vertices
54   int n = _graph->getNumberOf();
55   //graph
56   int * xadj=const_cast<int*>(_graph->getIndex());
57   int * adjncy=const_cast<int*>(_graph->getValue());
58   //ndomain
59   int nparts=ndomain;
60
61 #if !defined(MED_ENABLE_SCOTCH)
62   throw INTERP_KERNEL::Exception("SCOTCHGraph::partGraph : SCOTCH is not available. Check your products, please.");
63 #else
64   //output parameters
65   int* partition = new int[n+1];
66   
67   SCOTCH_Graph scotch_graph;
68   SCOTCH_graphInit(&scotch_graph);
69   SCOTCH_graphBuild(&scotch_graph,
70                     0, //base first indice 0
71                     n, //nb of graph nodes
72                     xadj,
73                     0,
74                     _cell_weight, //graph vertices loads
75                     0,
76                     xadj[n], //number of edges
77                     adjncy,
78                     _edge_weight);
79   SCOTCH_Strat scotch_strategy;
80   SCOTCH_stratInit(&scotch_strategy);
81   
82   //!user-defined options for the strategy
83   if (options_string!="")
84     SCOTCH_stratGraphMap(&scotch_strategy,options_string.c_str());
85
86   if (nparts>1)
87     {
88       if (MyGlobals::_Verbose>10) std::cout << "SCOTCHGraph::graphPart SCOTCH_graphPart" << std::endl;
89       SCOTCH_graphPart(&scotch_graph,nparts,&scotch_strategy,partition);
90     }
91   else  //partition for 1 subdomain
92     {
93     for (int i=0; i<n+1; i++)
94       partition[i]=0;
95     }
96   
97   SCOTCH_stratExit(&scotch_strategy);
98   SCOTCH_graphExit(&scotch_graph);
99
100   std::vector<int> index(n+1);
101   std::vector<int> value(n);
102   index[0]=0;
103   for (int i=0; i<n; i++)
104     {
105       index[i+1]=index[i]+1;
106       value[i]=partition[i];
107     }
108   delete [] partition;
109   
110   //creating a skylinearray with no copy of the index and partition array
111   //the fifth argument true specifies that only the pointers are passed 
112   //to the object
113   _partition = new MEDPARTITIONER::SkyLineArray(index,value);
114 #endif
115 }