starpu_py_parallel.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. for i in range(len(t)):
  62. t[i]=t[i]*a
  63. return t
  64. A=np.arange(N)
  65. #starpu.joblib.Parallel(mode="normal", n_jobs=2, perfmodel="log")(starpu.joblib.delayed(log10)(i+1)for i in range(N))
  66. # for x in [10, 100, 1000, 10000, 100000, 1000000]:
  67. # for X2 in range(x, x*10, x):
  68. # starpu.joblib.Parallel(mode="normal", n_jobs=2, perfmodel="log")(starpu.joblib.delayed(log10)(i+1)for i in range(X2))
  69. # print(range(X2))
  70. print("************************")
  71. print("parallel Normal version:")
  72. print("************************")
  73. print("--input is iterable argument list, example 1")
  74. starpu.joblib.Parallel(mode="normal", n_jobs=-2, perfmodel="first")(starpu.joblib.delayed(sqrt)(i**2)for i in range(N))
  75. #################scikit test###################
  76. DEFAULT_JOBLIB_BACKEND = starpu.joblib.get_active_backend()[0].__class__
  77. class MyBackend(DEFAULT_JOBLIB_BACKEND): # type: ignore
  78. def __init__(self, *args, **kwargs):
  79. self.count = 0
  80. super().__init__(*args, **kwargs)
  81. def start_call(self):
  82. self.count += 1
  83. return super().start_call()
  84. starpu.joblib.register_parallel_backend('testing', MyBackend)
  85. with starpu.joblib.parallel_backend("testing") as (ba, n_jobs):
  86. print("backend and n_jobs is", ba, n_jobs)
  87. ###############################################
  88. print("--input is iterable argument list, example 2, with multi input")
  89. a=np.arange(10)
  90. b=np.arange(10)
  91. starpu.joblib.Parallel(mode="normal", n_jobs=-2, perfmodel="first")(starpu.joblib.delayed(multi)(i,j) for i,j in zip(a,b))
  92. print("--input is iterable function list")
  93. p=starpu.joblib.Parallel(mode="normal", n_jobs=3, perfmodel="third")
  94. p(g_func)
  95. def producer():
  96. for i in range(6):
  97. print('Produced %s' % i)
  98. yield i
  99. #starpu.joblib.Parallel(n_jobs=2)(starpu.joblib.delayed(sqrt)(i) for i in producer())
  100. print("--input is numpy array")
  101. print("The input array is", A)
  102. starpu.joblib.Parallel(mode="normal", n_jobs=2, perfmodel="array")(starpu.joblib.delayed(scal)(2,A))
  103. print("The return array is", A)
  104. print("************************")
  105. print("parallel Future version:")
  106. print("************************")
  107. async def main():
  108. print("--input is iterable argument list, example 1")
  109. fut1=starpu.joblib.Parallel(mode="future", n_jobs=-3, perfmodel="first")(starpu.joblib.delayed(sqrt)(i**2)for i in range(N))
  110. res1=await fut1
  111. #print(res1)
  112. print("--input is iterable argument list, example 2, with multi input")
  113. a=np.arange(10)
  114. b=np.arange(10)
  115. fut2=starpu.joblib.Parallel(mode="future", n_jobs=-3, perfmodel="second")(starpu.joblib.delayed(multi)(i,j) for i,j in zip(a,b))
  116. res2=await fut2
  117. #print(res2)
  118. print("--input is iterable function list")
  119. fut3=starpu.joblib.Parallel(mode="future", n_jobs=2)(g_func)
  120. res3=await fut3
  121. #print(res3)
  122. print("--input is numpy array")
  123. print("The input array is", A)
  124. fut4=starpu.joblib.Parallel(mode="future", n_jobs=2, perfmodel="array")(starpu.joblib.delayed(scal)(2,A))
  125. res4=await fut4
  126. print("The return array is", A)
  127. asyncio.run(main())
  128. starpu.perfmodel_plot(perfmodel="first",view=False)
  129. starpu.perfmodel_plot(perfmodel="second",view=False)
  130. starpu.perfmodel_plot(perfmodel="third",view=False)