starpu_py_parallel.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. from math import sqrt
  20. #generate a list to store functions
  21. g_func=[]
  22. #function no input no output print hello world
  23. def hello():
  24. print ("Example 1: Hello, world!")
  25. g_func.append(starpu.joblib.delayed(hello)())
  26. #function no input no output
  27. def func1():
  28. print ("Example 2: This is a function no input no output")
  29. g_func.append(starpu.joblib.delayed(func1)())
  30. #function no input return a value
  31. def func2():
  32. print ("Example 3:")
  33. return 12
  34. g_func.append(starpu.joblib.delayed(func2)())
  35. #function has 2 int inputs and 1 int output
  36. def multi(a,b):
  37. res_multi=a*b
  38. print("Example 4: The result of ",a,"*",b,"is",res_multi)
  39. return res_multi
  40. g_func.append(starpu.joblib.delayed(multi)(2, 3))
  41. #function has 4 float inputs and 1 float output
  42. def add(a,b,c,d):
  43. res_add=a+b+c+d
  44. print("Example 5: The result of ",a,"+",b,"+",c,"+",d,"is",res_add)
  45. return res_add
  46. g_func.append(starpu.joblib.delayed(add)(1.2, 2.5, 3.6, 4.9))
  47. #function has 2 int inputs 1 float input and 1 float output 1 int output
  48. def sub(a,b,c):
  49. res_sub1=a-b-c
  50. res_sub2=a-b
  51. print ("Example 6: The result of ",a,"-",b,"-",c,"is",res_sub1,"and the result of",a,"-",b,"is",res_sub2)
  52. return res_sub1, res_sub2
  53. g_func.append(starpu.joblib.delayed(sub)(6, 2, 5.9))
  54. print("************************")
  55. print("parallel Normal version:")
  56. print("************************")
  57. print("--input is iterable argument list")
  58. starpu.joblib.parallel(mode="normal", n_jobs=-2)(starpu.joblib.delayed(sqrt)(i**2)for i in range(10))
  59. print("--input is iterable function list")
  60. starpu.joblib.parallel(mode="normal", n_jobs=3)(g_func)
  61. print("************************")
  62. print("parallel Future version:")
  63. print("************************")
  64. async def main():
  65. print("--input is iterable argument list")
  66. L_fut1=starpu.joblib.parallel(mode="future", n_jobs=-3)(starpu.joblib.delayed(sqrt)(i**2)for i in range(10))
  67. res1=[]
  68. for i in range(len(L_fut1)):
  69. L_res1=await L_fut1[i]
  70. res1.extend(L_res1)
  71. print(res1)
  72. print("--input is iterable function list")
  73. L_fut2=starpu.joblib.parallel(mode="future", n_jobs=2)(g_func)
  74. res2=[]
  75. for i in range(len(L_fut2)):
  76. L_res2=await L_fut2[i]
  77. res2.extend(L_res2)
  78. print(res2)
  79. asyncio.run(main())