starpu_py.py 4.4 KB

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