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:
// 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;
}
總結
以上是生活随笔為你收集整理的nvGRAPH三角形计数和遍历示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: TensorRT深度学习训练和部署图示
- 下一篇: C ++变量,文字和常量