<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>张德华</title>
    <description>莫负相逢人海间</description>
    <link>https://zhang-dehua.github.io/</link>
    <atom:link href="https://zhang-dehua.github.io/feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Sun, 17 Jul 2022 15:48:54 +0000</pubDate>
    <lastBuildDate>Sun, 17 Jul 2022 15:48:54 +0000</lastBuildDate>
    <generator>Jekyll v3.9.2</generator>
    
      <item>
        <title>辐射扩散</title>
        <description>&lt;h1 id=&quot;辐射流体力学&quot;&gt;辐射流体力学&lt;/h1&gt;

&lt;h2 id=&quot;辐射场的定义和基本概念&quot;&gt;辐射场的定义和基本概念&lt;/h2&gt;

&lt;h3 id=&quot;辐射场的描述&quot;&gt;辐射场的描述&lt;/h3&gt;

&lt;h4 id=&quot;光子&quot;&gt;光子&lt;/h4&gt;

&lt;p&gt;辐射场中的能量由光子携带，频率$\nu$的光子携带的能量为$E=h\nu$，光子是没有质量的粒子，光子在真空中光速为$c$，因此光子的动量$p=\frac{h\nu}{c}$，在于物质碰撞之前，光子都是沿着直线以真空中光速运动，并且频率不发生变化。&lt;/p&gt;

&lt;h4 id=&quot;光子的分布函数&quot;&gt;光子的分布函数&lt;/h4&gt;

\[f=f(\vec{r},\nu,\vec{\Omega},t) \\
dn=f d\vec{r} d\nu d\vec{\Omega}\]

&lt;p&gt;其中$f(r,\nu,\Omega,t)$为光子在时刻$t$，在相空间的分布，$\vec r$为空间点的位置，$\vec \Omega$为光子的运动方向。$dn$为时刻$t$，在以空间点$\vec r$为中心的体积微元$d \vec r$内的，频率在以$\nu$为中心的频率区间$d\nu$内的，运动方向在以$\Omega$为中心的立体角微元$d \Omega$内的光子的数量。&lt;/p&gt;

&lt;p&gt;在直角坐标系下有
\(d\vec r=dx dy dz \\
\vec \Omega=(\sin\theta \cos\phi,\sin\theta \sin\phi,\cos\theta),\ \theta \in [0,\ \pi],\ \phi \in [0,\ 2\pi]\\
d\vec \Omega = \sin\theta d\theta d\phi=d\mu d\phi,\ \mu=\cos\theta \in =[-1,\ 1]\)&lt;/p&gt;

&lt;h4 id=&quot;辐射强度&quot;&gt;辐射强度&lt;/h4&gt;

\[I(\vec r,\nu,\vec \Omega,t)=ch\nu f(\vec r,\nu, \vec \Omega,t) \\
dE=I(\vec r,\nu,\vec \Omega,t)\cos\theta d\nu d\vec \Omega d\sigma dt\]

&lt;p&gt;其中$I(\vec r,\nu,\vec \Omega,t)$为辐射强度，$dE$为在时刻$t$，单位时间内，通过以空间点$\vec r$为中心的面积微元的，频率在以$\nu$为中心的频率区间$d\nu$内的，运动方向在以$\Omega$为中心的立体角微元$d \Omega$内的光子的能量，$\theta$为光子运动方向$\vec \Omega$与面积微元法向量$\vec n$的夹角，即$\cos\theta=\vec \Omega\cdot\vec n$，如下图所示。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;.\figure\dE_intepretation_I.png&quot; alt=&quot;fig1.1&quot; style=&quot;zoom: 50%;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;若辐射强度$I$与角度$\vec \Omega$无关，则辐射场是各向同性的；若辐射强度$I$与空间位置$\vec r$无关，则辐射场是均匀。当辐射场与物质达到完全的热平衡时，辐射场就是各项同性且均匀的，此时辐射强度由普朗克函数$B(\nu,T)$给出
\(I=B(\nu,T)=\frac{2h\nu^3}{c^2}(e^{h\nu/kT}-1)^{-1}\)
其中$h$为普朗克常量。&lt;/p&gt;

&lt;h4 id=&quot;辐射能量密度&quot;&gt;辐射能量密度&lt;/h4&gt;

&lt;p&gt;辐射能量密度$U$是辐射强度$I$的0次角矩，表示单位体积内辐射场的能量，如下式
\(U=\int_{0}^{\infty}d\nu\int_{4\pi}d\vec \Omega h\nu  f=\frac{1}{c}\int_{0}^{\infty}d\nu\int_{4\pi}d\vec \Omega I\)
若辐射强度$I$由普朗克函数$B(\nu,T)$给出，则能量密度$U_p$为
\(U_p=\frac{1}{c}\int_{0}^{\infty}d\nu\int_{4\pi}d\vec \Omega B = \frac{4\pi}{c}\int_{0}^{\infty}d\nu \frac{2h\nu^3}{c^2}(e^{h\nu/kT}-1)^{-1} \\\)
设$x=h\nu/kT$，则有
\(U_p=\frac{8\pi k^4 T^4}{h^3c^3}\int_{0}^{\infty}\frac{x^3}{(e^x-1)}dx\)
其中积分项为
\(\int_{0}^{\infty}\frac{x^3}{(e^x-1)}dx=\frac{\pi^4}{15}\)
故
\(U_p=\frac{8\pi^5 k^4 T^4}{15h^3c^3}=aT^4,\ a=\frac{8\pi^5 k^4}{15h^3c^3}\)
此时，辐射能量密度也经常写作$U_p=\frac{4\sigma}{c}T^4$，其中$\sigma=ac/4$为斯特藩-玻尔兹曼常数。&lt;/p&gt;

&lt;h4 id=&quot;辐射通量&quot;&gt;辐射通量&lt;/h4&gt;

&lt;p&gt;辐射通量$\vec F$是辐射强度$I$的1次角矩，表示辐射场的能流，如下式
\(\vec F=\int_{0}^{\infty}d\nu\int_{4\pi}d\vec \Omega ch\nu  \vec \Omega f=\int_{0}^{\infty}d\nu\int_{4\pi}\vec \Omega I d\vec \Omega\)
在直角坐标系下，三个方向上的辐射通量分量分别为
\(F_x==\int_{0}^{\infty}d\nu\int_{4\pi} \Omega_x I d\vec \Omega \\
F_y==\int_{0}^{\infty}d\nu\int_{4\pi} \Omega_y I d\vec \Omega \\
F_z==\int_{0}^{\infty}d\nu\int_{4\pi} \Omega_z I d\vec \Omega \\\)
对于各向同性的辐射场，辐射通量显然为0，即各个方向上面都没有能量流。&lt;/p&gt;

&lt;h4 id=&quot;辐射压力张量&quot;&gt;辐射压力张量&lt;/h4&gt;

&lt;p&gt;辐射压力张量$\Bbb{P}$是辐射强度$I$的2次角矩。根据气体动理学理论，压力是由通过单位时间通过单位面积的粒子的动量定义的，将此定义应用于辐射场，可得到辐射压力张量，如下式
\(\Bbb{P}=\int_{0}^{\infty}d\nu\int_{4\pi}d\vec \Omega c \frac{h\nu}{c}  \vec \Omega \vec \Omega f=\frac{1}{c}\int_{0}^{\infty}d\nu\int_{4\pi}\vec \Omega \vec \Omega I d\vec \Omega\)
在直角坐标系下，辐射压力的九个分量分别为
\(\begin{cases}
\Bbb{P}_{xx}=\frac{1}{c}\int_{0}^{\infty}d\nu\int_{4\pi} \Omega_x \Omega_x I d\vec \Omega,\ 
\Bbb{P}_{xy}=\frac{1}{c}\int_{0}^{\infty}d\nu\int_{4\pi} \Omega_x \Omega_y I d\vec \Omega,\ 
\Bbb{P}_{xz}=\frac{1}{c}\int_{0}^{\infty}d\nu\int_{4\pi} \Omega_x \Omega_z I d\vec \Omega\\ 
\Bbb{P}_{yx}=\frac{1}{c}\int_{0}^{\infty}d\nu\int_{4\pi} \Omega_y \Omega_x I d\vec \Omega,\ 
\Bbb{P}_{yy}=\frac{1}{c}\int_{0}^{\infty}d\nu\int_{4\pi} \Omega_y \Omega_y I d\vec \Omega,\ 
\Bbb{P}_{yz}=\frac{1}{c}\int_{0}^{\infty}d\nu\int_{4\pi} \Omega_y \Omega_z I d\vec \Omega\\ 
\Bbb{P}_{zx}=\frac{1}{c}\int_{0}^{\infty}d\nu\int_{4\pi} \Omega_z \Omega_x I d\vec \Omega,\ 
\Bbb{P}_{zy}=\frac{1}{c}\int_{0}^{\infty}d\nu\int_{4\pi} \Omega_z \Omega_y I d\vec \Omega,\ 
\Bbb{P}_{zz}=\frac{1}{c}\int_{0}^{\infty}d\nu\int_{4\pi} \Omega_z \Omega_z I d\vec \Omega
\end{cases}\)
可以看出辐射压力张量是对称的，即$\Bbb{P}&lt;em&gt;{ij}=\Bbb{P}&lt;/em&gt;{ji}$。另外，由于$\Omega_x^2+\Omega_y^2+\Omega_z^2=1$，对任意辐射场，则有
\(\Bbb{P}_{xx}+\Bbb{P}_{yy}+\Bbb{P}_{zz}=\frac{1}{c}\int_{0}^{\infty}d\nu\int_{4\pi}d\vec \Omega I=U\)
平均压力为
\(\bar{\Bbb{P}}=\frac{1}{3}(\Bbb{P}_{xx}+\Bbb{P}_{yy}+\Bbb{P}_{zz})=\frac{1}{3}U\)
对于各向同性的辐射场，辐射压力张量的非对角元素为0，且对角元素等于平均压力$\bar{\Bbb{P}}$，即
\(\begin{cases}
\Bbb{P}_{ij}=\frac{1}{3}U,\ i=j\\
\Bbb{P}_{ij}=0,\ i\ne j
\end{cases}\)
对于普朗克分布的辐射强度，则有
\(\Bbb{P}_{xx}=\Bbb{P}_{yy}=\Bbb{P}_{zz}=\frac{1}{3}aT^4\)
对于与各向同性相反的一个极端情况，如果某时刻某空间点上的所有光子的运动方向均指向x方向，即辐射强度$I$在方向上的分布为在x方向上的Drac函数，则有
\(\begin{cases}
\Bbb{P}_{xx}=U\\
\Bbb{P}_{ij}=0,\ i\ne x,\ j\ne x
\end{cases}\)&lt;/p&gt;

&lt;h3 id=&quot;辐射场与物质的相互作用&quot;&gt;辐射场与物质的相互作用&lt;/h3&gt;

&lt;p&gt;辐射场（光子）与物质的相互作用有三种方式：吸收，散射，发射。&lt;/p&gt;

&lt;h4 id=&quot;吸收过程&quot;&gt;吸收过程&lt;/h4&gt;

&lt;p&gt;当光子穿过物质的时候，有一定的概率被物质吸收而消失。可以用宏观吸收系数$\sigma_a(\vec r,\nu,t)$来描述光子被物质吸收的过程，当光子在物质中运动距离为$ds$时，则光子被吸收的概率为
\(\sigma_a(\vec r,\nu,t)ds\)
其中吸收系数$\sigma_a$只依赖与光子的频率$\nu$以及物质的状态参数，而物质的状态参数又与空间位置$\vec r$和时间$t$有关，而通常假设光子被物质吸收的概率与其运动方向无关，表示光子被吸收前平均经过的距离。宏观吸收系数也经常被表$\sigma_a$示成为
\(\sigma_a(\nu)=\rho\chi_a(\nu) \\
\sigma_a(\nu)=n\mu_a(\nu)\)
其中，$\rho$是物质的质量密度，$\chi_a(\nu)$被称为质量吸收系数，$n$是物质的粒子数密度，$\mu_a(\nu)$被称为微观吸收系数。&lt;/p&gt;

&lt;h4 id=&quot;散射过程&quot;&gt;散射过程&lt;/h4&gt;

&lt;p&gt;与吸收过程类似，可以定义出光子的散射系数$\sigma_s(\vec r,\nu,t)$来描述光子被物质吸收的过程，当光子在物质中运动距离为$ds$时，则光子被散射的概率为
\(\sigma_s(\vec r,\nu,t)ds\)
并且常假设光子被物质散射的概率与其运动方向无关。光子被物质散射之后其频率与运动方向均发生改变，即$(\nu’ \rightarrow \nu,\vec \Omega’ \rightarrow \vec\Omega)$，可以定义微分散射系数$\sigma_s(\nu’\rightarrow\nu,\vec\Omega’\rightarrow\vec\Omega)$来描述散射过程的概率，若假设散射过程的概率仅与散射角有关而与散射前后的光子运动方向有关，则微分散射系数可以表示成$\sigma_s(\nu’\rightarrow\nu,\vec\Omega’\cdot\vec\Omega)$。光子在物质中运动距离为$ds$，频率被从$\nu’$散射到以$\nu$为中心的频率区间$d\nu$，方向被从$\Omega’$散射到以$\Omega$为中心的立体角微元$d\vec \Omega$的概率为
\(\sigma_s(\nu'\rightarrow\nu,\vec\Omega'\cdot\vec\Omega)d\nu d\vec\Omega ds\)
对微分散射系数$\sigma_s(\nu’\rightarrow\nu,\vec\Omega’\cdot\vec\Omega)$在频率和立体角上积分可以得到散射系数$\sigma_s(\nu’)$，即
\(\sigma_s(\nu')=\int_{0}^{\infty}d\nu \int_{4\pi}d\vec\Omega\sigma_s(\nu'\rightarrow\nu,\vec\Omega'\cdot\vec\Omega)\)
若选择发生散射前的角度$\vec \Omega’$为$z$轴，散射角度用$\mu_0=\vec\Omega’\cdot\vec\Omega=\cos\theta$表示，则上式可表示成
\(\sigma_s(\nu')=2\pi\int_{0}^{\infty}d\nu \int_{-1}^{1}d\mu_0\sigma_s(\nu'\rightarrow\nu,\mu_0)\)
微分散射系数$\sigma_s(\nu’\rightarrow\nu,\vec\Omega’\cdot\vec\Omega)$可以被表示成为以下的形式
\(\sigma_s(\nu'\rightarrow\nu,\vec\Omega'\cdot\vec\Omega)=\sigma_s(\nu')K(\nu'\rightarrow\nu,\vec\Omega'\cdot\vec\Omega)\)
其中$K(\nu’\rightarrow\nu,\vec\Omega’\cdot\vec\Omega)$为散射核,表示状态为$(\nu’,\vec \Omega’)$光子已经被散射的前提下，散射后状态变为$(\nu,\vec \Omega)$的条件概率，散射核对频率$\nu$和空间角$\vec \Omega$的积分为1，即
\(\int_{0}^{\infty}d\nu \int_{4\pi}d\vec\Omega K(\nu'\rightarrow\nu,\vec\Omega'\cdot\vec\Omega)=1\)
若散射后，光子频率不发生改变，即散射是相干的，则散射核可以表示成包含一个Drac函数的形式，如下
\(K(\nu'\rightarrow\nu,\vec\Omega'\cdot\vec\Omega)=K(\vec\Omega'\cdot\vec\Omega)\delta(\nu'-\nu)\)
此时$K(\vec\Omega’\cdot\vec\Omega)$对空间角$\vec \Omega$的积分为1，即
\(\int_{4\pi}d\vec\Omega K(\vec\Omega'\cdot\vec\Omega)=2\pi\int_{-1}^{1}d\mu_0 K(\mu_0)=1\)
若散射核$K(\nu’\rightarrow\nu,\vec\Omega’\cdot\vec\Omega)$与散射角无关，光子被散射到任意方向的概率都相等，即散射是各向同性的，则散射核可以表示成
\(K(\nu'\rightarrow\nu,\vec\Omega'\cdot\vec\Omega)=\frac{1}{4\pi}K(\nu'\rightarrow\nu)\)
此时$K(\nu’\rightarrow\nu)$对的对频率$\nu$积分为1，即
\(\int_{0}^{\infty}d\nu  K(\nu'\rightarrow\nu)=1\)
若散射既是相干的，也是各项同性的，则散射核可以表示成最简单形式，如下
\(K(\nu'\rightarrow\nu,\vec\Omega'\cdot\vec\Omega)=\frac{1}{4\pi}\delta(\nu'-\nu)\)
对于很多问题，上式是对散射核的一种合理的近似。&lt;/p&gt;

&lt;p&gt;综合考虑吸收与散射的过程，可以得到光子与物质的总相互作用系数$\sigma(\vec r,\nu,t)$如下
\(\sigma(\nu)=\sigma_a(\nu)+\sigma_s(\nu)\)
通过相互作用系数$\sigma(\nu)$可以得到光子与物质相互作用的平均自由程$\lambda$。若有一束总数为$N_0$光子，经过$ds$距离后，与物质相互作用（吸收和散射）的光子的数量为
\(dN=-N\sigma ds\)
即
\(\frac{dN}{ds}=-N\sigma\)
若$\sigma$与$s$无关，光子总数$N$关于运动距离$s$的解为
\(N(s)=N_0e^{-\sigma s}\)
可以得到这束光子的平均自由程如下
\(\lambda(\nu)=\bar{s}(\nu)=\frac{\int_{0}^{\infty}sN(s)\sigma ds}{N_0}=\frac{1}{\sigma(\nu)}\)
于此类似，可以得到吸收平均自由程$\lambda_a=1/\sigma_a$和散射平均自由程$\lambda_s=1/\sigma_s$，并且由$\sigma=\sigma_a+\sigma_s$可知
\(\lambda^{-1}=\lambda_a^{-1}+\lambda_s^{-1}\)&lt;/p&gt;

&lt;h4 id=&quot;发射过程&quot;&gt;发射过程&lt;/h4&gt;

&lt;p&gt;物质除可吸收光子，或者是光子的频率和角度发生改变之外，还可自发的发射光子。物质自发发射光子的过程可以由函数$q(\vec r,\nu,t)$描述，位于空间点$\vec r$的物质在单位时间单位体积内发射出的，在以$\nu$为中心的频率区间$d\nu$内的，在以$\vec \Omega$为中心的立体角微元$d\vec \Omega$内的光子数为
\(q(\vec r,\nu,t)d\nu d\vec\Omega\)
这里假设物质发射光子过程与角度无关，即向各个方向发射光子的概率相同。&lt;/p&gt;

&lt;h2 id=&quot;辐射输运方程&quot;&gt;辐射输运方程&lt;/h2&gt;

&lt;h3 id=&quot;辐射输运方程的原始形式&quot;&gt;辐射输运方程的原始形式&lt;/h3&gt;

&lt;h4 id=&quot;光子分布函数方程&quot;&gt;光子分布函数方程&lt;/h4&gt;

&lt;p&gt;类比于玻尔兹曼方程，可得到的光子分布函数方程如下
\(\frac{\partial f(\nu,\vec \Omega)}{\partial t}+c\vec\Omega\cdot\nabla f(\nu,\vec\Omega)=q(v)-c\sigma_a(\nu) f(\nu,\vec\Omega) \\
+c\int_{0}^{\infty}d\nu'\int_{4\pi}d\vec\Omega'[\sigma_s(\nu'\rightarrow\nu,\vec\Omega'\cdot\vec\Omega)f(\nu',\vec\Omega')-\sigma_s(\nu\rightarrow\nu',\vec\Omega\cdot\vec\Omega')f(\nu,\vec\Omega)]\)&lt;/p&gt;
&lt;h4 id=&quot;辐射强度方程&quot;&gt;辐射强度方程&lt;/h4&gt;

&lt;p&gt;对光子分布函数方程等号两端乘光子的能量$h\nu$可得到辐射强度方程如下
\(\frac{1}{c}\frac{\partial I(\nu,\vec \Omega)}{\partial t}+\vec\Omega\cdot\nabla I(\nu,\vec\Omega)=j(v)-\sigma_a(\nu) I(\nu,\vec\Omega) \\
+\int_{0}^{\infty}d\nu'\int_{4\pi}d\vec\Omega'\frac{\nu}{\nu'}\sigma_s(\nu'\rightarrow\nu,\vec\Omega'\cdot\vec\Omega)I(\nu',\vec\Omega')\\
-\int_{0}^{\infty}d\nu'\int_{4\pi}d\vec\Omega'\sigma_s(\nu\rightarrow\nu',\vec\Omega\cdot\vec\Omega')I(\nu,\vec\Omega)\)
其中$j(\vec r,\nu,t)=h\nu q(\vec r,\nu,t)$为单位体积内电子发射功率。考虑受激辐射，电子发射和散射将受到已经位于反应终态的光子数的影响而增强，增强因子大小为$1+\frac{c^2I}{2h\nu^3}$，则辐射强度方程为
\(\frac{1}{c}\frac{\partial I(\nu,\vec \Omega)}{\partial t}+\vec\Omega\cdot\nabla I(\nu,\vec\Omega)=j(v)[1+\frac{c^2I(\nu,\vec \Omega)}{2h\nu^3}]-\sigma_a(\nu) I(\nu,\vec\Omega) \\
+\int_{0}^{\infty}d\nu'\int_{4\pi}d\vec\Omega'\frac{\nu}{\nu'}\sigma_s(\nu'\rightarrow\nu,\vec\Omega'\cdot\vec\Omega)I(\nu',\vec\Omega')[1+\frac{c^2I(\nu,\vec \Omega)}{2h\nu^3}]\\
-\int_{0}^{\infty}d\nu'\int_{4\pi}d\vec\Omega' \sigma_s(\nu\rightarrow\nu',\vec\Omega\cdot\vec\Omega')I(\nu,\vec\Omega)[1+\frac{c^2I(\nu',\vec \Omega')}{2h\nu^{'3}}]\)&lt;/p&gt;

&lt;h3 id=&quot;输运方程的定解条件&quot;&gt;输运方程的定解条件&lt;/h3&gt;

&lt;h4 id=&quot;边界条件&quot;&gt;边界条件&lt;/h4&gt;

&lt;p&gt;\(I(\vec r^s,\nu,\vec\Omega,t)=\Gamma(\vec r^s,\nu,\vec\Omega,t),\quad \vec n\cdot\vec \Omega&amp;lt;0\)
若边界无辐射能流，则为真空边界条件
\(I(\vec r^s,\nu,\vec\Omega,t)=0,\quad \vec n\cdot\vec \Omega&amp;lt;0\)&lt;/p&gt;
&lt;h4 id=&quot;初始条件&quot;&gt;初始条件&lt;/h4&gt;

&lt;p&gt;\(I(\vec r,\nu,\vec\Omega,0)=\Lambda(\vec r,\nu,\vec\Omega)\)&lt;/p&gt;
&lt;h2 id=&quot;局域热平衡下的辐射输运方程&quot;&gt;局域热平衡下的辐射输运方程&lt;/h2&gt;

&lt;h3 id=&quot;基尔霍夫定律&quot;&gt;基尔霍夫定律&lt;/h3&gt;

&lt;p&gt;辐射输运方程的源项在局域热平衡的条件下可以大大简化。当系统处于完全热力学平衡时，散射项相互抵消，吸收项相互抵消，吸收项目等于发射项，这时辐射的强度是普朗克分布，由系统的温度决定，即
\(j(v)[1+\frac{c^2B(\nu,T)}{2h\nu^3}]=\sigma_a(\nu) B(\nu,T)\)
整理可以得到
\(j(v)=\frac{\sigma_a(\nu) B(\nu,T)}{1+\frac{c^2}{2h\nu^3}B(\nu,T)}\)
这就是所为的基尔霍夫定律。&lt;/p&gt;

&lt;h3 id=&quot;局域热平衡带来的简化&quot;&gt;局域热平衡带来的简化&lt;/h3&gt;

&lt;p&gt;这就得到了物质的发射率，这对于辐射场与物质没有完全热力学平衡的情况仍然是成立的，进一步忽略掉散射过程，则可以得到局域热零平衡下的辐射输运方程为
\(\frac{1}{c}\frac{\partial I(\nu,\vec \Omega)}{\partial t}+\vec\Omega\cdot\nabla I(\nu,\vec\Omega)=\frac{\sigma_a(\nu) B(\nu,T)}{1+\frac{c^2}{2h\nu^3}B(\nu,T)}[1+\frac{c^2I(\nu,\vec \Omega)}{2h\nu^3}]-\sigma_a(\nu) I(\nu,\vec\Omega)\)
化简可得
\(\frac{1}{c}\frac{\partial I(\nu,\vec \Omega)}{\partial t}+\vec\Omega\cdot\nabla I(\nu,\vec\Omega)=\sigma'_a(\nu) B(\nu,T)-\sigma'_a(\nu) I(\nu,\vec\Omega)\)
其中
\(\sigma'_a(\nu)=\frac{\sigma_a(\nu)}{1+\frac{c^2}{2h\nu^3}B(\nu,T)}=(1-e^{-\frac{h\nu}{kT}})\sigma_a(\nu)\)
人们更喜欢使用$\rho\chi’_a$的形式，即
\(\frac{1}{c}\frac{\partial I(\nu,\vec \Omega)}{\partial t}+\vec\Omega\cdot\nabla I(\nu,\vec\Omega)=\rho\chi'_a(\nu) B(\nu,T)-\rho\chi'_a(\nu) I(\nu,\vec\Omega)\)
后面讨论的各种近似描述就是建立在这个简化版本的辐射输运方程的基础之上的。&lt;/p&gt;

&lt;h2 id=&quot;辐射输运方程的扩散近似&quot;&gt;辐射输运方程的扩散近似&lt;/h2&gt;

&lt;h3 id=&quot;扩散近似&quot;&gt;扩散近似&lt;/h3&gt;

&lt;p&gt;辐射强度在几乎是各向同性的情况下，可以将辐射强度近似表示成为
\(I(\vec r,\nu,\vec \Omega,t)=\frac{1}{4\pi}I_0(\vec r,\nu,t)+\frac{3}{4\pi}\vec\Omega\cdot\vec I_1(\vec r,\nu,t)\)
根据数学关系式
\(\int d\vec\Omega=4\pi,\ \int\vec \Omega d \vec \Omega=0, \int \vec \Omega \vec \Omega\cdot\vec A d\vec \Omega=\frac{4\pi}{3}\vec A\)
可得到
\(I_0(\vec r,\nu,t)=\int_{4\pi} d\vec\Omega I(\vec r,\nu,\vec \Omega,t)=cU(\vec r,\nu,t)\\
\vec I_1(\vec r,\nu,t)=\int_{4\pi}\vec\Omega d\vec\Omega I(\vec r,\nu,\vec \Omega,t)=\vec F\)
即$I_o/c$和$\vec I_1$分别代表频率$\nu$上辐射场的能量密度和能流。将上述表达式代入
\(\frac{1}{c}\frac{\partial I(\nu,\vec \Omega)}{\partial t}+\vec\Omega\cdot\nabla I(\nu,\vec\Omega)=\rho\chi'_a(\nu) B(\nu,T)-\rho\chi'_a(\nu) I(\nu,\vec\Omega)\)
对立体角积分可得
\(\frac{1}{c}\frac{\partial I_0}{\partial t}+\nabla\cdot\vec I_1(\nu)=\rho\chi'_a(\nu) [4\pi B(\nu,T)-I_0(\nu)]\)
乘以$\vec\Omega$对立体角积分可得
\(\frac{1}{c}\frac{\partial \vec I_1(\nu)}{\partial t}+\frac{1}{3}\nabla I_0(\nu)=-\rho\chi'_a(\nu) \vec I_1(\nu)\)
将上式中$\frac{1}{c}\frac{\partial \vec I_1(\nu)}{\partial t}$忽略，即可得到
\(\vec I_1(\nu)=-\frac{1}{3\rho\chi'_a(\nu)}\nabla I_0(\nu)\)
在将此式带入$I_0$的方程得
\(\frac{1}{c}\frac{\partial I_0}{\partial t}-\nabla\cdot[\frac{1}{3\rho\chi'_a(\nu)}\nabla I_0(\nu)]=\rho\chi'_a(\nu) [4\pi B(\nu,T)-I_0(\nu)]\)&lt;/p&gt;

&lt;h3 id=&quot;平衡扩散近似&quot;&gt;平衡扩散近似&lt;/h3&gt;

&lt;p&gt;当辐射场与电子达到完全得局域热平衡后，电子温度与辐射场温度相等，电子与辐射扩散方程得右端项为0，即
\(I_0(\nu)=4\pi B(\nu,T)=\frac{8\pi h\nu^3}{c^2}(e^{h\nu/kT}-1)^{-1}\)
将其带入扩散近似方程则有
\(\frac{4\pi}{c}\frac{\partial B(\nu,T)}{\partial t}-\nabla\cdot[\frac{4\pi}{3\rho\chi'_a(\nu)}\nabla B(\nu,T)]=0\)
此时我们可以得到辐射场能量密度
\(E_r=U_p=\frac{4\pi}{c}\int_0^\infty B(\nu,T)d\nu=aT^4 \\\)
辐射场能流
\(\vec F=-\frac{4\pi}{3}\int_0^\infty \frac{1}{\rho\chi'_a(\nu)}\frac{\partial B(\nu,T)}{\partial T}d\nu\nabla T\\
=-\frac{4\pi}{3}\nabla T\frac{\int_0^\infty \frac{1}{\rho\chi'_a(\nu)}\frac{\partial B(\nu,T)}{\partial T}d\nu}{\int_0^\infty \frac{\partial B(\nu,T)}{\partial T}d\nu}
\int_0^\infty \frac{\partial B(\nu,T)}{\partial T}d\nu \\
=-\frac{4}{3}acl_RT^3\nabla T
=\frac{1}{3}l_Rc\nabla E_r\\
E_r=aT^4\\\)
其中$l_R$为Rosseland平均自由程如下
\(l_R=\frac{\int_0^\infty \frac{1}{\rho\chi'_a(\nu)}\frac{\partial B(\nu,T)}{\partial T}d\nu}{\int_0^\infty \frac{\partial B(\nu,T)}{\partial T}d\nu}\)
则辐射扩散方程为
\(\frac{\partial E_r}{\partial t}=\nabla\cdot(\frac{1}{3}l_Rc\nabla E_r)\\\)
或
\(\frac{\partial aT^4}{\partial t}=\nabla\cdot(\kappa_r\nabla T)\\
\kappa_r=\frac{4}{3}acl_RT^3\)&lt;/p&gt;

&lt;h3 id=&quot;非平衡扩散近似&quot;&gt;非平衡扩散近似&lt;/h3&gt;

&lt;p&gt;若辐射场达到局域热平衡，但与电子为达到平衡，即$T_r\ne T_e$，则有
\(I_0(\nu)=4\pi B(\nu,T_r)=\frac{8\pi h\nu^3}{c^2}(e^{h\nu/kT}-1)^{-1}\)
将其带入辐射扩散方程，可得
\(\frac{4\pi}{c}\frac{\partial B(\nu,Tr)}{\partial t}-\nabla\cdot[\frac{4\pi}{3\rho\chi'_a(\nu,T_e)}\nabla B(\nu,T_r)]=4\pi\rho[\chi’_a (\nu,T_e)B(\nu,T_e)-\chi’_a(\nu,T_e) B(\nu,T_r)]\)
对其关于$\nu$进行积分，&lt;/p&gt;

&lt;p&gt;其中辐射能量密度为
\(E_r(T_r)=U_p(T_r)=\frac{4\pi}{c}\int_0^\infty B(\nu,T_r)d\nu=aT_r^4 \\\)
辐射能流为
\(\vec F=-\frac{4\pi}{3}\int_0^\infty \frac{1}{\rho\chi'_a(\nu，T_e)}\frac{\partial B(\nu,T_r)}{\partial T_r}d\nu\nabla T_r\\
=-\frac{4\pi}{3}\nabla T_r\frac{\int_0^\infty \frac{1}{\rho\chi'_a(\nu,T_e)}\frac{\partial B(\nu,T_r)}{\partial T_r}d\nu}{\int_0^\infty \frac{\partial B(\nu,T_r)}{\partial T_r}d\nu}
\int_0^\infty \frac{\partial B(\nu,T_r)}{\partial T_r}d\nu \\
=-\frac{4}{3}acl_R^{}T_r^3\nabla T_r\\
=\frac{1}{3}l_Rc\nabla E_r\\
E_r=aT_r^4\\\)
其中$l_R(T_e,T_r)$为Rosseland平均自由程如下
\(l_R(T_e,T_r)=\frac{\int_0^\infty \frac{1}{\rho\chi'_a(\nu,T_e)}\frac{\partial B(\nu,T_r)}{\partial T_r}d\nu}{\int_0^\infty \frac{\partial B(\nu,T_r)}{\partial T_r}d\nu}\)
电子和辐射场能量交换
\(4\pi\rho\int_0^\infty[\chi’_a(\nu,T_e) B(\nu,T_e)-\chi’_a (\nu,T_e)B(\nu,T_r)]d\nu\\
=4\pi\rho\frac{\int_0^\infty\chi’_a(\nu,T_e) B(\nu,T_e)d\nu}{\int_0^\infty B(\nu,T_e)d\nu}\int_0^\infty B(\nu,T_e)d\nu-
4\pi\rho\frac{\int_0^\infty\chi’_a(\nu,T_e) B(\nu,T_r)d\nu}{\int_0^\infty B(\nu,T_r)d\nu}\int_0^\infty B(\nu,T_r)d\nu\\
=c\frac{\int_0^\infty\rho\chi’_a(\nu,T_e) B(\nu,T_e)d\nu}{\int_0^\infty B(\nu,T_e)d\nu}aT_e^4-
c\frac{\int_0^\infty\rho\chi’_a(\nu,T_e) B(\nu,T_r)d\nu}{\int_0^\infty B(\nu,T_r)d\nu}aT_r^4\\
=cl_P^{-1}(T_e)aT_e^4-cl_P^{-1}(T_e,T_r)aT_r^4\)
其中
\(l_P^{-1}(T_e)=\frac{\int_0^\infty\rho\chi’_a(\nu,T_e) B(\nu,T_e)d\nu}{\int_0^\infty B(\nu,T_e)d\nu}\\
l_P^{-1}(T_e,T_r)=\frac{\int_0^\infty\rho\chi’_a(\nu,T_e) B(\nu,T_r)d\nu}{\int_0^\infty B(\nu,T_r)d\nu}\)
为Planck平均自由程。&lt;/p&gt;

&lt;p&gt;因此扩散方程如下
\(\frac{\partial E_r}{\partial t}-\nabla\cdot[\frac{cl_R(T_e,T_r)}{3}\nabla E_r]=
cl_P^{-1}(T_e)aT_e^4-cl_P^{-1}(T_e,T_r)E_r\)
Rosseland平均自由程$l_R(T_e,T_r)$和Planck平均自由程$l_P^{-1}(T_e,T_r)$都是$T_e$和$T_r$的函数，通常取如下近似
\(l_R(T_e,T_r)\approx l_R(T_e)=\frac{\int_0^\infty \frac{1}{\rho\chi'_a(\nu,T_e)}\frac{\partial B(\nu,T_e)}{\partial T_e}d\nu}{\int_0^\infty \frac{\partial B(\nu,T_e)}{\partial T_e}d\nu}\\
l_P^{-1}(T_e,T_r)\approx l_P^{-1}(T_e)=\frac{\int_0^\infty\rho\chi’_a(\nu,T_e) B(\nu,T_e)d\nu}{\int_0^\infty B(\nu,T_e)d\nu}\\\)
因此扩散方程可以写成
\(\frac{\partial E_r}{\partial t}-\nabla\cdot(D_r\nabla E_r)=
\tau_{er}^{-1}(aT_e^4-E_r)\\
D_r=\frac{cl_R(T_e)}{3}\\
\tau_{er}^{-1}=cl_P^{-1}(T_e)\)&lt;/p&gt;

&lt;h3 id=&quot;多群扩散近似&quot;&gt;多群扩散近似&lt;/h3&gt;

&lt;p&gt;考虑辐射输运方程
\(\frac{1}{c}\frac{\partial I(\nu,\vec \Omega)}{\partial t}+\vec\Omega\cdot\nabla I(\nu,\vec\Omega)=\rho\chi'_a(\nu) B(\nu,T)-\rho\chi'_a(\nu) I(\nu,\vec\Omega)\)
将频率范围$[0,\infty]$按照$0=\nu_0&amp;lt;\nu_1&amp;lt;\nu_2&amp;lt;…&amp;lt;\nu_{n-1}&amp;lt;\nu_g=\infty$分成n个群，其中第$g$个群区间为$[\nu_{g-1},\nu_g]$。&lt;/p&gt;

&lt;p&gt;定义第$g$群的辐射强度定义为
\(I_g(\vec \Omega)=\int_{\nu_{g-1}}^{\nu_g}I(\nu,\vec \Omega)d\nu,\ \ 1\le g\le n\)
将辐射输运方程对第g群积分，可得
\(\frac{1}{c}\frac{\partial I_g(\vec \Omega)}{\partial t}+\vec\Omega\cdot\nabla I_g(\vec\Omega)=\rho\chi'_{ag}[ B_g(T)-I_g(\vec\Omega)]\)
其中
\(\rho\chi'_{ag}=\frac{\int_0^\infty \rho\chi'_{ag}[B(\nu,T)-I(\nu,\vec\Omega)]d\nu}{\int_0^\infty[B(\nu,T)-I(\nu,\vec\Omega)]d\nu}\)
对第$g$个群的辐射输运方程进行扩散近似
\(I_g(\vec \Omega)=\frac{1}{4\pi}I_{0g}+\frac{3}{4\pi}\vec\Omega\cdot\vec I_{1g}\)
假设$\rho\chi’&lt;em&gt;{ag}$与$\vec \Omega$无关，在对第$g$群关于立体角进行积分，可得
\(\frac{1}{c}\frac{\partial I_{0g}}{\partial t}+\nabla\cdot\vec I_{1g}=\rho\chi'_a(\nu) [4\pi B_g(T)-I_{0g}]\)
对第$g$群乘以$\vec \Omega$关于立体角进行积分
\(\frac{1}{c}\frac{\partial \vec I_{1g}}{\partial t}+\frac{1}{3}\nabla I_{0g}=-\rho\chi'_a(\nu) \vec I_{1g}\)
将上式中$\frac{1}{c}\frac{\partial \vec I&lt;/em&gt;{1g}}{\partial t}$忽略，即可得到
\(\vec I_{1g}=-\frac{1}{3\rho\chi'_{ag}}\nabla I_{0g}\)
将其带入$I_{0g}$方程
\(\frac{1}{c}\frac{\partial I_{0g}}{\partial t}-\nabla\cdot[\frac{1}{3\rho\chi'_{ag}}\nabla I_{0g}]=\rho\chi'_{ag} [4\pi B_g(T)-I_{0g}]\)
根据定义$I_{0g}=cE_g$，上述方程应为
\(\frac{\partial E_{g}}{\partial t}-\nabla\cdot[\frac{c}{3\rho\chi'_{ag}}\nabla E_{g}]=\rho\chi'_{ag}c [\frac{4\pi}{c} B_g(T)-E_{g}]\)
其中
\(\frac{4\pi}{c} B_g(T)=\int_{\nu_{g-1}}^{\nu_g} \frac{8\pi h\nu^3}{c^3}(e^{\frac{h\nu}{kT}}-1)^{-1} d\nu\)
设$x=\frac{h \nu}{kT}$，则有
\(\frac{4\pi}{c} B_g(T)=\frac{15}{\pi^4}aT^4\int_{x_{g-1}}^{x_g}\frac{x^3}{e^x-1}dx\\
=\frac{15}{\pi^4}b_gaT^4\\\)
其中$b_g$为
\(b_g=\int_{x_{g-1}}^{x_g}\frac{x^3}{e^x-1}dx,\ \  x_{g-1}=\frac{hv_{g-1}}{kT},\  x_{g}=\frac{hv_{g}}{kT}\)
对于方程中的两处$\rho\chi’_{ag}$通常分别Rosseland平均和Planck平均，即
\(l_{Rg}=\frac{\int_{\nu_{g-1}}^{\nu_g} \frac{1}{\rho\chi'_a(\nu)}\frac{\partial B(\nu,T)}{\partial T}d\nu}{\int_{\nu_{g-1}}^{\nu_g} \frac{\partial B(\nu,T)}{\partial T}d\nu}\\
l_{Pg}^{-1}=\frac{\int_{\nu_{g-1}}^{\nu_g}\rho\chi’_a(\nu) B(\nu,T)d\nu}{\int_{\nu_{g-1}}^{\nu_g} B(\nu,T)d\nu}\)
则多群辐射方程为
\(\frac{\partial E_{g}}{\partial t}-\nabla\cdot[\frac{l_{Rg}c}{3}\nabla E_{g}]=\frac{c}{l_{Pg}} [\frac{15}{\pi^4}b_gaT^4-E_{g}]\\\)
进一步化简可得
\(\frac{\partial E_{g}}{\partial t}-\nabla\cdot[D_{rg}\nabla E_{g}]=\tau_{erg}^{-1}[\frac{15}{\pi^4}b_gaT^4-E_{g}]\\
D_{rg}=\frac{l_{Rg}c}{3}\\
\tau_{erg}^{-1}=\frac{c}{l_{Pg}}\)&lt;/p&gt;

&lt;h2 id=&quot;不透明度&quot;&gt;不透明度&lt;/h2&gt;

\[\rho\chi_v^{'ff}=\frac{4\sqrt{2\pi}e^6}{3\sqrt{3}hcm_e^}g_{ff}n_i n_e Z^2T_e^{-\frac{1}{2}}\nu^{-3}(1-e^{-\frac{h\nu}{kt}})\]

&lt;p&gt;其中$g_{ff}$为Gaunt因子，表示量子力学的修正。考虑完全电离的等离子体，$n_i=\rho/(Am_p)$，$n_e=Z\rho/(Am_p)$，$A$为离子的质量数，$Z$为荷电荷数，则有
\(\rho\chi_v^{'ff}=\frac{4\sqrt{2\pi}e^6}{3\sqrt{3}hcm_e^{3/2}m_p^2}g_{ff}\frac{\rho^2Z^3}{A^2}(kT_e)^{-\frac{1}{2}}\nu^{-3}(1-e^{-\frac{h\nu}{kt}})\)
已知
\(B(\nu,T)=\frac{2h\nu^3}{c^2}(e^{h\nu/kT}-1)^{-1}\\
\frac{\partial B(\nu,T)}{\partial T}=\frac{2h^2\nu^4}{c^2kT^2}\frac{e^{h\nu/kT}}{(e^{h\nu/kT}-1)^2}\)&lt;/p&gt;

&lt;h3 id=&quot;rosseland不透明度&quot;&gt;Rosseland不透明度&lt;/h3&gt;

&lt;p&gt;则Rosseland平均自由程为
\(l_R(T)=\frac{\int_0^\infty \frac{1}{\rho\chi'_a(\nu,T)}\frac{\partial B(\nu,T)}{\partial T}d\nu}{\int_0^\infty \frac{\partial B(\nu,T)}{\partial T}d\nu}\\
=\frac{3\sqrt{3}hcm_e^{3/2}m_p^2}{4\sqrt{2\pi}e^6g_{ff}}
\frac{A^2}{\rho^2Z^3}\frac{\int_0^\infty
(kT)^{\frac{1}{2}}\nu^{3}
\frac{e^{h\nu/kT}}{(e^{h\nu/kT}-1)}
\frac{2h^2\nu^4}{c^2kT^2}\frac{e^{h\nu/kT}}{(e^{h\nu/kT}-1)^2}d\nu}
{\int_0^\infty \frac{2h^2\nu^4}{c^2kT^2}
\frac{e^{h\nu/kT}}{(e^{h\nu/kT}-1)^2}d\nu}\\
=\frac{3\sqrt{3}hcm_e^{3/2}m_p^2}{4\sqrt{2\pi}e^6g_{ff}}
\frac{A^2}{\rho^2Z^3}\frac{\int_0^\infty
(kT)^{\frac{1}{2}}\nu^{3}
\frac{e^{h\nu/kT}}{(e^{h\nu/kT}-1)}
\frac{2h^2\nu^4}{c^2kT^2}\frac{e^{h\nu/kT}}{(e^{h\nu/kT}-1)^2}d\nu}
{\frac{8k^4\pi^4T^3}{15c^2h^3}}\\
=\frac{3\sqrt{3}hcm_e^{3/2}m_p^2}{4\sqrt{2\pi}e^6g_{ff}}
\frac{15h^5}{4\pi^4(kT)^{9/2}}
\frac{A^2}{\rho^2Z^3}
\int_0^\infty
\frac{\nu^7e^{2h\nu/kT}}{(e^{h\nu/kT}-1)^3}d\nu\)
若设$x=\frac{h\nu}{kT}$，则有
\(l_R(T)=\frac{3\sqrt{3}hcm_e^{3/2}m_p^2}{4\sqrt{2\pi}e^6g_{ff}}
\frac{15h^5}{4\pi^4(kT)^{9/2}}
(\frac{kT}{h})^8
\frac{A^2}{\rho^2Z^3}
\int_0^\infty
\frac{x^7e^{2x}}{(e^{x}-1)^3}dx\\
=\frac{45\sqrt{3}cm_e^{3/2}m_p^2}{16\sqrt{2}\pi^{9/2}h^2e^6g_{ff}}
\frac{A^2(kT)^{7/2}}{\rho^2Z^3}
\int_0^\infty
\frac{x^7e^{2x}}{(e^{x}-1)^3}dx\)
其中积分项
\(\int_0^\infty
\frac{x^7e^{2x}}{(e^{x}-1)^3}dx=\frac{8}{3}[\pi^6+945\zeta(7)]\)
可得
\(l_R(T)
=\frac{15\sqrt{3}[\pi^6+945\zeta(7)]cm_e^{3/2}m_p^2}{2\sqrt{2}\pi^{9/2}h^2e^6g_{ff}}
\frac{A^2(kT)^{7/2}}{\rho^2Z^3}\)
取$g_{ff}=1.0$，带入物理常量，可得
\(l_R\approx71.71\frac{A^2(kT)^{7/2}}{\rho^2Z^3}\)
其中$l_R$单位是$cm$，$kT$单位是$keV$，$\rho$单位是$g/cm^3$。&lt;/p&gt;

&lt;h3 id=&quot;planck不透明度&quot;&gt;Planck不透明度&lt;/h3&gt;

&lt;p&gt;Planck平均自由程
\(l_P^{-1}(T)=\frac{\int_0^\infty\rho\chi’_a(\nu,T) B(\nu,T)d\nu}{\int_0^\infty B(\nu,T)d\nu}\\
=\frac{4\sqrt{2\pi}e^6g_{ff}}{3\sqrt{3}hcm_e^{3/2}m_p^2}\frac{\rho^2Z^3}{A^2}
\frac{\int_0^\infty (kT_e)^{-\frac{1}{2}}\nu^{-3}(1-e^{-\frac{h\nu}{kt}}) \frac{2h\nu^3}{c^2}(e^{h\nu/kT}-1)^{-1}d\nu}
{\int_0^\infty \frac{2h\nu^3}{c^2}(e^{h\nu/kT}-1)^{-1} d\nu}\\
=\frac{4\sqrt{2\pi}e^6g_{ff}}{3\sqrt{3}hcm_e^{3/2}m_p^2}
\frac{15 h^4}{\pi^4}(kT)^{-9/2}
\frac{\rho^2Z^3}{A^2}
\int_0^\infty e^{-\frac{h\nu}{kT}}d\nu\\
=\frac{4\sqrt{2\pi}e^6g_{ff}}{3\sqrt{3}hcm_e^{3/2}m_p^2}
\frac{15 h^3}{\pi^4}(kT)^{-7/2}
\frac{\rho^2Z^3}{A^2}
\int_0^\infty e^{-x}dx\\
=\frac{20\sqrt{2\pi}h^2e^6g_{ff}}{\sqrt{3}\pi^4cm_e^{3/2}m_p^2}
\frac{\rho^2Z^3}{A^2(kT)^{7/2}}\)
取$g_{ff}=1.0$，带入物理常量，可得
\(l_P^{-1}=0.422\frac{\rho^2Z^3}{A^2(kT)^{7/2}}\)
其中$l_R$单位是$cm$，$kT$单位是$keV$，$\rho$单位是$g/cm^3$。&lt;/p&gt;

&lt;h3 id=&quot;多群扩散中的不透明度&quot;&gt;多群扩散中的不透明度&lt;/h3&gt;

&lt;p&gt;则Rosseland平均自由程为
\(l_{Rg}
=\frac{45\sqrt{3}cm_e^{3/2}m_p^2}{16\sqrt{2}\pi^{9/2}h^2e^6g_{ff}}
\frac{A^2(kT)^{7/2}}{\rho^2Z^3}
\int_{x_{g-1}}^{x_g}
\frac{x^7e^{2x}}{(e^{x}-1)^3}dx\)
Planck平均自由程
\(l_{Pg}^{-1}
=\frac{4\sqrt{2\pi}e^6g_{ff}}{3\sqrt{3}hcm_e^{3/2}m_p^2}
\frac{15 h^3}{\pi^4}
\frac{\rho^2Z^3}{A^2(kT)^{7/2}}
\int_{x_{g-1}}^{x_g} e^{-x}dx\\
=\frac{4\sqrt{2\pi}e^6g_{ff}}{3\sqrt{3}hcm_e^{3/2}m_p^2}
\frac{15 h^3}{\pi^4}
\frac{\rho^2Z^3}{A^2(kT)^{7/2}}
(e^{-x_{g-1}}-e^{-x_{g}})\)&lt;/p&gt;
</description>
        <pubDate>Mon, 11 Jul 2022 00:00:00 +0000</pubDate>
        <link>https://zhang-dehua.github.io/2022/07/radiation_diffusion/</link>
        <guid isPermaLink="true">https://zhang-dehua.github.io/2022/07/radiation_diffusion/</guid>
        
        <category>高能量密度物理</category>
        
        
      </item>
    
      <item>
        <title>辐射</title>
        <description>&lt;h1 id=&quot;辐射流体力学&quot;&gt;辐射流体力学&lt;/h1&gt;

&lt;h2 id=&quot;辐射场的定义和基本概念&quot;&gt;辐射场的定义和基本概念&lt;/h2&gt;

&lt;h3 id=&quot;辐射场的描述&quot;&gt;辐射场的描述&lt;/h3&gt;

&lt;h4 id=&quot;光子&quot;&gt;光子&lt;/h4&gt;

&lt;p&gt;辐射场中的能量由光子携带，频率$\nu$的光子携带的能量为$E=h\nu$，光子是没有质量的粒子，光子在真空中光速为$c$，因此光子的动量$p=\frac{h\nu}{c}$，在于物质碰撞之前，光子都是沿着直线以真空中光速运动，并且频率不发生变化。&lt;/p&gt;

&lt;h4 id=&quot;光子的分布函数&quot;&gt;光子的分布函数&lt;/h4&gt;

\[f=f(\vec{r},\nu,\vec{\Omega},t) \\
dn=f d\vec{r} d\nu d\vec{\Omega}\]

&lt;p&gt;其中$f(r,\nu,\Omega,t)$为光子在时刻$t$，在相空间的分布，$\vec r$为空间点的位置，$\vec \Omega$为光子的运动方向。$dn$为时刻$t$，在以空间点$\vec r$为中心的体积微元$d \vec r$内的，频率在以$\nu$为中心的频率区间$d\nu$内的，运动方向在以$\Omega$为中心的立体角微元$d \Omega$内的光子的数量。&lt;/p&gt;

&lt;p&gt;在直角坐标系下有
\(d\vec r=dx dy dz \\
\vec \Omega=(\sin\theta \cos\phi,\sin\theta \sin\phi,\cos\theta),\ \theta \in [0,\ \pi],\ \phi \in [0,\ 2\pi]\\
d\vec \Omega = \sin\theta d\theta d\phi=d\mu d\phi,\ \mu=\cos\theta \in =[-1,\ 1]\)&lt;/p&gt;

&lt;h4 id=&quot;辐射强度&quot;&gt;辐射强度&lt;/h4&gt;

\[I(\vec r,\nu,\vec \Omega,t)=ch\nu f(\vec r,\nu, \vec \Omega,t) \\
dE=I(\vec r,\nu,\vec \Omega,t)\cos\theta d\nu d\vec \Omega d\sigma dt\]

&lt;p&gt;其中$I(\vec r,\nu,\vec \Omega,t)$为辐射强度，$dE$为在时刻$t$，单位时间内，通过以空间点$\vec r$为中心的面积微元的，频率在以$\nu$为中心的频率区间$d\nu$内的，运动方向在以$\Omega$为中心的立体角微元$d \Omega$内的光子的能量，$\theta$为光子运动方向$\vec \Omega$与面积微元法向量$\vec n$的夹角，即$\cos\theta=\vec \Omega\cdot\vec n$，如下图所示。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;.\figure\dE_intepretation_I.png&quot; alt=&quot;fig1.1&quot; style=&quot;zoom: 50%;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;若辐射强度$I$与角度$\vec \Omega$无关，则辐射场是各向同性的；若辐射强度$I$与空间位置$\vec r$无关，则辐射场是均匀。当辐射场与物质达到完全的热平衡时，辐射场就是各项同性且均匀的，此时辐射强度由普朗克函数$B(\nu,T)$给出
\(I=B(\nu,T)=\frac{2h\nu^3}{c^2}(e^{h\nu/kT}-1)^{-1}\)
其中$h$为普朗克常量。&lt;/p&gt;

&lt;h4 id=&quot;辐射能量密度&quot;&gt;辐射能量密度&lt;/h4&gt;

&lt;p&gt;辐射能量密度$U$是辐射强度$I$的0次角矩，表示单位体积内辐射场的能量，如下式
\(U=\int_{0}^{\infty}d\nu\int_{4\pi}d\vec \Omega h\nu  f=\frac{1}{c}\int_{0}^{\infty}d\nu\int_{4\pi}d\vec \Omega I\)
若辐射强度$I$由普朗克函数$B(\nu,T)$给出，则能量密度$U_p$为
\(U_p=\frac{1}{c}\int_{0}^{\infty}d\nu\int_{4\pi}d\vec \Omega B = \frac{4\pi}{c}\int_{0}^{\infty}d\nu \frac{2h\nu^3}{c^2}(e^{h\nu/kT}-1)^{-1} \\\)
设$x=h\nu/kT$，则有
\(U_p=\frac{8\pi k^4 T^4}{h^3c^3}\int_{0}^{\infty}\frac{x^3}{(e^x-1)}dx\)
其中积分项为
\(\int_{0}^{\infty}\frac{x^3}{(e^x-1)}dx=\frac{\pi^4}{15}\)
故
\(U_p=\frac{8\pi^5 k^4 T^4}{15h^3c^3}=aT^4,\ a=\frac{8\pi^5 k^4}{15h^3c^3}\)
此时，辐射能量密度也经常写作$U_p=\frac{4\sigma}{c}T^4$，其中$\sigma=ac/4$为斯特藩-玻尔兹曼常数。&lt;/p&gt;

&lt;h4 id=&quot;辐射通量&quot;&gt;辐射通量&lt;/h4&gt;

&lt;p&gt;辐射通量$\vec F$是辐射强度$I$的1次角矩，表示辐射场的能流，如下式
\(\vec F=\int_{0}^{\infty}d\nu\int_{4\pi}d\vec \Omega ch\nu  \vec \Omega f=\int_{0}^{\infty}d\nu\int_{4\pi}\vec \Omega I d\vec \Omega\)
在直角坐标系下，三个方向上的辐射通量分量分别为
\(F_x==\int_{0}^{\infty}d\nu\int_{4\pi} \Omega_x I d\vec \Omega \\
F_y==\int_{0}^{\infty}d\nu\int_{4\pi} \Omega_y I d\vec \Omega \\
F_z==\int_{0}^{\infty}d\nu\int_{4\pi} \Omega_z I d\vec \Omega \\\)
对于各向同性的辐射场，辐射通量显然为0，即各个方向上面都没有能量流。&lt;/p&gt;

&lt;h4 id=&quot;辐射压力张量&quot;&gt;辐射压力张量&lt;/h4&gt;

&lt;p&gt;辐射压力张量$\Bbb{P}$是辐射强度$I$的2次角矩。根据气体动理学理论，压力是由通过单位时间通过单位面积的粒子的动量定义的，将此定义应用于辐射场，可得到辐射压力张量，如下式
\(\Bbb{P}=\int_{0}^{\infty}d\nu\int_{4\pi}d\vec \Omega c \frac{h\nu}{c}  \vec \Omega \vec \Omega f=\frac{1}{c}\int_{0}^{\infty}d\nu\int_{4\pi}\vec \Omega \vec \Omega I d\vec \Omega\)
在直角坐标系下，辐射压力的九个分量分别为
\(\begin{cases}
\Bbb{P}_{xx}=\frac{1}{c}\int_{0}^{\infty}d\nu\int_{4\pi} \Omega_x \Omega_x I d\vec \Omega,\ 
\Bbb{P}_{xy}=\frac{1}{c}\int_{0}^{\infty}d\nu\int_{4\pi} \Omega_x \Omega_y I d\vec \Omega,\ 
\Bbb{P}_{xz}=\frac{1}{c}\int_{0}^{\infty}d\nu\int_{4\pi} \Omega_x \Omega_z I d\vec \Omega\\ 
\Bbb{P}_{yx}=\frac{1}{c}\int_{0}^{\infty}d\nu\int_{4\pi} \Omega_y \Omega_x I d\vec \Omega,\ 
\Bbb{P}_{yy}=\frac{1}{c}\int_{0}^{\infty}d\nu\int_{4\pi} \Omega_y \Omega_y I d\vec \Omega,\ 
\Bbb{P}_{yz}=\frac{1}{c}\int_{0}^{\infty}d\nu\int_{4\pi} \Omega_y \Omega_z I d\vec \Omega\\ 
\Bbb{P}_{zx}=\frac{1}{c}\int_{0}^{\infty}d\nu\int_{4\pi} \Omega_z \Omega_x I d\vec \Omega,\ 
\Bbb{P}_{zy}=\frac{1}{c}\int_{0}^{\infty}d\nu\int_{4\pi} \Omega_z \Omega_y I d\vec \Omega,\ 
\Bbb{P}_{zz}=\frac{1}{c}\int_{0}^{\infty}d\nu\int_{4\pi} \Omega_z \Omega_z I d\vec \Omega
\end{cases}\)
可以看出辐射压力张量是对称的，即$\Bbb{P}&lt;em&gt;{ij}=\Bbb{P}&lt;/em&gt;{ji}$。另外，由于$\Omega_x^2+\Omega_y^2+\Omega_z^2=1$，对任意辐射场，则有
\(\Bbb{P}_{xx}+\Bbb{P}_{yy}+\Bbb{P}_{zz}=\frac{1}{c}\int_{0}^{\infty}d\nu\int_{4\pi}d\vec \Omega I=U\)
平均压力为
\(\bar{\Bbb{P}}=\frac{1}{3}(\Bbb{P}_{xx}+\Bbb{P}_{yy}+\Bbb{P}_{zz})=\frac{1}{3}U\)
对于各向同性的辐射场，辐射压力张量的非对角元素为0，且对角元素等于平均压力$\bar{\Bbb{P}}$，即
\(\begin{cases}
\Bbb{P}_{ij}=\frac{1}{3}U,\ i=j\\
\Bbb{P}_{ij}=0,\ i\ne j
\end{cases}\)
对于普朗克分布的辐射强度，则有
\(\Bbb{P}_{xx}=\Bbb{P}_{yy}=\Bbb{P}_{zz}=\frac{1}{3}aT^4\)
对于与各向同性相反的一个极端情况，如果某时刻某空间点上的所有光子的运动方向均指向x方向，即辐射强度$I$在方向上的分布为在x方向上的Drac函数，则有
\(\begin{cases}
\Bbb{P}_{xx}=U\\
\Bbb{P}_{ij}=0,\ i\ne x,\ j\ne x
\end{cases}\)&lt;/p&gt;

&lt;h3 id=&quot;辐射场与物质的相互作用&quot;&gt;辐射场与物质的相互作用&lt;/h3&gt;

&lt;p&gt;辐射场（光子）与物质的相互作用有三种方式：吸收，散射，发射。&lt;/p&gt;

&lt;h4 id=&quot;吸收过程&quot;&gt;吸收过程&lt;/h4&gt;

&lt;p&gt;当光子穿过物质的时候，有一定的概率被物质吸收而消失。可以用宏观吸收系数$\sigma_a(\vec r,\nu,t)$来描述光子被物质吸收的过程，当光子在物质中运动距离为$ds$时，则光子被吸收的概率为
\(\sigma_a(\vec r,\nu,t)ds\)
其中吸收系数$\sigma_a$只依赖与光子的频率$\nu$以及物质的状态参数，而物质的状态参数又与空间位置$\vec r$和时间$t$有关，而通常假设光子被物质吸收的概率与其运动方向无关，表示光子被吸收前平均经过的距离。宏观吸收系数也经常被表$\sigma_a$示成为
\(\sigma_a(\nu)=\rho\chi_a(\nu) \\
\sigma_a(\nu)=n\mu_a(\nu)\)
其中，$\rho$是物质的质量密度，$\chi_a(\nu)$被称为质量吸收系数，$n$是物质的粒子数密度，$\mu_a(\nu)$被称为微观吸收系数。&lt;/p&gt;

&lt;h4 id=&quot;散射过程&quot;&gt;散射过程&lt;/h4&gt;

&lt;p&gt;与吸收过程类似，可以定义出光子的散射系数$\sigma_s(\vec r,\nu,t)$来描述光子被物质吸收的过程，当光子在物质中运动距离为$ds$时，则光子被散射的概率为
\(\sigma_s(\vec r,\nu,t)ds\)
并且常假设光子被物质散射的概率与其运动方向无关。光子被物质散射之后其频率与运动方向均发生改变，即$(\nu’ \rightarrow \nu,\vec \Omega’ \rightarrow \vec\Omega)$，可以定义微分散射系数$\sigma_s(\nu’\rightarrow\nu,\vec\Omega’\rightarrow\vec\Omega)$来描述散射过程的概率，若假设散射过程的概率仅与散射角有关而与散射前后的光子运动方向有关，则微分散射系数可以表示成$\sigma_s(\nu’\rightarrow\nu,\vec\Omega’\cdot\vec\Omega)$。光子在物质中运动距离为$ds$，频率被从$\nu’$散射到以$\nu$为中心的频率区间$d\nu$，方向被从$\Omega’$散射到以$\Omega$为中心的立体角微元$d\vec \Omega$的概率为
\(\sigma_s(\nu'\rightarrow\nu,\vec\Omega'\cdot\vec\Omega)d\nu d\vec\Omega ds\)
对微分散射系数$\sigma_s(\nu’\rightarrow\nu,\vec\Omega’\cdot\vec\Omega)$在频率和立体角上积分可以得到散射系数$\sigma_s(\nu’)$，即
\(\sigma_s(\nu')=\int_{0}^{\infty}d\nu \int_{4\pi}d\vec\Omega\sigma_s(\nu'\rightarrow\nu,\vec\Omega'\cdot\vec\Omega)\)
若选择发生散射前的角度$\vec \Omega’$为$z$轴，散射角度用$\mu_0=\vec\Omega’\cdot\vec\Omega=\cos\theta$表示，则上式可表示成
\(\sigma_s(\nu')=2\pi\int_{0}^{\infty}d\nu \int_{-1}^{1}d\mu_0\sigma_s(\nu'\rightarrow\nu,\mu_0)\)
微分散射系数$\sigma_s(\nu’\rightarrow\nu,\vec\Omega’\cdot\vec\Omega)$可以被表示成为以下的形式
\(\sigma_s(\nu'\rightarrow\nu,\vec\Omega'\cdot\vec\Omega)=\sigma_s(\nu')K(\nu'\rightarrow\nu,\vec\Omega'\cdot\vec\Omega)\)
其中$K(\nu’\rightarrow\nu,\vec\Omega’\cdot\vec\Omega)$为散射核,表示状态为$(\nu’,\vec \Omega’)$光子已经被散射的前提下，散射后状态变为$(\nu,\vec \Omega)$的条件概率，散射核对频率$\nu$和空间角$\vec \Omega$的积分为1，即
\(\int_{0}^{\infty}d\nu \int_{4\pi}d\vec\Omega K(\nu'\rightarrow\nu,\vec\Omega'\cdot\vec\Omega)=1\)
若散射后，光子频率不发生改变，即散射是相干的，则散射核可以表示成包含一个Drac函数的形式，如下
\(K(\nu'\rightarrow\nu,\vec\Omega'\cdot\vec\Omega)=K(\vec\Omega'\cdot\vec\Omega)\delta(\nu'-\nu)\)
此时$K(\vec\Omega’\cdot\vec\Omega)$对空间角$\vec \Omega$的积分为1，即
\(\int_{4\pi}d\vec\Omega K(\vec\Omega'\cdot\vec\Omega)=2\pi\int_{-1}^{1}d\mu_0 K(\mu_0)=1\)
若散射核$K(\nu’\rightarrow\nu,\vec\Omega’\cdot\vec\Omega)$与散射角无关，光子被散射到任意方向的概率都相等，即散射是各向同性的，则散射核可以表示成
\(K(\nu'\rightarrow\nu,\vec\Omega'\cdot\vec\Omega)=\frac{1}{4\pi}K(\nu'\rightarrow\nu)\)
此时$K(\nu’\rightarrow\nu)$对的对频率$\nu$积分为1，即
\(\int_{0}^{\infty}d\nu  K(\nu'\rightarrow\nu)=1\)
若散射既是相干的，也是各项同性的，则散射核可以表示成最简单形式，如下
\(K(\nu'\rightarrow\nu,\vec\Omega'\cdot\vec\Omega)=\frac{1}{4\pi}\delta(\nu'-\nu)\)
对于很多问题，上式是对散射核的一种合理的近似。&lt;/p&gt;

&lt;p&gt;综合考虑吸收与散射的过程，可以得到光子与物质的总相互作用系数$\sigma(\vec r,\nu,t)$如下
\(\sigma(\nu)=\sigma_a(\nu)+\sigma_s(\nu)\)
通过相互作用系数$\sigma(\nu)$可以得到光子与物质相互作用的平均自由程$\lambda$。若有一束总数为$N_0$光子，经过$ds$距离后，与物质相互作用（吸收和散射）的光子的数量为
\(dN=-N\sigma ds\)
即
\(\frac{dN}{ds}=-N\sigma\)
若$\sigma$与$s$无关，光子总数$N$关于运动距离$s$的解为
\(N(s)=N_0e^{-\sigma s}\)
可以得到这束光子的平均自由程如下
\(\lambda(\nu)=\bar{s}(\nu)=\frac{\int_{0}^{\infty}sN(s)\sigma ds}{N_0}=\frac{1}{\sigma(\nu)}\)
于此类似，可以得到吸收平均自由程$\lambda_a=1/\sigma_a$和散射平均自由程$\lambda_s=1/\sigma_s$，并且由$\sigma=\sigma_a+\sigma_s$可知
\(\lambda^{-1}=\lambda_a^{-1}+\lambda_s^{-1}\)&lt;/p&gt;

&lt;h4 id=&quot;发射过程&quot;&gt;发射过程&lt;/h4&gt;

&lt;p&gt;物质除可吸收光子，或者是光子的频率和角度发生改变之外，还可自发的发射光子。物质自发发射光子的过程可以由函数$q(\vec r,\nu,t)$描述，位于空间点$\vec r$的物质在单位时间单位体积内发射出的，在以$\nu$为中心的频率区间$d\nu$内的，在以$\vec \Omega$为中心的立体角微元$d\vec \Omega$内的光子数为
\(q(\vec r,\nu,t)d\nu d\vec\Omega\)
这里假设物质发射光子过程与角度无关，即向各个方向发射光子的概率相同。&lt;/p&gt;

&lt;h2 id=&quot;辐射输运方程&quot;&gt;辐射输运方程&lt;/h2&gt;

&lt;h3 id=&quot;辐射输运方程的原始形式&quot;&gt;辐射输运方程的原始形式&lt;/h3&gt;

&lt;h4 id=&quot;光子分布函数方程&quot;&gt;光子分布函数方程&lt;/h4&gt;

&lt;p&gt;类比于玻尔兹曼方程，可得到的光子分布函数方程如下
\(\frac{\partial f(\nu,\vec \Omega)}{\partial t}+c\vec\Omega\cdot\nabla f(\nu,\vec\Omega)=q(v)-c\sigma_a(\nu) f(\nu,\vec\Omega) \\
+c\int_{0}^{\infty}d\nu'\int_{4\pi}d\vec\Omega'[\sigma_s(\nu'\rightarrow\nu,\vec\Omega'\cdot\vec\Omega)f(\nu',\vec\Omega')-\sigma_s(\nu\rightarrow\nu',\vec\Omega\cdot\vec\Omega')f(\nu,\vec\Omega)]\)&lt;/p&gt;
&lt;h4 id=&quot;辐射强度方程&quot;&gt;辐射强度方程&lt;/h4&gt;

&lt;p&gt;对光子分布函数方程等号两端乘光子的能量$h\nu$可得到辐射强度方程如下
\(\frac{1}{c}\frac{\partial I(\nu,\vec \Omega)}{\partial t}+\vec\Omega\cdot\nabla I(\nu,\vec\Omega)=j(v)-\sigma_a(\nu) I(\nu,\vec\Omega) \\
+\int_{0}^{\infty}d\nu'\int_{4\pi}d\vec\Omega'\frac{\nu}{\nu'}\sigma_s(\nu'\rightarrow\nu,\vec\Omega'\cdot\vec\Omega)I(\nu',\vec\Omega')\\
-\int_{0}^{\infty}d\nu'\int_{4\pi}d\vec\Omega'\sigma_s(\nu\rightarrow\nu',\vec\Omega\cdot\vec\Omega')I(\nu,\vec\Omega)\)
其中$j(\vec r,\nu,t)=h\nu q(\vec r,\nu,t)$为单位体积内电子发射功率。考虑受激辐射，电子发射和散射将受到已经位于反应终态的光子数的影响而增强，增强因子大小为$1+\frac{c^2I}{2h\nu^3}$，则辐射强度方程为
\(\frac{1}{c}\frac{\partial I(\nu,\vec \Omega)}{\partial t}+\vec\Omega\cdot\nabla I(\nu,\vec\Omega)=j(v)[1+\frac{c^2I(\nu,\vec \Omega)}{2h\nu^3}]-\sigma_a(\nu) I(\nu,\vec\Omega) \\
+\int_{0}^{\infty}d\nu'\int_{4\pi}d\vec\Omega'\frac{\nu}{\nu'}\sigma_s(\nu'\rightarrow\nu,\vec\Omega'\cdot\vec\Omega)I(\nu',\vec\Omega')[1+\frac{c^2I(\nu,\vec \Omega)}{2h\nu^3}]\\
-\int_{0}^{\infty}d\nu'\int_{4\pi}d\vec\Omega' \sigma_s(\nu\rightarrow\nu',\vec\Omega\cdot\vec\Omega')I(\nu,\vec\Omega)[1+\frac{c^2I(\nu',\vec \Omega')}{2h\nu^{'3}}]\)&lt;/p&gt;

&lt;h3 id=&quot;输运方程的定解条件&quot;&gt;输运方程的定解条件&lt;/h3&gt;

&lt;h4 id=&quot;边界条件&quot;&gt;边界条件&lt;/h4&gt;

&lt;p&gt;\(I(\vec r^s,\nu,\vec\Omega,t)=\Gamma(\vec r^s,\nu,\vec\Omega,t),\quad \vec n\cdot\vec \Omega&amp;lt;0\)
若边界无辐射能流，则为真空边界条件
\(I(\vec r^s,\nu,\vec\Omega,t)=0,\quad \vec n\cdot\vec \Omega&amp;lt;0\)&lt;/p&gt;
&lt;h4 id=&quot;初始条件&quot;&gt;初始条件&lt;/h4&gt;

&lt;p&gt;\(I(\vec r,\nu,\vec\Omega,0)=\Lambda(\vec r,\nu,\vec\Omega)\)&lt;/p&gt;
&lt;h2 id=&quot;局域热平衡下的辐射输运方程&quot;&gt;局域热平衡下的辐射输运方程&lt;/h2&gt;

&lt;h3 id=&quot;基尔霍夫定律&quot;&gt;基尔霍夫定律&lt;/h3&gt;

&lt;p&gt;辐射输运方程的源项在局域热平衡的条件下可以大大简化。当系统处于完全热力学平衡时，散射项相互抵消，吸收项相互抵消，吸收项目等于发射项，这时辐射的强度是普朗克分布，由系统的温度决定，即
\(j(v)[1+\frac{c^2B(\nu,T)}{2h\nu^3}]=\sigma_a(\nu) B(\nu,T)\)
整理可以得到
\(j(v)=\frac{\sigma_a(\nu) B(\nu,T)}{1+\frac{c^2}{2h\nu^3}B(\nu,T)}\)
这就是所为的基尔霍夫定律。&lt;/p&gt;

&lt;h3 id=&quot;局域热平衡带来的简化&quot;&gt;局域热平衡带来的简化&lt;/h3&gt;

&lt;p&gt;这就得到了物质的发射率，这对于辐射场与物质没有完全热力学平衡的情况仍然是成立的，进一步忽略掉散射过程，则可以得到局域热零平衡下的辐射输运方程为
\(\frac{1}{c}\frac{\partial I(\nu,\vec \Omega)}{\partial t}+\vec\Omega\cdot\nabla I(\nu,\vec\Omega)=\frac{\sigma_a(\nu) B(\nu,T)}{1+\frac{c^2}{2h\nu^3}B(\nu,T)}[1+\frac{c^2I(\nu,\vec \Omega)}{2h\nu^3}]-\sigma_a(\nu) I(\nu,\vec\Omega)\)
化简可得
\(\frac{1}{c}\frac{\partial I(\nu,\vec \Omega)}{\partial t}+\vec\Omega\cdot\nabla I(\nu,\vec\Omega)=\sigma'_a(\nu) B(\nu,T)-\sigma'_a(\nu) I(\nu,\vec\Omega)\)
其中
\(\sigma'_a(\nu)=\frac{\sigma_a(\nu)}{1+\frac{c^2}{2h\nu^3}B(\nu,T)}=(1-e^{-\frac{h\nu}{kT}})\sigma_a(\nu)\)
人们更喜欢使用$\rho\chi’_a$的形式，即
\(\frac{1}{c}\frac{\partial I(\nu,\vec \Omega)}{\partial t}+\vec\Omega\cdot\nabla I(\nu,\vec\Omega)=\rho\chi'_a(\nu) B(\nu,T)-\rho\chi'_a(\nu) I(\nu,\vec\Omega)\)
后面讨论的各种近似描述就是建立在这个简化版本的辐射输运方程的基础之上的。&lt;/p&gt;

&lt;h2 id=&quot;辐射输运方程的扩散近似&quot;&gt;辐射输运方程的扩散近似&lt;/h2&gt;

&lt;h3 id=&quot;扩散近似&quot;&gt;扩散近似&lt;/h3&gt;

&lt;p&gt;辐射强度在几乎是各向同性的情况下，可以将辐射强度近似表示成为
\(I(\vec r,\nu,\vec \Omega,t)=\frac{1}{4\pi}I_0(\vec r,\nu,t)+\frac{3}{4\pi}\vec\Omega\cdot\vec I_1(\vec r,\nu,t)\)
根据数学关系式
\(\int d\vec\Omega=4\pi,\ \int\vec \Omega d \vec \Omega=0, \int \vec \Omega \vec \Omega\cdot\vec A d\vec \Omega=\frac{4\pi}{3}\vec A\)
可得到
\(I_0(\vec r,\nu,t)=\int_{4\pi} d\vec\Omega I(\vec r,\nu,\vec \Omega,t)=cU(\vec r,\nu,t)\\
\vec I_1(\vec r,\nu,t)=\int_{4\pi}\vec\Omega d\vec\Omega I(\vec r,\nu,\vec \Omega,t)=\vec F\)
即$I_o/c$和$\vec I_1$分别代表频率$\nu$上辐射场的能量密度和能流。将上述表达式代入
\(\frac{1}{c}\frac{\partial I(\nu,\vec \Omega)}{\partial t}+\vec\Omega\cdot\nabla I(\nu,\vec\Omega)=\rho\chi'_a(\nu) B(\nu,T)-\rho\chi'_a(\nu) I(\nu,\vec\Omega)\)
对立体角积分可得
\(\frac{1}{c}\frac{\partial I_0}{\partial t}+\nabla\cdot\vec I_1(\nu)=\rho\chi'_a(\nu) [4\pi B(\nu,T)-I_0(\nu)]\)
乘以$\vec\Omega$对立体角积分可得
\(\frac{1}{c}\frac{\partial \vec I_1(\nu)}{\partial t}+\frac{1}{3}\nabla I_0(\nu)=-\rho\chi'_a(\nu) \vec I_1(\nu)\)
将上式中$\frac{1}{c}\frac{\partial \vec I_1(\nu)}{\partial t}$忽略，即可得到
\(\vec I_1(\nu)=-\frac{1}{3\rho\chi'_a(\nu)}\nabla I_0(\nu)\)
在将此式带入$I_0$的方程得
\(\frac{1}{c}\frac{\partial I_0}{\partial t}-\nabla\cdot[\frac{1}{3\rho\chi'_a(\nu)}\nabla I_0(\nu)]=\rho\chi'_a(\nu) [4\pi B(\nu,T)-I_0(\nu)]\)&lt;/p&gt;

&lt;h3 id=&quot;平衡扩散近似&quot;&gt;平衡扩散近似&lt;/h3&gt;

&lt;p&gt;当辐射场与电子达到完全得局域热平衡后，电子温度与辐射场温度相等，电子与辐射扩散方程得右端项为0，即
\(I_0(\nu)=4\pi B(\nu,T)=\frac{8\pi h\nu^3}{c^2}(e^{h\nu/kT}-1)^{-1}\)
将其带入扩散近似方程则有
\(\frac{4\pi}{c}\frac{\partial B(\nu,T)}{\partial t}-\nabla\cdot[\frac{4\pi}{3\rho\chi'_a(\nu)}\nabla B(\nu,T)]=0\)
此时我们可以得到辐射场能量密度
\(E_r=U_p=\frac{4\pi}{c}\int_0^\infty B(\nu,T)d\nu=aT^4 \\\)
辐射场能流
\(\vec F=-\frac{4\pi}{3}\int_0^\infty \frac{1}{\rho\chi'_a(\nu)}\frac{\partial B(\nu,T)}{\partial T}d\nu\nabla T\\
=-\frac{4\pi}{3}\nabla T\frac{\int_0^\infty \frac{1}{\rho\chi'_a(\nu)}\frac{\partial B(\nu,T)}{\partial T}d\nu}{\int_0^\infty \frac{\partial B(\nu,T)}{\partial T}d\nu}
\int_0^\infty \frac{\partial B(\nu,T)}{\partial T}d\nu \\
=-\frac{4}{3}acl_RT^3\nabla T
=\frac{1}{3}l_Rc\nabla E_r\\
E_r=aT^4\\\)
其中$l_R$为Rosseland平均自由程如下
\(l_R=\frac{\int_0^\infty \frac{1}{\rho\chi'_a(\nu)}\frac{\partial B(\nu,T)}{\partial T}d\nu}{\int_0^\infty \frac{\partial B(\nu,T)}{\partial T}d\nu}\)
则辐射扩散方程为
\(\frac{\partial E_r}{\partial t}=\nabla\cdot(\frac{1}{3}l_Rc\nabla E_r)\\\)
或
\(\frac{\partial aT^4}{\partial t}=\nabla\cdot(\kappa_r\nabla T)\\
\kappa_r=\frac{4}{3}acl_RT^3\)&lt;/p&gt;

&lt;h3 id=&quot;非平衡扩散近似&quot;&gt;非平衡扩散近似&lt;/h3&gt;

&lt;p&gt;若辐射场达到局域热平衡，但与电子为达到平衡，即$T_r\ne T_e$，则有
\(I_0(\nu)=4\pi B(\nu,T_r)=\frac{8\pi h\nu^3}{c^2}(e^{h\nu/kT}-1)^{-1}\)
将其带入辐射扩散方程，可得
\(\frac{4\pi}{c}\frac{\partial B(\nu,Tr)}{\partial t}-\nabla\cdot[\frac{4\pi}{3\rho\chi'_a(\nu,T_e)}\nabla B(\nu,T_r)]=4\pi\rho[\chi’_a (\nu,T_e)B(\nu,T_e)-\chi’_a(\nu,T_e) B(\nu,T_r)]\)
对其关于$\nu$进行积分，&lt;/p&gt;

&lt;p&gt;其中辐射能量密度为
\(E_r(T_r)=U_p(T_r)=\frac{4\pi}{c}\int_0^\infty B(\nu,T_r)d\nu=aT_r^4 \\\)
辐射能流为
\(\vec F=-\frac{4\pi}{3}\int_0^\infty \frac{1}{\rho\chi'_a(\nu，T_e)}\frac{\partial B(\nu,T_r)}{\partial T_r}d\nu\nabla T_r\\
=-\frac{4\pi}{3}\nabla T_r\frac{\int_0^\infty \frac{1}{\rho\chi'_a(\nu,T_e)}\frac{\partial B(\nu,T_r)}{\partial T_r}d\nu}{\int_0^\infty \frac{\partial B(\nu,T_r)}{\partial T_r}d\nu}
\int_0^\infty \frac{\partial B(\nu,T_r)}{\partial T_r}d\nu \\
=-\frac{4}{3}acl_R^{}T_r^3\nabla T_r\\
=\frac{1}{3}l_Rc\nabla E_r\\
E_r=aT_r^4\\\)
其中$l_R(T_e,T_r)$为Rosseland平均自由程如下
\(l_R(T_e,T_r)=\frac{\int_0^\infty \frac{1}{\rho\chi'_a(\nu,T_e)}\frac{\partial B(\nu,T_r)}{\partial T_r}d\nu}{\int_0^\infty \frac{\partial B(\nu,T_r)}{\partial T_r}d\nu}\)
电子和辐射场能量交换
\(4\pi\rho\int_0^\infty[\chi’_a(\nu,T_e) B(\nu,T_e)-\chi’_a (\nu,T_e)B(\nu,T_r)]d\nu\\
=4\pi\rho\frac{\int_0^\infty\chi’_a(\nu,T_e) B(\nu,T_e)d\nu}{\int_0^\infty B(\nu,T_e)d\nu}\int_0^\infty B(\nu,T_e)d\nu-
4\pi\rho\frac{\int_0^\infty\chi’_a(\nu,T_e) B(\nu,T_r)d\nu}{\int_0^\infty B(\nu,T_r)d\nu}\int_0^\infty B(\nu,T_r)d\nu\\
=c\frac{\int_0^\infty\rho\chi’_a(\nu,T_e) B(\nu,T_e)d\nu}{\int_0^\infty B(\nu,T_e)d\nu}aT_e^4-
c\frac{\int_0^\infty\rho\chi’_a(\nu,T_e) B(\nu,T_r)d\nu}{\int_0^\infty B(\nu,T_r)d\nu}aT_r^4\\
=cl_P^{-1}(T_e)aT_e^4-cl_P^{-1}(T_e,T_r)aT_r^4\)
其中
\(l_P^{-1}(T_e)=\frac{\int_0^\infty\rho\chi’_a(\nu,T_e) B(\nu,T_e)d\nu}{\int_0^\infty B(\nu,T_e)d\nu}\\
l_P^{-1}(T_e,T_r)=\frac{\int_0^\infty\rho\chi’_a(\nu,T_e) B(\nu,T_r)d\nu}{\int_0^\infty B(\nu,T_r)d\nu}\)
为Planck平均自由程。&lt;/p&gt;

&lt;p&gt;因此扩散方程如下
\(\frac{\partial E_r}{\partial t}-\nabla\cdot[\frac{cl_R(T_e,T_r)}{3}\nabla E_r]=
cl_P^{-1}(T_e)aT_e^4-cl_P^{-1}(T_e,T_r)E_r\)
Rosseland平均自由程$l_R(T_e,T_r)$和Planck平均自由程$l_P^{-1}(T_e,T_r)$都是$T_e$和$T_r$的函数，通常取如下近似
\(l_R(T_e,T_r)\approx l_R(T_e)=\frac{\int_0^\infty \frac{1}{\rho\chi'_a(\nu,T_e)}\frac{\partial B(\nu,T_e)}{\partial T_e}d\nu}{\int_0^\infty \frac{\partial B(\nu,T_e)}{\partial T_e}d\nu}\\
l_P^{-1}(T_e,T_r)\approx l_P^{-1}(T_e)=\frac{\int_0^\infty\rho\chi’_a(\nu,T_e) B(\nu,T_e)d\nu}{\int_0^\infty B(\nu,T_e)d\nu}\\\)
因此扩散方程可以写成
\(\frac{\partial E_r}{\partial t}-\nabla\cdot(D_r\nabla E_r)=
\tau_{er}^{-1}(aT_e^4-E_r)\\
D_r=\frac{cl_R(T_e)}{3}\\
\tau_{er}^{-1}=cl_P^{-1}(T_e)\)&lt;/p&gt;

&lt;h3 id=&quot;多群扩散近似&quot;&gt;多群扩散近似&lt;/h3&gt;

&lt;p&gt;考虑辐射输运方程
\(\frac{1}{c}\frac{\partial I(\nu,\vec \Omega)}{\partial t}+\vec\Omega\cdot\nabla I(\nu,\vec\Omega)=\rho\chi'_a(\nu) B(\nu,T)-\rho\chi'_a(\nu) I(\nu,\vec\Omega)\)
将频率范围$[0,\infty]$按照$0=\nu_0&amp;lt;\nu_1&amp;lt;\nu_2&amp;lt;…&amp;lt;\nu_{n-1}&amp;lt;\nu_g=\infty$分成n个群，其中第$g$个群区间为$[\nu_{g-1},\nu_g]$。&lt;/p&gt;

&lt;p&gt;定义第$g$群的辐射强度定义为
\(I_g(\vec \Omega)=\int_{\nu_{g-1}}^{\nu_g}I(\nu,\vec \Omega)d\nu,\ \ 1\le g\le n\)
将辐射输运方程对第g群积分，可得
\(\frac{1}{c}\frac{\partial I_g(\vec \Omega)}{\partial t}+\vec\Omega\cdot\nabla I_g(\vec\Omega)=\rho\chi'_{ag}[ B_g(T)-I_g(\vec\Omega)]\)
其中
\(\rho\chi'_{ag}=\frac{\int_0^\infty \rho\chi'_{ag}[B(\nu,T)-I(\nu,\vec\Omega)]d\nu}{\int_0^\infty[B(\nu,T)-I(\nu,\vec\Omega)]d\nu}\)
对第$g$个群的辐射输运方程进行扩散近似
\(I_g(\vec \Omega)=\frac{1}{4\pi}I_{0g}+\frac{3}{4\pi}\vec\Omega\cdot\vec I_{1g}\)
假设$\rho\chi’&lt;em&gt;{ag}$与$\vec \Omega$无关，在对第$g$群关于立体角进行积分，可得
\(\frac{1}{c}\frac{\partial I_{0g}}{\partial t}+\nabla\cdot\vec I_{1g}=\rho\chi'_a(\nu) [4\pi B_g(T)-I_{0g}]\)
对第$g$群乘以$\vec \Omega$关于立体角进行积分
\(\frac{1}{c}\frac{\partial \vec I_{1g}}{\partial t}+\frac{1}{3}\nabla I_{0g}=-\rho\chi'_a(\nu) \vec I_{1g}\)
将上式中$\frac{1}{c}\frac{\partial \vec I&lt;/em&gt;{1g}}{\partial t}$忽略，即可得到
\(\vec I_{1g}=-\frac{1}{3\rho\chi'_{ag}}\nabla I_{0g}\)
将其带入$I_{0g}$方程
\(\frac{1}{c}\frac{\partial I_{0g}}{\partial t}-\nabla\cdot[\frac{1}{3\rho\chi'_{ag}}\nabla I_{0g}]=\rho\chi'_{ag} [4\pi B_g(T)-I_{0g}]\)
根据定义$I_{0g}=cE_g$，上述方程应为
\(\frac{\partial E_{g}}{\partial t}-\nabla\cdot[\frac{c}{3\rho\chi'_{ag}}\nabla E_{g}]=\rho\chi'_{ag}c [\frac{4\pi}{c} B_g(T)-E_{g}]\)
其中
\(\frac{4\pi}{c} B_g(T)=\int_{\nu_{g-1}}^{\nu_g} \frac{8\pi h\nu^3}{c^3}(e^{\frac{h\nu}{kT}}-1)^{-1} d\nu\)
设$x=\frac{h \nu}{kT}$，则有
\(\frac{4\pi}{c} B_g(T)=\frac{15}{\pi^4}aT^4\int_{x_{g-1}}^{x_g}\frac{x^3}{e^x-1}dx\\
=\frac{15}{\pi^4}b_gaT^4\\\)
其中$b_g$为
\(b_g=\int_{x_{g-1}}^{x_g}\frac{x^3}{e^x-1}dx,\ \  x_{g-1}=\frac{hv_{g-1}}{kT},\  x_{g}=\frac{hv_{g}}{kT}\)
对于方程中的两处$\rho\chi’_{ag}$通常分别Rosseland平均和Planck平均，即
\(l_{Rg}=\frac{\int_{\nu_{g-1}}^{\nu_g} \frac{1}{\rho\chi'_a(\nu)}\frac{\partial B(\nu,T)}{\partial T}d\nu}{\int_{\nu_{g-1}}^{\nu_g} \frac{\partial B(\nu,T)}{\partial T}d\nu}\\
l_{Pg}^{-1}=\frac{\int_{\nu_{g-1}}^{\nu_g}\rho\chi’_a(\nu) B(\nu,T)d\nu}{\int_{\nu_{g-1}}^{\nu_g} B(\nu,T)d\nu}\)
则多群辐射方程为
\(\frac{\partial E_{g}}{\partial t}-\nabla\cdot[\frac{l_{Rg}c}{3}\nabla E_{g}]=\frac{c}{l_{Pg}} [\frac{15}{\pi^4}b_gaT^4-E_{g}]\\\)
进一步化简可得
\(\frac{\partial E_{g}}{\partial t}-\nabla\cdot[D_{rg}\nabla E_{g}]=\tau_{erg}^{-1}[\frac{15}{\pi^4}b_gaT^4-E_{g}]\\
D_{rg}=\frac{l_{Rg}c}{3}\\
\tau_{erg}^{-1}=\frac{c}{l_{Pg}}\)&lt;/p&gt;

&lt;h2 id=&quot;不透明度&quot;&gt;不透明度&lt;/h2&gt;

\[\rho\chi_v^{'ff}=\frac{4\sqrt{2\pi}e^6}{3\sqrt{3}hcm_e^}g_{ff}n_i n_e Z^2T_e^{-\frac{1}{2}}\nu^{-3}(1-e^{-\frac{h\nu}{kt}})\]

&lt;p&gt;其中$g_{ff}$为Gaunt因子，表示量子力学的修正。考虑完全电离的等离子体，$n_i=\rho/(Am_p)$，$n_e=Z\rho/(Am_p)$，$A$为离子的质量数，$Z$为荷电荷数，则有
\(\rho\chi_v^{'ff}=\frac{4\sqrt{2\pi}e^6}{3\sqrt{3}hcm_e^{3/2}m_p^2}g_{ff}\frac{\rho^2Z^3}{A^2}(kT_e)^{-\frac{1}{2}}\nu^{-3}(1-e^{-\frac{h\nu}{kt}})\)
已知
\(B(\nu,T)=\frac{2h\nu^3}{c^2}(e^{h\nu/kT}-1)^{-1}\\
\frac{\partial B(\nu,T)}{\partial T}=\frac{2h^2\nu^4}{c^2kT^2}\frac{e^{h\nu/kT}}{(e^{h\nu/kT}-1)^2}\)&lt;/p&gt;

&lt;h3 id=&quot;rosseland不透明度&quot;&gt;Rosseland不透明度&lt;/h3&gt;

&lt;p&gt;则Rosseland平均自由程为
\(l_R(T)=\frac{\int_0^\infty \frac{1}{\rho\chi'_a(\nu,T)}\frac{\partial B(\nu,T)}{\partial T}d\nu}{\int_0^\infty \frac{\partial B(\nu,T)}{\partial T}d\nu}\\
=\frac{3\sqrt{3}hcm_e^{3/2}m_p^2}{4\sqrt{2\pi}e^6g_{ff}}
\frac{A^2}{\rho^2Z^3}\frac{\int_0^\infty
(kT)^{\frac{1}{2}}\nu^{3}
\frac{e^{h\nu/kT}}{(e^{h\nu/kT}-1)}
\frac{2h^2\nu^4}{c^2kT^2}\frac{e^{h\nu/kT}}{(e^{h\nu/kT}-1)^2}d\nu}
{\int_0^\infty \frac{2h^2\nu^4}{c^2kT^2}
\frac{e^{h\nu/kT}}{(e^{h\nu/kT}-1)^2}d\nu}\\
=\frac{3\sqrt{3}hcm_e^{3/2}m_p^2}{4\sqrt{2\pi}e^6g_{ff}}
\frac{A^2}{\rho^2Z^3}\frac{\int_0^\infty
(kT)^{\frac{1}{2}}\nu^{3}
\frac{e^{h\nu/kT}}{(e^{h\nu/kT}-1)}
\frac{2h^2\nu^4}{c^2kT^2}\frac{e^{h\nu/kT}}{(e^{h\nu/kT}-1)^2}d\nu}
{\frac{8k^4\pi^4T^3}{15c^2h^3}}\\
=\frac{3\sqrt{3}hcm_e^{3/2}m_p^2}{4\sqrt{2\pi}e^6g_{ff}}
\frac{15h^5}{4\pi^4(kT)^{9/2}}
\frac{A^2}{\rho^2Z^3}
\int_0^\infty
\frac{\nu^7e^{2h\nu/kT}}{(e^{h\nu/kT}-1)^3}d\nu\)
若设$x=\frac{h\nu}{kT}$，则有
\(l_R(T)=\frac{3\sqrt{3}hcm_e^{3/2}m_p^2}{4\sqrt{2\pi}e^6g_{ff}}
\frac{15h^5}{4\pi^4(kT)^{9/2}}
(\frac{kT}{h})^8
\frac{A^2}{\rho^2Z^3}
\int_0^\infty
\frac{x^7e^{2x}}{(e^{x}-1)^3}dx\\
=\frac{45\sqrt{3}cm_e^{3/2}m_p^2}{16\sqrt{2}\pi^{9/2}h^2e^6g_{ff}}
\frac{A^2(kT)^{7/2}}{\rho^2Z^3}
\int_0^\infty
\frac{x^7e^{2x}}{(e^{x}-1)^3}dx\)
其中积分项
\(\int_0^\infty
\frac{x^7e^{2x}}{(e^{x}-1)^3}dx=\frac{8}{3}[\pi^6+945\zeta(7)]\)
可得
\(l_R(T)
=\frac{15\sqrt{3}[\pi^6+945\zeta(7)]cm_e^{3/2}m_p^2}{2\sqrt{2}\pi^{9/2}h^2e^6g_{ff}}
\frac{A^2(kT)^{7/2}}{\rho^2Z^3}\)
取$g_{ff}=1.0$，带入物理常量，可得
\(l_R\approx71.71\frac{A^2(kT)^{7/2}}{\rho^2Z^3}\)
其中$l_R$单位是$cm$，$kT$单位是$keV$，$\rho$单位是$g/cm^3$。&lt;/p&gt;

&lt;h3 id=&quot;planck不透明度&quot;&gt;Planck不透明度&lt;/h3&gt;

&lt;p&gt;Planck平均自由程
\(l_P^{-1}(T)=\frac{\int_0^\infty\rho\chi’_a(\nu,T) B(\nu,T)d\nu}{\int_0^\infty B(\nu,T)d\nu}\\
=\frac{4\sqrt{2\pi}e^6g_{ff}}{3\sqrt{3}hcm_e^{3/2}m_p^2}\frac{\rho^2Z^3}{A^2}
\frac{\int_0^\infty (kT_e)^{-\frac{1}{2}}\nu^{-3}(1-e^{-\frac{h\nu}{kt}}) \frac{2h\nu^3}{c^2}(e^{h\nu/kT}-1)^{-1}d\nu}
{\int_0^\infty \frac{2h\nu^3}{c^2}(e^{h\nu/kT}-1)^{-1} d\nu}\\
=\frac{4\sqrt{2\pi}e^6g_{ff}}{3\sqrt{3}hcm_e^{3/2}m_p^2}
\frac{15 h^4}{\pi^4}(kT)^{-9/2}
\frac{\rho^2Z^3}{A^2}
\int_0^\infty e^{-\frac{h\nu}{kT}}d\nu\\
=\frac{4\sqrt{2\pi}e^6g_{ff}}{3\sqrt{3}hcm_e^{3/2}m_p^2}
\frac{15 h^3}{\pi^4}(kT)^{-7/2}
\frac{\rho^2Z^3}{A^2}
\int_0^\infty e^{-x}dx\\
=\frac{20\sqrt{2\pi}h^2e^6g_{ff}}{\sqrt{3}\pi^4cm_e^{3/2}m_p^2}
\frac{\rho^2Z^3}{A^2(kT)^{7/2}}\)
取$g_{ff}=1.0$，带入物理常量，可得
\(l_P^{-1}=0.422\frac{\rho^2Z^3}{A^2(kT)^{7/2}}\)
其中$l_R$单位是$cm$，$kT$单位是$keV$，$\rho$单位是$g/cm^3$。&lt;/p&gt;

&lt;h3 id=&quot;多群扩散中的不透明度&quot;&gt;多群扩散中的不透明度&lt;/h3&gt;

&lt;p&gt;则Rosseland平均自由程为
\(l_{Rg}
=\frac{45\sqrt{3}cm_e^{3/2}m_p^2}{16\sqrt{2}\pi^{9/2}h^2e^6g_{ff}}
\frac{A^2(kT)^{7/2}}{\rho^2Z^3}
\int_{x_{g-1}}^{x_g}
\frac{x^7e^{2x}}{(e^{x}-1)^3}dx\)
Planck平均自由程
\(l_{Pg}^{-1}
=\frac{4\sqrt{2\pi}e^6g_{ff}}{3\sqrt{3}hcm_e^{3/2}m_p^2}
\frac{15 h^3}{\pi^4}
\frac{\rho^2Z^3}{A^2(kT)^{7/2}}
\int_{x_{g-1}}^{x_g} e^{-x}dx\\
=\frac{4\sqrt{2\pi}e^6g_{ff}}{3\sqrt{3}hcm_e^{3/2}m_p^2}
\frac{15 h^3}{\pi^4}
\frac{\rho^2Z^3}{A^2(kT)^{7/2}}
(e^{-x_{g-1}}-e^{-x_{g}})\)&lt;/p&gt;

</description>
        <pubDate>Sun, 10 Jul 2022 00:00:00 +0000</pubDate>
        <link>https://zhang-dehua.github.io/2022/07/radiation/</link>
        <guid isPermaLink="true">https://zhang-dehua.github.io/2022/07/radiation/</guid>
        
        <category>模板标签</category>
        
        
      </item>
    
      <item>
        <title>并行计算-GPU编程</title>
        <description>&lt;h2 id=&quot;标题a&quot;&gt;标题A&lt;/h2&gt;

&lt;p&gt;内容A&lt;/p&gt;

&lt;h2 id=&quot;标题b&quot;&gt;标题B&lt;/h2&gt;

&lt;h3 id=&quot;标题b1&quot;&gt;标题B.1&lt;/h3&gt;

&lt;p&gt;内容2&lt;/p&gt;
</description>
        <pubDate>Sat, 23 Oct 2021 00:00:00 +0000</pubDate>
        <link>https://zhang-dehua.github.io/2021/10/cuda_programing/</link>
        <guid isPermaLink="true">https://zhang-dehua.github.io/2021/10/cuda_programing/</guid>
        
        <category>高性能计算</category>
        
        
      </item>
    
      <item>
        <title>并行计算-MPI编程</title>
        <description>&lt;h2 id=&quot;mpi的基础知识&quot;&gt;MPI的基础知识&lt;/h2&gt;

&lt;p&gt;MPI(Message Passing Interface 11)英文直译就是基于消息传递的用户界面。&lt;/p&gt;

&lt;h3 id=&quot;什么是mpi&quot;&gt;什么是MPI&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;MPI是一个库，而不是一门编程语言。MPI库中包含了可以被Fortran/C调用的一系列子例程和函数，MPI库函数/子例程遵守所有对库函数/子例程的调用规则，和一般的函数/过程没有什么区别 。MPI程序的源代码被C或Fortran的编译器编译之后，再与与MPI库链接，就生成了可执行的MPI程序。&lt;/li&gt;
  &lt;li&gt;MPI是一种标准或规范的代表，而不特指某一个对它的具体实现（如openMPI，MPICH等）。迄今为止所有的并行计算机制造商都提供对MPI的支持 。&lt;/li&gt;
  &lt;li&gt;MPI是一种消息传递的编程模型。最终目的是完成进程间的通信。&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;mpi的目的&quot;&gt;MPI的目的&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;较高的通信性能&lt;/li&gt;
  &lt;li&gt;较好的程序可移植性&lt;/li&gt;
  &lt;li&gt;强大的功能&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;mpi的语言绑定&quot;&gt;MPI的语言绑定&lt;/h3&gt;

&lt;p&gt;目前标准的MPI库都提供Fortran和C的API。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Fortran — 主要用于科学与工程计算&lt;/li&gt;
  &lt;li&gt;C — 主要用于系统和应用程序的开发&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;目前主要的-mpi-实现&quot;&gt;目前主要的 MPI 实现&lt;/h3&gt;

&lt;p&gt;以下两个执行MPI标准的库，均为免费开源。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.mpich.org/&quot;&gt;MPICH&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.open-mpi.org/&quot;&gt;OpemMPI&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;mpi的历史&quot;&gt;MPI的历史&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;1992年 — MPI 1.0版本问世&lt;/li&gt;
  &lt;li&gt;1997年 — MPI 2.0版本出现&lt;/li&gt;
  &lt;li&gt;2012年 — MPI 3.0版本出现&lt;/li&gt;
  &lt;li&gt;2018年  — MPI 4.0版本出现，目前最新版本&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;关于mpi的一些名称和概念&quot;&gt;关于MPI的一些名称和概念&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;进程（ process）&lt;/strong&gt;进程与程序相联， 程序一旦在操作系统中运行即成为进程。进程拥有独立的执行环境（内存、寄存器、程序计数器等），是操作系统中独立存在的可执行的基本程序单位， 串行应用程序编译形成的可执行代码，分为“指令”和“数据”两个部分，并在程序执行时独立地申请和占有内存空间，且所有计算均局限于该内存空间。多个进程可同时存在于单机内同一操作系统，操作系统负责调度分时共享处理机资源（ CPU、内存、存储、外设等），进程间相互独立（内存空间不相交） 。进程间可以相互交换信息， 例如数据交换、同步等待，消息是这些交换信息的基本单位，消息传递是指这些信息在进程间的相互交换，是实现进程间通信的唯一方式 。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;进程组（ process group）&lt;/strong&gt; 指MPI 程序的全部进程集合的一个有序子集，且进程组中每个进程被赋于一个在该组中唯一的序号(rank)， 用于在该组中标识该进程，序号的取值范围是[0,进程数- 1]  。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;通信器（ communicator）&lt;/strong&gt;通信器也叫通信域，可以理解为一类进程的集合即一个进程组，且在该进程组，进程间可以相互通信。任何MPI通信函数均必须在某个通信器内发生。 MPI系统提供省缺的通信器MPI_COMM_WORLD，所有启动的MPI进程通过调用函数MPI_Init()包含在该通信器内；各进程通过函数
MPI_Comm_size()获取通信器包含的的MPI进程个数。MPI系统在一个MPI程序运行时会自动创建两个通信器：一个称为MPI_COMM_WORLD，它包含 MPI 程序中所有进程，另一个称为 MPI_COMM_SELF，它指单个进程自己所构成的通信器。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;进程序号（ rank）&lt;/strong&gt; 用来在一个进程组或通信器中标识一个进程，MPI程序中的进程由进程组或通信器序号唯一确定， 序号相对于进程组或通信器而言（假设np个处理器，标号0…np-1）。同一个进程在不同的进程组或通信器中可以有不同的序号，进程的序号是在进程组或通信器被创建时赋予的。MPI 系统提供了一个特殊的进程序号MPI_PROC_NULL，它代表空进程(不存在的进程)， 与MPI_PROC_NULL 间的通信实际上没有任何作用  。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;消息（ message）&lt;/strong&gt; 分为数据（ data）和包装（ envelope） 两个部分。 包装由接收进程序号/发送进程序号、消息标号和通信器三部分组成，数据包含用户将要传递的内容 。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;通信（ communication）&lt;/strong&gt; 指在进程之间进行消息的收发、同步等操作。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;MPI对象&lt;/strong&gt; MPI系统内部定义的数据结构，包括数据类型（如MPI_INT）、通信器（ MPI_Comm）、通信请求（ MPI_Request）等，它们对用户不透明。在Fortran语言中，所有MPI对象均必须说明为整型变量INTEGER 。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;MPI联接器（ handles&lt;/strong&gt;） 联接MPI对象的具体变量，用户可以通过它访问和参与相应MPI对象的具体操作。例如， MPI系统内部提供的通信器MPI_COMM_WORLD。在Fortran语言中，所有MPI联接器均必须说明为整型变量INTEGER。&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;一个最简单的mpi程序-hello-world&quot;&gt;一个最简单的MPI程序 Hello world!&lt;/h2&gt;

&lt;p&gt;运行一个MPI程序的过程可以概括为：首先，将MPI程序源代码编译，与MPI库函数/子例程链接之后生成可执行的MPI程序；接下来，向操作系统提交MPI可执行程序，操作系统再将可执行程序复制N份交给N个处理器；然后，每个处理器各自运行自己的MPI程序，运行过程会调用MPI库函数子例程，得到该节点上的进程my_id，根据进程号程序执行不同的任务，并适时地进行不同进程间的消息传递，以完成并行的任务。&lt;/p&gt;

&lt;h3 id=&quot;hello-world-mpi程序的fortran语言实现&quot;&gt;Hello world MPI程序的Fortran语言实现&lt;/h3&gt;

&lt;div class=&quot;language-fortran highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;PROGRAM&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hello_world&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! or the older form: INCLUDE ’mpif.h’ !引入头文件&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;USE&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! 调用MPI库&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;IMPLICIT&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;NONE&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;INTEGER&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;proc_num&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ierr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name_len&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;CHARACTER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;LEN&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_MAX_PROCESSOR_NAME&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;processor_name&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; 
    &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;!MPI_MAX_PROCESSOR_NAME是MPI预定义的宏，即MPI所允许的机器名字的最大长度&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;CALL&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_INIT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ierr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;!MPI初始化&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;CALL&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_COMM_RANK&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_COMM_WORLD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ierr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;!得到当前正在运行的进程的标识号&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;CALL&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_COMM_SIZE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_COMM_WORLD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;proc_num&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ierr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;!得到所有参加运算的进程的个数&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;CALL&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_GET_PROCESSOR_NAME&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;processor_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name_len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ierr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;!得到运行本进程的机器的名称&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;WRITE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;proc_num&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;processor_name&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;FORMAT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'Hello World! Process '&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;l&quot;&gt;I2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;' of '&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;l&quot;&gt;I2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;' on '&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;l&quot;&gt;20A&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;CALL&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_FINALIZE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ierr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;!终止MPI&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;END&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;PROGRAM&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;mpi程序的编译和运行&quot;&gt;MPI程序的编译和运行&lt;/h3&gt;

&lt;p&gt;Fortran 语言的MPI程序由 mpif90 来编译，用法与一般的的 Fortran 编译器（如 Gfortran ）类似，向系统提交MPI程序可由 mpirun 命令完成，-np 参数表示该程序的进程数。&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;mpif90 &lt;span class=&quot;nt&quot;&gt;-c&lt;/span&gt; hello_world.f90
mpif90 &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; hello_world hello_world.o
mpirun &lt;span class=&quot;nt&quot;&gt;-np&lt;/span&gt; 5 hello_world
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;运行结果如下图&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;..\images\posts\use_mpi_hello_world.PNG&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;mpi的一些惯例&quot;&gt;MPI的一些惯例&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;MPI 的所有常量、变量与函数 /过程均以MPI_ 开头。&lt;/li&gt;
  &lt;li&gt;MPI 的 C 语言接口为函数， Fortran 接口为子程序，且对应接口的名称相同。&lt;/li&gt;
  &lt;li&gt;在C 程序中，所有常数的定义除下划线外一律由大写字母组成，在函数和数据类型定义中, 接MPI_ 之后的第一个字母大写，其余全部为小写字母，即MPI_Xxxx_xxx形式。&lt;/li&gt;
  &lt;li&gt;对于FORTRAN 程序， MPI 函数全部以过程方式调用，一般全用大写字母表示，即MPI_XXXX_XXX形式（ Fortran不区分大小写）。&lt;/li&gt;
  &lt;li&gt;除 MPI_WTIME 和 MPI_WTICK 外，所有C函数调用之后都将返回一个错误信息码，而 MPI 的所有 FORTRAN 子程序中都有一个哑元参数（ ierr）代表调用错误码，错误代码返回值为0表示函数/过程调用成功，返回值为非0则表示调用失败。&lt;/li&gt;
  &lt;li&gt;MPI 是按进程组(Process Group) 方式工作的，所有MPI 程序在开始时均被认为是在通信器 MPI_COMM_WORLD 所拥有的进程组中工作，之后用户可以根据自己的需要，建立其它的进程组。&lt;/li&gt;
  &lt;li&gt;所有MPI 的通信一定要在通信器(communicator) 中进行。&lt;/li&gt;
  &lt;li&gt;Fortran的数组下标是以1开始，而C语言的数组是以0开始。&lt;/li&gt;
  &lt;li&gt;由于C语言的函数调用机制是值传递，所以MPI的所有C函数中的输出参数用的都是指针 。&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;mpi基本的用法&quot;&gt;MPI基本的用法&lt;/h2&gt;

&lt;h3 id=&quot;mpi-的六个基本接口&quot;&gt;MPI 的六个基本接口&lt;/h3&gt;

&lt;p&gt;MPI有上百个可调用的函数（子例程），具体调用语法可查阅 &lt;a href=&quot;https://www.open-mpi.org/doc/v4.1/&quot;&gt;Open MPI 的说明文档&lt;/a&gt;，实际编写MPI程序中最常使用的MPI函数（子例程）的个数是十分有限的 ，并且理论上讲MPI所有的通信功能可以用它的6个基本的函数（子例程）来实现。以下以Fortran语言的MPI实现为例，说明MPI的6个基本的接口：&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;MPI初始化&lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;language-fortran highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;CALL&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_INIT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ierr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! OUT INTEGER :: ierr -返回值为0表示成功，返回值为非0表示出错&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ol&gt;
  &lt;li&gt;MPI结束&lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;language-fortran highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;CALL&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_FINALIZE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ierr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! OUT INTEGER :: ierr&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ol&gt;
  &lt;li&gt;得到当前进程在给定通信域（也叫通信子）中的编号&lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;language-fortran highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;CALL&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;　&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_COMM_RANK&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;comm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ierr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! IN  INTEGER :: comm -给定的通信域，可以为MPI预定义的通信域MPI_COMM_WORLD（包含所有进程），也可以为其他自定义通信域&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! OUT INTEGER :: my_id -当前节点的进程号&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! OUT INTEGER :: ierr -给定的通信域&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ol&gt;
  &lt;li&gt;得到给定通信域内的进程数&lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;language-fortran highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;　&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_COMM_SIZE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;comm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;proc_num&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ierr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! IN  INTEGER :: comm -给定的通信域&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! OUT INTEGER :: proc_num -给定通信域内的进程数&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! OUT INTEGER :: ierr&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ol&gt;
  &lt;li&gt;消息发送&lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;language-fortran highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;CALL&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;　&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_SEND&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;datatype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dest&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tag&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;comm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ierr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! IN  &amp;lt;TYPE&amp;gt;  :: buf -发送数据的起始地址&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! IN  INTEGER :: count -发送数据的个数&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! IN  &amp;lt;handle&amp;gt; :: data_type -发送数据的类型，可以是MPI预定义的数据类型，也可以是用户的自定义的新的数据结构（非连续）&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! IN  INTEGER :: dest -目的进程的编号&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! IN  INTEGER :: tag -此次发送的标签&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! IN  INTEGER :: comm -给定的通信域&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! OUT INTEGER :: ierr&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;说明：MPI_SEND将count个datatype数据类型的数据发送到目的进程， 目的进程在通信域中的标识号是dest，本次发送的消息标志是tag，使用这一标志就可以把本次发送的消息和本进程向同一目的进程发送的其它消息区别开来 ， 其中datatype数据类型可以是MPI的预定义类型 也可以是用户自定义的类型 （后文介绍）。&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;消息接收&lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;language-fortran highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;CALL&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;　&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_RECV&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;datatype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;source&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tag&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;comm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;status&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ierr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! OUT &amp;lt;TYPE&amp;gt;  :: buf -接受数据的起始地址&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! IN  INTEGER :: count -接受数据的个数&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! IN  &amp;lt;handle&amp;gt; :: data_type -接收数据的类型，可以是MPI预定义的数据类型，也可以是用户的自定义的新的数据结构（非连续）&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! IN  INTEGER :: source -所接受数据的进程的编号，可以为MPI_ANY_SOURCE（MPI预定义常量），用以匹配任意消息来源&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! IN  INTEGER :: tag -此次接收的标签，可以为MPI_ANY_TAG（MPI预定义常量）用以匹配任意消息标签&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! IN  INTEGER :: comm -给定的通信域&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! OUT INTEGER :: status(MPI_STATUS_SIZE) 返回状态和调试信息 &lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! OUT INTEGER :: ierr&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;说明：MPI_RECV从指定的进程source接收消息，发送消息的数据类型datatype和消息标识tag要与接收进程指定的datatype和tag相一致，接收消息的起始地址为buf，接收消息的长度必须不小于发送消息的长度，这是因为如果接收到的数据过大，接收缓冲区会发生溢出错误，如果一个长度短于count的消息到达，那么只有相应于这个消息的那些地址被修改，count可以是零，这种情况下消息的数据部分是空的，其中datatype数据类型可以是MPI的预定义类型 也可以是用户自定义的类型 （后文介绍），status是包含MPI_STATUS_SIZE个整数的数组，status(MPI_SOURCE)，status(MPI_TAG)和status(MPI_ERROR)分别表示发送数据的进程标识，发送数据使用tag标识和该接收操作返回的错误代码  。&lt;/p&gt;

&lt;h3 id=&quot;mpi-预定义数据类型&quot;&gt;MPI 预定义数据类型&lt;/h3&gt;

&lt;p&gt;MPI系统中数据的发送和接收都是基于数据类型进行的。数据类型可以是MPI系统预定义的，称为原始数据类型；也可以是用户在原始数据类型的基础上自己定义的新的数据类型。MPI 的基本数据类型定义与相应的Fortran 的数据类型对照关系如下:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;&lt;strong&gt;MPI Datatype&lt;/strong&gt;&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;&lt;strong&gt;Fortran Datatype&lt;/strong&gt;&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;MPI_INTEGER&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;INTEGER&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;MPI_REAL&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;REAL&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;MPI_DOUBLE_PRECISION&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;DOUBLE PRECISION&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;MPI_COMPLEX&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;COMPLEX&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;MPI_LOGICAL&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;LOGICAL&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;MPI_CHARACTER&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;CHARACTER(1)&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;MPI_BYTE（由一个字节组成 ）&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;无类型与之对应&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;MPI_PACKED&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;无类型与之对应&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;在MPI中，消息传递要做到数据类型的匹配。数据类型匹配有两方面的含义：一方面，宿主语言（Fortran或C）的类型和通信操作所指定的类型相匹配，比如在   Fortran 中声明为 INTEGER 类型的变量在发送和接收时要使用 MPI_INTEGER 与之相对应，声明为REAL类型的变量在发送和接收时要使用 MPI_REAL 与之相对应 前面部分给出的 Fortran 与 MPI 类型的对应关系就是为了满足类型匹配的要求；另一方面，发送方和接收方的类型相匹配，即发送方用 MPI_INTEGER 则接收方也必须使用 MPI_INTEGER，发送方用 MPI_REAL 则接收方也必须用MPI_REAL。但是，对于 MPI 提供的 MPI_BYTE 和 MPI_PACKED 它们可以和任何以字节为单位的存储相匹配，包含这些字节的类型是任意的，MPI_BYTE用于不加修改地传送内存中的二进制值， MPI_PACK用于数据的打包和解包（MPI_UNPACK ）。&lt;/p&gt;

&lt;h3 id=&quot;mpi消息的组成&quot;&gt;MPI消息的组成&lt;/h3&gt;

&lt;p&gt;MPI 消息包括信封和数据两个部分。信封指出了发送或接收消息的对象及相关信息， 而数据是本消息将要传递的内容。信封和数据又分别包括三个部分，可以用一个三元组来表示：
\(信封 &amp;lt;源/目,标识,通信域&amp;gt;\\
数据 &amp;lt;起始地址,数据个数,数据类型&amp;gt;\)
以MPI_SEND和MPI_RECV为例，下图分别给出了它们的信封和数据部分 ：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;E:\Gthub\zhang-dehua.github.io\images\posts\use_mpi_hello_massage01.PNG&quot; style=&quot;zoom:51%&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;E:\Gthub\zhang-dehua.github.io\images\posts\use_mpi_hello_massage02.PNG&quot; style=&quot;zoom:50%;&quot; /&gt;&lt;/p&gt;

&lt;h3 id=&quot;死锁现象&quot;&gt;死锁现象&lt;/h3&gt;

&lt;p&gt;编写MPI程序如果通信调用的顺序使用的不当很容易造成死锁。若所有进程都首先进行接收数据操作，那么所有进程都会等待从其他进程接收数据，而没有进程进行数据发送操作，因此程序会停滞，从而发生死锁。若所有的进程都首先进行发送数据操作，由于发送数据系统提供缓存区，若系统缓存区不足，则发送数据将会失败，显然把所有进程的发送数据操作都写在接收操作的前面也有可能是不安全的。因此MPI程序的发送必须合理的安排顺序，否则将会导致死锁，捆绑式的发送和接收操作 MPI_SENDRECV 也可以解决这个问题。&lt;/p&gt;

&lt;h3 id=&quot;其他常用的mpi函数子例程&quot;&gt;其他常用的MPI函数/子例程&lt;/h3&gt;

&lt;h4 id=&quot;计时函数&quot;&gt;计时函数&lt;/h4&gt;

&lt;p&gt;在MPI程序中经常会用到时间函数，比如用来统计程序运行的时间，或根据时间的不同选取不同的随机数种子，或根据时间的不同对程序的执行进行控制等。  MPI提供了时间函数可供调用 。&lt;/p&gt;

&lt;div class=&quot;language-fortran highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;MPI_WTIME&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;!DOUBLE PRECISION&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_WTICK&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;!DOUBLE PRECISION&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;MPI_WTIME（）返回一个用浮点数表示的秒数， 它表示从过去某一时刻到调用时刻所经历的时间 ，MPI_WTICK返回MPI_WTIME的精度单位是秒  。&lt;/p&gt;

&lt;h4 id=&quot;获取主机名和mpi版本号&quot;&gt;获取主机名和MPI版本号&lt;/h4&gt;

&lt;p&gt;在实际使用MPI编写并行程序的过程中，经常要将一些中间结果或最终的结果输出到程序自己创建的文件中，对于在不同机器上的进程 常希望输出的文件名包含该机器名，或者是需要根据不同的机器执行不同的操作，这样仅仅靠进程标识rank是不够的 MPI为此提供了一个专门的调用，使各个进程在运行时可以动态得到该进程所运行机器的名字 。另外MPI也提供了可以获得MPI版本号的调用。&lt;/p&gt;

&lt;div class=&quot;language-fortran highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;CALL&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_GET_PROCESSOR_NAME&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;processor_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name_len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ierr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! OUT CHARACTER(LEN=MPI_MAX_PROCESSOR_NAME) :: processor_name -主机名&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! OUT INTEGER :: name_len -名长度&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! OUT INTEGER :: ierr&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;CALL&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_GET_VERSION&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;version&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sub_version&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ierr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! OUT INTEGER :: version -主版本号&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! OUT INTEGER :: sub_version -子版本号&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! OUT INTEGER :: ierr&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;检测mpi是否初始化及异常终止mpi&quot;&gt;检测MPI是否初始化及异常终止MPI&lt;/h4&gt;

&lt;p&gt;MPI 程序中唯一一个可以用在 MPI_INIT 之前的MPI调用就是 MPI_INITALIZED，它的功能就是判断 MPI_INIT 是否已经执行。在编写 MPI 程序的过程中若发现已出现无法恢复的严重错误因而只好退出MPI程序的执行，MPI 提供了 MPI_ABORT 调用,并且在退出时可以返回给调用环境一个错误码.&lt;/p&gt;

&lt;div class=&quot;language-fortran highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;CALL&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_INITIALIZED&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;flag&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ierr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! OUT LOGICAL :: flag - MPI 是否初始化&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! OUT INTEGER :: ierr&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;CALL&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_ABORT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;comm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;errorcode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ierr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! IN INTEGER :: comm -要退出的通信域&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! IN INTEGER :: errorcode -返回到所嵌环境的错误码&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! OUT INTEGER :: ierr &lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;同步mpi过程&quot;&gt;同步MPI过程&lt;/h4&gt;

&lt;p&gt;MPI_BARRIER用于一个通信域中所有进程的同步，调用MPI_BARRIER时进程将处于等待状态，直到通信子中所有进程 都调用了该函数后才继续执行。&lt;/p&gt;

&lt;div class=&quot;language-fortran highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;MPI_BARRIER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;comm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ierr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! OUT INTEGER :: comm -通信域&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! OUT INTEGER :: ierr&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;捆绑发送和接收&quot;&gt;捆绑发送和接收&lt;/h4&gt;

&lt;p&gt;MPI提供了捆绑发送和接收操作，可以在一条MPI语句中同时实现向其它进程的数据发送和从其它进程接收数据操作。捆绑发送和接收操作把发送一个消息到一个目的地和从另一个进程接收一个消息合并到一个调用中，源和目的可以是相同的。捆绑发送接收操作虽然在语义上等同于一个发送操作和一个接收操作的结合，但是它可以有效地避免由于单独进行发送或接收操作时，由于次序的错误而造成的死锁。这是因为该操作由通信系统来实现，系统会优化通信次序从而有效地避免不合理的通信次序，最大限度避免死锁的产生。捆绑发送接收操作是不对称的，即一个由捆绑发送接收调用发出的消息可以被一个普通接收操作接收，一个捆绑发送接收调用可以接收一个普通发送操作发送的消息。&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-Fortran&quot;&gt;CALL MPI_SENDRECV(sendbuf, sendcount, sendtype, dest, sendtag,
        		  recvbuf, recvcount, rectype, source, recvtag, comm,
        		  status, ierr)
! IN &amp;lt;type&amp;gt; :: sendbuf -发送数据的起始地址
! IN INTEGER :: sendcount, sendtype, dest, sendtag -发送数据的个数，类型，目标进程号，消息标识
! IN INTEGER :: recvcount, recvtype, source, recvtag -接收数据的个数，类型，目标进程号，消息标识
! IN INTEGER :: comm -通信域
! OUT INTEGER :: STATUS(MPI_STATUS_SIZE) -返回状态
! OUT INTEGER :: ierr
! OUT &amp;lt;type&amp;gt; :: recvbuf -接收数据的起始地址
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;一个与 MPI_SENDRECV  类似的操作是 MPI_SENDRECV_REPLACE，它与MPI_SENDRECV的不同就在于其发送数据的地址与接收数据的地址相同。这一调用的执行结果是，将指定地址中原来的数据被传递给指定的目的进程，然后该地址上的数据又被从指定进程接收到的相应类型的数据所取代。&lt;/p&gt;

&lt;div class=&quot;language-fortran highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;CALL&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_SENDRECV_REPLACE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dest&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sendtag&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;source&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;recvtag&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;comm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;status&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ierr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! INOUT &amp;lt;type&amp;gt; :: buf -发送以及接收数据的起始地址&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! IN INTEGER :: count, type -发送以及接收数据的个数，类型&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! IN INTEGER :: dest, sendtag -发送数据的目标进程号，消息标识&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! IN INTEGER :: source, recvtag -接收数据目标进程号，消息标识&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! IN INTEGER :: comm -通信域&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! OUT INTEGER :: STATUS(MPI_STATUS_SIZE) -返回状态&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! OUT INTEGER :: ierr&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! OUT &amp;lt;type&amp;gt; :: recvbuf -接收数据的起始地址&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;简单的mpi并行程序&quot;&gt;简单的MPI并行程序&lt;/h2&gt;

&lt;h4 id=&quot;计算n&quot;&gt;计算n!&lt;/h4&gt;

&lt;div class=&quot;language-fortran highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;PROGRAM&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sum_n&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;USE&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! or the older form: INCLUDE ’mpif.h’&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;IMPLICIT&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;NONE&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;INTEGER&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;proc_num&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ierr&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;INTEGER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;DIMENSION&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_STATUS_SIZE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;satus&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;INTEGER&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sum_global&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sum_local&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;INTEGER&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n_start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n_global&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;REAL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;KIND&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time_start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time_end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time_total&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;CALL&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_INIT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ierr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;CALL&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_COMM_RANK&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_COMM_WORLD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ierr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;CALL&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_COMM_SIZE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_COMM_WORLD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;proc_num&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ierr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time_start&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_WTIME&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n_global&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10000&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sum_local&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n_start&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_id&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;+1&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;DO&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n_start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n_global&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;proc_num&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sum_local&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sum_local&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;END&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;DO&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;WRITE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot; &quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;WRITE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;===========================================&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;WRITE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;proc_num&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;FORMAT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'Process '&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;l&quot;&gt;I2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;' of '&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;l&quot;&gt;I2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;WRITE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;The local caculation finished.&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;WRITE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;n_start=&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n_start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;n_end=&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;proc_num&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;WRITE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;sum_local=&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sum_local&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;WRITE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;===========================================&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;CALL&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_REDUCE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sum_local&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sum_global&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_INTEGER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_SUM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_COMM_WORLD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ierr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time_end&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_WTIME&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time_total&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time_end&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time_start&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;IF&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_id&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;THEN&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;WRITE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot; &quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;WRITE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;===========================================&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;WRITE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;The global caculation finished.&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;WRITE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;The process is &quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_id&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;WRITE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;sum_local=&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sum_global&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;WRITE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Total time is&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time_total&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;WRITE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;===========================================&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;END&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;IF&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;CALL&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_FINALIZE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ierr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;END&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;PROGRAM&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;接收相邻进程的数据&quot;&gt;接收相邻进程的数据&lt;/h4&gt;

&lt;div class=&quot;language-fortran highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;PROGRAM&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;send_recv_id&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;USE&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! or the older form: INCLUDE ’mpif.h’&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;IMPLICIT&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;NONE&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;INTEGER&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;proc_num&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tag&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ierr&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;INTEGER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;DIMENSION&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_STATUS_SIZE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;status&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;REAL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;KIND&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time_start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time_end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time_total&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;INTEGER&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dest&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;source&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;INTEGER&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;right_data&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;CALL&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_INIT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ierr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;CALL&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_COMM_RANK&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_COMM_WORLD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ierr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;CALL&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_COMM_SIZE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_COMM_WORLD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;proc_num&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ierr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time_start&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_WTIME&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_data&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_id&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;+1000&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

    &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! send data to left process&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tag&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;IF&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_id&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;-1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;THEN&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dest&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_id&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;-1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;proc_num&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;ELSE&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dest&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_id&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;-1&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;END&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;IF&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;IF&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_id&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;+1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;proc_num&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;THEN&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;source&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_id&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;+1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;proc_num&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;ELSE&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;source&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_id&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;+1&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;END&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;IF&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! CALL MPI_SEND(my_data,1,MPI_INTEGER,dest,tag,MPI_COMM_WORLD,ierr)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! CALL MPI_RECV(right_data,1,MPI_INTEGER,source,tag,MPI_COMM_WORLD,status,ierr)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! CALL MPI_BARRIER(MPI_COMM_WORLD,ierr)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;CALL&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_SENDRECV&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_INTEGER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dest&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tag&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&amp;amp;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        		    &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;right_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_INTEGER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;source&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tag&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_COMM_WORLD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&amp;amp;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        		    &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;status&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ierr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

    &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! send data to right process&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tag&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;IF&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_id&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;-1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;THEN&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;source&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_id&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;-1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;proc_num&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;ELSE&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;source&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_id&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;-1&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;END&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;IF&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;IF&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_id&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;+1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;proc_num&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;THEN&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dest&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_id&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;+1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;proc_num&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;ELSE&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dest&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_id&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;+1&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;END&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;IF&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

    &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! CALL MPI_SEND(my_data,1,MPI_INTEGER,dest,tag,MPI_COMM_WORLD,ierr)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! CALL MPI_RECV(left_data,1,MPI_INTEGER,source,tag,MPI_COMM_WORLD,status,ierr)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! CALL MPI_BARRIER(MPI_COMM_WORLD,ierr)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;CALL&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_SENDRECV&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_INTEGER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dest&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tag&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&amp;amp;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_INTEGER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;source&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tag&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_COMM_WORLD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&amp;amp;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;status&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ierr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

    &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! Write resualts&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;WRITE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot; &quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;WRITE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;===========================================&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;WRITE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;proc_num&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;FORMAT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'Process '&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;l&quot;&gt;I2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;' of '&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;l&quot;&gt;I2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;WRITE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;my_data = &quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_data&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;WRITE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;left_data=&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;left_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;right_data=&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;right_data&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;WRITE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;===========================================&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time_end&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_WTIME&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time_total&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time_end&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time_start&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;CALL&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_BARRIER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_COMM_WORLD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ierr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;IF&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_id&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;THEN&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;WRITE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;   &quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;WRITE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;===========================================&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;WRITE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Process is &quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_id&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;WRITE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;The massage passing is finished.&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;WRITE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Total time is &quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time_total&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;WRITE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;===========================================&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;END&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;IF&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;CALL&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_FINALIZE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ierr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;END&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;PROGRAM&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;积分计算pi&quot;&gt;积分计算PI&lt;/h4&gt;

&lt;pre&gt;&lt;code class=&quot;language-Fortran&quot;&gt;program main
    use mpi
    implicit none
    real(kind=8), parameter :: PI25DT = 3.141592653589793238462643d0
    real(kind=8) :: my_pi, pi, h, sum, x, f, a
    REAL(KIND=8) :: time_start,time_end,time_total
    integer ::  n, my_id, proc_num, i, ierr


    ! function to integrate
    f(a) = 4.d0 / (1.d0 + a*a)

    call MPI_INIT(ierr)
    call MPI_COMM_RANK(MPI_COMM_WORLD, my_id, ierr)
    call MPI_COMM_SIZE(MPI_COMM_WORLD, proc_num, ierr)

    
    do
        
        if (my_id == 0) then
            write(*,*), &quot;Enter the number of intervals: (0 quits) &quot;
            read(*,*) n
        endif
        
        time_start = MPI_WTIME()
        ! broadcast n
        call MPI_BCAST(n, 1, MPI_INTEGER, 0, MPI_COMM_WORLD, ierr)

        ! check for quit signal
        if (n &amp;lt;= 0) then
            exit
        end if

        ! calculate the interval size
        h = 1.0d0/n
        sum = 0.0d0
        do i = my_id+1, n, proc_num
            x = h * (dble(i) - 0.5d0)
            sum = sum + f(x)
        enddo
        my_pi = h * sum
        ! collect all the partial sums
        call MPI_REDUCE(my_pi, pi, 1, MPI_DOUBLE_PRECISION, MPI_SUM, 0, MPI_COMM_WORLD, ierr)
        time_end = MPI_WTIME()
        time_total = time_end-time_start
        ! node 0 prints the C_elementwer.
        if (my_id .eq. 0) then
            write(*,*) &quot;   &quot;
            write(*,*) &quot;===========================================&quot;
            write(*,*), &quot;pi is ’, pi, ’ Error is&quot;, abs(pi - PI25DT)
            WRITE(*,*) &quot;Total time is&quot;, time_total
            write(*,*) &quot;===========================================&quot;
        endif
    end do

    call MPI_FINALIZE(ierr)
end
&lt;/code&gt;&lt;/pre&gt;

&lt;h4 id=&quot;计算矩阵乘向量&quot;&gt;计算矩阵乘向量&lt;/h4&gt;

&lt;div class=&quot;language-fortran highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;program&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;use&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mpi&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;proc_num&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ierr&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;dimension&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_STATUS_SIZE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;status&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m_num&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n_num&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;allocatable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;dimension&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(:,:)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;allocatable&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;dimension&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(:)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;B&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;C&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;buffer&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;real&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;kind&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;C_element&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;    
    &lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;manager&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;worker_num&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;integer&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sent_num&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sender&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_INIT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ierr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_COMM_RANK&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_COMM_WORLD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ierr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_COMM_SIZE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_COMM_WORLD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;proc_num&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ierr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;manager&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m_num&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n_num&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;worker_num&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;proc_num&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;-1&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;worker_num&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m_num&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;then&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_id&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;manager&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;then&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
            &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Process manager : ERROR!&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
            &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;The worker process number is larger than colums number of vector C!&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
            &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;The program is forced to abort!&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
            &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;CALL&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_ABORT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_COMM_WORLD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ierr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;allocate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;B&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n_num&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;buffer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n_num&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;STAT&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ierr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_id&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;manager&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;then&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;allocate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m_num&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n_num&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;C&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m_num&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;STAT&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ierr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n_num&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
            &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;B&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
            &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m_num&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
                &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
            &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;enddo&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;enddo&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! send b to each worker process&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_BCAST&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n_num&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_DOUBLE_PRECISION&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;manager&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_COMM_WORLD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ierr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! send a row to each worker process; tag with row number&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sent_num&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;worker_num&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
            &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;buffer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(:)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,:)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
            &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_SEND&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;buffer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n_num&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_DOUBLE_PRECISION&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_COMM_WORLD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ierr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
            &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sent_num&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sent_num&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;+1&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;enddo&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m_num&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
            &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_RECV&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;C_element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_DOUBLE_PRECISION&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
            &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_ANY_SOURCE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_ANY_TAG&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
            &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_COMM_WORLD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;status&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ierr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
            &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sender&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;status&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_SOURCE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
            &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;status&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_TAG&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! row is tag value&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
            &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;C_element&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
            &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sent_num&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m_num&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;then&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! send another row&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
                &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n_num&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
                    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;buffer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sent_num&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;+1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;j&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
                &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;enddo&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
                &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_SEND&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;buffer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n_num&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_DOUBLE_PRECISION&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
                &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sender&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sent_num&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;+1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_COMM_WORLD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ierr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
                &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sent_num&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sent_num&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;+1&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
            &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! Tell sender that there is no more work&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
                &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_SEND&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_BOTTOM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_DOUBLE_PRECISION&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
            &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sender&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_COMM_WORLD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ierr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
            &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;endif&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;enddo&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! workers receive b, then compute dot products until&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;! done message received&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_BCAST&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n_num&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_DOUBLE_PRECISION&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;manager&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
                        &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_COMM_WORLD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ierr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

        &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
            &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_RECV&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;buffer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n_num&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_DOUBLE_PRECISION&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
            &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;manager&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_ANY_TAG&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_COMM_WORLD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
            &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;status&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ierr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
            &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;status&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_TAG&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;exit&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
            &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;status&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_TAG&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
            &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;C_element&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
            &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n_num&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
                &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;C_element&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;C_element&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;buffer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
            &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;enddo&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
            &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_SEND&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;C_element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_DOUBLE_PRECISION&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;manager&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
            &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;row&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_COMM_WORLD&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ierr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;enddo&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;endif&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_id&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;ow&quot;&gt;.eq.&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;manager&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;then&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;============================================&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;This is the manager process &quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_id&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m_num&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;('C[',I3,']=',F10.1)&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;============================================&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;deallocate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;C&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;STAT&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ierr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;deallocate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;B&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;buffer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;STAT&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ierr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;MPI_FINALIZE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ierr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;矩阵相乘&quot;&gt;矩阵相乘&lt;/h4&gt;

&lt;pre&gt;&lt;code class=&quot;language-Fortran&quot;&gt;program main
    use MPI
    implicit none
    integer :: my_id, dest_id, source_id, proc_num, ierr
    integer, dimension(MPI_STATUS_SIZE) :: status

    
    real(kind=8) :: x, y, S_local, S_global
    real(kind=8), allocatable, dimension(:,:) :: A, B, B_temp, C
    real(kind=8) :: time_start,time_end,time_total
    integer :: N, NP, i, j, k, step

    time_start = MPI_WTIME()

    N = 2000
    CALL MPI_INIT(ierr)
    CALL MPI_COMM_RANK(MPI_COMM_WORLD,my_id,ierr)
    CALL MPI_COMM_SIZE(MPI_COMM_WORLD,proc_num,ierr)
    NP=N/proc_num
    
    if (my_id == 0) then
        if( mod(N,proc_num)/=0 )then
            write(*,*) &quot;Process 0: ERROR!&quot;
            write(*,*) &quot;The remainder of the division of N by roc_num should be zero.&quot;
            write(*,*) &quot;The program is forced to abort!&quot;
            CALL MPI_ABORT(MPI_COMM_WORLD,100,ierr)
        end if
    end if


    allocate(A(NP,N),B(N,NP),B_temp(N,NP),C(NP,N),STAT=ierr)

    ! Initialize A,B,C
    do j=1,N 
        do i=1,NP 
            x = (dble(my_id*NP+i)-1.0d0)/(dble(N)-1.0d0)
            y = (dble(j)-1.0d0)/(dble(N)-1.0d0)
            A(i,j) = exp(x)*sin(3.0d0*x)
        end do
    end do

    do j=1,Np
        do i=1,N 
            x = (dble(i)-1.0d0)/(dble(N)-1.0d0)
            y = (dble(my_id*NP+j)-1.0d0)/(dble(N)-1.0d0)
            B(i,j) = ( x+cos(4.0d0*x) )*(1.0d0+y)
        end do
    end do
    C = 0.0d0

    !
    do j=1,NP
        do i=1,NP
            do k=1,N
                C(i,my_id*NP+j)=C(i,my_id*NP+j)+A(i,k)*B(k,j)
            end do
        end do
    end do

    do step=1,proc_num-1
        if(my_id+step&amp;lt;=proc_num-1)then
            dest_id = my_id+step
        else
            dest_id = my_id+step-proc_num
        end if

        if(my_id-step&amp;gt;=0)then
            source_id = my_id-step
        else
            source_id = my_id-step+proc_num
        end if

        CALL MPI_SENDRECV(B,N*NP,MPI_DOUBLE_PRECISION,dest_id, step,&amp;amp;
        		        &amp;amp;B_temp,N*NP,MPI_DOUBLE_PRECISION,source_id,step,&amp;amp;
                        &amp;amp;MPI_COMM_WORLD,status,ierr)

        do j=1,NP
            do i=1,NP
                do k=1,N
                    C(i,source_id*NP+j)=C(i,source_id*NP+j)+A(i,k)*B_temp(k,j)
                end do
            end do
        end do

        CALL MPI_BARRIER(MPI_COMM_WORLD,ierr)
    end do

    S_local = 0.0d0
    do j=1,N 
        do i=1,NP
            S_local = S_local+C(i,j)**2
        end do
    end do

    CALL MPI_REDUCE(S_local,S_global,1,MPI_DOUBLE_PRECISION,MPI_SUM,0,MPI_COMM_WORLD,ierr)

    S_global = S_global/N**2

    time_end = MPI_WTIME()
    time_total = time_end-time_start

    if (my_id == 0) then
            write(*,*) &quot;====================================&quot;
            write(*,*) &quot;Process 0&quot;
            write(*,*) &quot;S_global=&quot;, S_global
            WRITE(*,*) &quot;Total time is&quot;, time_total
            write(*,*) &quot;====================================&quot;
    end if

    CALL MPI_FINALIZE(ierr)
end program
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;不同进程数运行所需时间&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;进程数&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;1&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;2&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;4&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;8&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;16&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;25&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;时间（秒）&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;70.65&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;26.76&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;16.01&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;8.30&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;4.45&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;3.51&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h4 id=&quot;二维laplace方程数值&quot;&gt;二维Laplace方程数值&lt;/h4&gt;

\[\frac{\partial^2T}{\partial x^2}+\frac{\partial^2T}{\partial y^2}=0 \\
T(x,y)|_{\Omega}=F(x,y)\]

&lt;h2 id=&quot;非阻塞通信&quot;&gt;非阻塞通信&lt;/h2&gt;

&lt;h2 id=&quot;聚合通信&quot;&gt;聚合通信&lt;/h2&gt;

&lt;h2 id=&quot;不连续数据的发送与接收&quot;&gt;不连续数据的发送与接收&lt;/h2&gt;

&lt;h2 id=&quot;mpi的进程组和通信域&quot;&gt;MPI的进程组和通信域&lt;/h2&gt;

&lt;h2 id=&quot;进程拓扑&quot;&gt;进程拓扑&lt;/h2&gt;

&lt;h2 id=&quot;并行io&quot;&gt;并行I/O&lt;/h2&gt;

</description>
        <pubDate>Fri, 23 Apr 2021 00:00:00 +0000</pubDate>
        <link>https://zhang-dehua.github.io/2021/04/using_MPI/</link>
        <guid isPermaLink="true">https://zhang-dehua.github.io/2021/04/using_MPI/</guid>
        
        <category>高性能计算</category>
        
        
      </item>
    
      <item>
        <title>我的第一个博客</title>
        <description>&lt;h2 id=&quot;标题a&quot;&gt;标题A&lt;/h2&gt;

&lt;p&gt;内容A&lt;/p&gt;

&lt;h2 id=&quot;标题b&quot;&gt;标题B&lt;/h2&gt;

&lt;h3 id=&quot;标题b1&quot;&gt;标题B.1&lt;/h3&gt;

&lt;p&gt;内容2&lt;/p&gt;
</description>
        <pubDate>Fri, 23 Apr 2021 00:00:00 +0000</pubDate>
        <link>https://zhang-dehua.github.io/2021/04/my_first_blog/</link>
        <guid isPermaLink="true">https://zhang-dehua.github.io/2021/04/my_first_blog/</guid>
        
        <category>普通标签</category>
        
        
      </item>
    
      <item>
        <title>博客模板</title>
        <description>&lt;h2 id=&quot;标题a&quot;&gt;标题A&lt;/h2&gt;

&lt;p&gt;内容A&lt;/p&gt;

&lt;h2 id=&quot;标题b&quot;&gt;标题B&lt;/h2&gt;

&lt;h3 id=&quot;标题b1&quot;&gt;标题B.1&lt;/h3&gt;

&lt;p&gt;内容2&lt;/p&gt;
</description>
        <pubDate>Fri, 23 Apr 2021 00:00:00 +0000</pubDate>
        <link>https://zhang-dehua.github.io/2021/04/blog_template/</link>
        <guid isPermaLink="true">https://zhang-dehua.github.io/2021/04/blog_template/</guid>
        
        <category>模板标签</category>
        
        
      </item>
    
  </channel>
</rss>
