callback.jl 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 = CPU_CODELETS["variable"],
  35. # cuda_func = CUDA_CODELETS["matrix_mult"],
  36. #opencl_func="ocl_matrix_mult",
  37. modes = [STARPU_RW],
  38. perfmodel = perfmodel
  39. )
  40. @starpu_block let
  41. hVal = starpu_data_register(val)
  42. task = starpu_task(cl = cl, handles = [hVal], callback=callback, callback_arg=(cl, [hVal]))
  43. starpu_task_submit(task)
  44. starpu_task_wait_for_all()
  45. end
  46. end
  47. function display()
  48. v = Ref(Int32(40))
  49. variable_with_starpu(v)
  50. println("variable -> ", v[])
  51. if v[] == 42
  52. println("result is correct")
  53. else
  54. println("result is incorret")
  55. end
  56. end
  57. # Disable garbage collector because of random segfault/hang when using mutex.
  58. # This issue should be solved with Julia release 1.5.
  59. GC.enable(false)
  60. starpu_init()
  61. display()
  62. starpu_shutdown()
  63. GC.enable(true)