Salome HOME
Revert "Synchronize adm files"
[modules/med.git] / src / MEDPartitioner / MEDPARTITIONER_ParMetisGraph.cxx
1 // Copyright (C) 2007-2014  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_ParMetisGraph.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_PARMETIS
29 #include <parmetis.h>
30 // #if PARMETIS_MAJOR_VERSION == 4
31 //    #define ParMETIS_PartKway ParMETIS_V3_PartKway
32 // #endif
33 #endif
34
35 using namespace MEDPARTITIONER;
36
37 ParMETISGraph::ParMETISGraph():Graph()
38 {
39 }
40
41 ParMETISGraph::ParMETISGraph(MEDPARTITIONER::SkyLineArray* graph, int* edgeweight)
42   :Graph(graph,edgeweight)
43 {
44 }
45
46 ParMETISGraph::~ParMETISGraph()
47 {
48 }
49
50 void ParMETISGraph::partGraph(int ndomain,
51                            const std::string& options_string,
52                            ParaDomainSelector *parallelizer)
53 {
54   using std::vector;
55   vector<int> ran,vx,va; //for randomize
56   
57   if (MyGlobals::_Verbose>10)
58     std::cout << "proc " << MyGlobals::_Rank << " : ParMETISGraph::partGraph" << std::endl;
59   
60   // number of graph vertices
61   int n=_graph->getNumberOf();
62   //graph
63   int * xadj=const_cast<int*>(_graph->getIndex());
64   int * adjncy=const_cast<int*>(_graph->getValue());
65   //constraints
66   int * vwgt=_cell_weight;
67   int * adjwgt=_edge_weight;
68   int wgtflag=(_edge_weight!=0)?1:0+(_cell_weight!=0)?2:0;
69   //base 0 or 1
70   int base=0;
71   //ndomain
72   int nparts=ndomain;
73   //options
74   /*
75     (0=default_option,option,random_seed) see defs.h
76     #define PMV3_OPTION_DBGLVL 1
77     #define PMV3_OPTION_SEED 2
78     #define PMV3_OPTION_IPART 3
79     #define PMV3_OPTION_PSR 3
80     seems no changes int options[4]={1,0,33,0}; //test for a random seed of 33
81   */
82   int options[4]={0,0,0,0};
83   // output parameters
84   int edgecut;
85 #if !defined(MED_ENABLE_PARMETIS)
86   throw INTERP_KERNEL::Exception("ParMETISGraph::partGraph : PARMETIS is not available. Check your products, please.");
87 #else
88   int* partition=new int[n];
89   
90   if (MyGlobals::_Verbose>10) 
91     std::cout << "proc " << MyGlobals::_Rank << " : ParMETISGraph::partGraph ParMETIS_PartKway new" << std::endl;
92   int * vtxdist=parallelizer->getProcVtxdist();
93   MPI_Comm comm=MPI_COMM_WORLD;
94   ParMETIS_PartKway(vtxdist, xadj, adjncy, vwgt, 
95                                     adjwgt, &wgtflag, &base, &nparts, options, 
96                                     &edgecut, partition, &comm );
97
98
99   /*doc from parmetis.h
100     void __cdecl ParMETIS_PartKway(
101     idxtype *vtxdist, idxtype *xadj, idxtype *adjncy, idxtype *vwgt, 
102     idxtype *adjwgt, int *wgtflag, int *numflag, int *nparts, int *options, 
103     int *edgecut, idxtype *part, MPI_Comm *comm);
104
105     void __cdecl ParMETIS_V3_PartKway(
106     idxtype *vtxdist, idxtype *xadj, idxtype *adjncy, idxtype *vwgt, 
107     idxtype *adjwgt, int *wgtflag, int *numflag, int *ncon, int *nparts, 
108     float *tpwgts, float *ubvec, int *options, int *edgecut, idxtype *part, 
109     MPI_Comm *comm);
110   */
111
112   vector<int> index(n+1);
113   vector<int> value(n);
114   index[0]=0;
115   if (ran.size()>0 && MyGlobals::_Atomize==0) //there is randomize
116     {
117       if (MyGlobals::_Is0verbose>100)
118         std::cout << "randomize" << std::endl;
119       for (int i=0; i<n; i++)
120         {
121           index[i+1]=index[i]+1;
122           value[ran[i]]=partition[i];
123         }
124     }
125   else
126     {
127       for (int i=0; i<n; i++)
128         {
129           index[i+1]=index[i]+1;
130           value[i]=partition[i];
131         }
132     }
133   delete [] partition;
134
135   //creating a skylinearray with no copy of the index and partition array
136   //the fifth argument true specifies that only the pointers are passed 
137   //to the object
138   
139   _partition = new MEDPARTITIONER::SkyLineArray(index,value);
140 #endif
141 }
142