mandelbrot_native.jl 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using LinearAlgebra
  2. function mandelbrot(pixels, params) :: Float32
  3. height :: Int64, width :: Int64 = size(pixels)
  4. zoom :: Float64 = width * 0.25296875
  5. iz :: Float64 = 1. / zoom
  6. diverge :: Float32 = 4.0
  7. max_iterations :: Float32 = ((width/2) * 0.049715909 * log10(zoom));
  8. imi :: Float32 = 1. / max_iterations
  9. centerr :: Float32 = params[1]
  10. centeri :: Float32 = params[2]
  11. offset :: Float32 = params[3]
  12. dim :: Float32 = params[4]
  13. cr :: Float64 = 0.
  14. zr :: Float64 = 0.
  15. ci :: Float64 = 0.
  16. zi :: Float64 = 0.
  17. n :: Int64 = 0
  18. tmp :: Float64 = 0.
  19. for y = 1:height
  20. for x = 1:width
  21. cr = centerr + (x-1 - (dim / 2)) * iz
  22. zr = cr
  23. ci = centeri + (y-1+offset - (dim / 2)) * iz
  24. zi = ci
  25. for n = 0:max_iterations
  26. if (zr*zr + zi*zi > diverge)
  27. break
  28. end
  29. tmp = zr*zr - zi*zi + cr
  30. zi = 2*zr*zi + ci
  31. zr = tmp
  32. end
  33. if (n < max_iterations)
  34. pixels[y,x] = round(15 * n * imi)
  35. else
  36. pixels[y,x] = 0
  37. end
  38. end
  39. end
  40. ret :: Float32 = 0.
  41. return ret
  42. end
  43. function mandelbrot_without_starpu(A ::Matrix{Int64}, params ::Matrix{Float32}, nslicesx ::Int64)
  44. width,height = size(A)
  45. step = height / nslicesx
  46. for taskx in (1 : nslicesx)
  47. start_id = floor(Int64, (taskx-1)*step+1)
  48. end_id = floor(Int64, (taskx-1)*step+step)
  49. a = view(A, start_id:end_id, :)
  50. p = view(params, (taskx-1)*4+1:(taskx-1)*4+4)
  51. mandelbrot(a, p)
  52. end
  53. end
  54. function pixels2img(pixels ::Matrix{Int64}, width ::Int64, height ::Int64, filename ::String)
  55. 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]]
  56. open(filename, "w") do f
  57. write(f, "P3\n$width $height\n255\n")
  58. for i = 1:height
  59. for j = 1:width
  60. write(f,"$(MAPPING[1+pixels[i,j]][1]) $(MAPPING[1+pixels[i,j]][2]) $(MAPPING[1+pixels[i,j]][3]) ")
  61. end
  62. write(f, "\n")
  63. end
  64. end
  65. end
  66. function min_times(cr ::Float64, ci ::Float64, dim ::Int64, nslices ::Int64)
  67. tmin=0;
  68. pixels ::Matrix{Int64} = zeros(dim, dim)
  69. params :: Matrix{Float32} = zeros(4*nslices,1)
  70. for i=0:(nslices-1)
  71. params[4*i+1,1] = cr
  72. params[4*i+2,1] = ci
  73. params[4*i+3,1] = i*dim/nslices
  74. params[4*i+4,1] = dim
  75. end
  76. for i = 1:10
  77. t = time_ns();
  78. mandelbrot_without_starpu(pixels, params, nslices)
  79. t = time_ns()-t
  80. if (tmin==0 || tmin>t)
  81. tmin=t
  82. end
  83. end
  84. pixels2img(pixels,dim,dim,"out$(dim).ppm")
  85. return tmin
  86. end
  87. function display_time(cr ::Float64, ci ::Float64, start_dim ::Int64, step_dim ::Int64, stop_dim ::Int64, nslices ::Int64)
  88. for dim in (start_dim : step_dim : stop_dim)
  89. res = min_times(cr, ci, dim, nslices)
  90. res=res/dim/dim; # time per pixel
  91. println("$(dim) $(res)")
  92. end
  93. end
  94. display_time(-0.800671,-0.158392,32,32,4096,4)