mandelbrot.jl 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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_task_insert(codelet_name = "mandelbrot",
  68. handles = [hA[taskx]],
  69. modes = [STARPU_W],
  70. cl_arg = (cr, ci, Int64((taskx-1)*dim/nslicesx), dim))
  71. end
  72. end
  73. end
  74. function pixels2img(pixels ::Matrix{Int64}, width ::Int64, height ::Int64, filename ::String)
  75. 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]]
  76. open(filename, "w") do f
  77. write(f, "P3\n$width $height\n255\n")
  78. for i = 1:height
  79. for j = 1:width
  80. write(f,"$(MAPPING[1+pixels[i,j]][1]) $(MAPPING[1+pixels[i,j]][2]) $(MAPPING[1+pixels[i,j]][3]) ")
  81. end
  82. write(f, "\n")
  83. end
  84. end
  85. end
  86. function min_times(cr ::Float64, ci ::Float64, dim ::Int64, nslices ::Int64, gen_images)
  87. tmin=0;
  88. pixels ::Matrix{Int64} = zeros(dim, dim)
  89. for i = 1:10
  90. t = time_ns();
  91. mandelbrot_with_starpu(pixels, cr, ci, dim, nslices)
  92. t = time_ns()-t
  93. if (tmin==0 || tmin>t)
  94. tmin=t
  95. end
  96. end
  97. if (gen_images == 1)
  98. pixels2img(pixels,dim,dim,"out$(dim).ppm")
  99. end
  100. return tmin
  101. end
  102. function display_time(cr ::Float64, ci ::Float64, start_dim ::Int64, step_dim ::Int64, stop_dim ::Int64, nslices ::Int64, gen_images)
  103. for dim in (start_dim : step_dim : stop_dim)
  104. res = min_times(cr, ci, dim, nslices, gen_images)
  105. res=res/dim/dim; # time per pixel
  106. println("$(dim) $(res)")
  107. end
  108. end
  109. display_time(-0.800671,-0.158392,32,32,512,4, 0)
  110. starpu_shutdown()