bfs.cpp 8.0 KB

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