<!DOCTYPE article PUBLIC "-//NLM//DTD JATS (Z39.96) Journal Archiving and Interchange DTD v1.0 20120330//EN" "JATS-archivearticle1.dtd">
<article xmlns:xlink="http://www.w3.org/1999/xlink">
  <front>
    <journal-meta>
      <journal-title-group>
        <journal-title>October</journal-title>
      </journal-title-group>
    </journal-meta>
    <article-meta>
      <title-group>
        <article-title>ACCELERATING THE PARTICLE-IN-CELL METHOD OF PLASMA AND PARTICLE BEAM SIMULATION USING CUDA TOOLS</article-title>
      </title-group>
      <contrib-group>
        <contrib contrib-type="author">
          <string-name>I.S. Kadochnikov</string-name>
          <xref ref-type="aff" rid="aff0">0</xref>
          <xref ref-type="aff" rid="aff1">1</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Ivan Kadochnikov</string-name>
        </contrib>
        <aff id="aff0">
          <label>0</label>
          <institution>Joint Institute for Nuclear Research</institution>
          ,
          <addr-line>6 Joliot-Curie St, Dubna, Moscow Region, 141980</addr-line>
          ,
          <country country="RU">Russia</country>
        </aff>
        <aff id="aff1">
          <label>1</label>
          <institution>Plekhanov Russian University of Economics</institution>
          ,
          <addr-line>Stremyanny lane, 36, Moscow, 117997</addr-line>
          ,
          <country country="RU">Russia</country>
        </aff>
      </contrib-group>
      <pub-date>
        <year>2019</year>
      </pub-date>
      <volume>4</volume>
      <issue>2019</issue>
      <fpage>225</fpage>
      <lpage>229</lpage>
      <abstract>
        <p>Numerical simulation of charged particle dynamics is required for the development of electron beam ion sources (EBIS). Special attention should be paid to the formation and evolution of different instabilities that can manifest in such sources. Understanding these processes will allow the efficiency of ion sources to be improved. The simulation of two-stream instability emerging between slow ions and fast electrons is particularly important. To perform the required simulation, “ef” and “ef_python” applications are under development. In these applications simulation is performed using the particlein-cell (PIC) method, which contains four main parts: a particle mover, a particle-to-grid scatter, a field solver, field-to-particle interpolation. Each part's performance depends on the number of particles, as well as on the granularity of the simulation spatial mesh. The field solver usually represents a major portion of computational difficulty. This report describes the efforts to improve the performance of the ef_python application using CuPy, AMGX and PyAMG libraries. With minimal changes in the source code, CuPy allowed us to port all simulation computations to CUDA. In addition, to accelerate the field solver, algebraic multigrid methods, implemented by the PyAMG library on CPU and the AMGX library on GPU, were utilized. Major speed-up was achieved, especially when running simulations with very high-resolution spatial grids on high-performance GPUs.</p>
      </abstract>
      <kwd-group>
        <kwd>plasma simulation</kwd>
        <kwd>particle-in-cell</kwd>
        <kwd>CUDA</kwd>
        <kwd>GPU</kwd>
        <kwd>CuPy</kwd>
      </kwd-group>
    </article-meta>
  </front>
  <body>
    <sec id="sec-1">
      <title>1. Introduction</title>
      <p>
        Numerical simulation of charged particle dynamics is required for the development of electron
beam ion sources (EBIS) [
        <xref ref-type="bibr" rid="ref1">1</xref>
        ]. Special attention should be paid to the formation and evolution of
different instabilities that can manifest in such sources. Understanding these processes can allow the
efficiency of ion sources to be improved. The simulation of two-stream instability emerging between
slow ions and fast electrons is particularly important.
      </p>
      <p>
        To perform the required simulation, “ef” and “ef_python” applications are under development
[
        <xref ref-type="bibr" rid="ref2">2</xref>
        ] using the particle-in-cell (PIC) method [
        <xref ref-type="bibr" rid="ref3">3</xref>
        ]. Ef is written in C++ for performance reasons, and there
are several initiatives to use MPI, OpenMP and CUDA to use parallel and GPU computing in ef.
Ef_python is written in python for fast and flexible development. It uses NumPy for simulation, and
this report describes the efforts to improve its performance and adapt it to use graphics accelerators.
      </p>
      <p>
        A previous study using a 2-dimensional dynamic particle model as a sample showed that both
OpenCL and CUDA could dramatically improve particle simulation performance compared to
NumPy [
        <xref ref-type="bibr" rid="ref4">4</xref>
        ]. For this project we chose to use CUDA as the acceleration backend because of the
availability of high-level libraries, namely CuPy and AMGX.
      </p>
    </sec>
    <sec id="sec-2">
      <title>2. Particle-in-cell</title>
      <p>The idea of the particle-in-cell method is to simulate individual positions and velocities of
particles over small time steps, while using a grid to approximate collective parameters, such as charge
density and current density created by particles. The simulation domain is defined by a cuboid regular
mesh and a time step. As part of the problem definition, Ef and Ef_pyhton allow defining conducting
volumes with a fixed potential within the simulation domain. Only simple volume shapes, such as a
cylinder or a sphere, are supported right now. The same shapes define particle sources that generate
new particles with a normal velocity distribution within their volumes at each time step. It is possible
to add externally defined electric and magnetic fields.</p>
      <p>Each simulation step within Ef_python consists of the following operations: advance particle
positions and momenta, generate new particles, calculate charge density, compute electric potential,
calculate electric field.</p>
      <sec id="sec-2-1">
        <title>2.1 Grid-to-particle: field interpolation</title>
        <p>To compute the electric field acting on each particle, linear interpolation of the electric field
from the 8 surrounding grid nodes is used. This is not the only possible interpolation kernel.
Grid-toparticle and particle-to-grid kernels are an important area of possible algorithmic optimization to
improve simulation accuracy and performance in this project.</p>
      </sec>
      <sec id="sec-2-2">
        <title>2.2 Particle mover</title>
        <p>
          A “particle mover” denotes the simulation component responsible for updating particle
positions and velocities at each time step. The choice of the particle mover is important for assuring
conservation of important invariants, such as energy. As the particle mover in ef_python, the
wellestablished leapfrog second-order explicit method, known as the Boris scheme, is used [
          <xref ref-type="bibr" rid="ref5">5</xref>
          ].
        </p>
      </sec>
      <sec id="sec-2-3">
        <title>2.3 Particle creation and destruction</title>
        <p>Particles that leave the simulation volume or collide with inner conducting volumes are
absorbed and removed from simulation. New particles generated by particle sources are added to the
system and their momentum is simulated half a time step back as it is required by the “leapfrog”
particle mover.</p>
      </sec>
      <sec id="sec-2-4">
        <title>2.4 Particle-to-grid: particle weighting</title>
        <p>The charge of each particle is linearly divided into the 8 surrounding grid nodes. This
firstorder scheme is sometimes called cloud-in-cell. The collective current is not used in ef or ef_python
right now, as the magnetic field created by particles is considered negligible.</p>
      </sec>
      <sec id="sec-2-5">
        <title>2.5 Field solver</title>
        <p>To simulate the electromagnetic field created by particles, Maxwell’s equations on the grid
can be approximately solved by many algorithms based on 3 main approaches: finite difference
methods (FDM), finite element methods (FEM) and spectral methods. In ef and ef_python, the FDM
method is used to solve the Poisson’s equation and compute the electric potential created by the
simulated particles, while considering the effect of conducting volumes within the simulation domain.
The electric field is then simply a numerically computed gradient of the potential on the grid.</p>
      </sec>
    </sec>
    <sec id="sec-3">
      <title>3. Simulation performance improvements</title>
      <p>The contribution of each step of simulation to the total execution time depends on different
simulation parameters and implementation details. Profiling tools allowed us to find bottlenecks and
improve performance using different simulation setups. The field solver is usually the major
component of simulation in terms of resource usage.</p>
      <sec id="sec-3-1">
        <title>3.1 Algorithm improvements</title>
        <p>As a result of profiling, several unreasonably slow function implementations were fixed,
including rewriting the FDM equation sparse matrix generator using diagonal SciPy matrix classes,
caching which mesh nodes are inside conducting volumes, delegating field-to-particle interpolation to
SciPy, and rewriting particle-to-field weighting for better NumPy parallelization.</p>
        <p>In order to safely conduct the major code restructuring and reimplementation required for this
project, over 200 unit tests were created, providing total code coverage of 91%.</p>
      </sec>
      <sec id="sec-3-2">
        <title>3.2 Algebraic multi-grid solver</title>
        <p>The point of major computation complexity of
ef_python is the field solver. As such, it was the first
component picked for acceleration. The SciPy conjugate
gradient solver for sparse matrices was used in
ef_python before these improvements.</p>
        <p>
          Multigrid methods are methods of numerically
solving differential equations on a hierarchy of grids
with increasing coarseness, allowing one to faster reduce
the large-wavelength error [
          <xref ref-type="bibr" rid="ref6">6</xref>
          ]. Algebraic multigrid
methods construct the multigrid scale hierarchy directly
from the matrix of linear equations, not explicitly
relying on the geometry of the system or the partial
differential equations being solved. Thus, algebraic
multigrid methods are easy to apply without complex
problem-dependent setup. The PyAMG library provides
many AMG methods for Python, and the AMGX library
implements AMG on CUDA. PyAMGX is a Python
wrapper over AMGX, allowing one to run AMGX from
Python, on one GPU only.
        </p>
      </sec>
      <sec id="sec-3-3">
        <title>3.3 Using CuPy for GPU acceleration</title>
        <p>CuPy is a library aiming to help easy
acceleration of the NumPy-based code on
GPU by providing in-place replacements for
most NumPy functions and operators. By
using CuPy we accelerated most of the
simulation operations in ef_python with minor
code changes.</p>
        <p>Grid interpolation was not available in
CuPy and had to be rewritten as a custom
CUDA kernel. The gradient function from
Numpy that was used to compute the electric
field from the potential was not implemented Figure 2 Comparison of the simulation time for the
in CuPy. It had to be reimplemented for the
case of regular grid steps using CuPy two-stream instability problem with higher precision
operators. of the field solver and a smaller number of particles</p>
        <p>Simulation classes were rewritten to
use class injection for the convenient runtime NumPy/CuPy and PyAMG/AMGX selection. This
allowed using the selected simulation backend with minimal code duplication. Only the field solver
and spatial mesh classes required explicit implementation-dependent subclasses.</p>
      </sec>
    </sec>
    <sec id="sec-4">
      <title>4. Sample simulation</title>
      <p>are shown in Figure 2 and Figure 3.</p>
    </sec>
    <sec id="sec-5">
      <title>5. Results and acknowledgements</title>
      <p>In addition to unit tests, sample
simulation was regularly performed to detect
errors during development. It was the
simulation of a radially symmetric charged
particle beam starting from a cylindrical
emitter. The shape of the beam caused by
electrostatic particle repulsion can be predicted
analytically. The comparison between the
theoretical shape and the numerical solution is
shown in Figure 1.</p>
      <p>The simulation of two-stream instability
was performed with ef using OpenMP with 1
and 12 threads to provide a baseline of
comparison. The performance was compared to
ef_python using CuPy and PyAMGX running
on Nvidia Tesla K40 and Nvidia Tesla K80
graphics cards. The results of this comparison</p>
      <p>With minimal changes to the program, CuPy allowed accelerating most of the simulation
operations on GPU through CUDA. In addition, to improve the field solver, algebraic multigrid
methods, implemented by the PyAMG library on CPU and the AMGX library on GPU, were utilized.
Major speed-up was achieved, especially when running simulations with very high-resolution spatial
grids on high-performance GPUs.</p>
      <p>Using multiple GPUs is an important future prospect for the ef_python application. This will
require using MPI for process coordination and data exchange. AMGX already has MPI-based
multiGPU support, but the PyAMGX interface library will have to be extended to add it in. Another
promising avenue of future research is using OpenCL as an acceleration backend, as it can utilize
nonNvidia GPUs.</p>
      <p>
        Profiling and testing were partly performed on the HybriLIT heterogeneous computing
platform [
        <xref ref-type="bibr" rid="ref7">7</xref>
        ] in the Laboratory of Information Technologies at JINR.
      </p>
      <p>The reported study was funded by RFBR according to the research project № 18-32-00239.</p>
    </sec>
  </body>
  <back>
    <ref-list>
      <ref id="ref1">
        <mixed-citation>
          [1]
          <string-name>
            <surname>Donets</surname>
            ,
            <given-names>E.D.</given-names>
          </string-name>
          ,
          <year>1976</year>
          .
          <article-title>Review of the JINR Electron Beam Ion Sources</article-title>
          .
          <source>IEEE Transactions on Nuclear Science</source>
          <volume>23</volume>
          , 897. https://doi.org/10.1109/TNS.
          <year>1976</year>
          .4328375
        </mixed-citation>
      </ref>
      <ref id="ref2">
        <mixed-citation>
          [2]
          <string-name>
            <surname>Ef</surname>
          </string-name>
          [WWW Document], n.d. .
          <source>GitHub</source>
          . URL https://github.com/epicf (accessed
          <volume>11</volume>
          .
          <year>11</year>
          .19).
        </mixed-citation>
      </ref>
      <ref id="ref3">
        <mixed-citation>
          [3]
          <string-name>
            <surname>Grigorʹev</surname>
            ,
            <given-names>I.N.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Vshivkov</surname>
            ,
            <given-names>V.A.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Fedoruk</surname>
            ,
            <given-names>M.P.</given-names>
          </string-name>
          ,
          <year>2002</year>
          .
          <article-title>Numerical “particle-in-cell” methods: theory and applications</article-title>
          . VSP, Utrecht; Boston.
        </mixed-citation>
      </ref>
      <ref id="ref4">
        <mixed-citation>
          [4]
          <string-name>
            <surname>Boytsov</surname>
            ,
            <given-names>A.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Kadochnikov</surname>
            ,
            <given-names>I.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Zuev</surname>
            ,
            <given-names>M.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Bulychev</surname>
            ,
            <given-names>A.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Zolotuhin</surname>
            ,
            <given-names>Y.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Getmanov</surname>
            ,
            <given-names>I.</given-names>
          </string-name>
          ,
          <year>2018</year>
          .
          <article-title>COMPARISON OF PYTHON 3 SINGLE-GPU PARALLELIZATION TECHNOLOGIES ON THE EXAMPLE OF A CHARGED PARTICLES DYNAMICS SIMULATION PROBLEM 5</article-title>
          .
        </mixed-citation>
      </ref>
      <ref id="ref5">
        <mixed-citation>
          [5]
          <string-name>
            <surname>Qin</surname>
            ,
            <given-names>H.</given-names>
          </string-name>
          , Zhang,
          <string-name>
            <given-names>S.</given-names>
            ,
            <surname>Xiao</surname>
          </string-name>
          ,
          <string-name>
            <surname>J.</surname>
          </string-name>
          , Liu,
          <string-name>
            <given-names>J.</given-names>
            ,
            <surname>Sun</surname>
          </string-name>
          ,
          <string-name>
            <given-names>Y.</given-names>
            ,
            <surname>Tang</surname>
          </string-name>
          ,
          <string-name>
            <surname>W.M.</surname>
          </string-name>
          ,
          <year>2013</year>
          .
          <article-title>Why is Boris algorithm so good?</article-title>
          <source>Physics of Plasmas</source>
          <volume>20</volume>
          ,
          <fpage>084503</fpage>
          . https://doi.org/10.1063/1.4818428
        </mixed-citation>
      </ref>
      <ref id="ref6">
        <mixed-citation>
          [6]
          <string-name>
            <surname>Shapira</surname>
            ,
            <given-names>Y.</given-names>
          </string-name>
          ,
          <year>2003</year>
          .
          <source>Matrix-Based Multigrid: Theory and Applications</source>
          . Springer Science &amp; Business Media.
        </mixed-citation>
      </ref>
      <ref id="ref7">
        <mixed-citation>
          [7]
          <string-name>
            <surname>Gh. Adam</surname>
            ,
            <given-names>M.</given-names>
          </string-name>
          <string-name>
            <surname>Bashashin</surname>
            ,
            <given-names>D.</given-names>
          </string-name>
          <string-name>
            <surname>Belyakov</surname>
            ,
            <given-names>M.</given-names>
          </string-name>
          <string-name>
            <surname>Kirakosyan</surname>
            ,
            <given-names>M.</given-names>
          </string-name>
          <string-name>
            <surname>Matveev</surname>
            ,
            <given-names>D.</given-names>
          </string-name>
          <string-name>
            <surname>Podgainy</surname>
            ,
            <given-names>T.</given-names>
          </string-name>
          <string-name>
            <surname>Sapozhnikova</surname>
            ,
            <given-names>O.</given-names>
          </string-name>
          <string-name>
            <surname>Streltsova</surname>
          </string-name>
          , Sh.
          <string-name>
            <surname>Torosyan</surname>
            ,
            <given-names>M.</given-names>
          </string-name>
          <string-name>
            <surname>Vala</surname>
            ,
            <given-names>L.</given-names>
          </string-name>
          <string-name>
            <surname>Valova</surname>
            ,
            <given-names>A.</given-names>
          </string-name>
          <string-name>
            <surname>Vorontsov</surname>
            ,
            <given-names>T.</given-names>
          </string-name>
          <string-name>
            <surname>Zaikina</surname>
            , E. Zemlyanaya,
            <given-names>M.</given-names>
          </string-name>
          <string-name>
            <surname>Zuev</surname>
          </string-name>
          .
          <article-title>IT-ecosystem of the HybriLIT heterogeneous platform for high-performance computing and training of IT-specialists</article-title>
          .
          <source>Selected Papers of the 8th International Conference “Distributed Computing and Grid-technologies in Science and Education” (GRID</source>
          <year>2018</year>
          ), Dubna, Russia,
          <source>September 10-14</source>
          ,
          <year>2018</year>
          , CEUR-WS.org/Vol-
          <volume>2267</volume>
          ″
        </mixed-citation>
      </ref>
    </ref-list>
  </back>
</article>