Skip to content

Commit

Permalink
add mg tests for all pairs jaccard
Browse files Browse the repository at this point in the history
  • Loading branch information
jnke2016 committed Jul 3, 2024
1 parent 4a89937 commit 5249f02
Showing 1 changed file with 59 additions and 4 deletions.
63 changes: 59 additions & 4 deletions cpp/tests/c_api/mg_similarity_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ typedef int32_t vertex_t;
typedef int32_t edge_t;
typedef float weight_t;

typedef enum { JACCARD, SORENSEN, OVERLAP } similarity_t;
typedef enum { JACCARD, SORENSEN, OVERLAP, ALL_PAIRS_JACCARD, ALL_PAIRS_SORENSEN, ALL_PAIRS_OVERLAP } similarity_t;

int generic_similarity_test(const cugraph_resource_handle_t* handle,
vertex_t* h_src,
Expand All @@ -42,6 +42,7 @@ int generic_similarity_test(const cugraph_resource_handle_t* handle,
bool_t use_weight,
similarity_t test_type)
{
printf("\nin all-pairs\n");
int test_ret_value = 0;
data_type_id_t vertex_tid = INT32;

Expand Down Expand Up @@ -92,6 +93,9 @@ int generic_similarity_test(const cugraph_resource_handle_t* handle,
ret_code = cugraph_jaccard_coefficients(
handle, graph, vertex_pairs, use_weight, FALSE, &result, &ret_error);
break;
case ALL_PAIRS_JACCARD:
ret_code = cugraph_all_pairs_jaccard_coefficients(
handle, graph, NULL, use_weight, SIZE_MAX, FALSE, &result, &ret_error);
case SORENSEN:
ret_code = cugraph_sorensen_coefficients(
handle, graph, vertex_pairs, use_weight, FALSE, &result, &ret_error);
Expand All @@ -106,15 +110,36 @@ int generic_similarity_test(const cugraph_resource_handle_t* handle,
TEST_ASSERT(test_ret_value, ret_code == CUGRAPH_SUCCESS, "cugraph similarity failed.");

cugraph_type_erased_device_array_view_t* similarity_coefficient;
cugraph_vertex_pairs_t* vertex_pairs_;

printf("\nresult tests = %p\n", result);
similarity_coefficient = cugraph_similarity_result_get_similarity(result);

vertex_pairs_ = cugraph_similarity_result_get_vertex_pairs(result);

cugraph_type_erased_device_array_view_t* first_view;

first_view = cugraph_vertex_pairs_get_first(vertex_pairs_);
/*
cugraph_type_erased_device_array_view_t* second_view =
cugraph_vertex_pairs_get_first(vertex_pairs_);
*/


//raft::print_device_vector("similarity_coefficient", similarity_coefficient.data(), similarity_coefficient.size(), std::cout);

weight_t h_similarity_coefficient[num_pairs];

ret_code = cugraph_type_erased_device_array_view_copy_to_host(
handle, (byte_t*)h_similarity_coefficient, similarity_coefficient, &ret_error);
TEST_ASSERT(test_ret_value, ret_code == CUGRAPH_SUCCESS, "copy_to_host failed.");

printf("\ncoefficient = ");
for (int i = 0; i < num_pairs; i++) {
//printf("src = %d, score = %f, ", first_view[i], h_similarity_coefficient[i]);
//printf("src = %d, dst = %d, score = %f, ", first_view[i], second_view[i], h_similarity_coefficient[i]);
}

for (int i = 0; (i < num_pairs) && (test_ret_value == 0); ++i) {
TEST_ASSERT(test_ret_value,
nearlyEqual(h_similarity_coefficient[i], h_result[i], 0.001),
Expand Down Expand Up @@ -157,6 +182,35 @@ int test_jaccard(const cugraph_resource_handle_t* handle)
JACCARD);
}


int test_all_pairs_jaccard(const cugraph_resource_handle_t* handle)
{
size_t num_edges = 16;
size_t num_vertices = 6;
size_t num_pairs = 22;

vertex_t h_src[] = {0, 1, 1, 2, 2, 2, 3, 4, 1, 3, 4, 0, 1, 3, 5, 5};
vertex_t h_dst[] = {1, 3, 4, 0, 1, 3, 5, 5, 0, 1, 1, 2, 2, 2, 3, 4};
weight_t h_wgt[] = {0.1f, 2.1f, 1.1f, 5.1f, 3.1f, 4.1f, 7.2f, 3.2f};
vertex_t h_first[] = {0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 5, 5};
vertex_t h_second[] = {1, 2, 3, 4, 0, 2, 3, 5, 0, 1, 3, 4, 5, 0, 1, 2, 4, 0, 2, 3, 1, 2};
weight_t h_result[] = {0.2, 0.25, 0.666667, 0.333333, 0.2, 0.4, 0.166667, 0.5, 0.25, 0.4, 0.2, 0.25, 0.25, 0.666667, 0.166667, 0.2, 0.666667, 0.3333333, 0.25, 0.666667, 0.5, 0.25};

return generic_similarity_test(handle,
h_src,
h_dst,
h_wgt,
h_first,
h_second,
h_result,
num_vertices,
num_edges,
num_pairs,
FALSE,
FALSE,
ALL_PAIRS_JACCARD);
}

int test_weighted_jaccard(const cugraph_resource_handle_t* handle)
{
size_t num_edges = 16;
Expand Down Expand Up @@ -311,9 +365,10 @@ int main(int argc, char** argv)
cugraph_resource_handle_t* handle = cugraph_create_resource_handle(raft_handle);

int result = 0;
result |= RUN_MG_TEST(test_jaccard, handle);
result |= RUN_MG_TEST(test_sorensen, handle);
result |= RUN_MG_TEST(test_overlap, handle);
result |= RUN_MG_TEST(test_all_pairs_jaccard, handle);
// result |= RUN_MG_TEST(test_jaccard, handle);
// result |= RUN_MG_TEST(test_sorensen, handle);
// result |= RUN_MG_TEST(test_overlap, handle);
// result |= RUN_MG_TEST(test_weighted_jaccard, handle);
// result |= RUN_MG_TEST(test_weighted_sorensen, handle);
// result |= RUN_MG_TEST(test_weighted_overlap, handle);
Expand Down

0 comments on commit 5249f02

Please sign in to comment.