gpu_mandelbrot.cu 2.7 KB

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