Advertisement
gisejo

Untitled

Oct 30th, 2019
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.15 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. int is_undirected;
  34.  
  35. struct tmp_edge{
  36. int src_id;
  37. int dst_id;
  38. float weight;
  39. };
  40.  
  41. int compare_edges(const void * a, const void * b)
  42. {
  43. return ( (*(struct tmp_edge *)a).dst_id - (*(struct tmp_edge *)b).dst_id );
  44. }
  45.  
  46. void check_status(nvgraphStatus_t status)
  47. {
  48. if ((int)status != 0)
  49. {
  50. printf("ERROR : %d\n",status);
  51. exit(0);
  52. }
  53. }
  54.  
  55. int main(int argc, char **argv)
  56. {
  57.  
  58. is_undirected = 1;
  59.  
  60. const char * file_name = "./graph.bin";
  61.  
  62. // open file
  63. fstream file(file_name, ios::in | ios::binary);
  64.  
  65. int vertices_count = 0;
  66. long long edges_count = 0;
  67.  
  68. // read header
  69. file.read((char*)(&vertices_count), sizeof(int));
  70. file.read((char*)(&edges_count), sizeof(long long));
  71.  
  72. // print graph
  73. //cout << "Graph has " << vertices_count << " vertices" << endl;
  74. //cout << "Graph has " << edges_count << " edges" << endl;
  75.  
  76. int n = vertices_count;
  77. int nnz = edges_count;//because nvgraphCSCTopology32I_st structure can take only int as 3 parameter, and there is no analog for 64-bit
  78.  
  79. if (is_undirected){
  80. nnz *= 2;
  81. }
  82.  
  83. const size_t vertex_numsets = 1, edge_numsets = 1;
  84. int *destination_offsets_h;
  85. int *source_indices_h;
  86. float *weights_h, *sssp_h;
  87. void** vertex_dim;
  88.  
  89. // nvgraph variables
  90. nvgraphStatus_t status;
  91. nvgraphHandle_t handle;
  92. nvgraphGraphDescr_t graph;
  93. nvgraphCSCTopology32I_t CSC_input;
  94. cudaDataType_t edge_dimT = CUDA_R_32F;
  95. cudaDataType_t vertex_dimT = CUDA_R_32F;
  96.  
  97. // use command-line specified CUDA device, otherwise use device with highest Gflops/s
  98. int cuda_device = 0;
  99. cuda_device = findCudaDevice(argc, (const char **)argv);
  100.  
  101. cudaDeviceProp deviceProp;
  102. checkCudaErrors(cudaGetDevice(&cuda_device));
  103.  
  104. checkCudaErrors(cudaGetDeviceProperties(&deviceProp, cuda_device));
  105.  
  106. printf("> Detected Compute SM %d.%d hardware with %d multi-processors\n",
  107. deviceProp.major, deviceProp.minor, deviceProp.multiProcessorCount);
  108.  
  109. if (deviceProp.major < 3)
  110. {
  111. printf("> nvGraph requires device SM 3.0+\n");
  112. printf("> Waiving.\n");
  113. exit(EXIT_WAIVED);
  114. }
  115.  
  116. // Init host data
  117. destination_offsets_h = (int*) malloc((n+1)*sizeof(int));
  118. source_indices_h = (int*) malloc(nnz*sizeof(int));
  119. weights_h = (float*)malloc(nnz*sizeof(float));
  120. sssp_h = (float*)malloc(n*sizeof(float));
  121. CSC_input = (nvgraphCSCTopology32I_t) malloc(sizeof(struct nvgraphCSCTopology32I_st));
  122.  
  123. for (int i = 0; i < n; i++){
  124. destination_offsets_h [i] = INT_MAX;
  125. }
  126.  
  127.  
  128.  
  129. struct tmp_edge * Edges = (tmp_edge*)malloc(nnz*sizeof(tmp_edge));
  130.  
  131. // get & print graph data for WEIGHTED graph
  132. for(int i = 0; i < edges_count; i++){
  133. int src_id = 0, dst_id = 0;
  134. float weight = 0;
  135.  
  136. // read i-th edge data
  137. file.read((char*)(&src_id), sizeof(int));
  138. file.read((char*)(&dst_id), sizeof(int));
  139. file.read((char*)(&weight), sizeof(float)); // remove it for unweighed graph
  140.  
  141. //print edge data
  142. //cout << i << " " << src_id << " " << dst_id << " | " << weight << endl;
  143.  
  144. Edges[i] = {src_id, dst_id, weight};
  145. if (is_undirected){
  146. Edges[edges_count + i] = {dst_id, src_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. int prev_dst_id = -1;
  154.  
  155. for(int i = 0; i < nnz; i++){
  156. weights_h [i] = Edges[i].weight;
  157.  
  158. int tmp_dst_id = Edges[i].dst_id;
  159.  
  160. if (tmp_dst_id > prev_dst_id){
  161.  
  162.  
  163. for (int j = prev_dst_id + 1; j <= tmp_dst_id; j++){
  164.  
  165. destination_offsets_h [j] = i;
  166.  
  167. }
  168.  
  169. prev_dst_id = tmp_dst_id;
  170.  
  171. }
  172.  
  173. source_indices_h [i] = Edges[i].src_id;
  174. //cout << i << " " << Edges[i].src_id << " " << Edges[i].dst_id << " " << Edges[i].weight << endl;
  175. }
  176.  
  177. for (int j = prev_dst_id + 1; j <= n; j++){
  178.  
  179. destination_offsets_h [j] = nnz;
  180.  
  181. }
  182.  
  183. check_status(nvgraphCreate(&handle));
  184. check_status(nvgraphCreateGraphDescr (handle, &graph));
  185.  
  186. CSC_input->nvertices = n;
  187. CSC_input->nedges = nnz;
  188. CSC_input->destination_offsets = destination_offsets_h;
  189. CSC_input->source_indices = source_indices_h;
  190.  
  191.  
  192. // Set graph connectivity and properties (tranfers)
  193. check_status(nvgraphSetGraphStructure(handle, graph, (void*)CSC_input, NVGRAPH_CSC_32));
  194. check_status(nvgraphAllocateVertexData(handle, graph, vertex_numsets, &vertex_dimT));
  195. check_status(nvgraphAllocateEdgeData (handle, graph, edge_numsets, &edge_dimT));
  196. check_status(nvgraphSetEdgeData(handle, graph, (void*)weights_h, 0));
  197.  
  198. // Solve
  199. int source_vert = 1;
  200. check_status(nvgraphSssp(handle, graph, 0, &source_vert, 0));
  201.  
  202. check_status(nvgraphGetVertexData(handle, graph, (void*)sssp_h, 0));
  203.  
  204. for (int i = 0; i<n; i++) printf("%f\n",sssp_h[i]); printf("\n");
  205.  
  206. free(destination_offsets_h);
  207. free(source_indices_h);
  208. free(weights_h);
  209. free(CSC_input);
  210.  
  211. //Clean
  212. check_status(nvgraphDestroyGraphDescr (handle, graph));
  213. check_status(nvgraphDestroy (handle));
  214.  
  215. file.close();
  216.  
  217. return EXIT_SUCCESS;
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement