mandelbrot.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 <stdio.h>
  18. #include <stdlib.h>
  19. #include <starpu.h>
  20. #include "../display.h"
  21. void cpu_mandelbrot(void **, void *);
  22. void gpu_mandelbrot(void **, void *);
  23. struct Params
  24. {
  25. float cr;
  26. float ci;
  27. unsigned taskx;
  28. unsigned tasky;
  29. unsigned width;
  30. unsigned height;
  31. };
  32. struct starpu_codelet cl =
  33. {
  34. .cpu_funcs = {cpu_mandelbrot},
  35. .cuda_funcs = {gpu_mandelbrot},
  36. .nbuffers = 1,
  37. .modes = {STARPU_RW}
  38. };
  39. void mandelbrot_with_starpu(int *pixels, float cr, float ci, unsigned width, unsigned height, unsigned nslicesx, unsigned nslicesy)
  40. {
  41. starpu_data_handle_t p_handle;
  42. starpu_matrix_data_register(&p_handle, STARPU_MAIN_RAM, (uintptr_t)pixels, width, width, height, sizeof(int));
  43. struct starpu_data_filter vert =
  44. {
  45. .filter_func = starpu_matrix_filter_vertical_block,
  46. .nchildren = nslicesy
  47. };
  48. struct starpu_data_filter horiz =
  49. {
  50. .filter_func = starpu_matrix_filter_block,
  51. .nchildren = nslicesx
  52. };
  53. starpu_data_map_filters(p_handle, 2, &vert, &horiz);
  54. unsigned taskx, tasky;
  55. struct Params *params = malloc(nslicesx*nslicesy*sizeof(struct Params));
  56. for (taskx = 0; taskx < nslicesx; taskx++){
  57. for (tasky = 0; tasky < nslicesy; tasky++){
  58. struct starpu_task *task = starpu_task_create();
  59. task->cl = &cl;
  60. task->handles[0] = starpu_data_get_sub_data(p_handle, 2, tasky, taskx);
  61. struct Params param = {cr, ci, taskx, tasky, width, height};
  62. params[taskx + tasky*nslicesx] = param;
  63. task->cl_arg = (params + taskx + tasky * nslicesx);
  64. task->cl_arg_size = sizeof(struct Params);
  65. starpu_task_submit(task);
  66. }
  67. }
  68. starpu_task_wait_for_all();
  69. starpu_data_unpartition(p_handle, STARPU_MAIN_RAM);
  70. starpu_data_unregister(p_handle);
  71. free(params);
  72. }
  73. void init_zero(int * pixels, unsigned width, unsigned height)
  74. {
  75. unsigned i,j;
  76. for (i = 0; i < height; i++){
  77. for (j = 0; j < width; j++){
  78. pixels[j + i*width] = 0;
  79. }
  80. }
  81. }
  82. void sort(double *arr, unsigned nbr_tests)
  83. {
  84. unsigned j;
  85. int is_sort = 0;
  86. while (!is_sort){
  87. is_sort = 1;
  88. for (j = 0; j < nbr_tests - 1; j++){
  89. if (arr[j] > arr[j+1]){
  90. is_sort = 0;
  91. double tmp = arr[j];
  92. arr[j] = arr[j+1];
  93. arr[j+1] = tmp;
  94. }
  95. }
  96. }
  97. }
  98. double median_time(float cr, float ci, unsigned width, unsigned height, unsigned nslicesx, unsigned nslicesy, unsigned nbr_tests)
  99. {
  100. int *Pixels = malloc(width*height*sizeof(int));
  101. unsigned i;
  102. double exec_times[nbr_tests];
  103. double start, stop, exec_t;
  104. for (i = 0; i < nbr_tests; i++){
  105. init_zero(Pixels, width, height);
  106. start = starpu_timing_now(); // starpu_timing_now() gives the time in microseconds.
  107. mandelbrot_with_starpu(Pixels, cr, ci, width, height, nslicesx, nslicesy);
  108. stop = starpu_timing_now();
  109. exec_t = (stop-start)/1.e6;
  110. exec_times[i] = exec_t;
  111. }
  112. char filename[30];
  113. sprintf(filename, "PPM/mandelbrot%d.ppm", width);
  114. printf("%s\n", filename);
  115. mandelbrot_graph(filename, Pixels, width, height);
  116. free(Pixels);
  117. sort(exec_times, nbr_tests);
  118. return exec_times[nbr_tests/2];
  119. }
  120. void fluctuation_time(float cr, float ci, unsigned width, unsigned height, unsigned nslicesx, unsigned nslicesy, unsigned nbr_tests, double *exec_times)
  121. {
  122. int *Pixels = malloc(width*height*sizeof(int));
  123. unsigned i;
  124. double start, stop, exec_t;
  125. for (i = 0; i < nbr_tests; i++){
  126. init_zero(Pixels, width, height);
  127. start = starpu_timing_now(); // starpu_timing_now() gives the time in microseconds.
  128. mandelbrot_with_starpu(Pixels, cr, ci, width, height, nslicesx, nslicesy);
  129. stop = starpu_timing_now();
  130. exec_t = (stop-start)/1.e6;
  131. exec_times[i] = exec_t;
  132. /* char filename[33]; */
  133. /* sprintf(filename, "../PPM/mandelbrot%d.ppm", i + 1); */
  134. /* printf("%s\n", filename); */
  135. /* mandelbrot_graph(filename, Pixels, width, height); */
  136. }
  137. free(Pixels);
  138. }
  139. void display_times(float cr, float ci, unsigned start_dim, unsigned step_dim, unsigned stop_dim, unsigned nslices, unsigned nbr_tests)
  140. {
  141. unsigned dim;
  142. FILE *myfile;
  143. myfile = fopen("DAT/mandelbrot_c_struct_times.dat", "w");
  144. for (dim = start_dim; dim <= stop_dim; dim += step_dim){
  145. printf("Dimension: %u...\n", dim);
  146. double t = median_time(cr, ci, dim, dim, nslices, nslices, nbr_tests);
  147. printf("w = %u ; h = %u ; t = %f\n", dim, dim, t);
  148. fprintf(myfile, "%f\n", t);
  149. }
  150. fclose(myfile);
  151. }
  152. void display_fluctuations(float cr, float ci, unsigned start_dim, unsigned step_dim, unsigned stop_dim, unsigned nslices, unsigned nbr_tests)
  153. {
  154. unsigned dim;
  155. FILE *myfile;
  156. myfile = fopen("DAT/mandelbrot_c_fluctuation.dat", "w");
  157. double *exec_times = malloc(nbr_tests * sizeof(double));
  158. fluctuation_time(cr, ci, start_dim, start_dim, nslices, nslices, nbr_tests, exec_times);
  159. /* printf("w = %u ; h = %u ; t = %f\n", dim, dim, t); */
  160. unsigned i;
  161. for (i = 0; i < nbr_tests; i++){
  162. printf("test %u: %f seconds\n", i, exec_times[i]);
  163. fprintf(myfile, "%u %f\n", i, exec_times[i]);
  164. }
  165. fclose(myfile);
  166. free(exec_times);
  167. }
  168. int main(int argc, char **argv)
  169. {
  170. if (argc != 8){
  171. printf("Usage: %s cr ci start_dim step_dim stop_dim nslices(must divide dims) nbr_tests\n", argv[0]);
  172. return 1;
  173. }
  174. if (starpu_init(NULL) != EXIT_SUCCESS){
  175. fprintf(stderr, "ERROR\n");
  176. return 77;
  177. }
  178. float cr = (float) atof(argv[1]);
  179. float ci = (float) atof(argv[2]);
  180. unsigned start_dim = (unsigned) atoi(argv[3]);
  181. unsigned step_dim = (unsigned) atoi(argv[4]);
  182. unsigned stop_dim = (unsigned) atoi(argv[5]);
  183. unsigned nslices = (unsigned) atoi(argv[6]);
  184. unsigned nbr_tests = (unsigned) atoi(argv[7]);
  185. display_times(cr, ci, start_dim, step_dim, stop_dim, nslices, nbr_tests);
  186. /* display_fluctuations(cr, ci, start_dim, step_dim, stop_dim, nslices, nbr_tests); */
  187. starpu_shutdown();
  188. return 0;
  189. }