nbody_between.c 9.4 KB

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