Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/med.git] / src / MEDSPLITTER / MEDSPLITTER_SCOTCHGraph.cxx
1 #include <cstdio>
2 extern "C" {
3 #define restrict
4 #include "bin/scotch.h"
5 }
6 #include "MEDSPLITTER_Graph.hxx"
7 #include "MEDSPLITTER_SCOTCHGraph.hxx"
8
9 using namespace MEDSPLITTER;
10         
11 SCOTCHGraph::SCOTCHGraph():Graph()
12 {
13 }
14
15 SCOTCHGraph::SCOTCHGraph(const MEDMEM::MEDSKYLINEARRAY* graph, int* edgeweight):Graph(graph,edgeweight)
16 {
17 }
18
19 SCOTCHGraph::~SCOTCHGraph()
20 {
21         if (m_partition!=0) {delete m_partition; m_partition=0;}
22         if (m_graph!=0) {delete m_graph; m_graph=0;}
23 }
24
25 void SCOTCHGraph::partGraph(int ndomain, const string& options_string)
26 {
27         // number of graph vertices
28         int n = m_graph->getNumberOf();
29         
30         //graph
31         int * xadj=const_cast<int*>(m_graph->getIndex());
32         int * adjncy = const_cast<int*>(m_graph->getValue());
33         
34         //ndomain
35         int nparts = ndomain;
36         
37         // output parameters
38         int* partition = new int[n+1];
39         
40         SCOTCH_Graph scotch_graph;
41         
42         SCOTCH_graphInit(&scotch_graph);
43         
44         
45         SCOTCH_graphBuild(&scotch_graph,
46                           1, //premier indice 1
47                           n, // nb of graph nodes
48                           xadj,
49                           0,
50                           m_cellweight, //graph vertices loads
51                           0,
52                           xadj[n], // number of edges
53                           adjncy,
54                           m_edgeweight);
55
56         SCOTCH_Strat scotch_strategy;                                     
57         SCOTCH_stratInit(&scotch_strategy);
58
59         //!user-defined options for the strategy
60         if (options_string!="")
61           SCOTCH_stratGraphMap(&scotch_strategy,options_string.c_str());
62                                                   
63
64         if (nparts>1)                                     
65                 SCOTCH_graphPart(&scotch_graph,nparts,&scotch_strategy,partition);
66         else
67           // partition for 1 subdomain
68                 for (int i=0; i<n+1; i++)
69                         partition[i]=1;
70                         
71         SCOTCH_stratExit(&scotch_strategy);
72         SCOTCH_graphExit(&scotch_graph);
73                                                                                  
74         int* index=new int [n+1];
75         index[0]=1;
76         for (int i=0; i<n; i++)
77         {
78                 index[i+1]=index[i]+1;
79         }
80         
81         //creating a skylinearray with no copy of the index and partition array
82         // the fifth argument true specifies that only the pointers are passed 
83         //to the object
84         m_partition = new MEDMEM::MEDSKYLINEARRAY(n,n, index, partition, true);
85 }