-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLAGraph_bfs_parent2.c
391 lines (318 loc) · 14.1 KB
/
LAGraph_bfs_parent2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
//------------------------------------------------------------------------------
// LAGraph_bfs_parent: push-pull breadth-first search
//------------------------------------------------------------------------------
/*
LAGraph: graph algorithms based on GraphBLAS
Copyright 2020 LAGraph Contributors.
SPDX-License-Identifier: BSD-2
(see Contributors.txt for a full list of Contributors; see
ContributionInstructions.txt for information on how you can Contribute to
this project).
All Rights Reserved.
NO WARRANTY. THIS MATERIAL IS FURNISHED ON AN "AS-IS" BASIS. THE LAGRAPH
CONTRIBUTORS MAKE NO WARRANTIES OF ANY KIND, EITHER EXPRESSED OR IMPLIED,
AS TO ANY MATTER INCLUDING, BUT NOT LIMITED TO, WARRANTY OF FITNESS FOR
PURPOSE OR MERCHANTABILITY, EXCLUSIVITY, OR RESULTS OBTAINED FROM USE OF
THE MATERIAL. THE CONTRIBUTORS DO NOT MAKE ANY WARRANTY OF ANY KIND WITH
RESPECT TO FREEDOM FROM PATENT, TRADEMARK, OR COPYRIGHT INFRINGEMENT.
Released under a BSD license, please see the LICENSE file distributed with
this Software or contact permission@sei.cmu.edu for full terms.
Created, in part, with funding and support from the United States
Government. (see Acknowledgments.txt file).
This program includes and/or can make use of certain third party source
code, object code, documentation and other files ("Third Party Software").
See LICENSE file for more details.
*/
//------------------------------------------------------------------------------
// LAGraph_bfs_parent2: direction-optimized push/pull breadth first search,
// contributed by Tim Davis, Texas A&M. Computes only the BFS tree.
// Requires SuiteSparse:GraphBLAS v4.0.1.
// Usage:
// info = LAGraph_bfs_parent2 (&pi, A, AT, source) ;
// GrB_Vector *pi: a vector containing the BFS tree, in 0-based indexing.
// pi(source) = source for source node. pi(i) = p if p is the parent
// of i. If pi(i) is not present, then node i has not been reached.
// GrB_Matrix A: a square matrix of any type. The values of A are not
// accessed. The presence of the entry A(i,j) indicates the edge
// (i,j). That is, an explicit entry A(i,j)=0 is treated as an edge.
// GrB_Matrix AT: an optional matrix of any type. If NULL, the algorithm
// is a conventional push-only BFS. If not NULL, AT must be the
// transpose of A, and a push-pull algorithm is used (NOTE: this
// assumes GraphBLAS stores its matrix in CSR form; see discussion in
// LAGraph_bfs_pushpull). Results are undefined if AT is not NULL but
// not identical to the transpose of A.
// int64_t source: the source node for the BFS.
// This algorithm can use the push-pull strategy, which requires both A and
// AT=A' to be passed in. If the graph is known to be symmetric, then the same
// matrix A can be passed in for both arguments. Results are undefined if AT
// is not the transpose of A.
// See LAGraph_bfs_pushpull for a discussion of the push/pull strategy.
// References:
// Carl Yang, Aydin Buluc, and John D. Owens. 2018. Implementing Push-Pull
// Efficiently in GraphBLAS. In Proceedings of the 47th International
// Conference on Parallel Processing (ICPP 2018). ACM, New York, NY, USA,
// Article 89, 11 pages. DOI: https://doi.org/10.1145/3225058.3225122
// Scott Beamer, Krste Asanovic and David A. Patterson, The GAP Benchmark
// Suite, http://arxiv.org/abs/1508.03619, 2015. http://gap.cs.berkeley.edu/
#include "LAGraph_internal.h"
#define LAGRAPH_FREE_ALL \
{ \
GrB_free (&w) ; \
GrB_free (&q) ; \
GrB_free (&pi) ; \
}
int LAGraph_BreadthFirstSearch // easy-mode
(
// outputs:
GrB_Vector *level_handle, // if NULL do not compute
GrB_Vector *parent_handle, // if NULL do not compute
// inputs:
LAGraph_Graph G,
GrB_Index source,
char *message // for error handling; may be NULL
// convention: 0:ok, < error, > warning.
// if no message, we set message [0] = '\0' (if not NULL)
) ;
GrB_Info LAGraph_bfs_parent2 // push-pull BFS, compute the tree only
(
// output:
GrB_Vector *pi_output, // pi(i) = p if p is the parent of node i
// inputs:
GrB_Matrix A, // input graph, any type
GrB_Matrix AT, // transpose of A (optional; push-only if NULL)
GrB_Vector Degree, // Degree(i) is the out-degree of node i
// (optional: push-only if NULL)
int64_t source // starting node of the BFS
)
{
//--------------------------------------------------------------------------
// check inputs
//--------------------------------------------------------------------------
int info ;
GrB_Vector q = NULL ; // the current frontier
GrB_Vector pi = NULL ; // parent vector
GrB_Vector w = NULL ; // to compute work remaining
GrB_Vector level = NULL ; // level vector
if (G == NULL || G->A == NULL)
{
// required argument is missing or mangled
LAGRAPH_ERROR ("required arguments are NULL", GrB_NULL_POINTER) ;
}
GrB_Index nrows, ncols, nvals ;
GrB_Matrix A = G.A ;
GrB_Matrix AT = G.AT ;
GrB_Vector Degree = G.rowdegree ;
LAGraph_TRY (GrB_Matrix_nrows (&nrows, A)) ;
LAGraph_TRY (GrB_Matrix_ncols (&ncols, A)) ;
LAGraph_TRY (GrB_Matrix_nvals (&nvals, A)) ;
if (nrows != ncols)
{
// A must be square
LAGRAPH_ERROR ("A must be square", GrB_INVALID_VALUE) ;
}
//--------------------------------------------------------------------------
// check the format of A and AT
//--------------------------------------------------------------------------
#ifdef SS4
LAGraph_SS_bfs_parent ( ... ) ;
bool A_csr = true, AT_csr = true ;
if (A != NULL)
{
GxB_Format_Value A_format ;
LAGr_get (A , GxB_FORMAT, &A_format) ;
A_csr = (A_format == GxB_BY_ROW) ;
}
if (AT != NULL)
{
GxB_Format_Value AT_format ;
LAGr_get (AT, GxB_FORMAT, &AT_format) ;
AT_csr = (AT_format == GxB_BY_ROW) ;
}
bool vxm_is_push = (A != NULL && A_csr ) ; // vxm (q,A) is a push step
bool vxm_is_pull = (A != NULL && !A_csr ) ; // vxm (q,A) is a pull step
bool mxv_is_push = (AT != NULL && !AT_csr) ; // mxv (AT,q) is a push step
bool mxv_is_pull = (AT != NULL && AT_csr) ; // mxv (AT,q) is a pull step
// can_push is true if the push-step can be performed
bool can_push = vxm_is_push || mxv_is_push ;
// can_pull is true if the pull-step can be performed
bool can_pull = vxm_is_pull || mxv_is_pull ;
// direction-optimization requires both push and pull, and Degree
bool push_pull = can_push && can_pull && (Degree != NULL) ;
//--------------------------------------------------------------------------
// initializations
//--------------------------------------------------------------------------
GrB_Index n = nrows ;
GrB_Type int_type = (n > INT32_MAX) ? GrB_INT64 : GrB_INT32 ;
GrB_Semiring semiring ;
// create an sparse integer vector q, and set q(source) = source
LAGr_Vector_new (&q, int_type, n) ;
LAGr_Vector_setElement (q, source, source) ;
GrB_Index nq = 1 ; // number of nodes in the current level
if (parent == NULL)
// just want the level, not parent
semiring = GrB_LOR_LAND_BOOL ;
semiring = GxB_ANY_PAIR_BOOL ;
else
{
// need the parent
if (n > INT32_MAX)
{
semiring = GrB_MIN_FIRST_INT64 ;
semiring = GxB_ANY_SECONDI_INT64 ;
}
else
{
semiring = GxB_ANY_SECONDI_INT32 ;
}
}
// pi = an empty bitmap vector
LAGr_Vector_new (&pi, int_type, n) ;
GxB_set (pi, GxB_SPARSITY_CONTROL, GxB_BITMAP + GxB_FULL) ;
// pi (source) = source denotes a root of the BFS tree
LAGr_Vector_setElement (pi, source, source) ;
if (push_pull) LAGr_Vector_new (&w, GrB_INT64, n) ;
double alpha = 8.0 ;
double beta1 = 8.0 ;
double beta2 = 512.0 ;
int64_t n_over_beta1 = (int64_t) (((double) n) / beta1) ;
int64_t n_over_beta2 = (int64_t) (((double) n) / beta2) ;
//--------------------------------------------------------------------------
// BFS traversal and label the nodes
//--------------------------------------------------------------------------
bool do_push = can_push ; // start with push, if available
GrB_Index last_nq = 0 ;
int64_t edges_unexplored = nvals ;
bool any_pull = false ; // true if any pull phase has been done
for (int64_t nvisited = 1 ; nvisited < n ; nvisited += nq)
{
//----------------------------------------------------------------------
// select push vs pull
//----------------------------------------------------------------------
int64_t edges_in_frontier = 0 ;
if (push_pull)
{
if (do_push && can_pull)
{
// check for switch from push to pull
bool growing = nq > last_nq ;
bool switch_to_pull ;
if (edges_unexplored < n)
{
// very little of the graph is left; disable the pull
push_pull = false ;
}
else if (any_pull)
{
// once any pull phase has been done, the # of edges in the
// frontier has no longer been tracked. But now the BFS
// has switched back to push, and we're checking for yet
// another switch to pull. This switch is unlikely, so
// just keep track of the size of the frontier, and switch
// if it starts growing again and is getting big.
switch_to_pull = (growing && nq > n_over_beta1) ;
}
else
{
// update the # of unexplored edges
// w<q>=Degree
// w(i) = outdegree of node i if node i is in the queue
LAGr_assign (w, q, NULL, Degree, GrB_ALL, n, GrB_DESC_RS) ;
// edges_in_frontier = sum (w) = # of edges incident on all
// nodes in the current frontier
LAGr_reduce (&edges_in_frontier, NULL,
GrB_PLUS_MONOID_INT64, w, NULL) ;
edges_unexplored -= edges_in_frontier ;
switch_to_pull = growing &&
(edges_in_frontier > (edges_unexplored / alpha)) ;
}
if (switch_to_pull)
{
// the # of edges incident on
do_push = false ;
}
}
else if (!do_push && can_push)
{
// check for switch from pull to push
bool shrinking = nq < last_nq ;
if (shrinking && (nq <= n_over_beta2))
{
do_push = true ;
}
}
}
any_pull = any_pull || (!do_push) ;
//----------------------------------------------------------------------
// q = next level of the BFS
//----------------------------------------------------------------------
GxB_set ((GrB_Matrix) q, GxB_SPARSITY_CONTROL,
do_push ? GxB_SPARSE : GxB_BITMAP) ;
if ((do_push && vxm_is_push) || (!do_push && vxm_is_pull))
{
// q'<!pi> = q'*A
// this is a push if A is in CSR format; pull if A is in CSC
LAGr_vxm (q, pi, NULL, semiring, q, A, GrB_DESC_RSC) ;
}
else // ((!do_push && mxv_is_pull) || (do_push && mxv_is_push))
{
// q<!pi> = AT*q
// this is a pull if AT is in CSR format; push if AT is in CSC
LAGr_mxv (q, pi, NULL, semiring, AT, q, GrB_DESC_RSC) ;
}
last_nq = nq ;
LAGr_Vector_nvals (&nq, q) ;
if (nq == 0) break ;
//----------------------------------------------------------------------
// assign parents
//----------------------------------------------------------------------
// q(i) currently contains the parent id of node i in tree.
// pi<q> = q
LAGr_assign (pi, q, NULL, q, GrB_ALL, n, GrB_DESC_S) ;
}
#else
if ( .. parent ... and no level)
{
// parent only
LAGRAPH_TRY (GrB_assign (pi, NULL, NULL, -1, GrB_ALL, n, GrB_DESC_S)) ;
while (1)
{
LAGRAPH_TRY (GrB_assign (q, q, NULL, ramp, GrB_ALL, n, GrB_DESC_S));
LAGRAPH_TRY (GrB_vxm (q, pi, NULL, semiring, q, A, GrB_DESC_RSC)) ;
LAGRAPH_TRY (GrB_Vector_nvals (&nq, q)) ;
if (nq == 0) break ;
LAGRAPH_TRY (GrB_assign (pi, q, NULL, q, GrB_ALL, n, GrB_DESC_S) ;
}
}
else if both parent and level
{
// parent and level
for (int64_t k = 0 ; k < n ; k++)
{
LAGRAPH_TRY (GrB_assign (q, q, NULL, ramp, GrB_ALL, n, GrB_DESC_S));
LAGRAPH_TRY (GrB_vxm (q, pi, NULL, semiring, q, A, GrB_DESC_RSC)) ;
LAGRAPH_TRY (GrB_Vector_nvals (&nq, q)) ;
if (nq == 0) break ;
LAGRAPH_TRY (GrB_assign (pi, q, NULL, q, GrB_ALL, n, GrB_DESC_S) ;
LAGRAPH_TRY (GrB_assign (level, q, NULL, k, GrB_ALL, n, GrB_DESC_S) ;
}
}
else
{
// level only
for (int64_t k = 0 ; k < n ; k++)
{
LAGRAPH_TRY (GrB_vxm (q, pi, NULL, semiring, q, A, GrB_DESC_RSC)) ;
LAGRAPH_TRY (GrB_Vector_nvals (&nq, q)) ;
if (nq == 0) break ;
LAGRAPH_TRY (GrB_assign (level, q, NULL, k, GrB_ALL, n, GrB_DESC_S) ;
}
#endif
//--------------------------------------------------------------------------
// free workspace and return result
//--------------------------------------------------------------------------
(*pi_output) = pi ;
pi = NULL ;
LAGRAPH_FREE_ALL ; // free all workspace (except for result pi)
return (GrB_SUCCESS) ;
#endif
}