cpu_mandelbrot.c 2.0 KB

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