starpu_py.py 4.4 KB

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