高性能的Python扩展(3)
admin
2023-07-31 01:42:38
0
  • 高性能的Python扩展(1)
  • 高性能的Python扩展(2)

简介

本文是这个系列的第三篇,我们关注于使用NumPy API为Python编写高性能的C扩展模块。在本文中,我们将使用OpenMP来并行第二部分中的实现。

回顾

Wrold是存储N体状态的一个类。我们的模拟将演化一系列时间步长下的状态。

1234567891011121314151617181920212223242526272829303132333435363738 class World(object):    \”\”\”World is a structure that holds the state of N bodies and    additional variables.     threads : (int) The number of threads to use for multithreaded              implementations.    dt      : (float) The time-step.      STATE OF THE WORLD:      N : (int) The number of bodies in the simulation.    m : (1D ndarray) The mass of each body.    r : (2D ndarray) The position of each body.    v : (2D ndarray) The velocity of each body.    F : (2D ndarray) The force on each body.     TEMPORARY VARIABLES:     Ft : (3D ndarray) A 2D force array for each thread\’s local storage.    s  : (2D ndarray) The vectors from one body to all others.     s3 : (1D ndarray) The norm of each s vector.      NOTE: Ft is used by parallel algorithms for thread-local          storage. s and s3 are only used by the Python          implementation.    \”\”\”    def __init__(self, N, threads=1,                  m_min=1, m_max=30.0, r_max=50.0, v_max=4.0, dt=1e3):        self.threads = threads        self.N  = N        self.m  = np.random.uniform(m_min, m_max, N)        self.r  = np.random.uniform(r_max, r_max, (N, 2))        self.v  = np.random.uniform(v_max, v_max, (N, 2))        self.F  = np.zeros_like(self.r)        self.Ft = np.zeros((threads, N, 2))        self.s  = np.zeros_like(self.r)        self.s3 = np.zeros_like(self.m)        self.dt = dt

在开始模拟时,N体被随机分配质量m,位置r和速度v。对于每个时间步长,接下来的计算有:

  1. 合力F,每个体上的合力根据所有其他体的计算。
  2. 速度v,由于力的作用每个体的速度被改变。
  3. 位置r,由于速度每个体的位置被改变。

计算力:串行代码

下面是之前文章实现中(全部的源代码在这里)的compute_F函数。这个函数计算模拟中每对体之间的相互作用力,其复杂度为O(N^2)。

12345678910111213141516171819202122232425262728 static inline void compute_F(npy_int64 N,                             npy_float64 *m,                             __m128d *r,                             __m128d *F) {  npy_int64 i, j;  __m128d s, s2, tmp;  npy_float64 s3;   // Set all forces to zero.  for(i = 0; i < N; ++i) {    F[i] = _mm_set1_pd(0);  }   // Compute forces between pairs of bodies.   for(i = 0; i < N; ++i) {    for(j = i + 1; j < N; ++j) {      s = r[j] r[i];       s2 = an class=\”crayon-v\”>i];       s2 = i>高性能的Python扩展(1)

  • 高性能的Python扩展(2)
  • 简介

    本文是这个系列的第三篇,我们关注于使用NumPy API为Python编写高性能的C扩展模块。在本文中,我们将使用OpenMP来并行第二部分中的实现。

    回顾

    Wrold是存储N体状态的一个类。我们的模拟将演化一系列时间步长下的状态。

    1234567891011121314151617181920212223242526272829303132333435363738 class World(object):    \”\”\”World is a structure that holds the state of N bodies and    additional variables.     threads : (int) The number of threads to use for multithreaded              implementations.    dt      : (float) The time-step.      STATE OF THE WORLD:      N : (int) The number of bodies in the simulation.    m : (1D ndarray) The mass of each body.    r : (2D ndarray) The position of each body.    v : (2D ndarray) The velocity of each body.    F : (2D ndarray) The force on each body.     TEMPORARY VARIABLES:     Ft : (3D ndarray) A 2D force array for each thread\’s local storage.    s  : (2D ndarray) The vectors from one body to all others.     s3 : (1D ndarray) The norm of each s vector.      NOTE: Ft is used by parallel algorithms for thread-local          storage. s and s3 are only used by the Python          implementation.    \”\”\”    def __init__(self, N, threads=1,                  m_min=1, m_max=30.0, r_max=50.0, v_max=4.0, dt=1e3):        self.threads = threads        self.N  = N        self.m  = np.random.uniform(m_min, m_max, N)        self.r  = np.random.uniform(r_max, r_max, (N, 2))        self.v  = np.random.uniform(v_max, v_max, (N, 2))        self.F  = np.zeros_like(self.r)        self.Ft = np.zeros((threads, N, 2))        self.s  = np.zeros_like(self.r)        self.s3 = np.zeros_like(self.m)        self.dt = dt

    在开始模拟时,N体被随机分配质量m,位置r和速度v。对于每个时间步长,接下来的计算有:

    1. 合力F,每个体上的合力根据所有其他体的计算。
    2. 速度v,由于力的作用每个体的速度被改变。
    3. 位置r,由于速度每个体的位置被改变。

    计算力:串行代码

    下面是之前文章实现中(全部的源代码在这里)的compute_F函数。这个函数计算模拟中每对体之间的相互作用力,其复杂度为O(N^2)。

    12345678910111213141516171819202122232425262728 static inline void compute_F(npy_int64 N,                             npy_float64 *m,                             __m128d *r,                             __m128d *F) {  npy_int64 i, j;  __m128d s, s2, tmp;  npy_float64 s3;   // Set all forces to zero.  for(i = 0; i < N; ++i) {    F[i] = _mm_set1_pd(0);  }   // Compute forces between pairs of bodies.   for(i = 0; i < N; ++i) {    for(j = i + 1; j < N; ++j) {      s = r[j] r[i];       s2 = s=\”crayon-s\”>    v : (2D ndarray) The velocity of each body.    F : (2D ndarray) The force on each body.     TEMPORARY VARIABLES:     Ft : (3D ndarray) A 2D force array for each thread\’s local storage.    s  : (2D ndarray) The vectors from one body to all others.     s3 : (1D ndarray) The norm of each s vector.      NOTE: Ft is used by parallel algorithms for thread-local

    相关内容

    热门资讯

    Mobi、epub格式电子书如... 在wps里全局设置里有一个文件关联,打开,勾选电子书文件选项就可以了。
    500 行 Python 代码... 语法分析器描述了一个句子的语法结构,用来帮助其他的应用进行推理。自然语言引入了很多意外的歧义,以我们...
    定时清理删除C:\Progra... C:\Program Files (x86)下面很多scoped_dir开头的文件夹 写个批处理 定...
    scoped_dir32_70... 一台虚拟机C盘总是莫名奇妙的空间用完,导致很多软件没法再运行。经过仔细检查发现是C:\Program...
    65536是2的几次方 计算2... 65536是2的16次方:65536=2⁶ 65536是256的2次方:65536=256 6553...
    小程序支付时提示:appid和... [Q]小程序支付时提示:appid和mch_id不匹配 [A]小程序和微信支付没有进行关联,访问“小...
    pycparser 是一个用... `pycparser` 是一个用 Python 编写的 C 语言解析器。它可以用来解析 C 代码并构...
    微信小程序使用slider实现... 众所周知哈,微信小程序里面的音频播放是没有进度条的,但最近有个项目呢,客户要求音频要有进度条控制,所...
    Apache Doris 2.... 亲爱的社区小伙伴们,我们很高兴地向大家宣布,Apache Doris 2.0.0 版本已于...
    python清除字符串里非数字... 本文实例讲述了python清除字符串里非数字字符的方法。分享给大家供大家参考。具体如下: impor...