<!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>PERFORMANCE ANALYSIS AND OPTIMIZATION OF MPDROOT</article-title>
      </title-group>
      <contrib-group>
        <contrib contrib-type="author">
          <string-name>J. Buša Jr.</string-name>
          <xref ref-type="aff" rid="aff0">0</xref>
          <xref ref-type="aff" rid="aff1">1</xref>
          <xref ref-type="aff" rid="aff2">2</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>S. Hnatič</string-name>
          <xref ref-type="aff" rid="aff1">1</xref>
          <xref ref-type="aff" rid="aff2">2</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>O.V. Rogachevsky</string-name>
          <xref ref-type="aff" rid="aff1">1</xref>
          <xref ref-type="aff" rid="aff3">3</xref>
        </contrib>
        <aff id="aff0">
          <label>0</label>
          <institution>Institute of Experimental Physics, Slovak Academy of Sciences</institution>
          ,
          <addr-line>Watsonova 47, Košice, 04001</addr-line>
          ,
          <country country="SK">Slovakia</country>
        </aff>
        <aff id="aff1">
          <label>1</label>
          <institution>Ján Buša Jr, Slavomír Hnatič</institution>
          ,
          <addr-line>Oleg Rogachevsky</addr-line>
        </aff>
        <aff id="aff2">
          <label>2</label>
          <institution>Mescheryakov Laboratory of Information Technologies, Joint Institute for Nuclear Research</institution>
          ,
          <addr-line>20 Joliot-Curie, Dubna, Moscow Region, 141980</addr-line>
          ,
          <country country="RU">Russia</country>
        </aff>
        <aff id="aff3">
          <label>3</label>
          <institution>Veksler and Baldin Laboratory of High Energy Physics, Joint Institute for Nuclear Research</institution>
          ,
          <addr-line>4 Baldin St., Dubna., Moscow Region, 141980</addr-line>
          ,
          <country country="RU">Russia</country>
        </aff>
      </contrib-group>
      <pub-date>
        <year>2021</year>
      </pub-date>
      <fpage>5</fpage>
      <lpage>9</lpage>
      <abstract>
        <p>MPDRoot is the software framework for simulation, reconstruction and physics analysis of the simulated and experimental data for MPD experiment at NICA. It is planned to obtain ~ 108 events of heavy ion collisions for physics analysis, hence it is crucial to have the effective and efficient methodology of the systematic performance improvement of MPDRoot's backend. In this work, we present the analysis of timing and instruction performance of MPDRoot's reconstruction by benchmarks and the Callgrind profiler. We evaluate the feasibility of speeding up reconstruction by the reduction of method-call overhead and the possible benefit of optimizing the math library. Based on the obtained results we draw conclusions about necessary steps to be taken in the near future of MPDRoot's development.</p>
      </abstract>
      <kwd-group>
        <kwd>MPD</kwd>
        <kwd>MPDRoot</kwd>
        <kwd>benchmarking</kwd>
        <kwd>code review</kwd>
        <kwd>code quality</kwd>
        <kwd>optimization</kwd>
      </kwd-group>
    </article-meta>
  </front>
  <body>
    <sec id="sec-1">
      <title>1. Introduction</title>
      <p>
        The architecture of MPDRoot is shown in Fig.1 and is composed of the three main parts [
        <xref ref-type="bibr" rid="ref1">1</xref>
        ]:
      </p>
      <p>Root – a set of building blocks and primitives written in C++ language, tailored for physics
experiments
FairRoot – simulation, reconstruction and analysis framework built on top of Root and the other
set of packages encapsulated in FairSoft</p>
      <p>MPDRoot – specific implementation for the MPD experiment at NICA</p>
    </sec>
    <sec id="sec-2">
      <title>2. Instruction and run-time profiling of MPDRoot</title>
      <p>
        The process of optimization requires finding runtime bottlenecks, which is commonly
achieved by measuring performance of various software entities in units of time and instructions. For
this purpose, MPDRoot and FairSoft/FairRoot suite must be built with debug symbols. The
information about the instruction profile of various parts of the MPDRoot’s reconstruction is obtained
by running the Callgrind tool from the Valgrind suite [
        <xref ref-type="bibr" rid="ref2">2</xref>
        ]. The output from the Callgrind profiler can
then be visualized in a KCachegrind tool.
      </p>
      <p>However, from the practical point of view, it is the physical time the software spends in its
given entity, which represents the true performance measure. The accurate measurement of such
performance is somewhat problematic as it uses expensive system clock calls. If often used, these
skew results and the direct timings of low-cost software entities are usually completely invalid. The
pie chart in Fig. 2 shows the difference between the instruction profiler results and the true physical
time for the various tasks of the MPDRoot’s reconstruction. This means, the instruction profile
obtained from Callgrind is suitable for finding performance bottlenecks.</p>
      <p>MPDRoot spends most of the time in digitizing (~45%) and clusterization (~48%) followed by
the Kalman filter (~5%) and the Fair engine (less than 1%). Hence, it is logical to look into digitizer
and clusterization algorithms for speedup optimization at the current stage of MPDRoot’s
development.</p>
    </sec>
    <sec id="sec-3">
      <title>3. Reducing method-call overhead</title>
      <p>It is possible to speedup the code by reducing the method-call overhead with the inline
keyword (or the flatten attribute), however the result of code inlining is many times counterproductive
[3]. In the most common case, the removal of the call stack results in a larger executable binary, which
in turn slows down the execution time. The table in Fig.3 shows the effect of reducing the method -call
overhead by inlining the most frequently called methods from Digitizer and ClusterFinder tasks.
% instructions
out of total
instructions
per call
% of calls
inlined
CalcOrigin
(Digitizer task)
GetCij
(ClusterFinder task)
4.8
12
18
380
100
90
task
speedup
4.2%
-1.2%
total
speedup
1.9%
-6.3%</p>
      <p>While inlining the cheap CalcOrigin method (18 instructions per call) results in an overall
speedup by 1.9% of time, inlining the more expensive GetCij method (380 instructions per call) slows
down the total runtime. Despite the obvious positive effect, inlining the CalcOrigin method at the
current stage of development is not a good idea, as this method can be modified in the future.</p>
    </sec>
    <sec id="sec-4">
      <title>4. Benchmarking and optimization of math methods</title>
      <p>Such a closed for modification, well tested software entity used in MPDRoot is the TMath
library. The results of the benchmark of TMath methods running in MPDRoot are presented in the
upper plot of Fig.4. The power, logarithm, and trigonometric are the most expensive methods.</p>
      <p>If one is careful with cumulative errors, the quickest way to achieve speedup of math methods
is to replace default double precision methods with their float precision analogues. Thus, one can get
up to 45% speedup (bottom plot of Fig.4).</p>
    </sec>
    <sec id="sec-5">
      <title>6. Acknowledgements References</title>
      <p>The plot in Fig.5 shows the instruction percentage of the five most present math methods in
MPDRoot’s reconstruction. Overall, the whole TMath library takes less than 2.5% out of the total
number of instructions, therefore any optimization of math library is currently not justifiable as it will
not lead to any significant reconstruction speedup.</p>
    </sec>
    <sec id="sec-6">
      <title>5. Conclusions and near future perspectives</title>
      <p>
        It is shown, that the effective methodology of MPDRoot’s optimization consists of firstly
isolating bottlenecks by timing benchmarks and instruction profiling. Out of those, the reasonable
candidates for optimization are entities already closed for modification in the MPDRoot’s
development lifecycle. Having such software entities in the MPDRoot’s codebase is necessary for
effective optimization. This means it is crucial to focus on improving the software quality, which will
in turn result in a well-tested modularized professional grade code [4], [5], [
        <xref ref-type="bibr" rid="ref3">6</xref>
        ].
      </p>
      <p>We will implement the following changes to the MPDRoot’s software development process:
1. Implementation of the code ownership feature - essential for the code review process.
2. Implementation of the QA tests engine – to minimize risks associated with algorithm logic
changes or extensions
3. Implementation of the unit test engine – to minimize risks associated with system changes or
extensions</p>
      <p>The work was supported by the RFBR grant (“Megascience – NICA”) No. 18-02-40102.</p>
    </sec>
  </body>
  <back>
    <ref-list>
      <ref id="ref1">
        <mixed-citation>
          [1]
          <string-name>
            <surname>Rogachevsky</surname>
            ,
            <given-names>O.V.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Bychkov</surname>
            ,
            <given-names>A.V.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Krylov</surname>
            ,
            <given-names>A.V.</given-names>
          </string-name>
          et al.
          <article-title>Software Development and Computing for the MPD Experiment</article-title>
          .
          <source>Phys. Part. Nuclei</source>
          <volume>52</volume>
          ,
          <fpage>817</fpage>
          -
          <lpage>820</lpage>
          (
          <year>2021</year>
          ).
        </mixed-citation>
      </ref>
      <ref id="ref2">
        <mixed-citation>
          [2] https://valgrind.org/docs/manual/cl-manual.
          <source>html (accessed 09.09</source>
          .
          <year>2021</year>
          ) [3] https://isocpp.org/wiki/faq/inline-functions
          <source>(accessed 09.09</source>
          .
          <year>2021</year>
          ) [4]
          <string-name>
            <surname>Robert</surname>
            <given-names>C.</given-names>
          </string-name>
          <article-title>Martin (“Uncle Bob”), Principles of OOD</article-title>
          . Available at: http://butunclebob.com/ArticleS.UncleBob.
          <source>PrinciplesOfOod (accessed 09.09</source>
          .
          <year>2021</year>
          ) [5]
          <string-name>
            <given-names>K.J.</given-names>
            <surname>Lieberherr</surname>
          </string-name>
          , I.M. Holland,
          <article-title>Assuring good style for object-oriented programs</article-title>
          .
          <source>IEEE Software. September</source>
          <year>1989</year>
          , pp.
          <fpage>38</fpage>
          -
          <lpage>48</lpage>
          , vol.
          <fpage>6</fpage>
        </mixed-citation>
      </ref>
      <ref id="ref3">
        <mixed-citation>
          [6]
          <string-name>
            <given-names>Andy</given-names>
            <surname>Hunt</surname>
          </string-name>
          , David Thomas,
          <source>The Art of Enbugging, IEEE Software, January/February</source>
          <year>2003</year>
          , pp.
          <fpage>10</fpage>
          -
          <lpage>11</lpage>
          , vol.
          <volume>20</volume>
          .
        </mixed-citation>
      </ref>
    </ref-list>
  </back>
</article>