mandelbrot.c 6.2 KB

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