Skip to content

Commit adcd31f

Browse files
testing diameter and multibfs
1 parent f8695d3 commit adcd31f

File tree

4 files changed

+270
-7
lines changed

4 files changed

+270
-7
lines changed

experimental/algorithm/LAGraph_EstimateDiameter.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
//------------------------------------------------------------------------------
1313

1414
// FIXME: almost ready for src; need to handle GxB
15-
// FIXME: need the CI test for this method
1615

1716
// Takes in a graph and estimates the diameter
1817
// and optionally also finds pseudo-peripheral nodes of the graph
@@ -157,6 +156,8 @@ int LAGraph_EstimateDiameter
157156
else
158157
{
159158
// srcs = randomized, still of size nsrcs-1, values in range 0 to UINT64_MAX
159+
// FIXME: if the graph is very sparse, select nodes at random with at
160+
// least one out-going edge.
160161
LAGRAPH_TRY (LAGraph_Random_Seed (srcs, seed, msg)) ;
161162
GRB_TRY (GxB_BinaryOp_new (&Mod,
162163
(n > INT32_MAX) ? ((GxB_binary_function)mod64) :

experimental/algorithm/LAGraph_ExactDiameter.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
//------------------------------------------------------------------------------
1313

1414
// FIXME: almost ready for src; says it's not vanilla but should be OK
15-
// FIXME: need the CI test for this method
1615

1716
// Takes in a graph and finds the diameter
1817
// and optionally also the peripheral nodes of the graph

experimental/algorithm/LAGraph_MultiSourceBFS.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
//------------------------------------------------------------------------------
1313

1414
// FIXME: almost ready for src; need a vanilla method
15-
// FIXME: need the CI test for this method
1615

1716
// Takes in a vector of source nodes and finds level and/or parent vectors for each,
1817
// stored together in a matrix
@@ -74,16 +73,17 @@ int LAGraph_MultiSourceBFS
7473
GrB_Matrix A = G->A ;
7574

7675
GrB_Index nsrc; // holds the number of source nodes
77-
GrB_Index n;
76+
GrB_Index n, nvals ;
7877
GRB_TRY (GrB_Matrix_nrows (&n, A)) ;
78+
GRB_TRY (GrB_Matrix_nvals (&nvals, A)) ;
7979
GRB_TRY (GrB_Vector_size (&nsrc, src)) ;
8080
for (int64_t s = 0; s < nsrc; s++)
8181
{
8282
GrB_Index currsrc;
8383
GRB_TRY (GrB_Vector_extractElement (&currsrc, src, s)) ;
8484
LG_ASSERT_MSG (currsrc < n, GrB_INVALID_INDEX, "invalid source node") ;
8585
}
86-
86+
bool very_sparse = (nvals <= n/16) ;
8787

8888
// determine the semiring type
8989
GrB_Type int_type = (n > INT32_MAX) ? GrB_INT64 : GrB_INT32 ;
@@ -98,7 +98,10 @@ int LAGraph_MultiSourceBFS
9898

9999
// create the parent matrix. pi(i, j) is the parent id of node j in source i's BFS
100100
GRB_TRY (GrB_Matrix_new (&pi, int_type, nsrc, n)) ;
101-
GRB_TRY (LG_SET_FORMAT_HINT (pi, LG_BITMAP + LG_FULL)) ;
101+
if (!very_sparse)
102+
{
103+
GRB_TRY (LG_SET_FORMAT_HINT (pi, LG_BITMAP + LG_FULL)) ;
104+
}
102105

103106
// pi (i, src) = src denotes the root of that row's BFS tree
104107
for (int64_t s = 0; s < nsrc; s++)
@@ -143,7 +146,10 @@ int LAGraph_MultiSourceBFS
143146
// create the level matrix. v(i,j) is the level of node j in source i's BFS
144147
// v (s, src) = 0 denotes the source node of that row
145148
GRB_TRY (GrB_Matrix_new (&v, int_type, nsrc, n)) ;
146-
GRB_TRY (LG_SET_FORMAT_HINT (v, LG_BITMAP + LG_FULL)) ;
149+
if (!very_sparse)
150+
{
151+
GRB_TRY (LG_SET_FORMAT_HINT (v, LG_BITMAP + LG_FULL)) ;
152+
}
147153
for (int64_t s = 0; s < nsrc; s++)
148154
{
149155
GrB_Index currsrc;

experimental/test/test_diameter.c

Lines changed: 257 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,257 @@
1+
//------------------------------------------------------------------------------
2+
// LAGraph/experimental/test/test_diameter.c: test diameter
3+
//------------------------------------------------------------------------------
4+
5+
// LAGraph, (c) 2019-2022 by The LAGraph Contributors, All Rights Reserved.
6+
// SPDX-License-Identifier: BSD-2-Clause
7+
//
8+
// For additional details (including references to third party source code and
9+
// other files) see the LICENSE file or contact [email protected]. See
10+
// Contributors.txt for a full list of contributors. Created, in part, with
11+
// funding and support from the U.S. Government (see Acknowledgments.txt file).
12+
// DM22-0790
13+
14+
// Contributed by Timothy A. Davis, Texas A&M University
15+
16+
//------------------------------------------------------------------------------
17+
18+
// FIXME: need to check the results with correct their values
19+
20+
#include <stdio.h>
21+
#include <acutest.h>
22+
23+
#include <LAGraphX.h>
24+
#include <LAGraph_test.h>
25+
26+
char msg [LAGRAPH_MSG_LEN] ;
27+
LAGraph_Graph G = NULL ;
28+
GrB_Matrix A = NULL ;
29+
GrB_Matrix C = NULL ;
30+
GrB_Vector peripheral = NULL ;
31+
GrB_Matrix level = NULL ;
32+
GrB_Matrix parent = NULL ;
33+
GrB_Vector sources = NULL ;
34+
GrB_Vector est_peripheral = NULL ;
35+
GrB_Vector eccentricity = NULL ;
36+
GrB_Index n ;
37+
#define LEN 512
38+
char filename [LEN+1] ;
39+
40+
typedef struct
41+
{
42+
bool symmetric ;
43+
const char *name ;
44+
}
45+
matrix_info ;
46+
47+
const matrix_info files [ ] =
48+
{
49+
{ 1, "A.mtx" },
50+
{ 1, "jagmesh7.mtx" },
51+
{ 0, "west0067.mtx" }, // unsymmetric
52+
{ 1, "bcsstk13.mtx" },
53+
{ 1, "karate.mtx" },
54+
{ 1, "ldbc-cdlp-undirected-example.mtx" },
55+
{ 1, "ldbc-undirected-example-bool.mtx" },
56+
{ 1, "ldbc-undirected-example-unweighted.mtx" },
57+
{ 1, "ldbc-undirected-example.mtx" },
58+
{ 1, "ldbc-wcc-example.mtx" },
59+
{ 0, "" },
60+
} ;
61+
62+
#undef OK
63+
#define OK(method) \
64+
{ \
65+
GrB_Info info = method ; \
66+
if (info != GrB_SUCCESS) \
67+
{ \
68+
printf ("info: %d, msg: %s\n", info, msg) ; \
69+
TEST_CHECK (false) ; \
70+
} \
71+
}
72+
73+
//------------------------------------------------------------------------------
74+
// test_diameter
75+
//------------------------------------------------------------------------------
76+
77+
void test_diameter (void)
78+
{
79+
OK (LAGraph_Init (msg)) ;
80+
OK (LAGraph_Random_Init (msg)) ;
81+
#if LAGRAPH_SUITESPARSE
82+
83+
for (int k = 0 ; ; k++)
84+
{
85+
86+
// load the matrix as A
87+
const char *aname = files [k].name ;
88+
bool symmetric = files [k].symmetric ;
89+
if (strlen (aname) == 0) break;
90+
printf ("\n================================== %s:\n", aname) ;
91+
TEST_CASE (aname) ;
92+
snprintf (filename, LEN, LG_DATA_DIR "%s", aname) ;
93+
FILE *f = fopen (filename, "r") ;
94+
TEST_CHECK (f != NULL) ;
95+
OK (LAGraph_MMRead (&A, f, msg)) ;
96+
OK (GrB_Matrix_nrows (&n, A)) ;
97+
LAGraph_PrintLevel pr = (n <= 100) ? LAGraph_COMPLETE : LAGraph_SHORT ;
98+
OK (LAGraph_Matrix_Print (A, pr, stdout, msg)) ;
99+
100+
// construct a directed graph G with adjacency matrix S
101+
int kind = symmetric ?
102+
LAGraph_ADJACENCY_UNDIRECTED :
103+
LAGraph_ADJACENCY_DIRECTED ;
104+
OK (LAGraph_New (&G, &A, kind, msg)) ;
105+
TEST_CHECK (A == NULL) ;
106+
107+
// compute the exact diameter
108+
GrB_Index diameter = 0 ;
109+
OK (LAGraph_ExactDiameter (&diameter, &peripheral, &eccentricity,
110+
G, 8, msg)) ;
111+
112+
printf ("\ndiameter: %" PRIu64 "\n", diameter) ;
113+
printf ("\nperipheral:\n") ;
114+
OK (LAGraph_Vector_Print (peripheral, pr, stdout, msg)) ;
115+
printf ("\neccentricity:\n") ;
116+
OK (LAGraph_Vector_Print (eccentricity, pr, stdout, msg)) ;
117+
118+
for (int jit = 0 ; jit <= 1 ; jit++)
119+
{
120+
OK (GxB_Global_Option_set (GxB_JIT_C_CONTROL,
121+
jit ? GxB_JIT_ON : GxB_JIT_OFF)) ;
122+
// compute the estimated diameter
123+
GrB_Index estimated_diameter = 0 ;
124+
OK (LAGraph_EstimateDiameter (&estimated_diameter, &est_peripheral,
125+
G, 8, 4, 42, msg)) ;
126+
printf ("\nest diameter: %" PRIu64 "\n", estimated_diameter) ;
127+
TEST_CHECK (estimated_diameter <= diameter) ;
128+
printf ("\nest_peripheral:\n") ;
129+
OK (LAGraph_Vector_Print (est_peripheral, pr, stdout, msg)) ;
130+
OK (GrB_free (&est_peripheral)) ;
131+
}
132+
133+
// try the multisource BFS directly
134+
OK (GrB_Vector_new (&sources, GrB_INT64, 4)) ;
135+
for (int i = 0 ; i < 4 ; i++)
136+
{
137+
OK (GrB_Vector_setElement (sources, i, i)) ;
138+
}
139+
OK (GrB_wait (sources, GrB_MATERIALIZE)) ;
140+
printf ("\nsource nodes for multiBFS:\n") ;
141+
OK (LAGraph_Vector_Print (sources, 5, stdout, msg)) ;
142+
OK (LAGraph_MultiSourceBFS (&level, &parent, G, sources, msg)) ;
143+
printf ("\nlevel:\n") ;
144+
OK (LAGraph_Matrix_Print (level, pr, stdout, msg)) ;
145+
printf ("\nparent:\n") ;
146+
OK (LAGraph_Matrix_Print (parent, pr, stdout, msg)) ;
147+
148+
OK (GrB_free (&sources)) ;
149+
OK (GrB_free (&level)) ;
150+
OK (GrB_free (&parent)) ;
151+
OK (GrB_free (&peripheral)) ;
152+
OK (GrB_free (&eccentricity)) ;
153+
OK (LAGraph_Delete (&G, msg)) ;
154+
}
155+
156+
#else
157+
printf ("test skipped\n") ;
158+
#endif
159+
OK (LAGraph_Random_Finalize (msg)) ;
160+
OK (LAGraph_Finalize (msg)) ;
161+
}
162+
163+
//------------------------------------------------------------------------------
164+
// test_diameter_huge
165+
//------------------------------------------------------------------------------
166+
167+
void test_diameter_huge (void)
168+
{
169+
OK (LAGraph_Init (msg)) ;
170+
OK (LAGraph_Random_Init (msg)) ;
171+
#if LAGRAPH_SUITESPARSE
172+
OK (GxB_Global_Option_set (GxB_JIT_C_CONTROL, GxB_JIT_OFF)) ;
173+
174+
snprintf (filename, LEN, LG_DATA_DIR "%s", "karate.mtx") ;
175+
FILE *f = fopen (filename, "r") ;
176+
TEST_CHECK (f != NULL) ;
177+
OK (LAGraph_MMRead (&C, f, msg)) ;
178+
OK (GrB_Matrix_nrows (&n, C)) ;
179+
OK (LAGraph_Matrix_Print (C, 5, stdout, msg)) ;
180+
181+
GrB_Index *I = NULL ;
182+
OK (LAGraph_Malloc ((void **) &I, n, sizeof (uint64_t), msg)) ;
183+
for (int k = 0 ; k < n ; k++)
184+
{
185+
I [k] = k ;
186+
}
187+
188+
// A (0:n-1, 0:n-1) = C, where C is n-by-n
189+
OK (GrB_Matrix_new (&A, GrB_BOOL, UINT32_MAX, UINT32_MAX)) ;
190+
OK (GrB_assign (A, NULL, NULL, C, I, n, I, n, NULL)) ;
191+
192+
// construct the graph
193+
OK (LAGraph_New (&G, &A, LAGraph_ADJACENCY_UNDIRECTED, msg)) ;
194+
TEST_CHECK (A == NULL) ;
195+
196+
// compute the estimated diameter
197+
GrB_Index estimated_diameter = 0 ;
198+
OK (LAGraph_EstimateDiameter (&estimated_diameter, &est_peripheral,
199+
G, 8, 4, 42, msg)) ;
200+
printf ("\nest diameter: %" PRIu64 "\n", estimated_diameter) ;
201+
printf ("\nest_peripheral:\n") ;
202+
OK (LAGraph_Vector_Print (est_peripheral, 2, stdout, msg)) ;
203+
OK (GrB_free (&est_peripheral)) ;
204+
205+
// try the multisource BFS directly
206+
OK (GrB_Vector_new (&sources, GrB_INT64, 4)) ;
207+
for (int i = 0 ; i < 4 ; i++)
208+
{
209+
OK (GrB_Vector_setElement (sources, i, i)) ;
210+
}
211+
OK (GrB_wait (sources, GrB_MATERIALIZE)) ;
212+
printf ("\nsource nodes for multiBFS:\n") ;
213+
OK (LAGraph_Vector_Print (sources, 5, stdout, msg)) ;
214+
OK (LAGraph_MultiSourceBFS (&level, &parent, G, sources, msg)) ;
215+
printf ("\nlevel:\n") ;
216+
OK (LAGraph_Matrix_Print (level, 2, stdout, msg)) ;
217+
printf ("\nparent:\n") ;
218+
OK (LAGraph_Matrix_Print (parent, 2, stdout, msg)) ;
219+
220+
OK (GrB_free (&sources)) ;
221+
OK (GrB_free (&level)) ;
222+
OK (GrB_free (&parent)) ;
223+
OK (GrB_free (&C)) ;
224+
OK (LAGraph_Free ((void **) &I, msg)) ;
225+
OK (LAGraph_Delete (&G, msg)) ;
226+
227+
#else
228+
printf ("test skipped\n") ;
229+
#endif
230+
OK (LAGraph_Random_Finalize (msg)) ;
231+
OK (LAGraph_Finalize (msg)) ;
232+
}
233+
234+
//------------------------------------------------------------------------------
235+
// test_errors
236+
//------------------------------------------------------------------------------
237+
238+
void test_errors (void)
239+
{
240+
LAGraph_Init (msg) ;
241+
#if LAGRAPH_SUITESPARSE
242+
// FIXME
243+
#else
244+
printf ("test skipped\n") ;
245+
#endif
246+
LAGraph_Finalize (msg) ;
247+
}
248+
249+
//****************************************************************************
250+
251+
TEST_LIST = {
252+
{"diameter", test_diameter},
253+
{"diameter_huge", test_diameter_huge},
254+
{"diameter_errors", test_errors},
255+
{NULL, NULL}
256+
} ;
257+

0 commit comments

Comments
 (0)