starpu_py_parallel.py 3.4 KB

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