starpu_py_parallel.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 starpu.joblib
  18. import time
  19. import asyncio
  20. from math import sqrt
  21. from math import log10
  22. import numpy as np
  23. #generate a list to store functions
  24. g_func=[]
  25. #function no input no output print hello world
  26. def hello():
  27. print ("Example 1: Hello, world!")
  28. g_func.append(starpu.joblib.delayed(hello)())
  29. #function no input no output
  30. def func1():
  31. print ("Example 2: This is a function no input no output")
  32. g_func.append(starpu.joblib.delayed(func1)())
  33. #function no input return a value
  34. def func2():
  35. print ("Example 3:")
  36. return 12
  37. g_func.append(starpu.joblib.delayed(func2)())
  38. #function has 2 int inputs and 1 int output
  39. def exp(a,b):
  40. res_exp=a**b
  41. print("Example 4: The result of ",a,"^",b,"is",res_exp)
  42. return res_exp
  43. g_func.append(starpu.joblib.delayed(exp)(2, 3))
  44. #function has 4 float inputs and 1 float output
  45. def add(a,b,c,d):
  46. res_add=a+b+c+d
  47. print("Example 5: The result of ",a,"+",b,"+",c,"+",d,"is",res_add)
  48. return res_add
  49. g_func.append(starpu.joblib.delayed(add)(1.2, 2.5, 3.6, 4.9))
  50. #function has 2 int inputs 1 float input and 1 float output 1 int output
  51. def sub(a,b,c):
  52. res_sub1=a-b-c
  53. res_sub2=a-b
  54. print ("Example 6: The result of ",a,"-",b,"-",c,"is",res_sub1,"and the result of",a,"-",b,"is",res_sub2)
  55. return res_sub1, res_sub2
  56. g_func.append(starpu.joblib.delayed(sub)(6, 2, 5.9))
  57. ##########functions of array calculation###############
  58. def scal(a, t):
  59. for i in range(len(t)):
  60. t[i]=t[i]*a
  61. return t
  62. def add_scal(a, t1, t2):
  63. for i in range(len(t1)):
  64. t1[i]=t1[i]*a+t2[i]
  65. return t1
  66. def scal_arr(a, t):
  67. for i in range(len(t)):
  68. t[i]=t[i]*a[i]
  69. return t
  70. def multi(a,b):
  71. res_multi=a*b
  72. return res_multi
  73. def multi_2arr(a, b):
  74. for i in range(len(a)):
  75. a[i]=a[i]*b[i]
  76. return a
  77. def multi_list(l):
  78. res = []
  79. for (a,b) in l:
  80. res.append(a*b)
  81. return res
  82. def log10_arr(t):
  83. for i in range(len(t)):
  84. t[i]=log10(t[i])
  85. return t
  86. ########################################################
  87. #################scikit test###################
  88. DEFAULT_JOBLIB_BACKEND = starpu.joblib.get_active_backend()[0].__class__
  89. class MyBackend(DEFAULT_JOBLIB_BACKEND): # type: ignore
  90. def __init__(self, *args, **kwargs):
  91. self.count = 0
  92. super().__init__(*args, **kwargs)
  93. def start_call(self):
  94. self.count += 1
  95. return super().start_call()
  96. starpu.joblib.register_parallel_backend('testing', MyBackend)
  97. with starpu.joblib.parallel_backend("testing") as (ba, n_jobs):
  98. print("backend and n_jobs is", ba, n_jobs)
  99. ###############################################
  100. N=100
  101. # A=np.arange(N)
  102. # B=np.arange(N)
  103. # a=np.arange(N)
  104. # b=np.arange(N, 2*N, 1)
  105. for x in [10, 100, 1000, 10000, 100000, 1000000, 10000000]:
  106. for X in range(x, x*10, x):
  107. print("X=",X)
  108. starpu.joblib.Parallel(mode="normal", n_jobs=-1, perfmodel="log_list")(starpu.joblib.delayed(log10)(i+1)for i in range(X))
  109. A=np.arange(1,X+1,1)
  110. starpu.joblib.Parallel(mode="normal", n_jobs=-1, perfmodel="log_arr")(starpu.joblib.delayed(log10_arr)(A))
  111. print("************************")
  112. print("parallel Normal version:")
  113. print("************************")
  114. print("--(sqrt)(i**2)for i in range(N)")
  115. start_exec1=time.time()
  116. start_cpu1=time.process_time()
  117. starpu.joblib.Parallel(mode="normal", n_jobs=-1, perfmodel="sqrt")(starpu.joblib.delayed(sqrt)(i**2)for i in range(N))
  118. end_exec1=time.time()
  119. end_cpu1=time.process_time()
  120. print("the program execution time is", end_exec1-start_exec1)
  121. print("the cpu execution time is", end_cpu1-start_cpu1)
  122. print("--(multi)(i,j) for i,j in zip(a,b)")
  123. a=np.arange(N)
  124. b=np.arange(N, 2*N, 1)
  125. start_exec2=time.time()
  126. start_cpu2=time.process_time()
  127. starpu.joblib.Parallel(mode="normal", n_jobs=-1, perfmodel="multi")(starpu.joblib.delayed(multi)(i,j) for i,j in zip(a,b))
  128. end_exec2=time.time()
  129. end_cpu2=time.process_time()
  130. print("the program execution time is", end_exec2-start_exec2)
  131. print("the cpu execution time is", end_cpu2-start_cpu2)
  132. print("--(scal_arr)((i for i in b), A)")
  133. A=np.arange(N)
  134. b=np.arange(N, 2*N, 1)
  135. start_exec3=time.time()
  136. start_cpu3=time.process_time()
  137. starpu.joblib.Parallel(mode="normal", n_jobs=-1, perfmodel="scal_arr")(starpu.joblib.delayed(scal_arr)((i for i in b), A))
  138. end_exec3=time.time()
  139. end_cpu3=time.process_time()
  140. print("the program execution time is", end_exec3-start_exec3)
  141. print("the cpu execution time is", end_cpu3-start_cpu3)
  142. print("--(multi_list)((i,j) for i,j in zip(a,b))")
  143. a=np.arange(N)
  144. b=np.arange(N, 2*N, 1)
  145. start_exec4=time.time()
  146. start_cpu4=time.process_time()
  147. starpu.joblib.Parallel(mode="normal", n_jobs=-1, perfmodel="multi_list")(starpu.joblib.delayed(multi_list)((i,j) for i,j in zip(a,b)))
  148. end_exec4=time.time()
  149. end_cpu4=time.process_time()
  150. print("the program execution time is", end_exec4-start_exec4)
  151. print("the cpu execution time is", end_cpu4-start_cpu4)
  152. print("--(multi_2arr)((i for i in a), (j for j in b))")
  153. a=np.arange(N)
  154. b=np.arange(N, 2*N, 1)
  155. start_exec5=time.time()
  156. start_cpu5=time.process_time()
  157. starpu.joblib.Parallel(mode="normal", n_jobs=-1, perfmodel="multi_2arr")(starpu.joblib.delayed(multi_2arr)((i for i in a), (j for j in b)))
  158. end_exec5=time.time()
  159. end_cpu5=time.process_time()
  160. print("the program execution time is", end_exec5-start_exec5)
  161. print("the cpu execution time is", end_cpu5-start_cpu5)
  162. print("--(multi_2arr)(A, B)")
  163. A=np.arange(N)
  164. B=np.arange(N, 2*N, 1)
  165. print("The input arrays are A", A, "B", B)
  166. start_exec6=time.time()
  167. start_cpu6=time.process_time()
  168. starpu.joblib.Parallel(mode="normal", n_jobs=-1, perfmodel="multi_2arr")(starpu.joblib.delayed(multi_2arr)(A, B))
  169. end_exec6=time.time()
  170. end_cpu6=time.process_time()
  171. print("the program execution time is", end_exec6-start_exec6)
  172. print("the cpu execution time is", end_cpu6-start_cpu6)
  173. print("The return arrays are A", A, "B", B)
  174. print("--(scal)(2, t=(j for j in a))")
  175. a=np.arange(N)
  176. start_exec7=time.time()
  177. start_cpu7=time.process_time()
  178. starpu.joblib.Parallel(mode="normal", n_jobs=-1, perfmodel="scal")(starpu.joblib.delayed(scal)(2, t=(j for j in a)))
  179. end_exec7=time.time()
  180. end_cpu7=time.process_time()
  181. print("the program execution time is", end_exec7-start_exec7)
  182. print("the cpu execution time is", end_cpu7-start_cpu7)
  183. print("--(scal)(2,A)")
  184. A=np.arange(N)
  185. print("The input array is", A)
  186. start_exec8=time.time()
  187. start_cpu8=time.process_time()
  188. starpu.joblib.Parallel(mode="normal", n_jobs=-1, perfmodel="scal")(starpu.joblib.delayed(scal)(2,A))
  189. end_exec8=time.time()
  190. end_cpu8=time.process_time()
  191. print("the program execution time is", end_exec8-start_exec8)
  192. print("the cpu execution time is", end_cpu8-start_cpu8)
  193. print("The return array is", A)
  194. print("--(add_scal)(t1=A,t2=B,a=2)")
  195. A=np.arange(N)
  196. B=np.arange(N)
  197. print("The input arrays are A", A, "B", B)
  198. start_exec9=time.time()
  199. start_cpu9=time.process_time()
  200. starpu.joblib.Parallel(mode="normal", n_jobs=-1, perfmodel="add_scal")(starpu.joblib.delayed(add_scal)(t1=A,t2=B,a=2))
  201. end_exec9=time.time()
  202. end_cpu9=time.process_time()
  203. print("the program execution time is", end_exec9-start_exec9)
  204. print("the cpu execution time is", end_cpu9-start_cpu9)
  205. print("The return arrays are A", A, "B", B)
  206. print("--input is iterable function list")
  207. start_exec10=time.time()
  208. start_cpu10=time.process_time()
  209. starpu.joblib.Parallel(mode="normal", n_jobs=-1, perfmodel="func")(g_func)
  210. end_exec10=time.time()
  211. end_cpu10=time.process_time()
  212. print("the program execution time is", end_exec10-start_exec10)
  213. print("the cpu execution time is", end_cpu10-start_cpu10)
  214. # def producer():
  215. # for i in range(6):
  216. # print('Produced %s' % i)
  217. # yield i
  218. #starpu.joblib.Parallel(n_jobs=2)(starpu.joblib.delayed(sqrt)(i) for i in producer())
  219. print("************************")
  220. print("parallel Future version:")
  221. print("************************")
  222. async def main():
  223. print("--(sqrt)(i**2)for i in range(N)")
  224. fut1=starpu.joblib.Parallel(mode="future", n_jobs=3, perfmodel="sqrt")(starpu.joblib.delayed(sqrt)(i**2)for i in range(N))
  225. res1=await fut1
  226. #print(res1)
  227. print("--(multi)(i,j) for i,j in zip(a,b)")
  228. a=np.arange(N)
  229. b=np.arange(N, 2*N, 1)
  230. fut2=starpu.joblib.Parallel(mode="future", n_jobs=3, perfmodel="multi")(starpu.joblib.delayed(multi)(i,j) for i,j in zip(a,b))
  231. res2=await fut2
  232. #print(res2)
  233. print("--(scal_arr)((i for i in b), A)")
  234. A=np.arange(N)
  235. b=np.arange(N, 2*N, 1)
  236. fut3=starpu.joblib.Parallel(mode="future", n_jobs=3, perfmodel="scal_arr")(starpu.joblib.delayed(scal_arr)((i for i in b), A))
  237. res3=await fut3
  238. #print(res3)
  239. print("--(multi_list)((i,j) for i,j in zip(a,b))")
  240. a=np.arange(N)
  241. b=np.arange(N, 2*N, 1)
  242. fut4=starpu.joblib.Parallel(mode="future", n_jobs=2, perfmodel="multi_list")(starpu.joblib.delayed(multi_list)((i,j) for i,j in zip(a,b)))
  243. res4=await fut4
  244. #print(res4)
  245. print("--(multi_2arr)((i for i in a), (j for j in b))")
  246. a=np.arange(N)
  247. b=np.arange(N, 2*N, 1)
  248. fut5=starpu.joblib.Parallel(mode="future", n_jobs=2, perfmodel="multi_2arr")(starpu.joblib.delayed(multi_2arr)((i for i in a), (j for j in b)))
  249. res5=await fut5
  250. #print(res5)
  251. print("--(multi_2arr)(b=B, a=A)")
  252. A=np.arange(N)
  253. B=np.arange(N, 2*N, 1)
  254. print("The input arrays are A", A, "B", B)
  255. fut6=starpu.joblib.Parallel(mode="future", n_jobs=2, perfmodel="multi_2arr")(starpu.joblib.delayed(multi_2arr)(b=B, a=A))
  256. res6=await fut6
  257. print("The return arrays are A", A, "B", B)
  258. print("--(scal)(2, (j for j in a))")
  259. a=np.arange(N)
  260. fut7=starpu.joblib.Parallel(mode="future", n_jobs=2, perfmodel="scal")(starpu.joblib.delayed(scal)(2, (j for j in a)))
  261. res7=await fut7
  262. #print(res6)
  263. print("--(scal)(2,t=A)")
  264. A=np.arange(N)
  265. print("The input array is", A)
  266. fut8=starpu.joblib.Parallel(mode="future", n_jobs=2, perfmodel="scal")(starpu.joblib.delayed(scal)(2,t=A))
  267. res8=await fut8
  268. print("The return array is", A)
  269. print("--(scal)(2,A,B)")
  270. A=np.arange(N)
  271. B=np.arange(N)
  272. print("The input arrays are A", A, "B", B)
  273. fut9=starpu.joblib.Parallel(mode="future", n_jobs=2, perfmodel="add_scal")(starpu.joblib.delayed(add_scal)(2,A,B))
  274. res9=await fut9
  275. print("The return arrays are A", A, "B", B)
  276. print("--input is iterable function list")
  277. fut10=starpu.joblib.Parallel(mode="future", n_jobs=2)(g_func)
  278. res10=await fut10
  279. #print(res9)
  280. asyncio.run(main())
  281. starpu.perfmodel_plot(perfmodel="sqrt",view=False)
  282. starpu.perfmodel_plot(perfmodel="multi",view=False)
  283. starpu.perfmodel_plot(perfmodel="scal_arr",view=False)
  284. starpu.perfmodel_plot(perfmodel="multi_list",view=False)
  285. starpu.perfmodel_plot(perfmodel="multi_2arr",view=False)
  286. starpu.perfmodel_plot(perfmodel="scal",view=False)
  287. starpu.perfmodel_plot(perfmodel="add_scal",view=False)
  288. starpu.perfmodel_plot(perfmodel="func",view=False)
  289. starpu.perfmodel_plot(perfmodel="log_list")
  290. starpu.perfmodel_plot(perfmodel="log_arr")