starpu_py.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. return t*a
  78. t=np.array([1,2,3,4,5,6,7,8,9,10])
  79. async def main():
  80. #submit function "hello"
  81. fut = starpu.task_submit()(hello)
  82. await fut
  83. #submit function "func1"
  84. fut1 = starpu.task_submit()(func1)
  85. await fut1
  86. #apply starpu.delayed(func1_deco())
  87. await func1_deco()
  88. #submit function "func2"
  89. fut2 = starpu.task_submit()(func2)
  90. res2 = await fut2
  91. #print the result of function
  92. print("This is a function no input and the return value is", res2)
  93. #submit function "multi"
  94. fut3 = starpu.task_submit()(multi, 2, 3)
  95. res3 = await fut3
  96. print("The result of function multi is :", res3)
  97. #submit function "add"
  98. fut4 = starpu.task_submit()(add, 1.2, 2.5, 3.6, 4.9)
  99. res4 = await fut4
  100. print("The result of function add is :", res4)
  101. #submit function "sub"
  102. fut5 = starpu.task_submit()(sub, 6, 2, 5.9)
  103. res5 = await fut5
  104. print("The result of function sub is:", res5)
  105. #apply starpu.delayed(add_deco)
  106. fut6 = add_deco(1,2,3)
  107. res6 = await fut6
  108. print("The result of function is", res6)
  109. #apply starpu.delayed(sub_deco)
  110. fut7 = sub_deco(fut6, 1)
  111. res7 = await fut7
  112. print("The first argument of this function is the result of Example 8")
  113. print("The result of function is", res7)
  114. fut8 = starpu.task_submit()(scal, 2, t)
  115. res8 = await fut8
  116. print("The result of Example 10 is", res8)
  117. asyncio.run(main())
  118. #starpu.task_wait_for_all()