nbody_between.c 9.5 KB

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