forked from gunrock/gunrock
-
Notifications
You must be signed in to change notification settings - Fork 1
/
simple_example.c
102 lines (86 loc) · 3.78 KB
/
simple_example.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// ----------------------------------------------------------------------------
// 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 <stdio.h>
#include <gunrock/gunrock.h>
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, 0, nodes, edges, rows, cols, 1,/*source=*/ 0, 0, false, false);
// 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, 0, nodes, edges, rows, cols, vals, 1,/*source=*/ 0,false);
// return top-ranked nodes and their PageRank values of floats
pagerank(top_nodes, top_ranks, nodes, edges, rows, cols, false);
///////////////////////////////////////////////////////////////////////////
// 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
free(bfs_label);
free(bc_scores);
free(conn_comp);
free(sssp_dist);
free(top_nodes);
free(top_ranks);
return 0;
}
// Leave this at the end of the file
// Local Variables:
// mode:c++
// c-file-style: "NVIDIA"
// End: