Advertisement
gisejo

Untitled

Oct 29th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.19 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. const size_t vertex_numsets = 1, edge_numsets = 1;
  76. int *destination_offsets_h;
  77. int *source_indices_h;
  78. float *weights_h, *sssp_h;
  79. void** vertex_dim;
  80.  
  81. // nvgraph variables
  82. nvgraphStatus_t status;
  83. nvgraphHandle_t handle;
  84. nvgraphGraphDescr_t graph;
  85. nvgraphCSCTopology32I_t CSC_input;
  86. cudaDataType_t edge_dimT = CUDA_R_32F;
  87. cudaDataType_t vertex_dimT = CUDA_R_32F;
  88.  
  89. // use command-line specified CUDA device, otherwise use device with highest Gflops/s
  90. int cuda_device = 0;
  91. cuda_device = findCudaDevice(argc, (const char **)argv);
  92.  
  93. cudaDeviceProp deviceProp;
  94. checkCudaErrors(cudaGetDevice(&cuda_device));
  95.  
  96. checkCudaErrors(cudaGetDeviceProperties(&deviceProp, cuda_device));
  97.  
  98. printf("> Detected Compute SM %d.%d hardware with %d multi-processors\n",
  99. deviceProp.major, deviceProp.minor, deviceProp.multiProcessorCount);
  100.  
  101. if (deviceProp.major < 3)
  102. {
  103. printf("> nvGraph requires device SM 3.0+\n");
  104. printf("> Waiving.\n");
  105. exit(EXIT_WAIVED);
  106. }
  107.  
  108. // Init host data
  109. destination_offsets_h = (int*) malloc((n+1)*sizeof(int));
  110. source_indices_h = (int*) malloc(nnz*sizeof(int));
  111. weights_h = (float*)malloc(nnz*sizeof(float));
  112. sssp_h = (float*)malloc(n*sizeof(float));
  113. CSC_input = (nvgraphCSCTopology32I_t) malloc(sizeof(struct nvgraphCSCTopology32I_st));
  114.  
  115. for (int i = 0; i < n; i++){
  116. destination_offsets_h [i] = INT_MAX;
  117. }
  118.  
  119.  
  120.  
  121. struct tmp_edge * Edges = (tmp_edge*)malloc(nnz*sizeof(tmp_edge));
  122.  
  123. // get & print graph data for WEIGHTED graph
  124. for(int i = 0; i < nnz; i++){
  125. int src_id = 0, dst_id = 0;
  126. float weight = 0;
  127.  
  128. // read i-th edge data
  129. file.read((char*)(&src_id), sizeof(int));
  130. file.read((char*)(&dst_id), sizeof(int));
  131. file.read((char*)(&weight), sizeof(float)); // remove it for unweighed graph
  132.  
  133. //print edge data
  134. //cout << i << " " << src_id << " " << dst_id << " | " << weight << endl;
  135.  
  136. Edges[i] = {src_id, dst_id, weight};
  137.  
  138. //cout << i << " " << Edges[i].src_id << " " << Edges[i].dst_id << " " << Edges[i].weight << endl;
  139. }
  140.  
  141. qsort(Edges, nnz, sizeof(tmp_edge), compare_edges);
  142.  
  143. int prev_dst_id = -1;
  144.  
  145. for(int i = 0; i < nnz; i++){
  146. weights_h [i] = Edges[i].weight;
  147.  
  148. int tmp_dst_id = Edges[i].dst_id;
  149.  
  150. if (tmp_dst_id > prev_dst_id){
  151.  
  152.  
  153. for (int j = prev_dst_id + 1; j <= tmp_dst_id; j++){
  154.  
  155. destination_offsets_h [j] = i;
  156.  
  157. }
  158.  
  159. prev_dst_id = tmp_dst_id;
  160.  
  161. }
  162.  
  163. source_indices_h [i] = Edges[i].src_id;
  164. //cout << i << " " << Edges[i].src_id << " " << Edges[i].dst_id << " " << Edges[i].weight << endl;
  165. }
  166.  
  167. for (int j = prev_dst_id + 1; j <= n; j++){
  168.  
  169. destination_offsets_h [j] = nnz;
  170.  
  171. }
  172.  
  173. check_status(nvgraphCreate(&handle));
  174. check_status(nvgraphCreateGraphDescr (handle, &graph));
  175.  
  176. CSC_input->nvertices = n;
  177. CSC_input->nedges = nnz;
  178. CSC_input->destination_offsets = destination_offsets_h;
  179. CSC_input->source_indices = source_indices_h;
  180.  
  181.  
  182. // Set graph connectivity and properties (tranfers)
  183. check_status(nvgraphSetGraphStructure(handle, graph, (void*)CSC_input, NVGRAPH_CSC_32));
  184. check_status(nvgraphAllocateVertexData(handle, graph, vertex_numsets, &vertex_dimT));
  185. check_status(nvgraphAllocateEdgeData (handle, graph, edge_numsets, &edge_dimT));
  186. check_status(nvgraphSetEdgeData(handle, graph, (void*)weights_h, 0));
  187.  
  188. // Solve
  189. int source_vert = 1;
  190. check_status(nvgraphSssp(handle, graph, 0, &source_vert, 0));
  191.  
  192. check_status(nvgraphGetVertexData(handle, graph, (void*)sssp_h, 0));
  193.  
  194. //for (int i = 0; i<n; i++) printf("%f\n",sssp_h[i]); printf("\n");
  195.  
  196.  
  197. source_vert = 0;
  198. check_status(nvgraphSssp(handle, graph, 0, &source_vert, 0));
  199.  
  200. check_status(nvgraphGetVertexData(handle, graph, (void*)sssp_h, 0));
  201.  
  202. for (int i = 0; i<n; i++) printf("%f\n",sssp_h[i]); printf("\n");
  203.  
  204.  
  205. free(destination_offsets_h);
  206. free(source_indices_h);
  207. free(weights_h);
  208. free(CSC_input);
  209.  
  210. //Clean
  211. check_status(nvgraphDestroyGraphDescr (handle, graph));
  212. check_status(nvgraphDestroy (handle));
  213.  
  214. file.close();
  215.  
  216. return EXIT_SUCCESS;
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement