nbody_between.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <starpu.h>
  4. #include <math.h>
  5. #include <stdint.h>
  6. #include "../includes/display.h"
  7. #include "../includes/sorting.h"
  8. struct Param {
  9. unsigned taskx;
  10. double epsilon;
  11. };
  12. void nbody_acc(void **, void *);
  13. void nbody_updt(void **, void *);
  14. void CUDA_nbody_acc(void **, void *);
  15. void CUDA_nbody_updt(void **, void *);
  16. static struct starpu_perfmodel model =
  17. {
  18. .type = STARPU_HISTORY_BASED,
  19. .symbol = "history_perf"
  20. };
  21. static struct starpu_codelet cl =
  22. {
  23. .cpu_funcs = {nbody_acc},
  24. .cuda_funcs = {CUDA_nbody_acc},
  25. //STRUCT PARAM
  26. /* .nbuffers = 3, */
  27. /* .modes = {STARPU_RW, STARPU_RW, STARPU_RW}, */
  28. //
  29. //ARRAY PARAM
  30. .nbuffers = 5,
  31. .modes = {STARPU_RW, STARPU_RW, STARPU_RW, STARPU_RW, STARPU_RW},
  32. //
  33. .model = &model
  34. };
  35. static struct starpu_codelet cl2 =
  36. {
  37. .cpu_funcs = {nbody_updt},
  38. .cuda_funcs = {CUDA_nbody_updt},
  39. //STRUCT PARAM
  40. /* .nbuffers = 3, */
  41. /* .modes = {STARPU_RW, STARPU_RW, STARPU_R}, */
  42. //
  43. //ARRAY PARAM
  44. .nbuffers = 4,
  45. .modes = {STARPU_RW, STARPU_RW, STARPU_R, STARPU_R},
  46. //
  47. .model = &model
  48. };
  49. void nbody_with_starpu(double *positions, double *velocities, double *accelerations, double *masses, double *parameters, unsigned nbr_planets, unsigned nbr_simulations, unsigned nslices)
  50. {
  51. starpu_data_handle_t P_handle, V_handle, A_handle, M_handle, Par_handle, Id_handle;
  52. starpu_matrix_data_register(&P_handle, STARPU_MAIN_RAM, (uintptr_t)positions, 2, 2, nbr_planets, sizeof(double));
  53. starpu_matrix_data_register(&V_handle, STARPU_MAIN_RAM, (uintptr_t)velocities, 2, 2, nbr_planets, sizeof(double));
  54. starpu_matrix_data_register(&A_handle, STARPU_MAIN_RAM, (uintptr_t)accelerations, 2, 2, nbr_planets, sizeof(double));
  55. starpu_vector_data_register(&M_handle, STARPU_MAIN_RAM, (uintptr_t)masses, nbr_planets, sizeof(double));
  56. starpu_vector_data_register(&Par_handle, STARPU_MAIN_RAM, (uintptr_t)parameters, 3, sizeof(double));
  57. struct starpu_data_filter vert =
  58. {
  59. .filter_func = starpu_matrix_filter_vertical_block,
  60. .nchildren = nslices
  61. };
  62. unsigned i;
  63. int64_t *Id = malloc(nslices * sizeof(int64_t));
  64. for (i = 0; i < nbr_simulations; i++){
  65. /* printf("Simulation %d: \n", i); */
  66. starpu_data_partition(A_handle, &vert);
  67. //STRUCT PARAM
  68. /* struct Param *params = malloc(nslices * sizeof(struct Param)); */
  69. /* unsigned epsilon = 2.5e8; */
  70. //
  71. int64_t task1, task2;
  72. for (task1 = 0; task1 < nslices; task1++){
  73. struct starpu_task *task = starpu_task_create();
  74. Id[task1] = task1;
  75. starpu_vector_data_register(&Id_handle, STARPU_MAIN_RAM, (uintptr_t)&(Id[task1]), 1, sizeof(int64_t));
  76. //STRUCT PARAM
  77. /* struct Param param = {task1, epsilon}; */
  78. /* params[task1] = param; */
  79. //
  80. task->cl = &cl;
  81. task->handles[0] = P_handle;
  82. task->handles[1] = starpu_data_get_sub_data(A_handle, 1, task1);
  83. task->handles[2] = M_handle;
  84. task->handles[3] = Par_handle;
  85. task->handles[4] = Id_handle;
  86. //STRUCT PARAM
  87. /* task->cl_arg = &(params[task1]); */
  88. /* task->cl_arg_size = sizeof(struct Param); */
  89. //
  90. starpu_task_submit(task);
  91. starpu_data_unregister_submit(Id_handle);
  92. }
  93. starpu_task_wait_for_all();
  94. ////////////////////////
  95. starpu_data_partition(P_handle, &vert);
  96. starpu_data_partition(V_handle, &vert);
  97. for (task2 = 0; task2 < nslices; task2++){
  98. struct starpu_task *task = starpu_task_create();
  99. //STRUCT PARAM
  100. /* struct Param param = {task1, epsilon}; */
  101. /* params[task2] = param; */
  102. //
  103. task->cl = &cl2;
  104. task->handles[0] = starpu_data_get_sub_data(P_handle, 1, task2);
  105. task->handles[1] = starpu_data_get_sub_data(V_handle, 1, task2);
  106. task->handles[2] = starpu_data_get_sub_data(A_handle, 1, task2);
  107. task->handles[3] = Par_handle;
  108. //STRUCT PARAM
  109. /* task->cl_arg = &(params[task2]); */
  110. /* task->cl_arg_size = sizeof(struct Param); */
  111. //
  112. starpu_task_submit(task);
  113. }
  114. starpu_task_wait_for_all();
  115. starpu_data_unpartition(P_handle, STARPU_MAIN_RAM);
  116. starpu_data_unpartition(V_handle, STARPU_MAIN_RAM);
  117. starpu_data_unpartition(A_handle, STARPU_MAIN_RAM);
  118. /* char filename[38]; */
  119. /* sprintf(filename, "PPM/nbody%d_%d.ppm", nbr_planets, i + 1); */
  120. /* nbody_graph_transpose(filename, positions, nbr_planets, 1000, 1000, -4e8, 4e8); */
  121. }
  122. starpu_data_unregister(P_handle);
  123. starpu_data_unregister(V_handle);
  124. starpu_data_unregister(A_handle);
  125. starpu_data_unregister(M_handle);
  126. starpu_data_unregister(Par_handle);
  127. /* char filename[36]; */
  128. /* sprintf(filename, "PPM/bug%d_%d.ppm", nbr_planets, i); */
  129. /* nbody_graph(filename, positions, nbr_planets, 1000, 1000, -4e8, 4e8); */
  130. }
  131. void init_positions(double *positions, unsigned nbr_planets)
  132. {
  133. unsigned i;
  134. double qiX, qiY;
  135. for (i = 0; i < nbr_planets; i++){
  136. double angle = ((RAND_MAX - rand()) / (double) (RAND_MAX)) * 2.0 * M_PI;
  137. double distToCenter = ((RAND_MAX - rand()) / (double) (RAND_MAX)) * 1.0e8 + 1.0e8;
  138. qiX = cos(angle) * distToCenter;
  139. qiY = sin(angle) * distToCenter;
  140. positions[2*i] = qiX;
  141. positions[2*i + 1] = qiY;
  142. }
  143. }
  144. void init_velocities(double *positions, double *velocities, unsigned nbr_planets)
  145. {
  146. unsigned i;
  147. for (i = 0; i < nbr_planets; i++){
  148. double viX = positions[2*i+1] * 4.0e-6;
  149. double viY = -positions[2*i] * 4.0e-6;
  150. velocities[2*i] = viX;
  151. velocities[2*i + 1] = viY;
  152. }
  153. }
  154. void init_masses(double *masses, unsigned nbr_planets)
  155. {
  156. unsigned i;
  157. for (i = 0; i < nbr_planets; i++){
  158. double mi = (rand() / (double) RAND_MAX) * 1e22;
  159. masses[i] = mi;
  160. }
  161. }
  162. double median_times(unsigned nbr_planets, unsigned nbr_simulations, unsigned nslices, unsigned nbr_tests)
  163. {
  164. double exec_times[nbr_tests];
  165. /* double *positions = malloc(4 * sizeof(double)); */
  166. /* double *velocities = calloc(4, sizeof(double)); */
  167. /* double *accelerations = calloc(4, sizeof(double)); */
  168. double *positions = malloc(nbr_planets * 2 * sizeof(double));
  169. double *velocities = malloc(nbr_planets * 2 * sizeof(double));
  170. double *accelerations = calloc(nbr_planets * 2, sizeof(double));
  171. double *masses = malloc(nbr_planets * sizeof(double));
  172. double *parameters = malloc(3 * sizeof(double));
  173. double G = 6.67408e-11;
  174. /* double dt = 36000; */
  175. double dt = 3600;
  176. double epsilon = 2.5e8;
  177. parameters[0] = G;
  178. parameters[1] = dt;
  179. parameters[2] = epsilon;
  180. init_positions(positions, nbr_planets);
  181. init_velocities(positions, velocities, nbr_planets);
  182. init_masses(masses, nbr_planets);
  183. /* positions[0] = 0; */
  184. /* positions[1] = 300000; */
  185. /* positions[2] = 600000; */
  186. /* positions[3] = 300000; */
  187. /* masses[0] = 5.9e24; */
  188. /* masses[1] = 5.9e24; */
  189. /* unsigned i; */
  190. /* for (i = 0; i < nbr_planets; i++){ */
  191. /* accelerations[i] = i; */
  192. /* } */
  193. /* nbody_graph("PPM/bug0.ppm", positions, nbr_planets, 1000, 1000, -4e8, 4e8); */
  194. /* unsigned k; */
  195. /* for (k = 1; k <= nbr_simulations; k++){ */
  196. double start, stop, exec_t;
  197. unsigned i;
  198. for (i = 0; i < nbr_tests; i++){
  199. start = starpu_timing_now();
  200. nbody_with_starpu(positions, velocities, accelerations, masses, parameters, nbr_planets, nbr_simulations, nslices);
  201. stop = starpu_timing_now();
  202. exec_t = (stop - start) / 1.e6;
  203. exec_times[i] = exec_t;
  204. }
  205. /* printf("\n\nSIMULATION %d:\n\n", k); */
  206. /* char filename[23]; */
  207. /* sprintf(filename, "PPM/bug%d.ppm", k); */
  208. /* nbody_graph(filename, positions, nbr_planets, 1000, 1000, -4e8, 4e8); */
  209. /* } */
  210. /* for (i = 0; i < nbr_planets; i++){ */
  211. /* printf("%f %f\n", accelerations[2*i], accelerations[2*i + 1]); */
  212. /* } */
  213. free(positions);
  214. free(velocities);
  215. free(accelerations);
  216. free(masses);
  217. quicksort(exec_times, 0, nbr_tests - 1);
  218. return exec_times[nbr_tests / 2];
  219. }
  220. void display_times(unsigned start_nbr, unsigned step_nbr, unsigned stop_nbr, unsigned nbr_simulations, unsigned nslices, unsigned nbr_tests)
  221. {
  222. FILE *myfile;
  223. myfile = fopen("DAT/nbody_c_array_times.dat", "w");
  224. unsigned nbr_planets;
  225. for (nbr_planets = start_nbr; nbr_planets <= stop_nbr; nbr_planets += step_nbr){
  226. double t = median_times(nbr_planets, nbr_simulations, nslices, nbr_tests);
  227. printf("ARRAY: %u planets: %f seconds\n", nbr_planets, t);
  228. fprintf(myfile, "%f\n", t);
  229. }
  230. fclose(myfile);
  231. }
  232. int main(int argc, char * argv[])
  233. {
  234. if (argc != 7){
  235. printf("Usage: %s start_nbr step_nbr stop_nbr nbr_simulations nslices nbr_tests\n", argv[0]);
  236. return 1;
  237. }
  238. if (starpu_init(NULL) != EXIT_SUCCESS){
  239. fprintf(stderr, "ERROR\n");
  240. return 77;
  241. }
  242. unsigned start_nbr = (unsigned) atoi(argv[1]);
  243. unsigned step_nbr = (unsigned) atoi(argv[2]);
  244. unsigned stop_nbr = (unsigned) atoi(argv[3]);
  245. unsigned nbr_simulations = (unsigned) atoi(argv[4]);
  246. unsigned nslices = (unsigned) atoi(argv[5]);
  247. unsigned nbr_tests = (unsigned) atoi(argv[6]);
  248. srand(time(NULL));
  249. display_times(start_nbr, step_nbr, stop_nbr, nbr_simulations, nslices, nbr_tests);
  250. starpu_shutdown();
  251. return 0;
  252. }