starpu_py.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 starpu
  17. import time
  18. import asyncio
  19. ############################################################################
  20. #function no input no output print hello world
  21. def hello():
  22. print ("Example 1:")
  23. print ("Hello, world!")
  24. #############################################################################
  25. #function no input no output
  26. def func1():
  27. print ("Example 2:")
  28. print ("This is a function no input no output")
  29. ##############################################################################
  30. #using decorator wrap the function no input no output
  31. @starpu.delayed
  32. def func1_deco():
  33. #time.sleep(1)
  34. print ("Example 3:")
  35. print ("This is a function no input no output wrapped by the decorator function")
  36. ##############################################################################
  37. #function no input return a value
  38. def func2():
  39. print ("Example 4:")
  40. return 12
  41. ###############################################################################
  42. #function has 2 int inputs and 1 int output
  43. def multi(a,b):
  44. print ("Example 5:")
  45. return a*b
  46. #print(multi(2, 3))
  47. ###############################################################################
  48. #function has 4 float inputs and 1 float output
  49. def add(a,b,c,d):
  50. print ("Example 6:")
  51. return a+b+c+d
  52. #print(add(1.2, 2.5, 3.6, 4.9))
  53. ###############################################################################
  54. #function has 2 int inputs 1 float input and 1 float output 1 int output
  55. def sub(a,b,c):
  56. print ("Example 7:")
  57. return a-b-c, a-b
  58. #print(sub(6, 2, 5.9))
  59. ###############################################################################
  60. #using decorator wrap the function with input
  61. @starpu.delayed(name="test")
  62. def add_deco(a,b,c):
  63. #time.sleep(1)
  64. print ("Example 8:")
  65. print ("This is a function with input and output wrapped by the decorator function:")
  66. return a+b+c
  67. ###############################################################################
  68. #using decorator wrap the function with input
  69. @starpu.delayed(color=1)
  70. def sub_deco(x,a):
  71. print ("Example 9:")
  72. print ("This is a function with input and output wrapped by the decorator function:")
  73. return x-a
  74. ###############################################################################
  75. async def main():
  76. #submit function "hello"
  77. fut = starpu.task_submit()(hello)
  78. await fut
  79. #submit function "func1"
  80. fut1 = starpu.task_submit()(func1)
  81. await fut1
  82. #apply starpu.delayed(func1_deco())
  83. await func1_deco()
  84. #submit function "func2"
  85. fut2 = starpu.task_submit()(func2)
  86. res2 = await fut2
  87. #print the result of function
  88. print("This is a function no input and the return value is", res2)
  89. #submit function "multi"
  90. fut3 = starpu.task_submit()(multi, 2, 3)
  91. res3 = await fut3
  92. print("The result of function multi is :", res3)
  93. #submit function "add"
  94. fut4 = starpu.task_submit()(add, 1.2, 2.5, 3.6, 4.9)
  95. res4 = await fut4
  96. print("The result of function add is :", res4)
  97. #submit function "sub"
  98. fut5 = starpu.task_submit()(sub, 6, 2, 5.9)
  99. res5 = await fut5
  100. print("The result of function sub is:", res5)
  101. #apply starpu.delayed(add_deco)
  102. fut6 = add_deco(1,2,3)
  103. res6 = await fut6
  104. print("The result of function is", res6)
  105. #apply starpu.delayed(sub_deco)
  106. fut7 = sub_deco(fut6, 1)
  107. res7 = await fut7
  108. print("The first argument of this function is the result of Example 8")
  109. print("The result of function is", res7)
  110. asyncio.run(main())
  111. #starpu.task_wait_for_all()