]> SALOME platform Git repositories - tools/medcoupling.git/blob - src/MEDPartitioner/MEDPARTITIONER_MetisGraph.cxx
Salome HOME
Merge from V6_main (04/10/2012)
[tools/medcoupling.git] / src / MEDPartitioner / MEDPARTITIONER_MetisGraph.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_MetisGraph.hxx"
21 #include "MEDPARTITIONER_ParaDomainSelector.hxx"
22 #include "MEDPARTITIONER_Utils.hxx"
23
24 #include "InterpKernelException.hxx"
25
26 #include <iostream>
27
28 #ifdef MED_ENABLE_METIS
29 extern "C"
30 {
31 #include "metis.h"
32 }
33 #endif
34
35 using namespace MEDPARTITIONER;
36
37 METISGraph::METISGraph():Graph()
38 {
39 }
40
41 METISGraph::METISGraph(MEDPARTITIONER::SkyLineArray* graph, int* edgeweight)
42   :Graph(graph,edgeweight)
43 {
44 }
45
46 METISGraph::~METISGraph()
47 {
48 }
49
50 void METISGraph::partGraph(int ndomain,
51                            const std::string& options_string,
52                            ParaDomainSelector *parallelizer)
53 {
54   using std::vector;
55   if (MyGlobals::_Verbose>10)
56     std::cout << "proc " << MyGlobals::_Rank << " : METISGraph::partGraph" << std::endl;
57   
58   //number of graph vertices
59   int n=_graph->getNumberOf();
60   //graph
61   int * xadj=const_cast<int*>(_graph->getIndex());
62   int * adjncy=const_cast<int*>(_graph->getValue());
63   //constraints
64   int * vwgt=_cell_weight;
65   int * adjwgt=_edge_weight;
66   int wgtflag=(_edge_weight!=0)?1:0+(_cell_weight!=0)?2:0;
67   //base 0 or 1
68   int base=0;
69   //ndomain
70   int nparts=ndomain;
71   //options
72   /*
73     (0=default_option,option,random_seed) see defs.h
74     #define PMV3_OPTION_DBGLVL 1
75     #define PMV3_OPTION_SEED 2
76     #define PMV3_OPTION_IPART 3
77     #define PMV3_OPTION_PSR 3
78     seems no changes int options[4]={1,0,33,0}; //test for a random seed of 33
79   */
80   int options[4]={0,0,0,0};
81 #if !defined(MED_ENABLE_METIS)
82   throw INTERP_KERNEL::Exception("METISGraph::partGraph : METIS is not available. Check your products, please.");
83 #else
84   //output parameters
85   int edgecut;
86   int* partition=new int[n];
87
88   if(nparts >1)
89     {
90       if (MyGlobals::_Verbose>10) 
91         std::cout << "METISGraph::partGraph METIS_PartGraph METIS_PartGraph(RecursiveOrKway)" << std::endl;
92       if (options_string != "k")
93         METIS_PartGraphRecursive(&n, xadj, adjncy, vwgt, adjwgt, &wgtflag,
94                                   &base, &nparts, options, &edgecut, partition);
95       else
96         METIS_PartGraphKway(&n, xadj, adjncy, vwgt, adjwgt, &wgtflag,
97                             &base, &nparts, options, &edgecut, partition);
98     }
99   else  //force this case because METIS send all 1 in value
100     {
101       for (int i=0; i<n; i++)
102         partition[i]=0;
103     }
104   vector<int> index(n+1);
105   vector<int> value(n);
106   index[0]=0;
107   for (int i=0; i<n; i++)
108     {
109       index[i+1]=index[i]+1;
110       value[i]=partition[i];
111     }
112   delete [] partition;
113
114   //creating a skylinearray with no copy of the index and partition array
115   //the fifth argument true specifies that only the pointers are passed 
116   //to the object
117   _partition = new MEDPARTITIONER::SkyLineArray(index,value);
118 #endif
119 }
120