Skip to content

Commit bc963e4

Browse files
committed
testing scc with Vector Hash
1 parent b265310 commit bc963e4

File tree

2 files changed

+112
-105
lines changed

2 files changed

+112
-105
lines changed

experimental/test/test_scc.c

Lines changed: 57 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -42,66 +42,65 @@ int scc_cover [7] = { 0, 0, 2, 0, 4, 2, 0 } ;
4242

4343
const matrix_info files [ ] =
4444
{
45-
{ "A2.mtx", 1, 6493938657738929428ull},
46-
{ "A.mtx", 1, 6493938657738929428ull},
47-
{ "bcsstk13.mtx", 1, 4873650117803742346ull},
48-
{ "cover.mtx", 3, 848279640410529436ull},
49-
{ "cover_structure.mtx", 3, 848279640410529436ull},
50-
{ "cryg2500.mtx", 1, 8070599988610413093ull},
51-
{ "full.mtx", 1, 15769435293242772098ull},
52-
{ "full_noheader.mtx", 1, 15769435293242772098ull},
53-
{ "full_symmetric.mtx", 1, 7920595475144714245ull},
54-
{ "jagmesh7.mtx", 1, 5114200449021899176ull},
55-
{ "karate.mtx", 1, 4176608668907330736ull},
56-
{ "ldbc-cdlp-directed-example.mtx", 2, 11183292771650049706ull},
57-
{ "ldbc-cdlp-undirected-example.mtx", 1, 4918287057298807835ull},
58-
{ "ldbc-directed-example-bool.mtx", 7, 11304580677056001228ull},
59-
{ "ldbc-directed-example.mtx", 7, 11304580677056001228ull},
60-
{ "ldbc-directed-example-unweighted.mtx", 7, 11304580677056001228ull},
61-
{ "ldbc-undirected-example-bool.mtx", 1, 9158223257798130275ull},
62-
{ "ldbc-undirected-example.mtx", 1, 9158223257798130275ull},
63-
{ "ldbc-undirected-example-unweighted.mtx", 1, 9158223257798130275ull},
64-
{ "ldbc-wcc-example.mtx", 1, 4317729120311459500ull},
65-
{ "LFAT5.mtx", 3, 17553140753101484131ull},
66-
{ "LFAT5_two.mtx", 6, 7979561620824911ull},
67-
{ "matrix_bool.mtx", 3, 848279640410529436ull},
68-
{ "matrix_fp32.mtx", 3, 848279640410529436ull},
69-
{ "matrix_fp32_structure.mtx", 3, 848279640410529436ull},
70-
{ "matrix_fp64.mtx", 3, 848279640410529436ull},
71-
{ "matrix_int16.mtx", 3, 848279640410529436ull},
72-
{ "matrix_int32.mtx", 3, 848279640410529436ull},
73-
{ "matrix_int64.mtx", 3, 848279640410529436ull},
74-
{ "matrix_int8.mtx", 3, 848279640410529436ull},
75-
{ "matrix_uint16.mtx", 3, 848279640410529436ull},
76-
{ "matrix_uint32.mtx", 3, 848279640410529436ull},
77-
{ "matrix_uint64.mtx", 3, 848279640410529436ull},
78-
{ "matrix_uint8.mtx", 3, 848279640410529436ull},
79-
{ "msf1.mtx", 4, 3301616701375337755ull},
80-
{ "msf2.mtx", 8, 11227097946539390519ull},
81-
{ "msf3.mtx", 5, 5965767602828141907ull},
82-
{ "olm1000.mtx", 1, 14473458856538426155ull},
83-
{ "pushpull.mtx", 1, 14764381483900318255ull},
84-
{ "sample2.mtx", 1, 4918287057298807835ull},
85-
{ "sample.mtx", 8, 11227097946539390519ull},
86-
{ "structure.mtx", 3, 848279640410529436ull},
87-
{ "test_BF.mtx", 3, 848279640410529436ull},
88-
{ "test_FW_1000.mtx", 1, 14473458856538426155ull},
89-
{ "test_FW_2003.mtx", 485, 13924889949050000093ull},
90-
{ "test_FW_2500.mtx", 646, 9579946550331191330ull},
91-
{ "tree-example.mtx", 1, 16959292359894689422ull},
92-
{ "west0067_jumbled.mtx", 1, 15563611237648677666ull},
93-
{ "west0067.mtx", 1, 15563611237648677666ull},
94-
{ "west0067_noheader.mtx", 1, 15563611237648677666ull},
95-
{ "zenios.mtx", 1391, 10773678236411609506ull},
45+
{ "A2.mtx", 1, 0x2de8d717be626313},
46+
{ "A.mtx", 1, 0x2de8d717be626313},
47+
{ "bcsstk13.mtx", 1, 0x41d903f08b46b543},
48+
{ "cover.mtx", 3, 0x30ae8cb78a807691},
49+
{ "cover_structure.mtx", 3, 0x30ae8cb78a807691},
50+
{ "cryg2500.mtx", 1, 0xd1cb8e3cc6be967},
51+
{ "full.mtx", 1, 0x99971e4f016b4644},
52+
{ "full_noheader.mtx", 1, 0x99971e4f016b4644},
53+
{ "full_symmetric.mtx", 1, 0x278859fec1de1f7f},
54+
{ "jagmesh7.mtx", 1, 0x66b315eea17941c8},
55+
{ "karate.mtx", 1, 0x8bad7c50644c4aa9},
56+
{ "ldbc-cdlp-directed-example.mtx", 2, 0x3a61ac294b7bb114},
57+
{ "ldbc-cdlp-undirected-example.mtx", 1, 0x4072e255fd8e310a},
58+
{ "ldbc-directed-example-bool.mtx", 7, 0xc66f5ecf1b7f6876},
59+
{ "ldbc-directed-example.mtx", 7, 0xc66f5ecf1b7f6876},
60+
{ "ldbc-directed-example-unweighted.mtx", 7, 0xc66f5ecf1b7f6876},
61+
{ "ldbc-undirected-example-bool.mtx", 1, 0xf53db7dbbeff3283},
62+
{ "ldbc-undirected-example.mtx", 1, 0xf53db7dbbeff3283},
63+
{ "ldbc-undirected-example-unweighted.mtx", 1, 0xf53db7dbbeff3283},
64+
{ "ldbc-wcc-example.mtx", 1, 0x36a78022528a2101},
65+
{ "LFAT5.mtx", 3, 0x79d4d8de0a22a863},
66+
{ "LFAT5_two.mtx", 6, 0xac369d0362d73d6},
67+
{ "matrix_bool.mtx", 3, 0x30ae8cb78a807691},
68+
{ "matrix_fp32.mtx", 3, 0x30ae8cb78a807691},
69+
{ "matrix_fp32_structure.mtx", 3, 0x30ae8cb78a807691},
70+
{ "matrix_fp64.mtx", 3, 0x30ae8cb78a807691},
71+
{ "matrix_int16.mtx", 3, 0x30ae8cb78a807691},
72+
{ "matrix_int32.mtx", 3, 0x30ae8cb78a807691},
73+
{ "matrix_int64.mtx", 3, 0x30ae8cb78a807691},
74+
{ "matrix_int8.mtx", 3, 0x30ae8cb78a807691},
75+
{ "matrix_uint16.mtx", 3, 0x30ae8cb78a807691},
76+
{ "matrix_uint32.mtx", 3, 0x30ae8cb78a807691},
77+
{ "matrix_uint64.mtx", 3, 0x30ae8cb78a807691},
78+
{ "matrix_uint8.mtx", 3, 0x30ae8cb78a807691},
79+
{ "msf1.mtx", 4, 0x6445d984131a9555},
80+
{ "msf2.mtx", 8, 0x72d532720c54b673},
81+
{ "msf3.mtx", 5, 0xf57eb057beb5a5c7},
82+
{ "olm1000.mtx", 1, 0x15cf9ea2db88ab18},
83+
{ "pushpull.mtx", 1, 0x1816384cd04f7e01},
84+
{ "sample2.mtx", 1, 0x4072e255fd8e310a},
85+
{ "sample.mtx", 8, 0x72d532720c54b673},
86+
{ "structure.mtx", 3, 0x30ae8cb78a807691},
87+
{ "test_BF.mtx", 3, 0x30ae8cb78a807691},
88+
{ "test_FW_1000.mtx", 1, 0x15cf9ea2db88ab18},
89+
{ "test_FW_2003.mtx", 485, 0xf79ad45d3a704eec},
90+
{ "test_FW_2500.mtx", 646, 0x4fa83d60352e7e19},
91+
{ "tree-example.mtx", 1, 0x8857b82baeba129},
92+
{ "west0067_jumbled.mtx", 1, 0xa861dc7526128ac7},
93+
{ "west0067.mtx", 1, 0xa861dc7526128ac7},
94+
{ "west0067_noheader.mtx", 1, 0xa861dc7526128ac7},
95+
{ "zenios.mtx", 1391, 0x15b2b99a80c3480e},
9696
{ "", 0, 0},
9797
} ;
98+
9899
//------------------------------------------------------------------------------
99100
// count_connected_components: count the # of components in a component vector
100101
//------------------------------------------------------------------------------
101102

102-
int count_connected_components (GrB_Vector C, uint64_t *vector_hash) ;
103-
104-
int count_connected_components (GrB_Vector C, uint64_t *vector_hash)
103+
int count_connected_components (GrB_Vector C)
105104
{
106105
GrB_Index n = 0 ;
107106
OK (GrB_Vector_size (&n, C)) ;
@@ -111,12 +110,6 @@ int count_connected_components (GrB_Vector C, uint64_t *vector_hash)
111110
int64_t comp = -1 ;
112111
int result = GrB_Vector_extractElement (&comp, C, i) ;
113112
if (result == GrB_SUCCESS && comp == i) ncomponents++ ;
114-
//hash all of the values into one number
115-
if (result == GrB_SUCCESS && vector_hash)
116-
{
117-
(*vector_hash) *= 89734512321ull;
118-
(*vector_hash) += comp + i;
119-
}
120113
}
121114
return (ncomponents) ;
122115
}
@@ -173,15 +166,13 @@ void test_scc (void)
173166
TEST_CHECK (ok) ;
174167
OK (GrB_free (&cgood)) ;
175168
}
176-
uint64_t hash = 9238018047ull;
177-
int result_cc_count = count_connected_components(c, &hash);
169+
int result_cc_count = count_connected_components(c);
178170
TEST_CHECK(result_cc_count == files[k].cc_count);
179-
TEST_CHECK(hash == files[k].hash);
180171
OK (LAGraph_Vector_Print (c, pr, stdout, msg)) ;
181-
uint64_t hash2;
182-
int hash_info = LAGraph_Hash_Vector(&hash2, c, msg);
172+
uint64_t hash = 0;
173+
int hash_info = LAGraph_Hash_Vector(&hash, c, msg);
183174
OK (hash_info);
184-
printf("%lx\n", hash2);
175+
TEST_CHECK(hash == files[k].hash);
185176
OK (GrB_free (&c)) ;
186177
}
187178
OK (GrB_free (&A)) ;

experimental/utility/LAGraph_Matrix_Hash.c

Lines changed: 55 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -22,49 +22,65 @@
2222
#undef LG_FREE_ALL
2323
#define LG_FREE_ALL \
2424
{ \
25-
GrB_free(&x); \
26-
GrB_free(&s); \
25+
GrB_free(&C); \
2726
GrB_free(&lg_hash_edge); \
28-
GrB_free(&lg_hash_edge_biop); \
29-
GrB_free(&lg_xor_hash); \
3027
}
3128

32-
static void hash_edge(
33-
uint64_t *z,
34-
uint64_t *x,
35-
GrB_Index ix,
36-
GrB_Index jx,
37-
uint64_t *y,
38-
GrB_Index iy,
39-
GrB_Index jy,
40-
uint64_t thunk
41-
) {
42-
uint64_t a = *x;
43-
a ^= ix + 0x9e3779b97f4a7c15ULL;
44-
a = (a << 13) | (a >> (64 - 13));
45-
a ^= jx + 0x9e3779b97f4a7c15ULL;
46-
a = (a << 17) | (a >> (64 - 17));
47-
*z = a;
29+
#define GOLDEN_GAMMA 0x9E3779B97F4A7C15LL
30+
31+
// The init function computes a cheesy hash based on splitmix64t
32+
void hash_edge (uint64_t *z, const uint64_t *x,
33+
GrB_Index i, GrB_Index j, const uint64_t *seed)
34+
{
35+
uint64_t result = (i + j * i + GOLDEN_GAMMA) ;
36+
result = (result ^ (*x)) * 0xBF58476D1CE4E5B9LL ;
37+
result = (result ^ (result >> 30)) * 0xBF58476D1CE4E5B9LL ;
38+
result = (result ^ (result >> 27)) * 0x94D049BB133111EBLL ;
39+
result = (result ^ (result >> 31)) ;
40+
(*z) = result ;
4841
}
4942

50-
GrB_Info LAGraph_Hash_Vector(uint64_t *hash, GrB_Vector v, char *msg){
51-
GrB_Vector x = NULL;
52-
GrB_Scalar s = NULL;
53-
GxB_IndexBinaryOp lg_hash_edge = NULL;
54-
GrB_BinaryOp lg_hash_edge_biop = NULL;
55-
GrB_Semiring lg_xor_hash = NULL;
56-
GrB_Index nrows;
57-
GRB_TRY (GrB_Vector_size(&nrows, v)) ;
58-
GRB_TRY (GrB_Scalar_new(&s, GrB_UINT64)) ;
59-
GRB_TRY (GrB_Scalar_setElement_UINT64(s, 0)) ;
60-
GRB_TRY (GrB_Vector_new(&x, GrB_UINT64, nrows)) ;
61-
GRB_TRY (GrB_Vector_assign_UINT64(x, NULL, NULL, (uint64_t) 0, GrB_ALL, 0, NULL)) ;
62-
GRB_TRY (GxB_IndexBinaryOp_new(&lg_hash_edge, (GxB_index_binary_function) hash_edge,
63-
GrB_UINT64, GrB_UINT64, GrB_UINT64, GrB_UINT64, NULL, NULL)) ;
64-
GRB_TRY (GxB_BinaryOp_new_IndexOp(&lg_hash_edge_biop, lg_hash_edge, s)) ;
65-
GRB_TRY (GrB_Semiring_new (&lg_xor_hash, GxB_BXOR_UINT64_MONOID, lg_hash_edge_biop)) ;
66-
GRB_TRY (GrB_mxv((GrB_Vector) s, NULL, NULL, lg_xor_hash, (GrB_Matrix) v, x, GrB_DESC_T0)) ;
67-
GRB_TRY (GrB_Scalar_extractElement_UINT64(hash, s)) ;
43+
#define HASH_EDGE_DEF \
44+
"void hash_edge (uint64_t *z, const uint64_t *x,\n" \
45+
" GrB_Index i, GrB_Index j, const uint64_t *seed)\n" \
46+
"{\n" \
47+
" uint64_t result = (i + j * i + 0x9E3779B97F4A7C15LL) ;\n" \
48+
" result = (result ^ (*x)) * 0xBF58476D1CE4E5B9LL ;\n" \
49+
" result = (result ^ (result >> 30)) * 0xBF58476D1CE4E5B9LL ;\n" \
50+
" result = (result ^ (result >> 27)) * 0x94D049BB133111EBLL ;\n" \
51+
" result = (result ^ (result >> 31)) ;\n" \
52+
" (*z) = result ;\n" \
53+
"}\n"
54+
55+
GrB_Info LAGraph_Hash_Matrix(
56+
uint64_t *hash, // [output] hash
57+
const GrB_Matrix A, // matrix to hash
58+
char *msg
59+
) {
60+
GrB_Matrix C = NULL;
61+
GrB_IndexUnaryOp lg_hash_edge = NULL;
62+
GrB_Index nrows, ncols;
63+
GRB_TRY (GrB_Matrix_nrows(&nrows, A)) ;
64+
GRB_TRY (GrB_Matrix_ncols(&ncols, A)) ;
65+
GRB_TRY (GrB_Matrix_new(&C, GrB_UINT64, nrows, ncols)) ;
66+
GRB_TRY (GxB_IndexUnaryOp_new(
67+
&lg_hash_edge, (GxB_index_unary_function) hash_edge,
68+
GrB_UINT64, GrB_UINT64, GrB_UINT64, "hash_edge", HASH_EDGE_DEF)) ;
69+
70+
// TODO: C takes extra memory which is not nessesary for this computation.
71+
// Compute without extra memory if possible.
72+
GRB_TRY (GrB_Matrix_apply_IndexOp_UINT64(
73+
C, NULL, NULL, lg_hash_edge, A, (uint64_t) 0, NULL));
74+
GRB_TRY (GrB_Matrix_reduce_UINT64(
75+
hash, GrB_BXOR_UINT64, GxB_BXOR_UINT64_MONOID, C, NULL)) ;
6876
LG_FREE_ALL;
6977
return GrB_SUCCESS;
70-
}
78+
}
79+
80+
GrB_Info LAGraph_Hash_Vector(
81+
uint64_t *hash, // [output] hash
82+
const GrB_Vector v, // Vector to hash
83+
char *msg
84+
) {
85+
return LAGraph_Hash_Matrix(hash, (GrB_Matrix) v, NULL);
86+
}

0 commit comments

Comments
 (0)