<!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 />
    <article-meta>
      <title-group>
        <article-title>COMPARISON OF PYTHON 3 SINGLE-GPU PARALLELIZATION TECHNOLOGIES ON THE EXAMPLE OF A CHARGED PARTICLES DYNAMICS SIMULATION PROBLEM</article-title>
      </title-group>
      <contrib-group>
        <contrib contrib-type="author">
          <string-name>A. Boytsov</string-name>
          <email>boytsov@jinr.ru</email>
          <xref ref-type="aff" rid="aff1">1</xref>
          <xref ref-type="aff" rid="aff4">4</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>I. Kadochnikov</string-name>
          <xref ref-type="aff" rid="aff1">1</xref>
          <xref ref-type="aff" rid="aff2">2</xref>
          <xref ref-type="aff" rid="aff4">4</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>M. Zuev</string-name>
          <email>zuevmax@jinr.ru</email>
          <xref ref-type="aff" rid="aff1">1</xref>
          <xref ref-type="aff" rid="aff4">4</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>A. Bulychev</string-name>
          <xref ref-type="aff" rid="aff0">0</xref>
          <xref ref-type="aff" rid="aff4">4</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Ya. Zolotuhin</string-name>
          <xref ref-type="aff" rid="aff3">3</xref>
          <xref ref-type="aff" rid="aff4">4</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>I. Getmanov</string-name>
          <email>igor.getmanov1@gmail.com</email>
          <xref ref-type="aff" rid="aff3">3</xref>
          <xref ref-type="aff" rid="aff4">4</xref>
        </contrib>
        <aff id="aff0">
          <label>0</label>
          <institution>Independent researcher</institution>
          ,
          <addr-line>Moscow</addr-line>
          ,
          <country country="RU">Russia</country>
        </aff>
        <aff id="aff1">
          <label>1</label>
          <institution>Joint Institute for Nuclear Research</institution>
          ,
          <addr-line>6 Joliot-Curie, Dubna, Moscow region, 141980</addr-line>
          ,
          <country country="RU">Russia</country>
        </aff>
        <aff id="aff2">
          <label>2</label>
          <institution>Plekhanov Russian University of Economics</institution>
          ,
          <addr-line>36 Stremyanny per., Moscow, 117997</addr-line>
          ,
          <country country="RU">Russia</country>
        </aff>
        <aff id="aff3">
          <label>3</label>
          <institution>The First Electrotechnical University “LETI”</institution>
          ,
          <addr-line>5 Professora Popova, St. Petersburg, 197376</addr-line>
          <country country="RU">Russia</country>
        </aff>
        <aff id="aff4">
          <label>4</label>
          <institution>2018 Alexey Boytsov</institution>
          ,
          <addr-line>Ivan Kadochnikov, Maxim Zuev, Andrey Bulychev, Yaroslav Zolotuhin, Igor Getmanov</addr-line>
        </aff>
      </contrib-group>
      <pub-date>
        <year>2018</year>
      </pub-date>
      <fpage>518</fpage>
      <lpage>522</lpage>
      <abstract>
        <p>Low energy ion and electron beams, produced by ion sources and electron guns, find their use in surface modification, nuclear medicine and injection into high-energy accelerators. Simulation of particle dynamics is a necessary step for optimization of beam parameters. Since such simulations require significant computational resources, parallelization is highly desirable to be able to accomplish them in a reasonable amount of time. From the implementation standpoint, dynamically typed interpreted languages, such as Python 3, allow high development speed that comes at cost of performance. It is tempting to transfer all computationally heavy tasks on a GPU to alleviate this drawback. Using the example of a charged particles dynamics simulation problem, various GPUparallelization technologies available in Python 3 are compared in terms of ease of use and computational speed. The reported study was funded by RFBR according to the research project № 18-32-00239\18. Computations were in part held on the basis of the heterogeneous computing cluster HybriLIT (LIT, JINR)</p>
      </abstract>
      <kwd-group>
        <kwd>Python3</kwd>
        <kwd>GPU computation</kwd>
        <kwd>particle dynamics</kwd>
        <kwd>parallelization</kwd>
        <kwd>Numba</kwd>
        <kwd>CUDA</kwd>
        <kwd>OpenCL</kwd>
      </kwd-group>
    </article-meta>
  </front>
  <body>
    <sec id="sec-1">
      <title>1. Introduction</title>
      <p>
        The problem of charged particle dynamics simulation has been a challenge for scientists and
engineers in many fields, such as plasma physics, accelerator engineering, beam transport, ion sources,
high current facilities etc. Simulation allows to predict conditions and results of experiments,
facilitating experiment facilities design. In the case of an ion source, it is necessary to simulate an
order of 1012 particles [
        <xref ref-type="bibr" rid="ref1">1</xref>
        ]. But simulation on this scale can take weeks of CPU time. There are many
approaches to drastically speed up computation by parallelization onto distributed clusters or hardware
accelerators. Using these techniques complicates development and typically there is a compromise
between ease of implementation and computation speed. Nowadays, GPUs have become de-facto
standard for parallel computation acceleration: most of the highest-performant supercomputers in the
world are GPU-based [
        <xref ref-type="bibr" rid="ref2">2</xref>
        ]. Therefore it is essential to utilize GPU capabilities for charge particle
simulation.
      </p>
      <p>
        A program utilizing a GPU for computing has components running on the CPU and GPU. The
GPU component performs the compute-intensive and parallelizable operations, and the CPU
component performs all the other tasks, such as managing the computation, configuring the GPU and
data I/O. It is necessary to explicitly manage data transfer between the CPU and GPU components of
the program. To manage the communication between the GPU and the program there are proprietary
and open APIs: Nvidia CUDA[
        <xref ref-type="bibr" rid="ref3">3</xref>
        ], AMD ROCm, OpenCL, SYCL, vulcan. They can be utilized from
many programming languages using libraries native to the language.
      </p>
      <p>
        We tested 3 popular libraries for GPU computing available for Python 3: Numba[
        <xref ref-type="bibr" rid="ref4">4</xref>
        ],
PyCUDA[
        <xref ref-type="bibr" rid="ref5">5</xref>
        ], PyOpenCL[
        <xref ref-type="bibr" rid="ref6">6</xref>
        ]. Numba translates Python functions to optimized machine code at runtime
using the industry-standard LLVM compiler library. Numba-compiled numerical algorithms in Python
can approach the speeds of native compiled code written in C or FORTRAN. Moreover, Numba
supports using Nvidia CUDA and AMD ROCm to translate and run python functions on the GPU.
PyCUDA gives access to Nvidia’s CUDA parallel computation API and PyOpenCL gives access to
OpenCL API. We want to compare all three available solutions on the example of a model problem. In
the next section we describe the example problem and then describe the obtained results.
      </p>
    </sec>
    <sec id="sec-2">
      <title>2. Example Problem Description</title>
      <p>We chose a simple problem as our example: simulating the classical dynamics of a set of N
identical charged particles in a 2-dimensional volume of space. Namely, each particle is represented as
its 2 cartesian coordinates, 2 velocity components, charge and mass. Dynamics equation are
dx = v ∙ dt, mdv= F ∙ dt
The integration was done using the basic Euler method with a constant time step.</p>
      <p>xt+1 = xt + vt ∙ h, vt+1 = vt + at ∙ h
The force acting on each particle simply as sum of Coulomb forces from the other particles.</p>
      <p>qiqj⃗r⃗⃗i⃗j
⃗F⃗⃗⃗i = ∑</p>
      <p>rij3
i≠j</p>
      <p>Coulomb’s constant is 1 here, as we use arbitrary units for simulation. The particles are
contained in a two-dimensional square volume. On reaching the boundary, a particle gets perfectly
elastically reflected. Initially the particle positions are evenly non-randomly distributed on a spatial
grid, while the velocity components are uniformly distributed in the interval (-Vmax, Vmax). Observing
the coordinate and velocity distribution of the particles over time allows to debug the simulation
implementations.
Proceedings of the VIII International Conference "Distributed Computing and Grid-technologies in Science and</p>
      <p>Education" (GRID 2018), Dubna, Moscow region, Russia, September 10 - 14, 2018</p>
    </sec>
    <sec id="sec-3">
      <title>3. Performance metrics</title>
      <p>To compare different GPU acceleration libraries for simulation a one-step function
was
implemented with each library using analogous code. Then the performance of simulation for each
library was measured depending on the scale of simulation. At each scale the number of particles and
volume dimensions were set, then the initial particle positions and velocities were generated. The data
was copied to GPU memory, and one batch of simulation steps was performed and timed, each step
corresponding to one step of numerical integration. The batch size was varied with scale, to keep the
batch processing time at a consistent level. The batch needed to be long enough to allow measuring
performance accurately, but short enough to measure and plot at many scales.</p>
      <p>For
each
batch, the
total simulation
time
was
measured
using the
standard
python
time.perf_counter() function. This was then converted into steps per second, particle updates per
second and particle interactions per second, which are more comparable across scales.</p>
    </sec>
    <sec id="sec-4">
      <title>4. Test results</title>
      <p>The development and performance testing were carried out using Google Colaboratory on a Nvidia
Tesla K80 GPU, Driver Version 396.44. Library versions are given in Table 1.
In this test both Numba and OpenCL used CUDA as the back-end interface to the GPU.
Numpy runs on the CPU and provides the baseline performance to compare GPU speed-up.</p>
      <p>The plots of performance results are shown in Figure 2. At small scales (n &lt; 102) GPU steps
per second are constant, while CPU stays ahead. This suggests that start-up overhead dominates
perparticle computation. At scales of 102 &lt; n &lt; 104 it can be observed that particle updates per second is
near-constant, that is to say, computation time grows as O(n). As each particle update consists of O(n)
force computations to derive acceleration, we can conclude that at these scales all update streams run
perfectly in parallel. As a check, one can compare the update thread count (n) to the number of threads
the GPU can execute in parallel. This can be derived from the GPU multiprocessor count and max
threads per multiprocessor. For the Tesla K80, this is 13 × 2048 = 26624 which agrees with the
critical point that can be seen at ~2 ∙ 104. At large scales of  &gt; 104 the GPU is fully utilized and
particle interactions per second tend to a constant value, signifying O(n2) performance.</p>
    </sec>
    <sec id="sec-5">
      <title>5. Conclusion</title>
      <p>We compared several GPU parallelization technologies available for Python 3. All provide
considerable (~103) speed-up compared to CPU computing using Numpy. OpenCL seems to
outperform Numpy and CUDA. However, this test did not check all factors that may affect GPU
performance. Among those outside of scope of this paper are: data type and shape of arrays being
processed, thread block size and shape, using scalar or constant parameters for mass and charge,
library version, driver version, GPU model, optimized block caching, optimizing the algorithm.</p>
    </sec>
    <sec id="sec-6">
      <title>Acknowledgements</title>
      <p>The reported study was funded by RFBR according to the research project № 18-32-00239\18.
Computations were held partially on the basis of the heterogeneous computing cluster HybriLIT
(LIT, JINR).</p>
    </sec>
  </body>
  <back>
    <ref-list>
      <ref id="ref1">
        <mixed-citation>
          [1]
          <string-name>
            <given-names>E. D.</given-names>
            <surname>Donets</surname>
          </string-name>
          , “
          <article-title>Review of the JINR Electron Beam Ion Sources,”</article-title>
          <source>IEEE Transactions on Nuclear Science</source>
          , vol.
          <volume>23</volume>
          , no.
          <issue>2</issue>
          , pp.
          <fpage>897</fpage>
          -
          <lpage>903</lpage>
          , Apr.
          <year>1976</year>
          .
        </mixed-citation>
      </ref>
      <ref id="ref2">
        <mixed-citation>
          [2] “June 2018 |
          <article-title>TOP500 Supercomputer Sites</article-title>
          .” [Online]. Available: https://www.top500.org/lists/2018/06/. [Accessed:
          <fpage>06</fpage>
          -Nov-2018].
        </mixed-citation>
      </ref>
      <ref id="ref3">
        <mixed-citation>
          [3] “CUDA Zone,” NVIDIA Developer,
          <fpage>18</fpage>
          -Jul-
          <year>2017</year>
          . [Online]. Available: https://developer.nvidia.com/cuda-zone. [Accessed:
          <fpage>06</fpage>
          -Nov-2018].
        </mixed-citation>
      </ref>
      <ref id="ref4">
        <mixed-citation>
          <article-title>[4] “Numba: A High Performance Python Compiler</article-title>
          .” [Online]. Available: https://numba.pydata.org/. [Accessed:
          <fpage>06</fpage>
          -Nov-2018].
        </mixed-citation>
      </ref>
      <ref id="ref5">
        <mixed-citation>
          [5] “PyCUDA.” [Online]. Available: https://mathema.tician.de/software/pycuda/. [Accessed:
          <fpage>06</fpage>
          -
          <lpage>Nov2018</lpage>
          ].
        </mixed-citation>
      </ref>
      <ref id="ref6">
        <mixed-citation>
          [6] “PyOpenCL.” [Online]. Available: https://mathema.tician.de/software/pyopencl/. [Accessed:
          <fpage>06</fpage>
          -Nov-2018].
        </mixed-citation>
      </ref>
    </ref-list>
  </back>
</article>