starpu_py_parallel.py 11 KB

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