% Created 2024-10-24 Thu 13:07
% Intended LaTeX compiler: pdflatex
\documentclass[usenames,dvipsnames,colorlinks,aspectratio=169]{beamer}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{longtable}
\usepackage{wrapfig}
\usepackage{rotating}
\usepackage[normalem]{ulem}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{capt-of}
\usepackage{hyperref}
\usepackage{color}
\usepackage{listings}
\usepackage{pdfpages}
\usepackage{xcolor}
\usepackage{graphicx}
\usepackage{xspace}
\usepackage{siunitx}
\usepackage{libertine}
\usepackage{fontawesome}
\mode<beamer>{\usetheme{Boadilla}}
\setbeamertemplate{navigation symbols}{}
\setbeamerfont{alerted text}{series=\bfseries}
\setbeamercolor{alerted text}{fg=blue}
\usecolortheme{default}
\usefonttheme{serif}
\usefonttheme{professionalfonts}
\lstloadlanguages{[ISO]C++}
\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.50,0,0.82}
\definecolor{codeorange}{rgb}{0.95,0.3,0.1}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}
\lstdefinestyle{mystyle}{
commentstyle=\color{codegreen},
keywordstyle=\color{codeorange},
numberstyle=\tiny\color{codegray},
stringstyle=\color{codepurple},
basicstyle=\footnotesize,
breakatwhitespace=false,
breaklines=true,
captionpos=b,
keepspaces=true,
numbers=left,
numbersep=5pt,
showspaces=false,
showstringspaces=false,
showtabs=false,
tabsize=2
}
\lstset{style=mystyle}
\usetheme{default}
\author{Brett Viren}
\date{\today}
\title{Wire-Cell Arrays}
\hypersetup{
 pdfauthor={Brett Viren},
 pdftitle={Wire-Cell Arrays},
 pdfkeywords={},
 pdfsubject={},
 pdfcreator={Emacs 28.2 (Org mode 9.7-pre)}, 
 pdflang={English}}
\begin{document}

\maketitle
\begin{frame}[label={sec:org1d70daf},fragile]{Problem Context}
 \begin{block}{Novel developments beginning now/soon:}
All are ``array-oriented'' and benefit from GPU-acceleration.

\begin{itemize}
\item Prompt signal processing
\item LAr pixel signal processing and simulation
\item Porting prototype pattern recognition
\end{itemize}
\end{block}
\begin{block}{Existing WCT data has baggage}
Currently, no really solid ``array'' data type in WCT.

\begin{itemize}
\item We have Eigen but it lacks GPU support and many algorithms.
\item We have flexible but non-array and complex data types, (\texttt{IFrame/ICluster/ITensor} and PC tree).
\item What GPU support we have, requires CPU/GPU/CPU round trips and data transformations.
\end{itemize}
\end{block}
\end{frame}
\begin{frame}[label={sec:org68aa688},fragile]{Basic solution concept}
 \begin{block}{Mimic patterns in the successful Python packages: Numpy, cupy, JAX, PyTorch}
\begin{itemize}
\item Whole-array operations allowing vectorizing and other optimizations.
\item Transparent choice of execution on CPU or GPU and other heterogeneous patterns.
\item Pass arrays without CPU/GPU/CPU round-trip.
\end{itemize}
\end{block}
\begin{block}{While fitting into some WCT-specific needs}
\begin{itemize}
\item Thread-safe exchange of data between WCT components.
\item Efficient transfer to/from existing Torch-based inference (DNNROI).
\end{itemize}
\end{block}
\begin{block}{And beautify some WCT ugliness}
\begin{itemize}
\item Provide a simpler and general \texttt{IData} model than currently exists.
\item Better alternative to some inconsistent and under-performing hand-wrought code.
\end{itemize}
\end{block}
\end{frame}
\begin{frame}[label={sec:orgb993408},fragile]{Two ``obvious'' candidates}
 \begin{block}{LibTorch ``tensors''}
\begin{itemize}
\item Good: WCT already uses it for DNNROI and \texttt{IDFT}, hugely popular with large community, generally good performance.
\item Bad: Python-first, C++ second, truly horrible documentation, non-trivial install, not \texttt{const}-correct.
\end{itemize}
\end{block}
\begin{block}{ArrayFire ``arrays''}
\begin{itemize}
\item Good: Fairly popular, C++-first, \texttt{const}-correct, excellent documentation.
\item Bad: Somewhat poor performance in certain critical benchmarks.
\end{itemize}
\end{block}
\end{frame}
\begin{frame}[label={sec:orgea28960},fragile]{Performance analysis}
 \begin{itemize}
\item WCT \texttt{OmnibusSigProc} was profiled to identify the major bottlenecks.
\begin{itemize}
\item Convolution via FFT, median, sort and general array arithmetic.
\end{itemize}
\item An apples-to-apples benchmark application was developed
\begin{itemize}
\item Targets: ArrayFire (AF), LibTorch (LT) and Eigen/FFTW/std::algorithms (EI).
\item Abstract, templated core benchmark algs with generic templated array mini-API.
\item Each target specializes the mini-API.
\end{itemize}
\item Configurable to explore 3 axes: array type, device and test.
\item Times are in units of CPU-core or GPU millisecond
\begin{itemize}
\item Average over many runs that span 1-10 seconds total.
\end{itemize}
\item Results use random arrays of size 1024x8192.
\begin{itemize}
\item Sizes relevant to LArTPC frames give consistent results.
\end{itemize}
\end{itemize}
\end{frame}
\begin{frame}[label={sec:org481f2f0}]{Arithmetic}
\begin{center}
\begin{tabular}{llr}
dev & tech & time\\
\hline
GPU & AF & 0.14\\
GPU & LT & 0.36\\
\hline
CPU & AF & 33.40\\
CPU & LT & 11.91\\
CPU & EI & 18.78\\
\hline
\end{tabular}
\end{center}

This test simply applies add, multiply and accumulate.
\end{frame}
\begin{frame}[label={sec:org8297d3b}]{FFT convolution}
\begin{center}
\begin{tabular}{llrl}
dev & tech & time & notes\\
\hline
GPU & AF & 2.20 & \\
GPU & AF & 2.02 & fwd is r2c\\
GPU & LT & 0.89 & \\
\hline
CPU & AF & 667.74 & \\
CPU & AF & 414.30 & fwd is r2c\\
CPU & LT & 158.24 & \\
CPU & EI & 489.61 & fftw3\\
CPU & EI & 309.31 & fftw3 fwd is r2c\\
\hline
\end{tabular}
\end{center}

Time includes forward DFT, multiplication in Fourier domain with pre-transformed kernel and return to interval domain with inverse DFT.
\begin{itemize}
\item The ``r2c'' label indicates explicit application of real-to-complex optimization for forward DFT.
\end{itemize}
\end{frame}
\begin{frame}[label={sec:org7b7c309},fragile]{Median and sort}
 \begin{center}
\begin{tabular}{lllrl}
\hline
median & GPU & AF & 1.30 & \\
median & GPU & LT & 0.18 & \\
\hline
median & CPU & AF & 1109.09 & \\
median & CPU & LT & 76.73 & \\
median & CPU & EI & 90.98 & \texttt{std::nth\_element}\\
\hline
\hline
sort & GPU & AF & 1.33 & \\
sort & GPU & LT & 1.55 & \\
\hline
sort & CPU & AF & 1119.37 & \\
sort & CPU & LT & 383.22 & \\
sort & CPU & EI & 317.24 & \texttt{std::sort}\\
\hline
\end{tabular}
\end{center}


\begin{itemize}
\item Very likely that AF is implementing \texttt{median} with a \texttt{sort} instead of the partitioned algorithm.
\item AF sort is surprisingly bad on CPU.
\end{itemize}
\end{frame}
\begin{frame}[label={sec:orgea4a695},fragile]{Problems with the solutions and possible mitigation}
 \begin{itemize}
\item AF DFT performance
\item AF Median performance
\item LT \texttt{const}-incorrectness
\item LT lack of documentation
\end{itemize}
\end{frame}
\begin{frame}[label={sec:org3f06ca5}]{AF DFT performance}
\begin{block}{Problem: naive AF DFT is 3x slower than LT.  LT/CPU beats FFTW (``impossible'').}
\begin{itemize}
\item Likely LT is auto detecting when r2c and c2r optimization can be applied while AF is not.
\item Manual r2c optimization added to AF and EI/FFTW benchmark and 3x goes to 2x.
\begin{itemize}
\item Likely c2r would close the gap further and FFTW would (better!) match LT.
\end{itemize}
\item AF/GPU did not speed up as much as expected with r2c.
\begin{itemize}
\item Remaining 1ms or so difference is perhaps negligible
\end{itemize}
\end{itemize}
\end{block}
\begin{block}{Mitigation}
Adopting AF implies the need for WCT to effectively extend AF's API to include
DFT functions that reap r2c and c2r.
\begin{itemize}
\item r2c and c2r also require additional functions to handle Hermitian-symmetry.
\end{itemize}
\end{block}
\end{frame}
\begin{frame}[label={sec:orgaa7de67},fragile]{Const-correctness issue}
 \begin{block}{Problem: \texttt{torch::Tensor} is essentially \texttt{shared\_ptr<T>}, \texttt{T} is never \texttt{const}}
This is legal: \texttt{const torch::Tensor ct; ct.zero\_();} 

\begin{itemize}
\item The \texttt{zero\_()} call mutates the ``const'' tensor.
\end{itemize}
\end{block}
\begin{block}{Mitigation: develop \texttt{const}-correct facade class}
\begin{itemize}
\item Never expose ``bare'' \texttt{torch::Tensor} and instead defined a ``wrapper'' or ``facade''.
\item Replicate required subset of torch API - non-trivial effort.
\end{itemize}
\end{block}
\begin{block}{Mitigation: always copy}
A hypothetical \texttt{torch::Tensor IArray::array()} could always return a copy.
\begin{itemize}
\item Caller can mutate if they wish, copies add up (consider a large \texttt{FanOut}).
\end{itemize}
May also include a non-copy version and rely on trust:
\begin{itemize}
\item \texttt{torch::Tensor IArray::array\_you\_promise\_not\_to\_mutate()}
\end{itemize}
\end{block}
\end{frame}
\begin{frame}[label={sec:orga67b221},fragile]{Const-correcting LibTorch}
 WCT can define an ``array facade'' class which provides a subset of the \texttt{torch::Tensor} API.
\begin{itemize}
\item Need never expose the ``bare'' \texttt{torch::Tensor} type to the user.
\item Can enforce \texttt{const}-correctness.
\item Required API is large, requiring substantial effort to replicate.
\item Could introduce some performance depredations.
\end{itemize}
\end{frame}
\begin{frame}[label={sec:orgd72d7e2}]{}
\end{frame}
\end{document}
