Salome HOME
PTScotch with MEDCOUPLING_USE_64BIT_IDS=ON
[tools/medcoupling.git] / src / MEDPartitioner / MEDPARTITIONER_PTScotchGraph.cxx
1 // Copyright (C) 2017-2020  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_PTScotchGraph.hxx"
21 #include "MEDPARTITIONER_Utils.hxx"
22
23 #include "MEDCouplingSkyLineArray.hxx"
24 #include "MEDCouplingMemArray.hxx"
25 #include "MCType.hxx"
26
27 #include <cstdio>
28 #include <mpi.h>
29
30 #ifdef MED_ENABLE_PTSCOTCH
31 extern "C"
32 {
33 #define restrict
34 #include "ptscotch.h"
35 }
36 #endif
37
38 using namespace MEDPARTITIONER;
39
40
41 PTSCOTCHGraph::PTSCOTCHGraph(MEDCoupling::MEDCouplingSkyLineArray *graph, int *edgeweight, DataArrayIdType *vlbloctab):Graph(graph,edgeweight),_vlbloctab(vlbloctab)
42 {
43 }
44
45 PTSCOTCHGraph::~PTSCOTCHGraph()
46 {
47 }
48
49 void PTSCOTCHGraph::partGraph(int ndomain, const std::string& options_string, ParaDomainSelector* sel)
50 {
51   if (MyGlobals::_Verbose>10)
52     std::cout << "proc " << MyGlobals::_Rank << " : PTSCOTCHGraph::partGraph" << std::endl;
53   
54   //number of graph vertices
55   int n = FromIdType<int>(_graph->getNumberOf());
56   //graph
57 #ifdef MEDCOUPLING_USE_64BIT_IDS
58   std::vector<int> indexVec( _graph->getIndex(), _graph->getIndexArray()->end() );
59   std::vector<int> valueVec( _graph->getValues(), _graph->getValuesArray()->end() );
60   int * xadj=indexVec.data();
61   int * adjncy=valueVec.data();
62 #else
63   int * xadj=const_cast<int*>(_graph->getIndex());
64   int * adjncy=const_cast<int*>(_graph->getValues());
65 #endif
66   //ndomain
67   int nparts=ndomain;
68
69 #if !defined(MED_ENABLE_PTSCOTCH)
70   throw INTERP_KERNEL::Exception("PTSCOTCHGraph::partGraph : PTSCOTCH is not available. Check your products, please.");
71 #else
72   //output parameters
73   int* partition = new int[n+1];
74   
75 #ifdef MEDCOUPLING_USE_64BIT_IDS
76   std::vector<int> vlbloctabVec;
77   int *vlbloctab(nullptr);
78   if( _vlbloctab )
79     {
80       vlbloctabVec.insert( vlbloctabVec.end() , _vlbloctab->begin() , _vlbloctab->end() );
81       vlbloctab = vlbloctabVec.data();
82     }
83 #else
84   mcIdType *vlbloctab = _vlbloctab?const_cast<mcIdType*>(_vlbloctab->begin()):nullptr;
85 #endif
86   SCOTCH_randomReset();
87   SCOTCH_Dgraph scotch_graph;
88   SCOTCH_dgraphInit(&scotch_graph, MPI_COMM_WORLD);
89   SCOTCH_dgraphBuild(&scotch_graph,
90                      0,             // baseval               , base first indice 0
91                      n,             // vertlocnbr            , nb of local graph nodes
92                      n,             // vertlocmax            , should be set to vertlocnbr for graphs without holes
93                      xadj,          // vertloctab[vertnbr+1] , index vertex table
94                      0,             // vendloctab            , index end vertex table if disjoint, set to zero
95                      _cell_weight,  // veloloctab            , graph vertices loads, set to zero
96                      vlbloctab,     // vlblocltab            , vertex label array : global vertex index
97                      xadj[n],       // edgelocnbr            , number of edges
98                      xadj[n],       // edgelocsiz            , same as edgelocnbr if edgeloctab is compact
99                      adjncy,        // edgeloctab[edgelocnbr], global indexes of edges
100                      0,             // edgegsttab            , optional, should be computed internally, set to zero
101                      _edge_weight); // edloloctab            , graph edges loads, set to zero
102   
103   SCOTCH_Strat scotch_strategy;
104   SCOTCH_stratInit(&scotch_strategy);
105   
106   //!user-defined options for the strategy
107   if (options_string!="")
108     SCOTCH_stratGraphMap(&scotch_strategy,options_string.c_str());
109
110   if (nparts>1)
111     {
112       if (MyGlobals::_Verbose>10) std::cout << "SCOTCHGraph::graphPart SCOTCH_graphPart" << std::endl;
113       SCOTCH_dgraphPart(&scotch_graph,nparts,&scotch_strategy,partition);
114     }
115   else  //partition for 1 subdomain
116     {
117     for (int i=0; i<n+1; i++)
118       partition[i]=0;
119     }
120   
121   SCOTCH_stratExit(&scotch_strategy);
122   SCOTCH_dgraphExit(&scotch_graph);
123
124   std::vector<mcIdType> index(n+1);
125   std::vector<mcIdType> value(n);
126   index[0]=0;
127   for (int i=0; i<n; i++)
128     {
129       index[i+1]=index[i]+1;
130       value[i]=ToIdType(partition[i]);
131     }
132   delete [] partition;
133   
134   //creating a skylinearray with no copy of the index and partition array
135   //the fifth argument true specifies that only the pointers are passed 
136   //to the object
137   _partition = MEDCoupling::MEDCouplingSkyLineArray::New(index,value);
138 #endif
139 }