gemm.jl 3.9 KB

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