Advertisement
gisejo

Untitled

Oct 28th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.91 KB | None | 0 0
  1. /*
  2.  * Copyright (c) 2016, NVIDIA CORPORATION.  All rights reserved.
  3.  *
  4.  * Please refer to the NVIDIA end user license agreement (EULA) associated
  5.  * with this source code for terms and conditions that govern your use of
  6.  * this software. Any use, reproduction, disclosure, or distribution of
  7.  * this software and related documentation outside the terms of the EULA
  8.  * is strictly prohibited.
  9.  *
  10.  */
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <cuda_runtime.h>
  15. #include "helper_cuda.h"
  16. #include "nvgraph.h"
  17.  
  18. #include<iostream>
  19. #include<string>
  20. #include<iomanip>
  21. #include<fstream>
  22. #include<cstdlib>
  23. #include<bits/stdc++.h>
  24.  
  25. using namespace std;
  26.  
  27.  
  28. /* Single Source Shortest Path (SSSP)
  29.  *  Calculate the shortest path distance from a single vertex in the graph
  30.  *  to all other vertices.
  31.  */
  32.  
  33. struct tmp_edge{
  34.     int src_id;
  35.     int dst_id;
  36.     float weight;
  37. };
  38.  
  39. int compare_edges(const void * a, const void * b)
  40. {
  41.     return ( (*(struct tmp_edge *)a).dst_id - (*(struct tmp_edge *)b).dst_id );
  42. }
  43.  
  44. void check_status(nvgraphStatus_t status)
  45. {
  46.     if ((int)status != 0)
  47.     {
  48.         printf("ERROR : %d\n",status);
  49.         exit(0);
  50.     }
  51. }
  52.  
  53. int main(int argc, char **argv)
  54. {
  55.  
  56.     const char * file_name = "./graph.bin";
  57.  
  58.         // open file
  59.     fstream file(file_name, ios::in | ios::binary);
  60.  
  61.     int vertices_count = 0;
  62.     long long edges_count = 0;
  63.  
  64.     // read header
  65.     file.read((char*)(&vertices_count), sizeof(int));
  66.     file.read((char*)(&edges_count), sizeof(long long));
  67.  
  68.     // print graph
  69.     cout << "Graph has " << vertices_count << " vertices" << endl;
  70.     cout << "Graph has " << edges_count << " edges" << endl;
  71.  
  72.     int n = vertices_count;
  73.     int nnz = edges_count;//because nvgraphCSCTopology32I_st structure can take only int as 3 parameter, and there is no analog for 64-bit
  74.  
  75.  
  76.     cout << "Graph has " << n << " vertices" << endl;
  77.     cout << "Graph has " << nnz << " edges" << endl;
  78.  
  79.     const size_t  vertex_numsets = 2, edge_numsets = 1;
  80.     int *destination_offsets_h;
  81.     int *source_indices_h;
  82.     float *weights_h, *sssp_1_h, *sssp_2_h;
  83.     void** vertex_dim;
  84.  
  85.     // nvgraph variables
  86.     nvgraphStatus_t status;
  87.     nvgraphHandle_t handle;
  88.     nvgraphGraphDescr_t graph;
  89.     nvgraphCSCTopology32I_t CSC_input;
  90.     cudaDataType_t edge_dimT = CUDA_R_32F;
  91.     cudaDataType_t* vertex_dimT;
  92.  
  93.     // use command-line specified CUDA device, otherwise use device with highest Gflops/s
  94.     int cuda_device = 0;
  95.     cuda_device = findCudaDevice(argc, (const char **)argv);
  96.  
  97.     cudaDeviceProp deviceProp;
  98.     checkCudaErrors(cudaGetDevice(&cuda_device));
  99.  
  100.     checkCudaErrors(cudaGetDeviceProperties(&deviceProp, cuda_device));
  101.  
  102.     printf("> Detected Compute SM %d.%d hardware with %d multi-processors\n",
  103.            deviceProp.major, deviceProp.minor, deviceProp.multiProcessorCount);
  104.  
  105.     if (deviceProp.major < 3)
  106.     {
  107.         printf("> nvGraph requires device SM 3.0+\n");
  108.         printf("> Waiving.\n");
  109.         exit(EXIT_WAIVED);
  110.     }
  111.  
  112.     // Init host data
  113.     destination_offsets_h = (int*) malloc((n+1)*sizeof(int));
  114.     source_indices_h = (int*) malloc(nnz*sizeof(int));
  115.     weights_h = (float*)malloc(nnz*sizeof(float));
  116.     sssp_1_h = (float*)malloc(n*sizeof(float));
  117.     sssp_2_h = (float*)malloc(n*sizeof(float));
  118.     vertex_dim  = (void**)malloc(vertex_numsets*sizeof(void*));
  119.     vertex_dimT = (cudaDataType_t*)malloc(vertex_numsets*sizeof(cudaDataType_t));
  120.     CSC_input = (nvgraphCSCTopology32I_t) malloc(sizeof(struct nvgraphCSCTopology32I_st));
  121.  
  122.     vertex_dim[0]= (void*)sssp_1_h; vertex_dim[1]= (void*)sssp_2_h;
  123.     vertex_dimT[0] = CUDA_R_32F; vertex_dimT[1]= CUDA_R_32F;
  124.  
  125.     for (int i = 0; i < n; i++){
  126.         destination_offsets_h [i] = INT_MAX;
  127.     }
  128.  
  129.     destination_offsets_h [n] = nnz;
  130.  
  131.     struct tmp_edge * Edges = (tmp_edge*)malloc(nnz*sizeof(tmp_edge));
  132.  
  133.     // get & print graph data for WEIGHTED graph
  134.     for(int i = 0; i < nnz; i++){
  135.         int src_id = 0, dst_id = 0;
  136.         float weight = 0;
  137.  
  138.         // read i-th edge data
  139.         file.read((char*)(&src_id), sizeof(int));
  140.         file.read((char*)(&dst_id), sizeof(int));
  141.         file.read((char*)(&weight), sizeof(float)); // remove it for unweighed graph
  142.  
  143.         //print edge data
  144.         //cout << i << "  " << src_id << " " << dst_id << " | " << weight << endl;
  145.  
  146.         Edges[i] = {src_id, dst_id, weight};
  147.  
  148.         //cout << i << " " <<  Edges[i].src_id << " " << Edges[i].dst_id << " " << Edges[i].weight << endl;
  149.     }
  150.  
  151.     qsort(Edges, nnz, sizeof(tmp_edge), compare_edges);
  152.  
  153.     cout<<"hui";
  154.     fflush(stdout);    
  155.    
  156.     current_dst_id = -1;
  157.  
  158.     for(int i = 0; i < nnz; i++){
  159.         weights_h [i] = Edges[i].weight;
  160.  
  161.         int tmp_dst_id = Edges[i].dst_id;
  162.  
  163.         if (tmp_dst_id >current_dst_id){
  164.  
  165.             destination_offsets_h [tmp_dst_id] = i;
  166.         }
  167.  
  168.         source_indices_h [i] = Edges[i].src_id;
  169.         //cout << i << " " <<  Edges[i].src_id << " " << Edges[i].dst_id << " " << Edges[i].weight << endl;
  170.     }
  171.    
  172.  
  173.  
  174.  
  175.     /*
  176.  
  177.     weights_h [0] = 0.333333;
  178.     weights_h [1] = 0.500000;
  179.     weights_h [2] = 0.333333;
  180.     weights_h [3] = 0.500000;
  181.     weights_h [4] = 0.500000;
  182.     weights_h [5] = 1.000000;
  183.     weights_h [6] = 0.333333;
  184.     weights_h [7] = 0.500000;
  185.     weights_h [8] = 0.500000;
  186.     weights_h [9] = 0.500000;
  187.  
  188.     destination_offsets_h [0] = 0;
  189.     destination_offsets_h [1] = 1;
  190.     destination_offsets_h [2] = 3;
  191.     destination_offsets_h [3] = 4;
  192.     destination_offsets_h [4] = 6;
  193.     destination_offsets_h [5] = 8;
  194.     destination_offsets_h [6] = 10;
  195.  
  196.     source_indices_h [0] = 2;
  197.     source_indices_h [1] = 0;
  198.     source_indices_h [2] = 2;
  199.     source_indices_h [3] = 0;
  200.     source_indices_h [4] = 4;
  201.     source_indices_h [5] = 5;
  202.     source_indices_h [6] = 2;
  203.     source_indices_h [7] = 3;
  204.     source_indices_h [8] = 3;
  205.     source_indices_h [9] = 4;
  206.  
  207.     */
  208.  
  209.     check_status(nvgraphCreate(&handle));
  210.     check_status(nvgraphCreateGraphDescr (handle, &graph));
  211.  
  212.     CSC_input->nvertices = n;
  213.     CSC_input->nedges = nnz;
  214.     CSC_input->destination_offsets = destination_offsets_h;
  215.     CSC_input->source_indices = source_indices_h;
  216.  
  217.     // Set graph connectivity and properties (tranfers)
  218.     check_status(nvgraphSetGraphStructure(handle, graph, (void*)CSC_input, NVGRAPH_CSC_32));
  219.     check_status(nvgraphAllocateVertexData(handle, graph, vertex_numsets, vertex_dimT));
  220.     check_status(nvgraphAllocateEdgeData  (handle, graph, edge_numsets, &edge_dimT));
  221.     check_status(nvgraphSetEdgeData(handle, graph, (void*)weights_h, 0));
  222.  
  223.     // Solve
  224.     int source_vert = 0;
  225.     check_status(nvgraphSssp(handle, graph, 0,  &source_vert, 0));
  226.    
  227.     cout << "1";
  228.     fflush(stdout);
  229.    
  230.     // Solve with another source
  231.     source_vert = 5;
  232.     check_status(nvgraphSssp(handle, graph, 0,  &source_vert, 1));
  233.    
  234.     // Get and print result
  235.     cout << "2";
  236.     fflush(stdout);
  237.    
  238.  
  239.     check_status(nvgraphGetVertexData(handle, graph, (void*)sssp_1_h, 0));
  240.  
  241.     cout << "3";
  242.     fflush(stdout);
  243.    
  244.     // expect sssp_1_h = (0.000000 0.500000 0.500000 1.333333 0.833333 1.333333)^T
  245.     printf("sssp_1_h\n");
  246.     for (int i = 0; i<n; i++)  printf("%f\n",sssp_1_h[i]); printf("\n");
  247.     printf("\nDone!\n");
  248.  
  249.  
  250.     check_status(nvgraphGetVertexData(handle, graph, (void*)sssp_2_h, 1));
  251.     // expect sssp_2_h = (FLT_MAX FLT_MAX FLT_MAX 1.000000 1.500000 0.000000 )^T
  252.     printf("sssp_2_h\n");
  253.     for (int i = 0; i<n; i++)  printf("%f\n",sssp_2_h[i]); printf("\n");
  254.     printf("\nDone!\n");
  255.  
  256.     free(destination_offsets_h);
  257.     free(source_indices_h);
  258.     free(weights_h);
  259.     free(sssp_1_h);
  260.     free(sssp_2_h);
  261.     free(vertex_dim);
  262.     free(vertex_dimT);
  263.     free(CSC_input);
  264.  
  265.     //Clean
  266.     check_status(nvgraphDestroyGraphDescr (handle, graph));
  267.     check_status(nvgraphDestroy (handle));
  268.  
  269.     file.close();
  270.  
  271.     return EXIT_SUCCESS;
  272. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement