nvGRAPH API参考分析(二)
nvGRAPH API参考分析(二)
nvGRAPH Code Examples
本文提供了简单的示例。
nvGRAPH convert topology example
void check(nvgraphStatus_t status) {
if (status != NVGRAPH_STATUS_SUCCESS) {
printf(“ERROR : %d\n”,status);
exit(0);
}
}
int main(int argc, char argv) {
size_t n = 6, nnz = 10;
// nvgraph variables
nvgraphHandle_t handle;
nvgraphCSCTopology32I_t CSC_input;
nvgraphCSRTopology32I_t CSR_output;
float src_weights_d, dst_weights_d;
cudaDataType_t edge_dimT = CUDA_R_32F;
// Allocate source data
CSC_input = (nvgraphCSCTopology32I_t) malloc(sizeof(struct nvgraphCSCTopology32I_st));
CSC_input->nvertices = n; CSC_input->nedges = nnz;
cudaMalloc( (void)&(CSC_input->destination_offsets), (n+1)sizeof(int));
cudaMalloc( (void)&(CSC_input->source_indices), nnzsizeof(int));
cudaMalloc( (void*)&src_weights_d, nnzsizeof(float));
// Copy source data
float src_weights_h[] = {0.333333f, 0.5f, 0.333333f, 0.5f, 0.5f, 1.0f, 0.333333f, 0.5f, 0.5f, 0.5f};
int destination_offsets_h[] = {0, 1, 3, 4, 6, 8, 10};
int source_indices_h[] = {2, 0, 2, 0, 4, 5, 2, 3, 3, 4};
cudaMemcpy(CSC_input->destination_offsets, destination_offsets_h, (n+1)sizeof(int), cudaMemcpyDefault);
cudaMemcpy(CSC_input->source_indices, source_indices_h, nnzsizeof(int), cudaMemcpyDefault);
cudaMemcpy(src_weights_d, src_weights_h, nnzsizeof(float), cudaMemcpyDefault);
// Allocate destination data
CSR_output = (nvgraphCSRTopology32I_t) malloc(sizeof(struct nvgraphCSRTopology32I_st));
cudaMalloc( (void**)&(CSR_output->source_offsets), (n+1)sizeof(int));
cudaMalloc( (void*)&(CSR_output->destination_indices), nnzsizeof(int));
cudaMalloc( (void**)&dst_weights_d, nnz*sizeof(float));
// Starting nvgraph and convert
check(nvgraphCreate (&handle));
check(nvgraphConvertTopology(handle, NVGRAPH_CSC_32, CSC_input, src_weights_d,
&edge_dimT, NVGRAPH_CSR_32, CSR_output, dst_weights_d));
// Free memory
check(nvgraphDestroy(handle));
cudaFree(CSC_input->destination_offsets);
cudaFree(CSC_input->source_indices);
cudaFree(CSR_output->source_offsets);
cudaFree(CSR_output->destination_indices);
cudaFree(src_weights_d);
cudaFree(dst_weights_d);
free(CSC_input);
free(CSR_output);
return 0;
}nvGRAPH convert graph example
void check(nvgraphStatus_t status) {
if (status != NVGRAPH_STATUS_SUCCESS) {
printf(“ERROR : %d\n”,status);
exit(0);
}
}
int main(int argc, char argv) {
size_t n = 6, nnz = 10, vert_sets = 2, edge_sets = 1;
// nvgraph variables
nvgraphHandle_t handle; nvgraphGraphDescr_t src_csc_graph;
nvgraphCSCTopology32I_t CSC_input;
cudaDataType_t edge_dimT = CUDA_R_32F;
cudaDataType_t vertex_dimT;
// Allocate host data
float pr_1 = (float)malloc(nsizeof(float));
void vertex_dim = (void)malloc(vert_setssizeof(void));
vertex_dimT = (cudaDataType_t*)malloc(vert_setssizeof(cudaDataType_t));
CSC_input = (nvgraphCSCTopology32I_t) malloc(sizeof(struct nvgraphCSCTopology32I_st));
// Initialize host data
float weights_h[] = {0.333333f, 0.5f, 0.333333f, 0.5f, 0.5f, 1.0f, 0.333333f, 0.5f, 0.5f, 0.5f};
int destination_offsets_h[] = {0, 1, 3, 4, 6, 8, 10};
int source_indices_h[] = {2, 0, 2, 0, 4, 5, 2, 3, 3, 4};
float bookmark_h[] = {0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f};
vertex_dim[0] = (void)bookmark_h; vertex_dim[1]= (void*)pr_1;
vertex_dimT[0] = CUDA_R_32F; vertex_dimT[1]= CUDA_R_32F, vertex_dimT[2]= CUDA_R_32F;
// Starting nvgraph
check(nvgraphCreate (&handle));
check(nvgraphCreateGraphDescr (handle, &src_csc_graph));
CSC_input->nvertices = n; CSC_input->nedges = nnz;
CSC_input->destination_offsets = destination_offsets_h;
CSC_input->source_indices = source_indices_h;
// Set graph connectivity and properties (tranfers)
check(nvgraphSetGraphStructure(handle, src_csc_graph, (void*)CSC_input, NVGRAPH_CSC_32));
check(nvgraphAllocateVertexData(handle, src_csc_graph, vert_sets, vertex_dimT));
check(nvgraphAllocateEdgeData (handle, src_csc_graph, edge_sets, &edge_dimT));
for (int i = 0; i < 2; ++i)
check(nvgraphSetVertexData(handle, src_csc_graph, vertex_dim[i], i));
check(nvgraphSetEdgeData(handle, src_csc_graph, (void*)weights_h, 0));
// Convert to CSR graph
nvgraphGraphDescr_t dst_csr_graph;
check(nvgraphCreateGraphDescr (handle, &dst_csr_graph));
check(nvgraphConvertGraph(handle, src_csc_graph, dst_csr_graph, NVGRAPH_CSR_32));
check(nvgraphDestroyGraphDescr(handle, src_csc_graph));
check(nvgraphDestroyGraphDescr(handle, dst_csr_graph));
check(nvgraphDestroy(handle));
free(pr_1); free(vertex_dim); free(vertex_dimT);
free(CSC_input);
return 0;
}nvGRAPH pagerank example
void check(nvgraphStatus_t status) {
if (status != NVGRAPH_STATUS_SUCCESS) {
printf(“ERROR : %d\n”,status);
exit(0);
}
}
int main(int argc, char argv) {
size_t n = 6, nnz = 10, vert_sets = 2, edge_sets = 1;
float alpha1 = 0.9f; void alpha1_p = (void ) &alpha1;
// nvgraph variables
nvgraphHandle_t handle; nvgraphGraphDescr_t graph;
nvgraphCSCTopology32I_t CSC_input;
cudaDataType_t edge_dimT = CUDA_R_32F;
cudaDataType_t vertex_dimT;
// Allocate host data
float pr_1 = (float)malloc(nsizeof(float));
void vertex_dim = (void)malloc(vert_setssizeof(void));
vertex_dimT = (cudaDataType_t)malloc(vert_setssizeof(cudaDataType_t));
CSC_input = (nvgraphCSCTopology32I_t) malloc(sizeof(struct nvgraphCSCTopology32I_st));
// Initialize host data
float weights_h[] = {0.333333f, 0.5f, 0.333333f, 0.5f, 0.5f, 1.0f, 0.333333f, 0.5f, 0.5f, 0.5f};
int destination_offsets_h[] = {0, 1, 3, 4, 6, 8, 10};
int source_indices_h[] = {2, 0, 2, 0, 4, 5, 2, 3, 3, 4};
float bookmark_h[] = {0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f};
vertex_dim[0] = (void)bookmark_h; vertex_dim[1]= (void)pr_1;
vertex_dimT[0] = CUDA_R_32F; vertex_dimT[1]= CUDA_R_32F, vertex_dimT[2]= CUDA_R_32F;
// Starting nvgraph
check(nvgraphCreate (&handle));
check(nvgraphCreateGraphDescr (handle, &graph));
CSC_input->nvertices = n; CSC_input->nedges = nnz;
CSC_input->destination_offsets = destination_offsets_h;
CSC_input->source_indices = source_indices_h;
// Set graph connectivity and properties (tranfers)
check(nvgraphSetGraphStructure(handle, graph, (void*)CSC_input, NVGRAPH_CSC_32));
check(nvgraphAllocateVertexData(handle, graph, vert_sets, vertex_dimT));
check(nvgraphAllocateEdgeData (handle, graph, edge_sets, &edge_dimT));
for (int i = 0; i < 2; ++i)
check(nvgraphSetVertexData(handle, graph, vertex_dim[i], i));
check(nvgraphSetEdgeData(handle, graph, (void*)weights_h, 0));check(nvgraphPagerank(handle, graph, 0, alpha1_p, 0, 0, 1, 0.0f, 0));
// Get result
check(nvgraphGetVertexData(handle, graph, vertex_dim[1], 1));
check(nvgraphDestroyGraphDescr(handle, graph));
check(nvgraphDestroy(handle));
free(pr_1); free(vertex_dim); free(vertex_dimT);
free(CSC_input);
return 0;
}nvGRAPH SSSP example
void check(nvgraphStatus_t status) {
if (status != NVGRAPH_STATUS_SUCCESS) {
printf(“ERROR : %d\n”,status);
exit(0);
}
}
int main(int argc, char argv) {
const size_t n = 6, nnz = 10, vertex_numsets = 1, edge_numsets = 1;
float sssp_1_h;
void vertex_dim;
// nvgraph variables
nvgraphStatus_t status; nvgraphHandle_t handle;
nvgraphGraphDescr_t graph;
nvgraphCSCTopology32I_t CSC_input;
cudaDataType_t edge_dimT = CUDA_R_32F;
cudaDataType_t vertex_dimT;
// Init host data
sssp_1_h = (float*)malloc(nsizeof(float));
vertex_dim = (void**)malloc(vertex_numsetssizeof(void*));
vertex_dimT = (cudaDataType_t*)malloc(vertex_numsetssizeof(cudaDataType_t));
CSC_input = (nvgraphCSCTopology32I_t) malloc(sizeof(struct nvgraphCSCTopology32I_st));
vertex_dim[0]= (void)sssp_1_h; vertex_dimT[0] = CUDA_R_32F;
float weights_h[] = {0.333333, 0.5, 0.333333, 0.5, 0.5, 1.0, 0.333333, 0.5, 0.5, 0.5};
int destination_offsets_h[] = {0, 1, 3, 4, 6, 8, 10};
int source_indices_h[] = {2, 0, 2, 0, 4, 5, 2, 3, 3, 4};
check(nvgraphCreate(&handle));
check(nvgraphCreateGraphDescr (handle, &graph));
CSC_input->nvertices = n; CSC_input->nedges = nnz;
CSC_input->destination_offsets = destination_offsets_h;
CSC_input->source_indices = source_indices_h;
// Set graph connectivity and properties (tranfers)
check(nvgraphSetGraphStructure(handle, graph, (void*)CSC_input, NVGRAPH_CSC_32));
check(nvgraphAllocateVertexData(handle, graph, vertex_numsets, vertex_dimT));
check(nvgraphAllocateEdgeData (handle, graph, edge_numsets, &edge_dimT));
check(nvgraphSetEdgeData(handle, graph, (void*)weights_h, 0));
// Solve
int source_vert = 0;
check(nvgraphSssp(handle, graph, 0, &source_vert, 0));
// Get and print result
check(nvgraphGetVertexData(handle, graph, (void*)sssp_1_h, 0));
//Clean
free(sssp_1_h); free(vertex_dim);
free(vertex_dimT); free(CSC_input);
check(nvgraphDestroyGraphDescr(handle, graph));
check(nvgraphDestroy(handle));
return 0;
}nvGRAPH Semi-Ring SPMV example
void check(nvgraphStatus_t status) {
if (status != NVGRAPH_STATUS_SUCCESS) {
printf(“ERROR : %d\n”,status);
exit(0);
}
}
int main(int argc, char argv) {
size_t n = 5, nnz = 10, vertex_numsets = 2, edge_numsets = 1;
float alpha = 1.0, beta = 0.0;
void alpha_p = (void )&alpha, beta_p = (void )β
void vertex_dim;
cudaDataType_t edge_dimT = CUDA_R_32F;
cudaDataType_t vertex_dimT;
// nvgraph variables
nvgraphStatus_t status; nvgraphHandle_t handle;
nvgraphGraphDescr_t graph;
nvgraphCSRTopology32I_t CSR_input;
// Init host data
vertex_dim = (void)malloc(vertex_numsetssizeof(void));
vertex_dimT = (cudaDataType_t)malloc(vertex_numsetssizeof(cudaDataType_t));
CSR_input = (nvgraphCSRTopology32I_t) malloc(sizeof(struct nvgraphCSRTopology32I_st));
float x_h[] = {1.1f, 2.2f, 3.3f, 4.4f, 5.5f};
float y_h[] = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f};
vertex_dim[0]= (void)x_h; vertex_dim[1]= (void*)y_h;
vertex_dimT[0] = CUDA_R_32F; vertex_dimT[1]= CUDA_R_32F;
float weights_h[] = {1.0f, 4.0f, 2.0f, 3.0f, 5.0f, 7.0f, 8.0f, 9.0f, 6.0f, 1.5f};
int source_offsets_h[] = {0, 2, 4, 7, 9, 10};
int destination_indices_h[] = {0, 1, 1, 2, 0, 3, 4, 2, 4, 2};
check(nvgraphCreate(&handle));
check(nvgraphCreateGraphDescr(handle, &graph));
CSR_input->nvertices = n; CSR_input->nedges = nnz;
CSR_input->source_offsets = source_offsets_h;
CSR_input->destination_indices = destination_indices_h;
// Set graph connectivity and properties (tranfers)
check(nvgraphSetGraphStructure(handle, graph, (void*)CSR_input, NVGRAPH_CSR_32));
check(nvgraphAllocateVertexData(handle, graph, vertex_numsets, vertex_dimT));
for (int i = 0; i < vertex_numsets; ++i)
check(nvgraphSetVertexData(handle, graph, vertex_dim[i], i));
check(nvgraphAllocateEdgeData (handle, graph, edge_numsets, &edge_dimT));
check(nvgraphSetEdgeData(handle, graph, (void*)weights_h, 0));
// Solve
check(nvgraphSrSpmv(handle, graph, 0, alpha_p, 0, beta_p, 1, NVGRAPH_PLUS_TIMES_SR));
//Get result
check(nvgraphGetVertexData(handle, graph, (void*)y_h, 1));
//Clean
check(nvgraphDestroyGraphDescr(handle, graph));
check(nvgraphDestroy(handle));
free(vertex_dim); free(vertex_dimT); free(CSR_input);
return 0;
}nvGRAPH Triangles Counting example
#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 : %d in %s : %d\n”, status, FILE , LINE );
exit(0);
}
}
int main(int argc, char **argv)
{
// nvgraph variables
nvgraphHandle_t handle;
nvgraphGraphDescr_t graph;
nvgraphCSRTopology32I_t CSR_input;
// Init host data
CSR_input = (nvgraphCSRTopology32I_t) malloc(sizeof(struct nvgraphCSRTopology32I_st));// Undirected graph:
// 0 2-------4
// \ / \ / \
// \ / \ / \
// \ / \ / \
// 1-------3-------5
// 3 triangles
// CSR of lower triangular of adjacency matrix:
const size_t n = 6, nnz = 8;
int source_offsets[] = {0, 0, 1, 2, 4, 6, 8};
int destination_indices[] = {0, 1, 1, 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;
// Set graph connectivity
check(nvgraphSetGraphStructure(handle, graph, (void*)CSR_input, NVGRAPH_CSR_32));uint64_t trcount = 0;
check(nvgraphTriangleCount(handle, graph, &trcount));
printf("Triangles count: %" PRIu64 "\n", trcount);free(CSR_input);
check(nvgraphDestroyGraphDescr(handle, graph));
check(nvgraphDestroy(handle));
return 0;
}
- nvGRAPH Traversal example
void check_status(nvgraphStatus_t status){
if ((int)status != 0) {
printf(“ERROR : %d\n”,status);
exit(0);
}
}
int main(int argc, char argv){
//Example of graph (CSR format)
const size_t n = 7, nnz = 12, vertex_numsets = 2, edge_numset = 0;
int source_offsets_h[] = {0, 1, 3, 4, 6, 8, 10, 12};
int destination_indices_h[] = {5, 0, 2, 0, 4, 5, 2, 3, 3, 4, 1, 5};
//where to store results (distances from source) and where to store results (predecessors in search tree)
int bfs_distances_h[n], bfs_predecessors_h[n];
// nvgraph variables
nvgraphStatus_t status;
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_numsetssizeof(cudaDataType_t));
vertex_dimT[distances_index] = CUDA_R_32I;
vertex_dimT[predecessors_index] = CUDA_R_32I;
//Creating nvgraph objects
check_status(nvgraphCreate (&handle));
check_status(nvgraphCreateGraphDescr (handle, &graph));
// Set graph connectivity and properties (tranfers)
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;
//Setting the traversal parameters
nvgraphTraversalParameter_t traversal_param;
nvgraphTraversalParameterInit(&traversal_param);
nvgraphTraversalSetDistancesIndex(&traversal_param, distances_index);
nvgraphTraversalSetPredecessorsIndex(&traversal_param, predecessors_index);
nvgraphTraversalSetUndirectedFlag(&traversal_param, false);
//Computing traversal using BFS algorithm
check_status(nvgraphTraversal(handle, graph, NVGRAPH_TRAVERSAL_BFS, &source_vert, traversal_param));
// Get result
check_status(nvgraphGetVertexData(handle, graph, (void*)bfs_distances_h, distances_index));
check_status(nvgraphGetVertexData(handle, graph, (void*)bfs_predecessors_h, predecessors_index));
// expect bfs distances_h = (1 0 1 3 3 2 2147483647)
for (int i = 0; i<n; i++) printf(“Distance to vertex %d: %i\n”,i, bfs_distances_h[i]); printf("\n");
// expect bfs predecessors = (1 -1 1 5 5 0 -1)
for (int i = 0; i<n; i++) printf(“Predecessor of vertex %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 API参考分析(二)相关推荐
- nvGRAPH API参考分析(一)
nvGRAPH API参考分析(一) 本文通过描述nvGRAPH库函数的输入/输出参数,数据类型和错误代码来指定其行为. 返回值nvgraphStatus_t 除以下内容外,所有nvGRAPH库返回值 ...
- 微软官方的.NET Framework API 参考网址
微软官方的.NET Framework API 参考网址,值得收藏和查阅~~~ https://docs.microsoft.com/zh-cn/dotnet/api/?view=netframewo ...
- android camera2 API流程分析
Android camera2 API流程分析 Android5.0之后,新推出来了一个类,android.hardware.camera2,与原来的camera的类实现照相和拍视频的流程有所不同,原 ...
- 透过【百度地图API】分析双闭包问题
原文:透过[百度地图API]分析双闭包问题 摘要: 有位API爱好者问到,昨天的教程里为什么不使用for循环?他使用for循环后,也发现代码无效.这是什么原因? ------------------- ...
- Google 地图 API 参考
杨航收集技术资料,分享给大家 Google 地图 API 参考 Google 地图 API 现在与 Google AJAX API 载入器集成,后者创建了一个公共命名空间,以便载入和使用多个 Goog ...
- Facebook 游戏开发更新文档 API 参考文档 v6.0
Facebook 游戏开发更新文档 API 参考文档 v6.0 更新日志 1.排行榜 此版本全新推出排行榜 API!提供一套强大的 API, 使得游戏可获取排行榜.查询得分 情况和设置新分数(支持分数 ...
- 微软codepush搭建服务器,通过 CodePush API 参考对本机 SDK 作出响应 - Visual Studio App Center | Microsoft Docs...
响应 Native Client SDK API 参考 02/19/2020 本文内容 CodePush 插件由以下两个组件组成: 可以导入/要求的 JavaScript 模块,并允许应用在运行时与服 ...
- java微信开发API解析(二)-获取消息和回复消息
java微信开发API解析(二)-获取消息和回复消息 说明 * 本演示样例依据微信开发文档:http://mp.weixin.qq.com/wiki/home/index.html最新版(4/3/20 ...
- vb 关于窗口样式的API以及处理文本的API参考
管我们使用什么计算机语言开发,VC,VB,BCB,JAVA,NET你都脱离不开操作系统,它就是我们软件的生存土壤,JAVA的跨平台其实是因为它的虚拟机,实质上虚拟机还是要依靠操作系统,.net可以说博 ...
最新文章
- Build RESTful client
- 数据结构:基数排序(Radix sort)
- Java数据结构与算法:排序算法
- java 中的chartdata_获取Helm Charts中的文件夹列表
- verilog 除法器
- 18张动图,向你展示难得一见的瞬间
- linux ubuntu apache php 网站 'page not found'
- 2018通达信l2服务器源码,通达信强势龙头指标源码无未来,牛股连板涨停启动源码...
- VBA字典数组转置维度变化
- java找不到符号 符号:类Xxx 位置xxx的解决方案
- 深蓝英文字幕助手(一款看原声影片和英文字幕学英语的小软件)发布
- 文献阅读笔记 《具有目标定位和边界保持的基于个人注视的目标分割》
- 夜拍王荣耀10 VS同档位旗舰机夜拍功能,实战结果一目了然!
- 病态!------沉沦的病态
- 学姐写毕业论文,图表在PDF上,复制下来格式全乱,头疼了一晚,幸亏有我
- 一维数组与二维数组及相关问题
- python3程序设计课后答案-Python 3 程序设计学习指导与习题解答--详细介绍
- python面对对象建立自己的电子宠物的编码_一种基于Kinect技术的电子宠物的制作方法...
- 高仿支付宝增加减少item功能和动画效果
- Win11怎么更改管理员头像?Win11更改用户头像的方法
热门文章
- Redis 笔记(14)— 持久化及数据恢复(数据持久方式 RDB 和 AOF、数据恢复、混合持久化)
- Go 学习笔记(64)— Go error.New 创建接口错误对象、fmt.Errorf 创建接口错误对象、errors.Is 和 errors.As
- 如何kill同一个应用的所有进程
- 通用流水线处理器技术参数
- CUDA统一内存分析
- Yolo:实时目标检测实战(下)
- 2021年大数据ELK(八):Elasticsearch安装IK分词器插件
- HarmonyOS 界面跳转以及界面跳转的同时传递参数
- python 判断字符串是否为空,字典是否为空,列表是否为空,元组是否为空的方法
- python TypeError: ‘module‘ object is not callable