vector_scal.jl 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. import Libdl
  17. using StarPU
  18. using LinearAlgebra
  19. @target STARPU_CPU+STARPU_CUDA
  20. @codelet function vector_scal(m::Int32, v :: Vector{Float32}, k :: Float32, l :: Float32) :: Float32
  21. N :: Int32 = length(v)
  22. # Naive version
  23. @parallel for i in (1 : N)
  24. v[i] = v[i] * m + l + k
  25. end
  26. end
  27. starpu_init()
  28. function vector_scal_with_starpu(v :: Vector{Float32}, m :: Int32, k :: Float32, l :: Float32)
  29. tmin=0
  30. @starpu_block let
  31. hV = starpu_data_register(v)
  32. tmin=0
  33. for i in (1 : 1)
  34. t=time_ns()
  35. @starpu_sync_tasks begin
  36. starpu_task_insert(codelet_name = "vector_scal",
  37. modes = [STARPU_RW],
  38. handles = [hV],
  39. cl_arg=(m, k, l))
  40. end
  41. t=time_ns()-t
  42. if (tmin==0 || tmin>t)
  43. tmin=t
  44. end
  45. end
  46. end
  47. return tmin
  48. end
  49. function check(ref, res, m, k, l)
  50. expected = ref .* m .+ (k+l)
  51. for i in 1:length(expected)
  52. got = res[i]
  53. exp = expected[i]
  54. err = abs(exp - got) / exp
  55. if err > 0.0001
  56. error("[$i] -> $got != $exp (err $err)")
  57. end
  58. end
  59. end
  60. function compute_times(io,start_dim, step_dim, stop_dim)
  61. for size in (start_dim : step_dim : stop_dim)
  62. V = Array(rand(Cfloat, size))
  63. V_ref = copy(V)
  64. starpu_memory_pin(V)
  65. m :: Int32 = 10
  66. k :: Float32 = 2.
  67. l :: Float32 = 3.
  68. println("INPUT ", V[1:10])
  69. mt = vector_scal_with_starpu(V, m, k, l)
  70. starpu_memory_unpin(V)
  71. println("OUTPUT ", V[1:10])
  72. println(io,"$size $mt")
  73. println("$size $mt")
  74. check(V_ref, V, m, k, l)
  75. end
  76. end
  77. if size(ARGS, 1) < 1
  78. filename="x.dat"
  79. else
  80. filename=ARGS[1]
  81. end
  82. io=open(filename,"w")
  83. compute_times(io,1024,1024,4096)
  84. close(io)
  85. starpu_shutdown()