gpu_mandelbrot_between.cu 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 <stdio.h>
  18. #include <stdint.h>
  19. #include <starpu.h>
  20. #define THREADS_PER_BLOCK 64
  21. static inline long long jlstarpu_max(long long a, long long b)
  22. {
  23. return (a > b) ? a : b;
  24. }
  25. static inline long long jlstarpu_interval_size(long long start, long long step, long long stop)
  26. {
  27. if (stop >= start){
  28. return jlstarpu_max(0, (stop - start + 1) / step);
  29. } else {
  30. return jlstarpu_max(0, (stop - start - 1) / step);
  31. }
  32. }
  33. __device__ static inline long long jlstarpu_max__device(long long a, long long b)
  34. {
  35. return (a > b) ? a : b;
  36. }
  37. __device__ static inline long long jlstarpu_interval_size__device(long long start, long long step, long long stop)
  38. {
  39. if (stop >= start){
  40. return jlstarpu_max__device(0, (stop - start + 1) / step);
  41. } else {
  42. return jlstarpu_max__device(0, (stop - start - 1) / step);
  43. }
  44. }
  45. __global__ void mandelbrot(int64_t kernel_ids__start_1, int64_t kernel_ids__step_1, int64_t kernel_ids__dim_1, int64_t kernel_ids__start_2,
  46. int64_t kernel_ids__step_2, int64_t kernel_ids__dim_2, double* ptr_hF6lCYyJ, int64_t local_width,
  47. int64_t* ptr_qoUGBRtY, int64_t local_height, double conv_limit, int64_t* ptr_A5zD9sJZ,
  48. uint32_t ld_A5zD9sJZ)
  49. {
  50. int64_t THREAD_ID = (int64_t) ((((blockIdx).x) * ((blockDim).x)) + ((threadIdx).x));
  51. if ((THREAD_ID) >= (((1) * (kernel_ids__dim_2)) * (kernel_ids__dim_1)))
  52. {
  53. return ;
  54. };
  55. int64_t kernel_ids__index_1 = (int64_t) (((THREAD_ID) / ((1) * (kernel_ids__dim_2))) % (kernel_ids__dim_1));
  56. int64_t kernel_ids__index_2 = (int64_t) (((THREAD_ID) / (1)) % (kernel_ids__dim_2));
  57. int64_t x = (int64_t) ((kernel_ids__start_1) + ((kernel_ids__index_1) * (kernel_ids__step_1)));
  58. int64_t y = (int64_t) ((kernel_ids__start_2) + ((kernel_ids__index_2) * (kernel_ids__step_2)));
  59. double max_iterations = (double) (ptr_hF6lCYyJ[(5) - (1)]);
  60. double zoom = (double) ((ptr_hF6lCYyJ[(3) - (1)]) * (0.25296875));
  61. int64_t X = (int64_t) ((x) + ((local_width) * ((ptr_qoUGBRtY[(2) - (1)]) - (1))));
  62. int64_t Y = (int64_t) ((y) + ((local_height) * ((ptr_qoUGBRtY[(1) - (1)]) - (1))));
  63. double cr = (double) ((ptr_hF6lCYyJ[(1) - (1)]) + (((X) - ((ptr_hF6lCYyJ[(3) - (1)]) / (2))) / (zoom)));
  64. double zr = (double) (cr);
  65. double ci = (double) ((ptr_hF6lCYyJ[(2) - (1)]) + (((Y) - ((ptr_hF6lCYyJ[(4) - (1)]) / (2))) / (zoom)));
  66. double zi = (double) (ci);
  67. int64_t n = (int64_t) (0);
  68. int64_t b1 = (int64_t) (((n) < (max_iterations)) + ((((zr) * (zr)) + ((zi) * (zi))) < ((conv_limit) * (conv_limit))));
  69. while ((b1) >= (2))
  70. {
  71. double tmp = (double) ((((zr) * (zr)) - ((zi) * (zi))) + (cr));
  72. zi = ((2) * (zr) * (zi)) + (ci);
  73. zr = tmp;
  74. n = (n) + (1);
  75. b1 = ((n) <= (max_iterations)) + ((((zr) * (zr)) + ((zi) * (zi))) <= ((conv_limit) * (conv_limit)));
  76. }
  77. ;
  78. if ((n) < (max_iterations))
  79. {
  80. ptr_A5zD9sJZ[((y) + (((x) - (1)) * (ld_A5zD9sJZ))) - (1)] = ((255) * (n)) / (max_iterations);
  81. } else
  82. {
  83. ptr_A5zD9sJZ[((y) + (((x) - (1)) * (ld_A5zD9sJZ))) - (1)] = 0;
  84. }
  85. ;
  86. }
  87. extern "C" void CUDA_mandelbrot(void** buffers_uwrYFDVe, void* cl_arg_uwrYFDVe)
  88. {
  89. uint32_t ld_A5zD9sJZ = (uint32_t) (STARPU_MATRIX_GET_LD(buffers_uwrYFDVe[(1) - (1)]));
  90. int64_t* ptr_A5zD9sJZ = (int64_t*) (STARPU_MATRIX_GET_PTR(buffers_uwrYFDVe[(1) - (1)]));
  91. double* ptr_hF6lCYyJ = (double*) (STARPU_VECTOR_GET_PTR(buffers_uwrYFDVe[(2) - (1)]));
  92. int64_t* ptr_qoUGBRtY = (int64_t*) (STARPU_VECTOR_GET_PTR(buffers_uwrYFDVe[(3) - (1)]));
  93. int64_t local_width = (int64_t) (STARPU_MATRIX_GET_NY(buffers_uwrYFDVe[(1) - (1)]));
  94. int64_t local_height = (int64_t) (STARPU_MATRIX_GET_NX(buffers_uwrYFDVe[(1) - (1)]));
  95. double conv_limit = (double) (2.0);
  96. int64_t kernel_ids__start_1 = (int64_t) (1);
  97. int64_t kernel_ids__step_1 = (int64_t) (1);
  98. int64_t kernel_ids__dim_1 = (int64_t) (jlstarpu_interval_size(kernel_ids__start_1, kernel_ids__step_1, local_width));
  99. int64_t kernel_ids__start_2 = (int64_t) (1);
  100. int64_t kernel_ids__step_2 = (int64_t) (1);
  101. int64_t kernel_ids__dim_2 = (int64_t) (jlstarpu_interval_size(kernel_ids__start_2, kernel_ids__step_2, local_height));
  102. int64_t nthreads = (int64_t) (((1) * (kernel_ids__dim_1)) * (kernel_ids__dim_2));
  103. int64_t nblocks = (int64_t) ((((nthreads) + (THREADS_PER_BLOCK)) - (1)) / (THREADS_PER_BLOCK));
  104. mandelbrot
  105. <<< nblocks, THREADS_PER_BLOCK, 0, starpu_cuda_get_local_stream()
  106. >>> (kernel_ids__start_1, kernel_ids__step_1, kernel_ids__dim_1, kernel_ids__start_2,
  107. kernel_ids__step_2, kernel_ids__dim_2, ptr_hF6lCYyJ, local_width,
  108. ptr_qoUGBRtY, local_height, conv_limit, ptr_A5zD9sJZ,
  109. ld_A5zD9sJZ);
  110. ;
  111. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  112. }