mult_def.jl 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # A of size (y,z)
  2. # B of size (z,x)
  3. # C of size (y,x)
  4. # |---------------|
  5. # z | B |
  6. # |---------------|
  7. # z x
  8. # |----| |---------------|
  9. # | | | |
  10. # | | | |
  11. # | A | y | C |
  12. # | | | |
  13. # | | | |
  14. # |----| |---------------|
  15. #
  16. function multiply_with_starpu(A :: Matrix{Float32}, B :: Matrix{Float32}, C :: Matrix{Float32}, nslicesx, nslicesy)
  17. vert = StarpuDataFilter(STARPU_MATRIX_FILTER_VERTICAL_BLOCK, nslicesx)
  18. horiz = StarpuDataFilter(STARPU_MATRIX_FILTER_BLOCK, nslicesy)
  19. @starpu_block let
  20. hA,hB,hC = starpu_data_register(A, B, C)
  21. starpu_data_partition(hB, vert)
  22. starpu_data_partition(hA, horiz)
  23. starpu_data_map_filters(hC, vert, horiz)
  24. @starpu_sync_tasks for taskx in (1 : nslicesx)
  25. for tasky in (1 : nslicesy)
  26. @starpu_async_cl cl(hA[tasky], hB[taskx], hC[taskx, tasky])
  27. end
  28. end
  29. end
  30. return nothing
  31. end
  32. function approximately_equals(
  33. A :: Matrix{Cfloat},
  34. B :: Matrix{Cfloat},
  35. eps = 1e-2
  36. )
  37. (height, width) = size(A)
  38. for j in (1 : width)
  39. for i in (1 : height)
  40. if (abs(A[i,j] - B[i,j]) > eps * max(abs(B[i,j]), abs(A[i,j])))
  41. println("A[$i,$j] : $(A[i,j]), B[$i,$j] : $(B[i,j])")
  42. return false
  43. end
  44. end
  45. end
  46. return true
  47. end
  48. function median_time(nb_tests, xdim, zdim, ydim, nslicesx, nslicesy)
  49. exec_times = Float64[]
  50. for i in (1 : nb_tests)
  51. A = Array(rand(Cfloat, ydim, zdim))
  52. B = Array(rand(Cfloat, zdim, xdim))
  53. C = zeros(Float32, ydim, xdim)
  54. D = A * B
  55. tic()
  56. multiply_with_starpu(A, B, C, nslicesx, nslicesy)
  57. t = toq()
  58. if (!approximately_equals(D, C))
  59. error("Invalid result")
  60. end
  61. push!(exec_times, t)
  62. end
  63. sort!(exec_times)
  64. return exec_times[1 + div(nb_tests-1, 2)]
  65. end
  66. function display_times(start_dim, step_dim, stop_dim, nb_tests, nslicesx, nslicesy)
  67. for dim in (start_dim : step_dim : stop_dim)
  68. mt = median_time(nb_tests, dim, dim, dim, nslicesx, nslicesy)
  69. println("$dim ; $mt")
  70. end
  71. end