Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/med.git] / src / MEDSPLITTER / MEDSPLITTER_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 #include <cstdio>
20 extern "C" {
21 #define restrict
22 #include "scotch.h"
23 }
24 #include "MEDSPLITTER_Graph.hxx"
25 #include "MEDSPLITTER_SCOTCHGraph.hxx"
26
27 using namespace MEDSPLITTER;
28   
29 SCOTCHGraph::SCOTCHGraph():Graph()
30 {
31 }
32
33 SCOTCHGraph::SCOTCHGraph(MEDMEM::MEDSKYLINEARRAY* graph, int* edgeweight):Graph(graph,edgeweight)
34 {
35 }
36
37 SCOTCHGraph::~SCOTCHGraph()
38 {
39 }
40
41 void SCOTCHGraph::partGraph(int ndomain, const string& options_string, ParaDomainSelector* sel)
42 {
43   // number of graph vertices
44   int n = m_graph->getNumberOf();
45
46   //graph
47   int * xadj=const_cast<int*>(m_graph->getIndex());
48   int * adjncy = const_cast<int*>(m_graph->getValue());
49
50   //ndomain
51   int nparts = ndomain;
52
53   // output parameters
54   int* partition = new int[n+1];
55
56   SCOTCH_Graph scotch_graph;
57
58   SCOTCH_graphInit(&scotch_graph);
59
60
61   SCOTCH_graphBuild(&scotch_graph,
62                     1, //premier indice 1
63                     n, // nb of graph nodes
64                     xadj,
65                     0,
66                     m_cellweight, //graph vertices loads
67                     0,
68                     xadj[n], // number of edges
69                     adjncy,
70                     m_edgeweight);
71
72   SCOTCH_Strat scotch_strategy;           
73   SCOTCH_stratInit(&scotch_strategy);
74
75   //!user-defined options for the strategy
76   if (options_string!="")
77     SCOTCH_stratGraphMap(&scotch_strategy,options_string.c_str());
78
79
80   if (nparts>1)           
81     SCOTCH_graphPart(&scotch_graph,nparts,&scotch_strategy,partition);
82   else
83     // partition for 1 subdomain
84     for (int i=0; i<n+1; i++)
85       partition[i]=0;
86
87   SCOTCH_stratExit(&scotch_strategy);
88   SCOTCH_graphExit(&scotch_graph);
89
90   int* index=new int [n+1];
91   index[0]=1;
92   for (int i=0; i<n; i++)
93   {
94     index[i+1]=index[i]+1;
95   }
96
97   //creating a skylinearray with no copy of the index and partition array
98   // the fifth argument true specifies that only the pointers are passed 
99   //to the object
100   m_partition = new MEDMEM::MEDSKYLINEARRAY(n,n, index, partition, true);
101 }