gpu_mandelbrot.cu 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 <starpu.h>
  17. #include <stdint.h>
  18. #include <stdio.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. __global__ void gpuMandelbrotKernel
  30. (
  31. uint32_t nxP, uint32_t nyP,
  32. uint32_t ldP,
  33. int * subP,
  34. struct Params params
  35. )
  36. {
  37. unsigned width = params.width;
  38. unsigned height = params.height;
  39. unsigned taskx = params.taskx;
  40. unsigned tasky = params.tasky;
  41. float centerr = params.cr;
  42. float centeri = params.ci;
  43. float zoom = width * 0.25296875;
  44. int maxiter = (width/2) * 0.049715909 * log10(zoom);
  45. float conv_lim = 2.0;
  46. uint32_t id;
  47. int n,i,j,x,y;
  48. float zr,zi,cr,ci;
  49. id = blockIdx.x * blockDim.x + threadIdx.x;
  50. i = id % nxP;
  51. j = id / nxP;
  52. if (j >= nyP){
  53. return;
  54. }
  55. x = i + taskx * nxP;
  56. y = j + tasky * nyP;
  57. zr = cr = centerr + (x - width/2.)/zoom;
  58. zi = ci = centeri + (y - height/2.)/zoom;
  59. float m = zr*zr + zi*zi;
  60. for (n = 0; n <= maxiter && m < conv_lim * conv_lim; n++) {
  61. float tmp = zr * zr - zi*zi + cr;
  62. zi = 2*zr*zi + ci;
  63. zr = tmp;
  64. m = zr*zr + zi*zi;
  65. }
  66. int color;
  67. if (n < maxiter)
  68. color = 255.*n/maxiter;
  69. else
  70. color = 0;
  71. subP[i + j*ldP] = color;
  72. }
  73. #define THREADS_PER_BLOCK 64
  74. extern "C" void gpu_mandelbrot(void *descr[], void *args)
  75. {
  76. int *d_subP;
  77. uint32_t nxP, nyP;
  78. uint32_t ldP;
  79. uint32_t nblocks;
  80. struct Params *params = (struct Params *) args;
  81. d_subP = (int *) STARPU_MATRIX_GET_PTR(descr[0]);
  82. nxP = STARPU_MATRIX_GET_NX(descr[0]);
  83. nyP = STARPU_MATRIX_GET_NY(descr[0]);
  84. ldP = STARPU_MATRIX_GET_LD(descr[0]);
  85. nblocks = (nxP * nyP + THREADS_PER_BLOCK - 1)/THREADS_PER_BLOCK;
  86. gpuMandelbrotKernel <<< nblocks, THREADS_PER_BLOCK, 0, starpu_cuda_get_local_stream() >>> (nxP, nyP, ldP, d_subP, *params);
  87. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  88. }