gemm.jl 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 StarPU
  17. using LinearAlgebra.BLAS
  18. @target STARPU_CPU+STARPU_CUDA
  19. @codelet function gemm(A :: Matrix{Float32}, B :: Matrix{Float32}, C :: Matrix{Float32}, alpha :: Float32, beta :: Float32) :: Nothing
  20. M :: Int32 = height(A)
  21. N :: Int32 = width(B)
  22. K :: Int32 = width(A)
  23. lda :: Int32 = ld(A)
  24. ldb :: Int32 = ld(B)
  25. ldc :: Int32 = ld(C)
  26. STARPU_SGEMM("N", "N", M, N, K, alpha, A, lda, B, ldb, beta, C, ldc)
  27. return
  28. end
  29. function multiply_with_starpu(A :: Matrix{Float32}, B :: Matrix{Float32}, C :: Matrix{Float32}, alpha :: Float32, beta :: Float32, nslicesx, nslicesy)
  30. scale= 3
  31. tmin=0
  32. vert = starpu_data_filter(STARPU_MATRIX_FILTER_VERTICAL_BLOCK, nslicesx)
  33. horiz = starpu_data_filter(STARPU_MATRIX_FILTER_BLOCK, nslicesy)
  34. @starpu_block let
  35. hA,hB,hC = starpu_data_register(A, B, C)
  36. starpu_data_partition(hB, vert)
  37. starpu_data_partition(hA, horiz)
  38. starpu_data_map_filters(hC, vert, horiz)
  39. tmin=0
  40. for i in (1 : 10 )
  41. t=time_ns()
  42. @starpu_sync_tasks begin
  43. for taskx in (1 : nslicesx)
  44. for tasky in (1 : nslicesy)
  45. starpu_task_insert(codelet_name = "gemm",
  46. handles = [hA[tasky], hB[taskx], hC[taskx, tasky]],
  47. cl_arg = (alpha, beta),
  48. modes = [STARPU_R, STARPU_R, STARPU_RW])
  49. end
  50. end
  51. end
  52. t=time_ns()-t
  53. if (tmin==0 || tmin>t)
  54. tmin=t
  55. end
  56. end
  57. end
  58. return tmin
  59. end
  60. function approximately_equals(
  61. A :: Matrix{Cfloat},
  62. B :: Matrix{Cfloat},
  63. eps = 1e-2
  64. )
  65. (height, width) = size(A)
  66. for j in (1 : width)
  67. for i in (1 : height)
  68. if (abs(A[i,j] - B[i,j]) > eps * max(abs(B[i,j]), abs(A[i,j])))
  69. println("A[$i,$j] : $(A[i,j]), B[$i,$j] : $(B[i,j])")
  70. return false
  71. end
  72. end
  73. end
  74. return true
  75. end
  76. function check(expected, A, B, C, alpha, beta)
  77. for i in 1 : 10
  78. gemm!('N', 'N', alpha, A, B, beta, expected)
  79. end
  80. height,width = size(C)
  81. for i in 1:height
  82. for j in 1:width
  83. got = C[i, j]
  84. exp = expected[i, j]
  85. err = abs(exp - got) / exp
  86. if err > 0.0001
  87. error("[$i] -> $got != $exp (err $err)")
  88. end
  89. end
  90. end
  91. end
  92. function compute_times(io,start_dim, step_dim, stop_dim, nslicesx, nslicesy)
  93. for dim in (start_dim : step_dim : stop_dim)
  94. A = Array(rand(Cfloat, dim, dim))
  95. B = Array(rand(Cfloat, dim, dim))
  96. C = zeros(Float32, dim, dim)
  97. C_ref = copy(C)
  98. starpu_memory_pin(A)
  99. starpu_memory_pin(B)
  100. starpu_memory_pin(C)
  101. alpha = 4.0f0
  102. beta = 2.0f0
  103. mt = multiply_with_starpu(A, B, C, alpha, beta, nslicesx, nslicesy)
  104. gflop = 2 * dim * dim * dim * 1.e-9
  105. gflops = gflop / (mt * 1.e-9)
  106. size=dim*dim*dim*4*3/1024/1024
  107. println(io,"$dim $gflops")
  108. println("$dim $gflops")
  109. starpu_memory_unpin(A)
  110. starpu_memory_unpin(B)
  111. starpu_memory_unpin(C)
  112. check(C_ref, A, B, C, alpha, beta)
  113. end
  114. end
  115. if size(ARGS, 1) < 1
  116. filename="x.dat"
  117. else
  118. filename=ARGS[1]
  119. end
  120. starpu_init()
  121. starpu_cublas_init()
  122. nblock_x = Int32(ceil(sqrt(starpu_worker_get_count())))
  123. nblock_y = nblock_x
  124. io=open(filename,"w")
  125. compute_times(io,64,512,4096,nblock_x,nblock_y)
  126. close(io)
  127. starpu_shutdown()