nbody.c 7.6 KB

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