bfs.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <math.h>
  5. #include <limits.h>
  6. #include <starpu.h>
  7. #include "common.h"
  8. #include "timer.h"
  9. #define NB_ITERATION 10
  10. extern void omp_bfs_func(void *buffers[], void *_args);
  11. void Usage(int argc, char**argv){
  12. fprintf(stderr,"Usage: %s <input_file>\n", argv[0]);
  13. }
  14. void read_file(char *input_f, unsigned int *nb_nodes, unsigned int *nb_edges,
  15. Node **origin_graph_nodes, bool **origin_graph_mask,
  16. bool **origin_updating_graph_mask, bool **origin_graph_visited,
  17. int **origin_graph_edges, int **origin_cost)
  18. {
  19. FILE *fp;
  20. int source = 0;
  21. printf("Reading File\n");
  22. //Read in Graph from a file
  23. fp = fopen(input_f,"r");
  24. if(!fp)
  25. {
  26. printf("Error Reading graph file\n");
  27. exit(1);
  28. }
  29. fscanf(fp, "%u", nb_nodes);
  30. // allocate host memory
  31. *origin_graph_nodes = (Node *) malloc(sizeof(Node) * (*nb_nodes));
  32. *origin_graph_mask = (bool *) malloc(sizeof(bool) * (*nb_nodes));
  33. *origin_updating_graph_mask = (bool *) malloc(sizeof(bool) * (*nb_nodes));
  34. *origin_graph_visited = (bool *) malloc(sizeof(bool) * (*nb_nodes));
  35. int start, edgeno;
  36. // initalize the memory
  37. for( unsigned int i = 0; i < *nb_nodes; i++)
  38. {
  39. fscanf(fp,"%d %d",&start,&edgeno);
  40. (*origin_graph_nodes)[i].starting = start;
  41. (*origin_graph_nodes)[i].no_of_edges = edgeno;
  42. (*origin_graph_mask)[i]=false;
  43. (*origin_updating_graph_mask)[i]=false;
  44. (*origin_graph_visited)[i]=false;
  45. }
  46. //read the source node from the file
  47. fscanf(fp, "%d", &source);
  48. source=0;
  49. //set the source node as true in the mask
  50. (*origin_graph_mask)[source]=true;
  51. (*origin_graph_visited)[source]=true;
  52. fscanf(fp, "%u", nb_edges);
  53. int id, cost;
  54. *origin_graph_edges = (int*) malloc(sizeof(int) * (*nb_edges));
  55. for(unsigned int i=0; i < *nb_edges ; i++)
  56. {
  57. fscanf(fp,"%d",&id);
  58. fscanf(fp,"%d",&cost);
  59. (*origin_graph_edges)[i] = id;
  60. }
  61. // allocate mem for the result on host side
  62. *origin_cost = (int*) malloc( sizeof(int)* (*nb_nodes));
  63. for(unsigned int i = 0; i < (*nb_nodes); i++)
  64. (*origin_cost)[i]=-1;
  65. (*origin_cost)[source]=0;
  66. fclose(fp);
  67. }
  68. //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);
  69. //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);
  70. ////////////////////////////////////////////////////////////////////////////////
  71. // Main Program
  72. ////////////////////////////////////////////////////////////////////////////////
  73. int main( int argc, char** argv)
  74. {
  75. int ret;
  76. char *input_f;
  77. Timer timer;
  78. unsigned int nb_nodes = 0, nb_edges = 0;
  79. Node *origin_graph_nodes, *graph_nodes;
  80. bool *origin_graph_mask, *graph_mask;
  81. bool *origin_updating_graph_mask, *updating_graph_mask;
  82. bool *origin_graph_visited, *graph_visited;
  83. int *origin_graph_edges, *graph_edges;
  84. int *origin_cost, *cost;
  85. static struct starpu_perfmodel bfs_model;
  86. static struct starpu_codelet bfs_cl;
  87. bfs_model.type = STARPU_HISTORY_BASED;
  88. bfs_model.symbol = "omp_bfs";
  89. bfs_cl.modes[0] = STARPU_R;
  90. bfs_cl.modes[1] = STARPU_R;
  91. bfs_cl.modes[2] = STARPU_RW;
  92. bfs_cl.modes[3] = STARPU_RW;
  93. bfs_cl.modes[4] = STARPU_RW;
  94. bfs_cl.modes[5] = STARPU_RW;
  95. bfs_cl.where = STARPU_CPU;
  96. bfs_cl.type = STARPU_FORKJOIN;
  97. bfs_cl.max_parallelism = INT_MAX;
  98. bfs_cl.cpu_funcs[0] = omp_bfs_func;
  99. bfs_cl.cpu_funcs[1] = NULL;
  100. bfs_cl.nbuffers = 6;
  101. bfs_cl.model = &bfs_model;
  102. starpu_data_handle_t graph_nodes_handle;
  103. starpu_data_handle_t graph_edges_handle;
  104. starpu_data_handle_t graph_mask_handle;
  105. starpu_data_handle_t updating_graph_mask_handle;
  106. starpu_data_handle_t graph_visited_handle;
  107. starpu_data_handle_t cost_handle;
  108. if(argc != 2){
  109. Usage(argc, argv);
  110. exit(1);
  111. }
  112. input_f = argv[1];
  113. read_file(input_f, &nb_nodes, &nb_edges, &origin_graph_nodes,
  114. &origin_graph_mask, &origin_updating_graph_mask,
  115. &origin_graph_visited, &origin_graph_edges, &origin_cost);
  116. graph_nodes = (Node *) malloc(sizeof(Node)*nb_nodes);
  117. graph_mask = (bool *) malloc(sizeof(bool)*nb_nodes);
  118. updating_graph_mask = (bool *) malloc(sizeof(bool)*nb_nodes);
  119. graph_visited = (bool *) malloc(sizeof(bool)*nb_nodes);
  120. graph_edges = (int*) malloc(sizeof(int)*nb_edges);
  121. cost = (int*) malloc( sizeof(int)*nb_nodes);
  122. memcpy(graph_nodes, origin_graph_nodes, nb_nodes*sizeof(Node));
  123. memcpy(graph_edges, origin_graph_edges, nb_edges*sizeof(int));
  124. ret = starpu_init(NULL);
  125. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  126. starpu_vector_data_register(&graph_nodes_handle, STARPU_MAIN_RAM,
  127. (uintptr_t) graph_nodes, nb_nodes,
  128. sizeof(graph_nodes[0] ));
  129. starpu_vector_data_register(&graph_edges_handle, STARPU_MAIN_RAM,
  130. (uintptr_t)graph_edges, nb_edges,
  131. sizeof(graph_edges[0]));
  132. starpu_vector_data_register(&graph_mask_handle, STARPU_MAIN_RAM,
  133. (uintptr_t)graph_mask, nb_nodes,
  134. sizeof(graph_mask[0] ));
  135. starpu_vector_data_register(&updating_graph_mask_handle, STARPU_MAIN_RAM,
  136. (uintptr_t)updating_graph_mask,
  137. nb_nodes,
  138. sizeof(updating_graph_mask[0]));
  139. starpu_vector_data_register(&graph_visited_handle, STARPU_MAIN_RAM,
  140. (uintptr_t)graph_visited, nb_nodes,
  141. sizeof(graph_visited[0]));
  142. starpu_vector_data_register(&cost_handle, STARPU_MAIN_RAM, (uintptr_t)cost,
  143. nb_nodes, sizeof(cost[0]));
  144. for(int it=0; it < NB_ITERATION; it++)
  145. {
  146. starpu_data_acquire(graph_mask_handle, STARPU_W);
  147. starpu_data_acquire(updating_graph_mask_handle, STARPU_W);
  148. starpu_data_acquire(graph_visited_handle, STARPU_W);
  149. starpu_data_acquire(cost_handle, STARPU_W);
  150. memcpy(graph_mask, origin_graph_mask, nb_nodes * sizeof(bool));
  151. memcpy(updating_graph_mask, origin_updating_graph_mask, nb_nodes * sizeof(bool));
  152. memcpy(graph_visited, origin_graph_visited, nb_nodes * sizeof(bool));
  153. memcpy(cost, origin_cost, nb_nodes * sizeof(int));
  154. starpu_data_release(graph_mask_handle);
  155. starpu_data_release(updating_graph_mask_handle);
  156. starpu_data_release(graph_visited_handle);
  157. starpu_data_release(cost_handle);
  158. struct starpu_task *task = starpu_task_create();
  159. task->cl = &bfs_cl;
  160. task->handles[0] = graph_nodes_handle;
  161. task->handles[1] = graph_edges_handle;
  162. task->handles[2] = graph_mask_handle;
  163. task->handles[3] = updating_graph_mask_handle;
  164. task->handles[4] = graph_visited_handle;
  165. task->handles[5] = cost_handle;
  166. task->synchronous = 1;
  167. printf("Start traversing the tree\n");
  168. timer.start();
  169. ret = starpu_task_submit(task);
  170. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  171. timer.stop();
  172. }
  173. starpu_data_unregister(graph_nodes_handle);
  174. starpu_data_unregister(graph_edges_handle);
  175. starpu_data_unregister(graph_mask_handle);
  176. starpu_data_unregister(updating_graph_mask_handle);
  177. starpu_data_unregister(graph_visited_handle);
  178. starpu_data_unregister(cost_handle);
  179. starpu_shutdown();
  180. printf("File: %s, Avergae Time: %f, Total time: %f\n", input_f,
  181. timer.getAverageTime(), timer.getTotalTime());
  182. //Store the result into a file
  183. FILE *fpo = fopen("result.txt","w");
  184. for(unsigned int i=0;i<nb_nodes;i++)
  185. fprintf(fpo,"%d) cost:%d\n", i, cost[i]);
  186. fclose(fpo);
  187. printf("Result stored in result.txt\n");
  188. // cleanup memory
  189. free(graph_nodes);
  190. free(graph_edges);
  191. free(graph_mask);
  192. free(updating_graph_mask);
  193. free(graph_visited);
  194. free(cost);
  195. free(origin_graph_nodes);
  196. free(origin_graph_edges);
  197. free(origin_graph_mask);
  198. free(origin_updating_graph_mask);
  199. free(origin_graph_visited);
  200. free(origin_cost);
  201. }