diff --git a/shared_lib_tests/CMakeLists.txt b/shared_lib_tests/CMakeLists.txt index 48df1a440..eb35dc98f 100644 --- a/shared_lib_tests/CMakeLists.txt +++ b/shared_lib_tests/CMakeLists.txt @@ -14,4 +14,7 @@ add_executable(shared_lib_pr shared_lib_pr.c) target_link_libraries(shared_lib_pr gunrock) add_executable(shared_lib_sssp shared_lib_sssp.c) -target_link_libraries(shared_lib_sssp gunrock) \ No newline at end of file +target_link_libraries(shared_lib_sssp gunrock) + +add_executable(shared_lib_example simple_example.c) +target_link_libraries(shared_lib_example gunrock) \ No newline at end of file diff --git a/shared_lib_tests/simple_example.c b/shared_lib_tests/simple_example.c new file mode 100644 index 000000000..4014a8caf --- /dev/null +++ b/shared_lib_tests/simple_example.c @@ -0,0 +1,100 @@ +// ---------------------------------------------------------------------------- +// Gunrock -- Fast and Efficient GPU Graph Library +// ---------------------------------------------------------------------------- +// This source code is distributed under the terms of LICENSE.TXT +// in the root directory of this source distribution. +// ---------------------------------------------------------------------------- + +/** + * @brief Simple test for shared library simple interface. + * @file simple_example.c + */ + +#include +#include + +int main(int argc, char* argv[]) +{ + /////////////////////////////////////////////////////////////////////////// + // define input graph require input of row-offsets and column-indices + // for primitives use per edge weight value also requires edge-values + int rows[] = + { + 0, 3, 6, 11, 15, 19, 23, 26 + }; + int cols[] = + { + 1, 2, 3, 0, 2, 4, 0, 1, 3, 4, 5, 0, 2, 5, 6, 1, 2, 5, 6, 2, 3, 4, 6, 3, 4, 5 + }; + unsigned int vals[] = + { + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 + }; + int nodes = sizeof(rows) / sizeof(rows[0]) - 1; // number of nodes + int edges = sizeof(cols) / sizeof(cols[0]); // number of edges + + /////////////////////////////////////////////////////////////////////////// + // allocate host arrays to store test results + int* bfs_label = ( int*)malloc(sizeof( int) * nodes); + float* bc_scores = (float*)malloc(sizeof(float) * nodes); + int* conn_comp = ( int*)malloc(sizeof( int) * nodes); + unsigned int *sssp_dist = + (unsigned int*)malloc(sizeof( unsigned int) * nodes); + int* top_nodes = ( int*)malloc(sizeof( int) * nodes); + float* top_ranks = (float*)malloc(sizeof(float) * nodes); + + /////////////////////////////////////////////////////////////////////////// + // run different primitive tests + // graph traversal from given source return integer labels + bfs(bfs_label, nodes, edges, rows, cols, /*source=*/ 0); + // node betweenness centrality from given source + // store computed results to bc_scores of floats + bc(bc_scores, nodes, edges, rows, cols, /*source=*/ 0); + // return number of component and per node component ID + int num_components = cc(conn_comp, nodes, edges, rows, cols); + // return shortest distance for each vertex from given source + sssp(sssp_dist, nodes, edges, rows, cols, vals, /*source=*/ 0); + // return top-ranked nodes and their PageRank values of floats + pagerank(top_nodes, top_ranks, nodes, edges, rows, cols); + + /////////////////////////////////////////////////////////////////////////// + // demo prints allow at most ten nodes + int i; nodes = nodes < 10 ? nodes : 10; + + printf("\n Breath-first search labels:\n"); + for (i = 0; i < nodes; ++i) + printf(" i: [%d] | label: [%d]\n", i, bfs_label[i]); + + printf("\n Node betweenness centrality:\n"); + for (i = 0; i < nodes; ++i) + printf(" i: [%d] | score: [%.4f]\n", i, bc_scores[i]); + + printf("\n Connected components IDs:\n"); + printf(" Total number of components: %d\n", num_components); + for (i = 0; i < nodes; ++i) + printf(" i: [%d] | component: [%d]\n", i, conn_comp[i]); + + printf("\n Single-source shortest path:\n"); + for (i = 0; i < nodes; ++i) + printf(" i: [%d] | distance: [%d]\n", i, sssp_dist[i]); + + printf("\n Top-ranked nodes and their ranks:\n"); + for (i = 0; i < nodes; ++i) + printf(" i: [%d] | rank: [%.4f]\n", top_nodes[i], top_ranks[i]); + + // clean up + if (bfs_label) free(bfs_label); + if (bc_scores) free(bc_scores); + if (conn_comp) free(conn_comp); + if (sssp_dist) free(sssp_dist); + if (top_nodes) free(top_nodes); + if (top_ranks) free(top_ranks); + + return 0; +} + +// Leave this at the end of the file +// Local Variables: +// mode:c++ +// c-file-style: "NVIDIA" +// End: \ No newline at end of file