bfs.cpp 8.1 KB

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