nbody.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. * Copyright (C) 2019 Mael Keryell
  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 <starpu.h>
  20. #include <math.h>
  21. #include "../includes/display.h"
  22. #include "../includes/sorting.h"
  23. struct Param {
  24. unsigned taskx;
  25. double epsilon;
  26. };
  27. void cpu_nbody(void **, void *);
  28. void cpu_nbody2(void **, void *);
  29. void gpu_nbody(void **, void *);
  30. void gpu_nbody2(void **, void *);
  31. static struct starpu_perfmodel model =
  32. {
  33. .type = STARPU_HISTORY_BASED,
  34. .symbol = "history_perf"
  35. };
  36. static struct starpu_codelet cl =
  37. {
  38. .cpu_funcs = {cpu_nbody},
  39. .cuda_funcs = {gpu_nbody},
  40. .nbuffers = 3,
  41. .modes = {STARPU_RW, STARPU_RW, STARPU_RW},
  42. .model = &model
  43. };
  44. static struct starpu_codelet cl2 =
  45. {
  46. .cpu_funcs = {cpu_nbody2},
  47. .cuda_funcs = {gpu_nbody2},
  48. .nbuffers = 3,
  49. .modes = {STARPU_RW, STARPU_RW, STARPU_R},
  50. .model = &model
  51. };
  52. void nbody_with_starpu(double *positions, double *velocities, double *accelerations, double *masses, unsigned nbr_planets, unsigned nbr_simulations, unsigned nslices)
  53. {
  54. starpu_data_handle_t P_handle, V_handle, A_handle, M_handle;
  55. starpu_matrix_data_register(&P_handle, STARPU_MAIN_RAM, (uintptr_t)positions, nbr_planets, nbr_planets, 2, sizeof(double));
  56. starpu_matrix_data_register(&V_handle, STARPU_MAIN_RAM, (uintptr_t)velocities, nbr_planets, nbr_planets, 2, sizeof(double));
  57. starpu_matrix_data_register(&A_handle, STARPU_MAIN_RAM, (uintptr_t)accelerations, nbr_planets, nbr_planets, 2, sizeof(double));
  58. starpu_matrix_data_register(&M_handle, STARPU_MAIN_RAM, (uintptr_t)masses, nbr_planets, nbr_planets, 1, sizeof(double));
  59. struct starpu_data_filter horiz =
  60. {
  61. .filter_func = starpu_matrix_filter_block,
  62. .nchildren = nslices
  63. };
  64. unsigned i;
  65. for (i = 0; i < nbr_simulations; i++){
  66. /* printf("Simulation %d: \n", i); */
  67. starpu_data_partition(A_handle, &horiz);
  68. struct Param *params = malloc(nslices * sizeof(struct Param));
  69. unsigned epsilon = 2.5e8;
  70. unsigned task1, task2;
  71. for (task1 = 0; task1 < nslices; task1++){
  72. struct starpu_task *task = starpu_task_create();
  73. struct Param param = {task1, epsilon};
  74. params[task1] = param;
  75. task->cl = &cl;
  76. task->handles[0] = P_handle;
  77. task->handles[1] = starpu_data_get_sub_data(A_handle, 1, task1);
  78. task->handles[2] = M_handle;
  79. task->cl_arg = &(params[task1]);
  80. task->cl_arg_size = sizeof(struct Param);
  81. starpu_task_submit(task);
  82. }
  83. starpu_task_wait_for_all();
  84. ////////////////////////
  85. starpu_data_partition(P_handle, &horiz);
  86. starpu_data_partition(V_handle, &horiz);
  87. for (task2 = 0; task2 < nslices; task2++){
  88. struct starpu_task *task = starpu_task_create();
  89. struct Param param = {task1, epsilon};
  90. params[task2] = param;
  91. task->cl = &cl2;
  92. task->handles[0] = starpu_data_get_sub_data(P_handle, 1, task2);
  93. task->handles[1] = starpu_data_get_sub_data(V_handle, 1, task2);
  94. task->handles[2] = starpu_data_get_sub_data(A_handle, 1, task2);
  95. task->cl_arg = &(params[task2]);
  96. task->cl_arg_size = sizeof(struct Param);
  97. starpu_task_submit(task);
  98. }
  99. starpu_task_wait_for_all();
  100. starpu_data_unpartition(P_handle, STARPU_MAIN_RAM);
  101. starpu_data_unpartition(V_handle, STARPU_MAIN_RAM);
  102. starpu_data_unpartition(A_handle, STARPU_MAIN_RAM);
  103. /* char filename[41]; */
  104. /* sprintf(filename, "../PPM/nbody%d_%d.ppm", nbr_planets, i + 1); */
  105. /* nbody_graph(filename, positions, nbr_planets, 1000, 1000, -4e8, 4e8); */
  106. }
  107. starpu_data_unregister(P_handle);
  108. starpu_data_unregister(V_handle);
  109. starpu_data_unregister(A_handle);
  110. starpu_data_unregister(M_handle);
  111. }
  112. void init_positions(double *positions, unsigned nbr_planets)
  113. {
  114. unsigned i;
  115. double qiX, qiY;
  116. for (i = 0; i < nbr_planets; i++){
  117. double angle = ((RAND_MAX - rand()) / (double) (RAND_MAX)) * 2.0 * M_PI;
  118. double distToCenter = ((RAND_MAX - rand()) / (double) (RAND_MAX)) * 1.0e8 + 1.0e8;
  119. qiX = cos(angle) * distToCenter;
  120. qiY = sin(angle) * distToCenter;
  121. positions[i] = qiX;
  122. positions[i + nbr_planets] = qiY;
  123. }
  124. }
  125. void init_velocities(double *positions, double *velocities, unsigned nbr_planets)
  126. {
  127. unsigned i;
  128. for (i = 0; i < nbr_planets; i++){
  129. double viX = positions[i + nbr_planets] * 4.0e-6;
  130. double viY = -positions[i] * 4.0e-6;
  131. velocities[i] = viX;
  132. velocities[i + nbr_planets] = viY;
  133. }
  134. }
  135. void init_masses(double *masses, unsigned nbr_planets)
  136. {
  137. unsigned i;
  138. for (i = 0; i < nbr_planets; i++){
  139. double mi = (rand() / (double) RAND_MAX) * 5e25;
  140. masses[i] = mi;
  141. }
  142. }
  143. double median_times(unsigned nbr_planets, unsigned nbr_simulations, unsigned nslices, unsigned nbr_tests)
  144. {
  145. double exec_times[nbr_tests];
  146. double *positions = malloc(nbr_planets * 2 * sizeof(double));
  147. double *velocities = malloc(nbr_planets * 2 * sizeof(double));
  148. double *accelerations = calloc(nbr_planets * 2, sizeof(double));
  149. double *masses = malloc(nbr_planets * sizeof(double));
  150. init_positions(positions, nbr_planets);
  151. init_velocities(positions, velocities, nbr_planets);
  152. init_masses(masses, nbr_planets);
  153. /* unsigned i; */
  154. /* for (i = 0; i < nbr_planets; i++){ */
  155. /* accelerations[i] = i; */
  156. /* } */
  157. /* nbody_graph("PPM/bug0.ppm", positions, nbr_planets, 1000, 1000, -4e8, 4e8); */
  158. /* unsigned k; */
  159. /* for (k = 1; k <= nbr_simulations; k++){ */
  160. double start, stop, exec_t;
  161. unsigned i;
  162. for (i = 0; i < nbr_tests; i++){
  163. start = starpu_timing_now();
  164. nbody_with_starpu(positions, velocities, accelerations, masses, nbr_planets, nbr_simulations, nslices);
  165. stop = starpu_timing_now();
  166. exec_t = (stop - start) / 1.e6;
  167. exec_times[i] = exec_t;
  168. }
  169. /* printf("\n\nSIMULATION %d:\n\n", k); */
  170. /* char filename[23]; */
  171. /* sprintf(filename, "PPM/bug%d.ppm", k); */
  172. /* nbody_graph(filename, positions, nbr_planets, 1000, 1000, -4e8, 4e8); */
  173. /* } */
  174. free(positions);
  175. free(velocities);
  176. free(accelerations);
  177. free(masses);
  178. quicksort(exec_times, 0, nbr_tests - 1);
  179. return exec_times[nbr_tests / 2];
  180. }
  181. void display_times(unsigned start_nbr, unsigned step_nbr, unsigned stop_nbr, unsigned nbr_simulations, unsigned nslices, unsigned nbr_tests)
  182. {
  183. FILE *myfile;
  184. myfile = fopen("DAT/nbody_c_struct_times.dat", "w");
  185. unsigned nbr_planets;
  186. for (nbr_planets = start_nbr; nbr_planets <= stop_nbr; nbr_planets += step_nbr){
  187. double t = median_times(nbr_planets, nbr_simulations, nslices, nbr_tests);
  188. printf("STRUCT: %u planets: %f seconds\n", nbr_planets, t);
  189. fprintf(myfile, "%f\n", t);
  190. }
  191. fclose(myfile);
  192. }
  193. int main(int argc, char * argv[])
  194. {
  195. if (argc != 7){
  196. printf("Usage: %s start_nbr step_nbr stop_nbr nbr_simulations nslices nbr_tests\n", argv[0]);
  197. return 1;
  198. }
  199. if (starpu_init(NULL) != EXIT_SUCCESS){
  200. fprintf(stderr, "ERROR\n");
  201. return 77;
  202. }
  203. unsigned start_nbr = (unsigned) atoi(argv[1]);
  204. unsigned step_nbr = (unsigned) atoi(argv[2]);
  205. unsigned stop_nbr = (unsigned) atoi(argv[3]);
  206. unsigned nbr_simulations = (unsigned) atoi(argv[4]);
  207. unsigned nslices = (unsigned) atoi(argv[5]);
  208. unsigned nbr_tests = (unsigned) atoi(argv[6]);
  209. srand(time(NULL));
  210. display_times(start_nbr, step_nbr, stop_nbr, nbr_simulations, nslices, nbr_tests);
  211. starpu_shutdown();
  212. return 0;
  213. }