cpu_mandelbrot.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 <stdint.h>
  18. #include <starpu.h>
  19. #include <math.h>
  20. struct Params
  21. {
  22. float cr;
  23. float ci;
  24. unsigned taskx;
  25. unsigned tasky;
  26. unsigned width;
  27. unsigned height;
  28. };
  29. void cpu_mandelbrot(void *descr[], void *cl_arg)
  30. {
  31. struct Params *params = cl_arg;
  32. int *subP;
  33. uint32_t nxP, nyP;
  34. uint32_t ldP;
  35. subP = (int *)STARPU_MATRIX_GET_PTR(descr[0]);
  36. nxP = STARPU_MATRIX_GET_NX(descr[0]);
  37. nyP = STARPU_MATRIX_GET_NY(descr[0]);
  38. ldP = STARPU_MATRIX_GET_LD(descr[0]);
  39. float centerr = params->cr;
  40. float centeri = params->ci;
  41. unsigned Idx = params->taskx;
  42. unsigned Idy = params->tasky;
  43. unsigned width = params->width;
  44. unsigned height = params->height;
  45. float zoom = width * 0.25296875;
  46. float conv_limit = 2.0;
  47. int max_iter = (width/2) * 0.049715909 * log10(zoom);
  48. int x,y,n;
  49. for (y = 0; y < nyP; y++){
  50. for (x = 0; x < nxP; x++){
  51. float X = x + Idx*nxP; //Coordinates in the whole matrice.
  52. float Y = y + Idy*nyP;
  53. float cr = centerr + (X - (width/2))/zoom;
  54. float ci = centeri + (Y - (height/2))/zoom;
  55. float zr = cr;
  56. float zi = ci;
  57. float m = zr * zr + zi * zi;
  58. for (n = 0; n <= max_iter && m < conv_limit * conv_limit; n++) {
  59. float tmp = zr*zr - zi*zi + cr;
  60. zi = 2*zr*zi + ci;
  61. zr = tmp;
  62. m = zr*zr + zi*zi;
  63. }
  64. int color;
  65. if (n<max_iter)
  66. color = 255.*n/max_iter;
  67. else
  68. color = 0;
  69. subP[x + y*ldP] = color;
  70. }
  71. }
  72. }