nvGRAPH三角形计数和遍历示例

nvGRAPH三角形计数和遍历示例

#include “ stdlib.h”

#include“ inttypes.h”

#include“ stdio.h”

#include“ nvgraph.h”

#define check( a )\

{\

nvgraphStatus_t status =(a); \

if((status)!= NVGRAPH_STATUS_SUCCESS){\

        printf(“ERROR :%s,%s:%d \ n”,status,__ FILE __,__ LINE __); \

exit(0); \

} \

}

int main(int argc,char ** argv)

{

// nvgraph变量

nvgraphHandle_t handle;

nvgraphGraphDescr_t graph;

nvgraphCSRTopology32I_t CSR_input;

//初始化主机数据

CSR_input =(nvgraphCSRTopology32I_t)malloc(sizeof(struct nvgraphCSRTopology32I_st));

//无向 graph:

nvGRAPH三角形计数和遍历示例

// 3个三角形

//邻接矩阵下三角形的CSR:

const size_t n = 6,nnz = 8;

int source_offsets [] = {0,0,1,2,4,4,6,8};

int destination_indices [] = {0,1,1,2,2,2,3,3,4};

check(nvgraphCreate(&handle));

check(nvgraphCreateGraphDescr( handle&graph));

CSR_input-> nvertices = n;

CSR_input-> nedges = nnz;

CSR_input-> source_offsets = source_offsets;

CSR_input-> destination_indices = destination_indices;

//设置 graph连接性

check(nvgraphSetGraphStructure(handle,graph,(void *)CSR_input,NVGRAPH_CSR_32));

uint64_t trcount = 0;

check(nvgraphTriangleCount( handle, graph,&trcount));

printf(“三角形数:%” PRIu64 “ \ n”,trcount);

free(CSR_input);

check(nvgraphDestroyGraphDescr( handle, graph));

check(nvgraphDestroy( handle));

return 0;

}

nvGRAPH遍历示例

void check_status(nvgraphStatus_t status){

if((int)status!= 0){

printf(“error:%d \ n”,status);

exit(0);

}

}

int main(int argc,char ** argv){

// graph示例(CSR格式)

const size_t n = 7,nnz = 12,vertex_numsets = 2,edge_numset = 0;

int source_offsets_h [] = {0,1,3,4,6,6,8,10,12};

int destination_indices_h [] = {5,0,2,0,4,5,5,2,3,3,4,1,5};

//存储结果的位置(与源的距离)和存储结果的位置(搜索树中的前身)

int bfs_distances_h [n],bfs_predecessors_h [n];

// nvgraph变量

nvgraphStatus_tstatus;

nvgraphHandle_t handle;

nvgraphGraphDescr_t graph;

nvgraphCSRTopology32I_t CSR_input;

cudaDataType_t * vertex_dimT;

size_t distances_index = 0;

size_t predecessors_index = 1;

vertex_dimT =(cudaDataType_t *)malloc(vertex_numsets * sizeof(cudaDataType_t));

vertex_dimT [distances_index] = CUDA_R_32I;

vertex_dimT [predecessors_index] = CUDA_R_32I;

//创建nvgraph对象

check_status(nvgraphCreate(&handle));

check_status(nvgraphCreateGraphDescr( handle&graph));

//设置 graph的连通性和属性(转移)

CSR_input =(nvgraphCSRTopology32I_t)malloc(sizeof(struct nvgraphCSCTopology32I_st));

CSR_input-> nvertices = n;

CSR_input-> nedges = nnz;

CSR_input-> source_offsets = source_offsets_h;

CSR_input-> destination_indices = destination_indices_h;

check_status(nvgraphSetGraphStructure(handle,graph,(void *)CSR_input,NVGRAPH_CSR_32));;

check_status(nvgraphAllocateVertexData( handle, graph,vertex_numsets,vertex_dimT));

int source_vert = 1;

//设置遍历参数

nvgraphTraversalParameter_t traversal_param;

nvgraphTraversalParameterInit(&traversal_param);

nvgraphTraversalSetDistancesIndex(&traversal_param,distances_index);

nvgraphTraversalSetPredecessorsIndex(&traversal_param,predecessors_index);

nvgraphTraversalSetUndirectedFlag(&traversal_param,false);

//使用BFS算法进行遍历

check_status(nvgraphTraversal( handle, graph,NVGRAPH_TRAVERSAL_BFS,&source_vert,traversal_param));

//获取结果

check_status(nvgraphGetVertexData(handle, graph表,(void *)bfs_distances_h,distances_index));

check_status(nvgraphGetVertexData( handle, graph,(void *)bfs_predecessors_h,predecessors_index));

//

for(int i = 0; i <n; i ++),期望bfs distances_h =(1 0 1 3 3 2 2147483647) printf(“距顶点%d的距离:%i \ n”,i,bfs_distances_h [i]) ; printf(“ \ n”);

//

for(int i = 0; i <n; i ++),期望bfs前驱体=(1 -1 1 5 5 0 -1) printf(“顶点%d的前驱体:%i \ n”,i,bfs_predecessors_h [i ]); printf(“ \ n”);

free(vertex_dimT);

free(CSR_input);

check_status(nvgraphDestroyGraphDescr( handle, graph));

check_status(nvgraphDestroy(handle));

return 0;

}

上一篇:position定位


下一篇:基于TBDS的flume异常问题排查过程