mult_def.jl 6.8 KB

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