mandelbrot.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. void cpu_mandelbrot(void **, void *);
  20. void gpu_mandelbrot(void **, void *);
  21. static struct starpu_perfmodel model =
  22. {
  23. .type = STARPU_HISTORY_BASED,
  24. .symbol = "history_perf"
  25. };
  26. static struct starpu_codelet cl =
  27. {
  28. .cpu_funcs = {cpu_mandelbrot},
  29. //.cuda_funcs = {gpu_mandelbrot},
  30. .nbuffers = 2,
  31. .modes = {STARPU_W, STARPU_R},
  32. .model = &model
  33. };
  34. void mandelbrot_with_starpu(long long *pixels, float *params, long long dim, long long nslicesx)
  35. {
  36. starpu_data_handle_t pixels_handle;
  37. starpu_data_handle_t params_handle;
  38. starpu_matrix_data_register(&pixels_handle, STARPU_MAIN_RAM, (uintptr_t)pixels, dim, dim, dim, sizeof(long long));
  39. starpu_matrix_data_register(&params_handle, STARPU_MAIN_RAM, (uintptr_t)params, 4*nslicesx, 4*nslicesx, 1, sizeof(float));
  40. struct starpu_data_filter horiz =
  41. {
  42. .filter_func = starpu_matrix_filter_block,
  43. .nchildren = nslicesx
  44. };
  45. starpu_data_partition(pixels_handle, &horiz);
  46. starpu_data_partition(params_handle, &horiz);
  47. long long taskx;
  48. for (taskx = 0; taskx < nslicesx; taskx++){
  49. struct starpu_task *task = starpu_task_create();
  50. task->cl = &cl;
  51. task->handles[0] = starpu_data_get_child(pixels_handle, taskx);
  52. task->handles[1] = starpu_data_get_child(params_handle, taskx);
  53. if (starpu_task_submit(task)!=0) fprintf(stderr,"submit task error\n");
  54. }
  55. starpu_task_wait_for_all();
  56. starpu_data_unpartition(pixels_handle, STARPU_MAIN_RAM);
  57. starpu_data_unpartition(params_handle, STARPU_MAIN_RAM);
  58. starpu_data_unregister(pixels_handle);
  59. starpu_data_unregister(params_handle);
  60. }
  61. void pixels2img(long long *pixels, long long width, long long height, const char *filename)
  62. {
  63. FILE *fp = fopen(filename, "w");
  64. if (!fp)
  65. return;
  66. int MAPPING[16][3] = {{66,30,15},{25,7,26},{9,1,47},{4,4,73},{0,7,100},{12,44,138},{24,82,177},{57,125,209},{134,181,229},{211,236,248},{241,233,191},{248,201,95},{255,170,0},{204,128,0},{153,87,0},{106,52,3}};
  67. fprintf(fp, "P3\n%lld %lld\n255\n", width, height);
  68. long long i, j;
  69. for (i = 0; i < height; ++i) {
  70. for (j = 0; j < width; ++j) {
  71. fprintf(fp, "%d %d %d ", MAPPING[pixels[j*width+i]][0], MAPPING[pixels[j*width+i]][1], MAPPING[pixels[j*width+i]][2]);
  72. }
  73. }
  74. fclose(fp);
  75. }
  76. double min_times(double cr, double ci, long long dim, long long nslices)
  77. {
  78. long long *pixels = calloc(dim*dim, sizeof(long long));
  79. float *params = calloc(4*nslices, sizeof(float));
  80. double t_min = 0;
  81. long long i;
  82. for (i=0; i<nslices; i++) {
  83. params[4*i+0] = cr;
  84. params[4*i+1] = ci;
  85. params[4*i+2] = i*dim/nslices;
  86. params[4*i+3] = dim;
  87. }
  88. double start, stop, exec_t;
  89. for (i = 0; i < 10; i++){
  90. start = starpu_timing_now(); // starpu_timing_now() gives the time in microseconds.
  91. mandelbrot_with_starpu(pixels, params, dim, nslices);
  92. stop = starpu_timing_now();
  93. exec_t = (stop-start)*1.e3;
  94. if (t_min==0 || t_min>exec_t)
  95. t_min = exec_t;
  96. }
  97. char filename[64];
  98. snprintf(filename, 64, "out%lld.ppm", dim);
  99. pixels2img(pixels,dim,dim,filename);
  100. free(pixels);
  101. free(params);
  102. return t_min;
  103. }
  104. void display_times(double cr, double ci, long long start_dim, long long step_dim, long long stop_dim, long long nslices)
  105. {
  106. long long dim;
  107. for (dim = start_dim; dim <= stop_dim; dim += step_dim) {
  108. printf("Dimension: %lld...\n", dim);
  109. double res = min_times(cr, ci, dim, nslices);
  110. res = res / dim / dim; // time per pixel
  111. printf("%lld %lf\n", dim, res);
  112. }
  113. }
  114. int main(int argc, char **argv)
  115. {
  116. if (argc != 7){
  117. printf("Usage: %s cr ci start_dim step_dim stop_dim nslices(must divide dims)\n", argv[0]);
  118. return 1;
  119. }
  120. if (starpu_init(NULL) != EXIT_SUCCESS){
  121. fprintf(stderr, "ERROR\n");
  122. return 77;
  123. }
  124. double cr = (float) atof(argv[1]);
  125. double ci = (float) atof(argv[2]);
  126. long long start_dim = atoll(argv[3]);
  127. long long step_dim = atoll(argv[4]);
  128. long long stop_dim = atoll(argv[5]);
  129. long long nslices = atoll(argv[6]);
  130. display_times(cr, ci, start_dim, step_dim, stop_dim, nslices);
  131. starpu_shutdown();
  132. return 0;
  133. }