bfs.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2012-2013 Inria
  4. * Copyright (C) 2012,2016-2017 CNRS
  5. * Copyright (C) 2014,2017 Université de Bordeaux
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <math.h>
  22. #include <limits.h>
  23. #include <starpu.h>
  24. #include "common.h"
  25. #include "timer.h"
  26. #define NB_ITERATION 10
  27. extern void omp_bfs_func(void *buffers[], void *_args);
  28. void Usage(int argc, char**argv)
  29. {
  30. fprintf(stderr,"Usage: %s <input_file>\n", argv[0]);
  31. }
  32. void read_file(char *input_f, unsigned int *nb_nodes, unsigned int *nb_edges,
  33. Node **origin_graph_nodes, bool **origin_graph_mask,
  34. bool **origin_updating_graph_mask, bool **origin_graph_visited,
  35. int **origin_graph_edges, int **origin_cost)
  36. {
  37. FILE *fp;
  38. int source = 0;
  39. printf("Reading File\n");
  40. //Read in Graph from a file
  41. fp = fopen(input_f,"r");
  42. if(!fp)
  43. {
  44. printf("Error Reading graph file\n");
  45. exit(1);
  46. }
  47. fscanf(fp, "%u", nb_nodes);
  48. // allocate host memory
  49. *origin_graph_nodes = (Node *) malloc(sizeof(Node) * (*nb_nodes));
  50. *origin_graph_mask = (bool *) malloc(sizeof(bool) * (*nb_nodes));
  51. *origin_updating_graph_mask = (bool *) malloc(sizeof(bool) * (*nb_nodes));
  52. *origin_graph_visited = (bool *) malloc(sizeof(bool) * (*nb_nodes));
  53. int start, edgeno;
  54. // initalize the memory
  55. for( unsigned int i = 0; i < *nb_nodes; i++)
  56. {
  57. fscanf(fp,"%d %d",&start,&edgeno);
  58. (*origin_graph_nodes)[i].starting = start;
  59. (*origin_graph_nodes)[i].no_of_edges = edgeno;
  60. (*origin_graph_mask)[i]=false;
  61. (*origin_updating_graph_mask)[i]=false;
  62. (*origin_graph_visited)[i]=false;
  63. }
  64. //read the source node from the file
  65. fscanf(fp, "%d", &source);
  66. source=0;
  67. //set the source node as true in the mask
  68. (*origin_graph_mask)[source]=true;
  69. (*origin_graph_visited)[source]=true;
  70. fscanf(fp, "%u", nb_edges);
  71. int id, cost;
  72. *origin_graph_edges = (int*) malloc(sizeof(int) * (*nb_edges));
  73. for(unsigned int i=0; i < *nb_edges ; i++)
  74. {
  75. fscanf(fp,"%d",&id);
  76. fscanf(fp,"%d",&cost);
  77. (*origin_graph_edges)[i] = id;
  78. }
  79. // allocate mem for the result on host side
  80. *origin_cost = (int*) malloc( sizeof(int)* (*nb_nodes));
  81. for(unsigned int i = 0; i < (*nb_nodes); i++)
  82. (*origin_cost)[i]=-1;
  83. (*origin_cost)[source]=0;
  84. fclose(fp);
  85. }
  86. //extern void omp_bfs_func(Node* h_graph_nodes, int* h_graph_edges, bool *h_graph_mask, bool *h_updating_graph_mask, bool *h_graph_visited, int* h_cost, int nb_nodes, int nb_edges);
  87. //extern void cuda_bfs_func(Node* h_graph_nodes, int* h_graph_edges, bool *h_graph_mask, bool *h_updating_graph_mask, bool *h_graph_visited, int* h_cost, int nb_nodes, int nb_edges);
  88. ////////////////////////////////////////////////////////////////////////////////
  89. // Main Program
  90. ////////////////////////////////////////////////////////////////////////////////
  91. int main( int argc, char** argv)
  92. {
  93. int ret;
  94. char *input_f;
  95. Timer timer;
  96. unsigned int nb_nodes = 0, nb_edges = 0;
  97. Node *origin_graph_nodes, *graph_nodes;
  98. bool *origin_graph_mask, *graph_mask;
  99. bool *origin_updating_graph_mask, *updating_graph_mask;
  100. bool *origin_graph_visited, *graph_visited;
  101. int *origin_graph_edges, *graph_edges;
  102. int *origin_cost, *cost;
  103. static struct starpu_perfmodel bfs_model;
  104. static struct starpu_codelet bfs_cl;
  105. bfs_model.type = STARPU_HISTORY_BASED;
  106. bfs_model.symbol = "omp_bfs";
  107. bfs_cl.modes[0] = STARPU_R;
  108. bfs_cl.modes[1] = STARPU_R;
  109. bfs_cl.modes[2] = STARPU_RW;
  110. bfs_cl.modes[3] = STARPU_RW;
  111. bfs_cl.modes[4] = STARPU_RW;
  112. bfs_cl.modes[5] = STARPU_RW;
  113. bfs_cl.where = STARPU_CPU;
  114. bfs_cl.type = STARPU_FORKJOIN;
  115. bfs_cl.max_parallelism = INT_MAX;
  116. bfs_cl.cpu_funcs[0] = omp_bfs_func;
  117. bfs_cl.nbuffers = 6;
  118. bfs_cl.model = &bfs_model;
  119. starpu_data_handle_t graph_nodes_handle;
  120. starpu_data_handle_t graph_edges_handle;
  121. starpu_data_handle_t graph_mask_handle;
  122. starpu_data_handle_t updating_graph_mask_handle;
  123. starpu_data_handle_t graph_visited_handle;
  124. starpu_data_handle_t cost_handle;
  125. if(argc != 2)
  126. {
  127. Usage(argc, argv);
  128. exit(1);
  129. }
  130. input_f = argv[1];
  131. read_file(input_f, &nb_nodes, &nb_edges, &origin_graph_nodes,
  132. &origin_graph_mask, &origin_updating_graph_mask,
  133. &origin_graph_visited, &origin_graph_edges, &origin_cost);
  134. graph_nodes = (Node *) calloc(nb_nodes, sizeof(Node));
  135. graph_mask = (bool *) calloc(nb_nodes, sizeof(bool));
  136. updating_graph_mask = (bool *) calloc(nb_nodes, sizeof(bool));
  137. graph_visited = (bool *) calloc(nb_nodes, sizeof(bool));
  138. graph_edges = (int *) calloc(nb_edges, sizeof(int));
  139. cost = (int *) calloc(nb_nodes, sizeof(int));
  140. memcpy(graph_nodes, origin_graph_nodes, nb_nodes*sizeof(Node));
  141. memcpy(graph_edges, origin_graph_edges, nb_edges*sizeof(int));
  142. ret = starpu_init(NULL);
  143. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  144. starpu_vector_data_register(&graph_nodes_handle, STARPU_MAIN_RAM,
  145. (uintptr_t) graph_nodes, nb_nodes,
  146. sizeof(graph_nodes[0] ));
  147. starpu_vector_data_register(&graph_edges_handle, STARPU_MAIN_RAM,
  148. (uintptr_t)graph_edges, nb_edges,
  149. sizeof(graph_edges[0]));
  150. starpu_vector_data_register(&graph_mask_handle, STARPU_MAIN_RAM,
  151. (uintptr_t)graph_mask, nb_nodes,
  152. sizeof(graph_mask[0] ));
  153. starpu_vector_data_register(&updating_graph_mask_handle, STARPU_MAIN_RAM,
  154. (uintptr_t)updating_graph_mask,
  155. nb_nodes,
  156. sizeof(updating_graph_mask[0]));
  157. starpu_vector_data_register(&graph_visited_handle, STARPU_MAIN_RAM,
  158. (uintptr_t)graph_visited, nb_nodes,
  159. sizeof(graph_visited[0]));
  160. starpu_vector_data_register(&cost_handle, STARPU_MAIN_RAM, (uintptr_t)cost,
  161. nb_nodes, sizeof(cost[0]));
  162. for(int it=0; it < NB_ITERATION; it++)
  163. {
  164. starpu_data_acquire(graph_mask_handle, STARPU_W);
  165. starpu_data_acquire(updating_graph_mask_handle, STARPU_W);
  166. starpu_data_acquire(graph_visited_handle, STARPU_W);
  167. starpu_data_acquire(cost_handle, STARPU_W);
  168. memcpy(graph_mask, origin_graph_mask, nb_nodes * sizeof(bool));
  169. memcpy(updating_graph_mask, origin_updating_graph_mask, nb_nodes * sizeof(bool));
  170. memcpy(graph_visited, origin_graph_visited, nb_nodes * sizeof(bool));
  171. memcpy(cost, origin_cost, nb_nodes * sizeof(int));
  172. starpu_data_release(graph_mask_handle);
  173. starpu_data_release(updating_graph_mask_handle);
  174. starpu_data_release(graph_visited_handle);
  175. starpu_data_release(cost_handle);
  176. struct starpu_task *task = starpu_task_create();
  177. task->cl = &bfs_cl;
  178. task->handles[0] = graph_nodes_handle;
  179. task->handles[1] = graph_edges_handle;
  180. task->handles[2] = graph_mask_handle;
  181. task->handles[3] = updating_graph_mask_handle;
  182. task->handles[4] = graph_visited_handle;
  183. task->handles[5] = cost_handle;
  184. task->synchronous = 1;
  185. printf("Start traversing the tree\n");
  186. timer.start();
  187. ret = starpu_task_submit(task);
  188. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  189. timer.stop();
  190. }
  191. starpu_data_unregister(graph_nodes_handle);
  192. starpu_data_unregister(graph_edges_handle);
  193. starpu_data_unregister(graph_mask_handle);
  194. starpu_data_unregister(updating_graph_mask_handle);
  195. starpu_data_unregister(graph_visited_handle);
  196. starpu_data_unregister(cost_handle);
  197. starpu_shutdown();
  198. printf("File: %s, Avergae Time: %f, Total time: %f\n", input_f,
  199. timer.getAverageTime(), timer.getTotalTime());
  200. //Store the result into a file
  201. FILE *fpo = fopen("result.txt","w");
  202. for(unsigned int i=0;i<nb_nodes;i++)
  203. fprintf(fpo,"%u) cost:%d\n", i, cost[i]);
  204. fclose(fpo);
  205. printf("Result stored in result.txt\n");
  206. // cleanup memory
  207. free(graph_nodes);
  208. free(graph_edges);
  209. free(graph_mask);
  210. free(updating_graph_mask);
  211. free(graph_visited);
  212. free(cost);
  213. free(origin_graph_nodes);
  214. free(origin_graph_edges);
  215. free(origin_graph_mask);
  216. free(origin_updating_graph_mask);
  217. free(origin_graph_visited);
  218. free(origin_cost);
  219. }