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