是否可以在 Python 中并行化此程序(辛普森法则)?

Is it possible to parallelize this program (Simpson's Rule) in Python?

我是并行化范例的新手。我已经有了以串行形式工作的算法,但我无法将其并行化。

我问了一圈,有些人告诉我,我的程序编写方式无法并行化。

这是我的序列号,如有任何帮助或建议,将不胜感激。

import numpy as np  #Importar la librería de numpy para usar sus funciones

if __name__ == "__main__":

  #Numero de elementos debe ser par
  N = 1_000_000_000#int(input("Número de iteraciones/intervalos (número par): "))
  
  #Declara la funcion a integrar
  f = lambda x : x*0.5
  
  #Limites
  a = 3#int(input("Dame el limite inferior: "))
  b = 10#int(input("Dame el limite superior: "))
  
  #delta de x
  dx = (b-a)/N
  
  #División de intervalos
  x = np.linspace(a,b,N+1) 
  
  y = f(x)
  
  #Suma de cada uno de los intervalos
  resultado = dx/3 * np.sum(y[0:-1:2] + 4*y[1::2] + y[2::2])
  
  print("N = " + str(N))
  print("El valor de la integral es: ")
  print(resultado)

Q : "Is it possible to parallelize this program (Simpson's Rule) in Python?"

同一枚硬币更重要的一面不是如何并行化程序(或其部分),而是实际发生的附加成本惩罚。从如何并行化的几个选项中,应该选择最好的一个。这里,由于大 data/memory-footprints ( SpaceDOMAIN 中的不利 O( n ) 缩放,由于尺寸大于 V * 8 * 1E9 [B](所有对象的内存足迹总和使用,包括临时存储变量),接下来,由于 memory-I/O 容量和瓶颈,间接增加 TimeDOMAIN(持续时间)中的 O( n )-缩放在所有 RAM-I/O 个可用通道中)一种称为矢量化的细粒度并行性似乎最适合,因为它增加了几乎为零的附加成本,但有助于降低 RAM-I/O 成本,而且成本极低-复杂性f(x)-lambda,这使得对连续块的内存预取(由于索引跳跃)几乎不可能延迟屏蔽。

至少有两个地方,代码转换会有所帮助,大多数在 CPU 微架构上,它们的本地向量指令可以在最新版本中用于真正的 HPC- numpy 向量化代码的并行执行等级。

A performance tuning mandatory disclaimer :
Detailed profiling will show you, for each particular target code-execution platform { x86 | ARM | ... } and its actual UMA / NUMA memory-I/O & CPU-core cache-hierarchy, where your code loses most of the time (and how successfully or how poor does the actual processor cache-hierarchy mask the real-costs of accessing vast footprints of RAM with all the adverse effects of the physical RAM memory-I/O costs as these do not, for 1E9-sizes, fit into CPU-core cache/registers ).

a )
我们可能会在 producing/storing 临时对象上花费更少的时间,并使用勇敢的 numpy 代码智能矢量化技巧:

y = f( np.linspace( a,       # lo-bound
                    b,       # hi-bound
                    N + 1    #   steps
                    )        # no storage of a temporary, yet HUGE object x ~ 8[GB] in RAM
       )                     # +ask lambda to in-flight compute & create just y = f( x[:] )

如有疑问,请随时阅读有关各种计算/存储相关访问成本的更多信息latencies

b)
我们可能会减少,至少重复内存访问模式的某些部分,(如上所述,由于大约 ~1E9 的大小,不能保留在缓存中并且必须重新执行并从物理 RAM 中重新获取) 仍然满足计算:

# proper fusing of the code against non-even N is left for clarity of vectorised
#                  and left to the kind user
#_________________________________________________________________ double steps
resultado = ( 4 * np.sum( f( np.linspace( a + dx,       # lo-bound
                                          b - dx,       # hi-bound
                                          ( N / 2 ) + 1 #    inner double steps
                                          )
                             ) #--------- lambda on a smaller, RAM/cache-compact object
                          )  #----------- numpy-controls the summation over contiguous RAM
            + 2 * np.sum( f( np.linspace( a + dx + dx,  # lo-bound
                                          b - dx - dx,  # hi-bound
                                          ( N / 2 ) + 1 #    inner double steps
                                          )
                             ) #--------- lambda on a smaller, RAM/cache-compact object
                          )  #----------- numpy-controls the summation overcontiguous RAM
            + f( a )
            + f( b )
              ) * dx / 3

虽然模型代码并不渴望解决所有极端情况,但核心优势来自于使用 RAM 连续内存布局,这种布局非常高效 numpy.sum()-ed 并且避免了重复- 访问内存区域,由于命令式(非连续)索引跳跃而重新访问(numpy 可以优化它自己的一些索引以最大化内存访问 pattern/cache-hits,然而“外部”、编码索引跳跃几乎总是超出这种智能的范围,但硬连线,numpy-优化技术诀窍(硅越少基于思维或千里眼 ;o) )