cpu_mandelbrot.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  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 <starpu.h>
  18. #include <math.h>
  19. #include "cpu_mandelbrot.h"
  20. void cpu_mandelbrot(void *descr[], void *cl_arg)
  21. {
  22. long long *pixels;
  23. pixels = (long long int *)STARPU_MATRIX_GET_PTR(descr[0]);
  24. struct params *params = (struct params *) cl_arg;
  25. long width = STARPU_MATRIX_GET_NY(descr[0]);
  26. long height = STARPU_MATRIX_GET_NX(descr[0]);
  27. double zoom = width * 0.25296875;
  28. double iz = 1. / zoom;
  29. float diverge = 4.0;
  30. float max_iterations = (width/2) * 0.049715909 * log10(zoom);
  31. float imi = 1. / max_iterations;
  32. double centerr = params->centerr;
  33. double centeri = params->centeri;
  34. long offset = params->offset;
  35. long dim = params->dim;
  36. double cr = 0;
  37. double zr = 0;
  38. double ci = 0;
  39. double zi = 0;
  40. long n = 0;
  41. double tmp = 0;
  42. int ldP = STARPU_MATRIX_GET_LD(descr[0]);
  43. long long x,y;
  44. for (y = 0; y < height; y++)
  45. {
  46. for (x = 0; x < width; x++)
  47. {
  48. cr = centerr + (x - (dim/2)) * iz;
  49. zr = cr;
  50. ci = centeri + (y+offset - (dim/2)) * iz;
  51. zi = ci;
  52. for (n = 0; n <= max_iterations; n++)
  53. {
  54. if (zr*zr + zi*zi>diverge) break;
  55. tmp = zr*zr - zi*zi + cr;
  56. zi = 2*zr*zi + ci;
  57. zr = tmp;
  58. }
  59. if (n<max_iterations)
  60. pixels[y +x*ldP] = round(15.*n*imi);
  61. else
  62. pixels[y +x*ldP] = 0;
  63. }
  64. }
  65. }
  66. char* CPU = "cpu_mandelbrot";
  67. char* GPU = "";
  68. extern char *starpu_find_function(char *name, char *device)
  69. {
  70. if (!strcmp(device,"gpu")) return GPU;
  71. return CPU;
  72. }