|
| 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