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