callback.jl 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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
  18. @codelet function variable(val ::Ref{Int32}) :: Nothing
  19. val[] = val[] + 1
  20. return
  21. end
  22. function callback(args)
  23. cl = args[1]
  24. handles = args[2]
  25. task = starpu_task(cl = cl, handles=handles)
  26. starpu_task_submit(task)
  27. end
  28. function variable_with_starpu(val ::Ref{Int32})
  29. perfmodel = starpu_perfmodel(
  30. perf_type = starpu_perfmodel_type(STARPU_HISTORY_BASED),
  31. symbol = "history_perf"
  32. )
  33. cl = starpu_codelet(
  34. cpu_func = "variable",
  35. modes = [STARPU_RW],
  36. perfmodel = perfmodel
  37. )
  38. @starpu_block let
  39. hVal = starpu_data_register(val)
  40. starpu_task_insert(codelet_name = "variable",
  41. cl = cl,
  42. handles = [hVal],
  43. callback = callback,
  44. callback_arg = (cl, [hVal]))
  45. starpu_task_wait_for_all()
  46. end
  47. end
  48. function display()
  49. v = Ref(Int32(40))
  50. variable_with_starpu(v)
  51. println("variable -> ", v[])
  52. if v[] == 42
  53. println("result is correct")
  54. else
  55. error("result is incorret")
  56. end
  57. end
  58. # Disable garbage collector because of random segfault/hang when using mutex.
  59. # This issue should be solved with Julia release 1.5.
  60. GC.enable(false)
  61. starpu_init()
  62. display()
  63. starpu_shutdown()
  64. GC.enable(true)