mandelbrot.jl 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. import Libdl
  17. using StarPU
  18. using LinearAlgebra
  19. @target STARPU_CPU+STARPU_CUDA
  20. @codelet function mandelbrot(pixels ::Matrix{Int64}, centerr ::Float64, centeri ::Float64, offset ::Int64, dim ::Int64 ) :: Nothing
  21. height :: Int64 = height(pixels)
  22. width :: Int64 = width(pixels)
  23. zoom :: Float64 = width * 0.25296875
  24. iz :: Float64 = 1. / zoom
  25. diverge :: Float32 = 4.0
  26. max_iterations :: Float32 = ((width/2) * 0.049715909 * log10(zoom));
  27. imi :: Float32 = 1. / max_iterations
  28. cr :: Float64 = 0.
  29. zr :: Float64 = 0.
  30. ci :: Float64 = 0.
  31. zi :: Float64 = 0.
  32. n :: Int64 = 0
  33. tmp :: Float64 = 0.
  34. @parallel for y = 1:height
  35. for x = 1:width
  36. cr = centerr + (x-1 - (dim / 2)) * iz
  37. zr = cr
  38. ci = centeri + (y-1+offset - (dim / 2)) * iz
  39. zi = ci
  40. max_it :: Float64 = max_iterations
  41. n = 0
  42. for i = 0:max_it
  43. n = i
  44. if (zr*zr + zi*zi > diverge)
  45. break
  46. end
  47. tmp = zr*zr - zi*zi + cr
  48. zi = 2*zr*zi + ci
  49. zr = tmp
  50. end
  51. if (n < max_iterations)
  52. pixels[y,x] = round(15 * n * imi)
  53. else
  54. pixels[y,x] = 0
  55. end
  56. end
  57. end
  58. return
  59. end
  60. starpu_init()
  61. function mandelbrot_with_starpu(A ::Matrix{Int64}, cr ::Float64, ci ::Float64, dim ::Int64, nslicesx ::Int64)
  62. horiz = starpu_data_filter(STARPU_MATRIX_FILTER_BLOCK, nslicesx)
  63. @starpu_block let
  64. hA = starpu_data_register(A)
  65. starpu_data_partition(hA,horiz)
  66. @starpu_sync_tasks for taskx in (1 : nslicesx)
  67. @starpu_async_cl mandelbrot(hA[taskx]) [STARPU_W] (cr, ci, Int64((taskx-1)*dim/nslicesx), dim)
  68. end
  69. end
  70. end
  71. function pixels2img(pixels ::Matrix{Int64}, width ::Int64, height ::Int64, filename ::String)
  72. MAPPING = [[66,30,15],[25,7,26],[9,1,47],[4,4,73],[0,7,100],[12,44,138],[24,82,177],[57,125,209],[134,181,229],[211,236,248],[241,233,191],[248,201,95],[255,170,0],[204,128,0],[153,87,0],[106,52,3]]
  73. open(filename, "w") do f
  74. write(f, "P3\n$width $height\n255\n")
  75. for i = 1:height
  76. for j = 1:width
  77. write(f,"$(MAPPING[1+pixels[i,j]][1]) $(MAPPING[1+pixels[i,j]][2]) $(MAPPING[1+pixels[i,j]][3]) ")
  78. end
  79. write(f, "\n")
  80. end
  81. end
  82. end
  83. function min_times(cr ::Float64, ci ::Float64, dim ::Int64, nslices ::Int64, gen_images)
  84. tmin=0;
  85. pixels ::Matrix{Int64} = zeros(dim, dim)
  86. for i = 1:10
  87. t = time_ns();
  88. mandelbrot_with_starpu(pixels, cr, ci, dim, nslices)
  89. t = time_ns()-t
  90. if (tmin==0 || tmin>t)
  91. tmin=t
  92. end
  93. end
  94. if (gen_images == 1)
  95. pixels2img(pixels,dim,dim,"out$(dim).ppm")
  96. end
  97. return tmin
  98. end
  99. function display_time(cr ::Float64, ci ::Float64, start_dim ::Int64, step_dim ::Int64, stop_dim ::Int64, nslices ::Int64, gen_images)
  100. for dim in (start_dim : step_dim : stop_dim)
  101. res = min_times(cr, ci, dim, nslices, gen_images)
  102. res=res/dim/dim; # time per pixel
  103. println("$(dim) $(res)")
  104. end
  105. end
  106. display_time(-0.800671,-0.158392,32,32,512,4, 0)
  107. starpu_shutdown()