mandelbrot_between.c 6.0 KB

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