starpu_py_parallel.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. import numpy as np
  22. #generate a list to store functions
  23. g_func=[]
  24. #function no input no output print hello world
  25. def hello():
  26. print ("Example 1: Hello, world!")
  27. g_func.append(starpu.joblib.delayed(hello)())
  28. #function no input no output
  29. def func1():
  30. print ("Example 2: This is a function no input no output")
  31. g_func.append(starpu.joblib.delayed(func1)())
  32. #function no input return a value
  33. def func2():
  34. print ("Example 3:")
  35. return 12
  36. g_func.append(starpu.joblib.delayed(func2)())
  37. #function has 2 int inputs and 1 int output
  38. def multi(a,b):
  39. res_multi=a*b
  40. print("Example 4: The result of ",a,"*",b,"is",res_multi)
  41. return res_multi
  42. g_func.append(starpu.joblib.delayed(multi)(2, 3))
  43. #function has 4 float inputs and 1 float output
  44. def add(a,b,c,d):
  45. res_add=a+b+c+d
  46. print("Example 5: The result of ",a,"+",b,"+",c,"+",d,"is",res_add)
  47. return res_add
  48. g_func.append(starpu.joblib.delayed(add)(1.2, 2.5, 3.6, 4.9))
  49. #function has 2 int inputs 1 float input and 1 float output 1 int output
  50. def sub(a,b,c):
  51. res_sub1=a-b-c
  52. res_sub2=a-b
  53. print ("Example 6: The result of ",a,"-",b,"-",c,"is",res_sub1,"and the result of",a,"-",b,"is",res_sub2)
  54. return res_sub1, res_sub2
  55. g_func.append(starpu.joblib.delayed(sub)(6, 2, 5.9))
  56. #the size of generator
  57. N=100
  58. # a=np.array([1,2,3,4,5,6,7,8,9,10])
  59. # print(type(a))
  60. def scal(a, t):
  61. return t*a
  62. A=np.arange(N)
  63. #starpu.joblib.Parallel(mode="normal", n_jobs=2, perfmodel="log")(starpu.joblib.delayed(log10)(i+1)for i in range(N))
  64. # for x in [10, 100, 1000, 10000, 100000, 1000000]:
  65. # for X2 in range(x, x*10, x):
  66. # starpu.joblib.Parallel(mode="normal", n_jobs=2, perfmodel="log")(starpu.joblib.delayed(log10)(i+1)for i in range(X2))
  67. # print(range(X2))
  68. print("************************")
  69. print("parallel Normal version:")
  70. print("************************")
  71. print("--input is iterable argument list, example 1")
  72. starpu.joblib.Parallel(mode="normal", n_jobs=-2, perfmodel="first")(starpu.joblib.delayed(sqrt)(i**2)for i in range(N))
  73. #################scikit test###################
  74. DEFAULT_JOBLIB_BACKEND = starpu.joblib.get_active_backend()[0].__class__
  75. class MyBackend(DEFAULT_JOBLIB_BACKEND): # type: ignore
  76. def __init__(self, *args, **kwargs):
  77. self.count = 0
  78. super().__init__(*args, **kwargs)
  79. def start_call(self):
  80. self.count += 1
  81. return super().start_call()
  82. starpu.joblib.register_parallel_backend('testing', MyBackend)
  83. with starpu.joblib.parallel_backend("testing") as (ba, n_jobs):
  84. print("backend and n_jobs is", ba, n_jobs)
  85. ###############################################
  86. print("--input is iterable argument list, example 2, with multi input")
  87. a=np.arange(10)
  88. b=np.arange(10)
  89. starpu.joblib.Parallel(mode="normal", n_jobs=-2, perfmodel="first")(starpu.joblib.delayed(multi)(i,j) for i,j in zip(a,b))
  90. print("--input is iterable function list")
  91. starpu.joblib.Parallel(mode="normal", n_jobs=3, perfmodel="third")(g_func)
  92. print("--input is numpy array")
  93. starpu.joblib.Parallel(mode="normal", n_jobs=2, perfmodel="array")(starpu.joblib.delayed(scal)(2,A))
  94. print("************************")
  95. print("parallel Future version:")
  96. print("************************")
  97. async def main():
  98. print("--input is iterable argument list, example 1")
  99. fut1=starpu.joblib.Parallel(mode="future", n_jobs=-3, perfmodel="first")(starpu.joblib.delayed(sqrt)(i**2)for i in range(N))
  100. res1=await fut1
  101. #print(res1)
  102. print("--input is iterable argument list, example 2, with multi input")
  103. a=np.arange(10)
  104. b=np.arange(10)
  105. fut2=starpu.joblib.Parallel(mode="future", n_jobs=-3, perfmodel="second")(starpu.joblib.delayed(multi)(i,j) for i,j in zip(a,b))
  106. res2=await fut2
  107. #print(res2)
  108. print("--input is iterable function list")
  109. fut3=starpu.joblib.Parallel(mode="future", n_jobs=2)(g_func)
  110. res3=await fut3
  111. #print(res3)
  112. print("--input is numpy array")
  113. fut4=starpu.joblib.Parallel(mode="future", n_jobs=2, perfmodel="array")(starpu.joblib.delayed(scal)(2,A))
  114. res4=await fut4
  115. #print(res4)
  116. asyncio.run(main())
  117. starpu.perfmodel_plot(perfmodel="first",view=False)
  118. starpu.perfmodel_plot(perfmodel="second",view=False)
  119. starpu.perfmodel_plot(perfmodel="third",view=False)