PaStiX Handbook  6.2.1
graph.c
Go to the documentation of this file.
1 /**
2  *
3  * @file graph.c
4  *
5  * PaStiX graph structure routines
6  *
7  * @copyright 2004-2021 Bordeaux INP, CNRS (LaBRI UMR 5800), Inria,
8  * Univ. Bordeaux. All rights reserved.
9  *
10  * @version 6.2.0
11  * @author Xavier Lacoste
12  * @author Pierre Ramet
13  * @author Mathieu Faverge
14  * @author Tony Delarue
15  * @date 2021-01-25
16  *
17  * @addtogroup pastix_graph
18  * @{
19  *
20  **/
21 #include "common.h"
22 #include "graph/graph.h"
23 
24 #define assert_graph(_graph_) \
25  assert(_graph_->fmttype == SpmCSC); \
26  assert(_graph_->flttype == SpmPattern); \
27  assert(_graph_->values == NULL); \
28 
29 /**
30  *******************************************************************************
31  *
32  * @brief Free the content of the graph structure.
33  *
34  *******************************************************************************
35  *
36  * @param[inout] graph
37  * The pointer graph structure to free.
38  *
39  *******************************************************************************/
40 void
41 graphExit( pastix_graph_t *graph )
42 {
43  /* Parameter checks */
44  if ( graph == NULL ) {
45  errorPrint("graphExit: graph pointer is NULL");
46  return;
47  }
48  assert_graph( graph );
49 
50  spmExit( graph );
51 
52  return;
53 }
54 
55 /**
56  *******************************************************************************
57  *
58  * @brief Rebase the graph to the given value.
59  *
60  *******************************************************************************
61  *
62  * @param[inout] graph
63  * The graph to rebase.
64  *
65  * @param[in] baseval
66  * The base value to use in the graph (0 or 1).
67  *
68  *******************************************************************************/
69 void
70 graphBase( pastix_graph_t *graph,
71  pastix_int_t baseval )
72 {
73  /* Parameter checks */
74  if ( graph == NULL ) {
75  errorPrint("graphBase: graph pointer is NULL");
76  return;
77  }
78  if ( (graph->colptr == NULL) ||
79  (graph->rowptr == NULL) )
80  {
81  errorPrint("graphBase: graph pointer is not correctly initialized");
82  return;
83  }
84  if ( (baseval != 0) &&
85  (baseval != 1) )
86  {
87  errorPrint("graphBase: baseval is incorrect, must be 0 or 1");
88  return;
89  }
90 
91  assert_graph(graph);
92 
93  spmBase( graph, baseval );
94 
95  return;
96 }
97 
98 /**
99  *******************************************************************************
100  *
101  * @ingroup pastix_graph
102  *
103  * @brief This routine copy a given ordering in a new one.
104  *
105  * This function copies a graph structure into another one. If all subpointers
106  * are NULL, then they are all allocated and contains the original graphsrc
107  * values on exit. If one or more array pointers are not NULL, then, only those
108  * are copied to the graphdst structure.
109  *
110  *******************************************************************************
111  *
112  * @param[inout] graphdst
113  * The destination graph
114  *
115  * @param[in] graphsrc
116  * The source graph
117  *
118  *******************************************************************************
119  *
120  * @retval PASTIX_SUCCESS on successful exit
121  * @retval PASTIX_ERR_BADPARAMETER if one parameter is incorrect.
122  *
123  *******************************************************************************/
124 int
125 graphCopy( pastix_graph_t *graphdst,
126  const pastix_graph_t *graphsrc )
127 {
128  pastix_graph_t *graph_tmp;
129 
130  /* Parameter checks */
131  if ( graphdst == NULL ) {
133  }
134  if ( graphsrc == NULL ) {
136  }
137  if ( graphsrc == graphdst ) {
139  }
140  assert_graph(graphsrc);
141  assert_graph(graphdst);
142 
143  /* Clear the prexisting graph */
144  graphExit( graphdst );
145 
146  /* Copy the source graph */
147  graph_tmp = spmCopy( graphsrc );
148  memcpy( graphdst, graph_tmp, sizeof(pastix_graph_t) );
149 
150  memFree_null( graph_tmp );
151 
152  return PASTIX_SUCCESS;
153 }
154 
155 /**
156  *******************************************************************************
157  *
158  * @ingroup pastix_graph
159  *
160  * @brief This routine sortes the subarray of edges of each vertex.
161  *
162  * WARNING: The sort is always performed, do not call this routine
163  * when it is not required.
164  *
165  *******************************************************************************
166  *
167  * @param[inout] graph
168  * On entry, the pointer to the graph structure.
169  * On exit, the same graph with subarrays of edges sorted by ascending
170  * order.
171  *
172  *******************************************************************************/
173 void
174 graphSort( pastix_graph_t *graph )
175 {
176  assert_graph(graph);
177  spmSort(graph);
178 }
179 
180 /**
181  *******************************************************************************
182  *
183  * @ingroup pastix_graph
184  *
185  * @brief Symmetrize a given graph
186  *
187  *******************************************************************************
188  *
189  * @param[inout] graph
190  * The initialized graph structure that will be symmetrized.
191  *
192  *******************************************************************************
193  *
194  * @retval PASTIX_SUCCESS on success,
195  * @retval PASTIX_ERR_BADPARAMETER if incorrect parameters are given.
196  *
197  *******************************************************************************/
198 int
199 graphSymmetrize( pastix_graph_t *graph )
200 {
201  /* Parameter checks */
202  if ( graph == NULL ) {
204  }
205  assert_graph(graph);
206 
207  spmSymmetrize( graph );
208  return PASTIX_SUCCESS;
209 }
210 
211 /**
212  *******************************************************************************
213  *
214  * @ingroup pastix_graph
215  *
216  * @brief Update dofs, nnz, nnzexp, gnnz, n, nexp, gN of a given graph
217  *
218  *******************************************************************************
219  *
220  * @param[inout] graph
221  * The initialized graph structure that will be updated.
222  *
223  *******************************************************************************
224  *
225  * @retval PASTIX_SUCCESS on success,
226  * @retval PASTIX_ERR_BADPARAMETER if incorrect parameters are given.
227  *
228  *******************************************************************************/
229 int
230 graphUpdateComputedFields( pastix_graph_t *graph )
231 {
232  /* Parameter checks */
233  if ( graph == NULL ) {
235  }
236  assert_graph(graph);
237 
238  spmUpdateComputedFields( graph );
239  return PASTIX_SUCCESS;
240 }
241 
242 /**
243  *******************************************************************************
244  *
245  * @ingroup pastix_graph
246  *
247  * @brief This routine build a graph thanks to an spm;
248  *
249  * This function copies an spm structure into a graph one. If all subpointers
250  * are NULL, then they are all allocated and contains the original spm
251  * values on exit. If one or more array pointers are not NULL, then, only those
252  * are copied to the graphdst structure.
253  * We will take care that our graph does not contain coefficients, therefore has
254  * SpmPattern floating type and is a SpmCSC format type.
255  *
256  *******************************************************************************
257  *
258  * @param[inout] graph
259  * The destination graph.
260  *
261  * @param[in] graphsrc
262  * The source Sparse Matrix.
263  *
264  *******************************************************************************
265  *
266  * @retval PASTIX_SUCCESS on successful exit
267  * @retval PASTIX_ERR_BADPARAMETER if one parameter is incorrect.
268  *
269  *******************************************************************************/
270 int
271 graphSpm2Graph( pastix_graph_t *graph,
272  const spmatrix_t *spm )
273 {
274  spmatrix_t *spm2;
275 
276  /* Parameter checks */
277  if ( graph == NULL ) {
279  }
280  if ( spm == NULL ) {
282  }
283 
284  /*
285  * Clear the prexisting graph
286  * Might be uninitialized, so we call spmExit instead of graphExit.
287  */
288  spmExit( graph );
289 
290  /* Copy existing datas */
291  spm2 = spmCopy(spm);
292  memcpy( graph, spm2, sizeof(pastix_graph_t) );
293 
294  /* A graph does not contain values */
295  if( spm->flttype != SpmPattern ) {
296  assert( graph->values != NULL );
297 
298  graph->flttype = SpmPattern;
299  memFree_null(graph->values);
300  }
301 
302  /* Make sure the graph is in CSC format */
303  spmConvert( SpmCSC, graph );
304 
305  /* Free the new allocated spm2 */
306  memFree_null( spm2 );
307 
308  return PASTIX_SUCCESS;
309 }
310 
311 /**
312  * @}
313  */
graphBase
void graphBase(pastix_graph_t *graph, pastix_int_t baseval)
Rebase the graph to the given value.
Definition: graph.c:70
graphSort
void graphSort(pastix_graph_t *graph)
This routine sortes the subarray of edges of each vertex.
Definition: graph.c:174
graphCopy
int graphCopy(pastix_graph_t *graphdst, const pastix_graph_t *graphsrc)
This routine copy a given ordering in a new one.
Definition: graph.c:125
graphSpm2Graph
int graphSpm2Graph(pastix_graph_t *graph, const spmatrix_t *spm)
This routine build a graph thanks to an spm;.
Definition: graph.c:271
graphUpdateComputedFields
int graphUpdateComputedFields(pastix_graph_t *graph)
Update dofs, nnz, nnzexp, gnnz, n, nexp, gN of a given graph.
Definition: graph.c:230
PASTIX_SUCCESS
@ PASTIX_SUCCESS
Definition: api.h:344
graphSymmetrize
int graphSymmetrize(pastix_graph_t *graph)
Symmetrize a given graph.
Definition: graph.c:199
graph.h
graphExit
void graphExit(pastix_graph_t *graph)
Free the content of the graph structure.
Definition: graph.c:41
PASTIX_ERR_BADPARAMETER
@ PASTIX_ERR_BADPARAMETER
Definition: api.h:351