mandelbrot.jl 3.4 KB

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