Advertisement
gisejo

Untitled

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