Advertisement
gisejo

Untitled

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