mandelbrot.c 5.6 KB

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