mult_def.jl 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. using Base.LinAlg
  2. include("mult_naive.jl")
  3. # A of size (y,z)
  4. # B of size (z,x)
  5. # C of size (y,x)
  6. # |---------------|
  7. # z | B |
  8. # |---------------|
  9. # z x
  10. # |----| |---------------|
  11. # | | | |
  12. # | | | |
  13. # | A | y | C |
  14. # | | | |
  15. # | | | |
  16. # |----| |---------------|
  17. #
  18. function multiply_with_starpu(A :: Matrix{Float32}, B :: Matrix{Float32}, C :: Matrix{Float32}, nslicesx, nslicesy)
  19. vert = StarpuDataFilter(STARPU_MATRIX_FILTER_VERTICAL_BLOCK, nslicesx)
  20. horiz = StarpuDataFilter(STARPU_MATRIX_FILTER_BLOCK, nslicesy)
  21. @starpu_block let
  22. hA,hB,hC = starpu_data_register(A, B, C)
  23. starpu_data_partition(hB, vert)
  24. starpu_data_partition(hA, horiz)
  25. starpu_data_map_filters(hC, vert, horiz)
  26. @starpu_sync_tasks for taskx in (1 : nslicesx)
  27. for tasky in (1 : nslicesy)
  28. @starpu_async_cl cl(hA[tasky], hB[taskx], hC[taskx, tasky])
  29. end
  30. end
  31. end
  32. return nothing
  33. end
  34. function multiply_with_starpu_cpu(A :: Matrix{Float32}, B :: Matrix{Float32}, C :: Matrix{Float32}, nslicesx, nslicesy)
  35. vert = StarpuDataFilter(STARPU_MATRIX_FILTER_VERTICAL_BLOCK, nslicesx)
  36. horiz = StarpuDataFilter(STARPU_MATRIX_FILTER_BLOCK, nslicesy)
  37. @starpu_block let
  38. hA,hB,hC = starpu_data_register(A, B, C)
  39. starpu_data_partition(hB, vert)
  40. starpu_data_partition(hA, horiz)
  41. starpu_data_map_filters(hC, vert, horiz)
  42. @starpu_sync_tasks for taskx in (1 : nslicesx)
  43. for tasky in (1 : nslicesy)
  44. @starpu_async_cl clcpu(hA[tasky], hB[taskx], hC[taskx, tasky])
  45. end
  46. end
  47. end
  48. return nothing
  49. end
  50. function multiply_with_starpu_gpu(A :: Matrix{Float32}, B :: Matrix{Float32}, C :: Matrix{Float32}, nslicesx, nslicesy)
  51. vert = StarpuDataFilter(STARPU_MATRIX_FILTER_VERTICAL_BLOCK, nslicesx)
  52. horiz = StarpuDataFilter(STARPU_MATRIX_FILTER_BLOCK, nslicesy)
  53. @starpu_block let
  54. hA,hB,hC = starpu_data_register(A, B, C)
  55. starpu_data_partition(hB, vert)
  56. starpu_data_partition(hA, horiz)
  57. starpu_data_map_filters(hC, vert, horiz)
  58. @starpu_sync_tasks for taskx in (1 : nslicesx)
  59. for tasky in (1 : nslicesy)
  60. @starpu_async_cl clgpu(hA[tasky], hB[taskx], hC[taskx, tasky])
  61. end
  62. end
  63. end
  64. return nothing
  65. end
  66. function approximately_equals(
  67. A :: Matrix{Cfloat},
  68. B :: Matrix{Cfloat},
  69. eps = 1e-2
  70. )
  71. (height, width) = size(A)
  72. for j in (1 : width)
  73. for i in (1 : height)
  74. if (abs(A[i,j] - B[i,j]) > eps * max(abs(B[i,j]), abs(A[i,j])))
  75. println("A[$i,$j] : $(A[i,j]), B[$i,$j] : $(B[i,j])")
  76. return false
  77. end
  78. end
  79. end
  80. return true
  81. end
  82. function median_times(nb_tests, xdim, zdim, ydim, nslicesx, nslicesy)
  83. exec_times_st ::Vector{Float64} = [0 for i = 1:nb_tests]
  84. exec_times_cpu ::Vector{Float64} = [0 for i = 1:nb_tests]
  85. exec_times_gpu ::Vector{Float64} = [0 for i = 1:nb_tests]
  86. exec_times_jl ::Vector{Float64} = [0 for i = 1:nb_tests]
  87. A = Array(rand(Cfloat, ydim, zdim))
  88. B = Array(rand(Cfloat, zdim, xdim))
  89. C = zeros(Float32, ydim, xdim)
  90. D = A * B
  91. for i in (1 : nb_tests)
  92. # tic()
  93. # multiply_with_starpu(A, B, C, nslicesx, nslicesy)
  94. # t = toq()
  95. # if (!approximately_equals(D, C))
  96. # error("Invalid st result")
  97. # end
  98. # exec_times_st[i] = t
  99. # tic()
  100. # multiply_with_starpu_cpu(A, B, C, nslicesx, nslicesy)
  101. # tcpu = toq()
  102. # if (!approximately_equals(D, C))
  103. # error("Invalid cpu result")
  104. # end
  105. # exec_times_cpu[i] = tcpu
  106. # tic()
  107. # multiply_with_starpu_gpu(A, B, C, nslicesx, nslicesy)
  108. # tgpu = toq()
  109. # if (!approximately_equals(D, C))
  110. # error("Invalid gpu result")
  111. # end
  112. # exec_times_gpu[i] = tgpu
  113. al ::Float32 = 1.0
  114. be ::Float32 = 0.0
  115. tic()
  116. # multjl(A, B, C)
  117. BLAS.gemm!('N','N', al, A, B, be, C)
  118. # C = BLAS.gemm!('N', 'N', 1.0, A, B)
  119. tjl = toq()
  120. if (!approximately_equals(D, C))
  121. error("Invalid jl result")
  122. end
  123. exec_times_jl[i] = tjl
  124. end
  125. # sort!(exec_times_st)
  126. # sort!(exec_times_cpu)
  127. # sort!(exec_times_gpu)
  128. sort!(exec_times_jl)
  129. results ::Vector{Float64} = [exec_times_jl[1 + div(nb_tests-1, 2)]]#, exec_times_cpu[1 + div(nb_tests-1, 2)], exec_times_gpu[1 + div(nb_tests-1, 2)], exec_times_jl[1 + div(nb_tests-1, 2)]]
  130. return results
  131. end
  132. function display_times(start_dim, step_dim, stop_dim, nb_tests, nslicesx, nslicesy)
  133. # mtc = map( (x->parse(Float64,x)), open("DAT/mult_c.dat") do f
  134. # readlines(f)
  135. # end)
  136. # mtext = map( (x->parse(Float64,x)), open("DAT/mult_ext.dat") do f
  137. # readlines(f)
  138. # end)
  139. # mtjl = map( (x->parse(Float64,x)), open("DAT/mult_jl.dat") do f
  140. # readlines(f)
  141. # end)
  142. # open("../DAT/mult_ext.dat", "w") do f
  143. # open("../DAT/mult_jl.dat", "w") do f
  144. open("../DAT/mult_jl_times.dat", "w") do ft
  145. # open("DAT/mult.dat", "w") do f
  146. # i = 1
  147. for dim in (start_dim : step_dim : stop_dim)
  148. println("Dimension: $dim")
  149. # println("C: $(mtc[i])")
  150. res ::Vector{Float64} = median_times(nb_tests, dim, dim, dim, nslicesx, nslicesy)
  151. println("jl: $(res[1])")
  152. # println("jlcpu: $(res[2])")
  153. # println("jlgpu: $(res[3])")
  154. # println("jl: $(res[4])")
  155. # write(f, "$(dim) $(res[4]/res[1]) $(res[4]/res[2]) $(res[4]/res[3]) $(res[4]/mtc[i])\n")
  156. # write(f, "$dim $(mtjl[i]/res[1]) $(mtjl[i]/mtext[i]) $(mtjl[i]/mtc[i])\n")
  157. # write(ft, "$(res[1]) $(mtc[i]) $(mtext[i]) $(mtjl[i])\n")
  158. write(ft, "$(res[1])\n")
  159. # i = i + 1
  160. # end
  161. end
  162. end
  163. end