% Created 2025-01-17 Fri 11:40
% Intended LaTeX compiler: pdflatex
\documentclass[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}
\usetheme{default}
\author{Brett Viren}
\date{\today}
\title{Wire-Cell Toolkit Randomness}
\hypersetup{
 pdfauthor={Brett Viren},
 pdftitle={Wire-Cell Toolkit Randomness},
 pdfkeywords={},
 pdfsubject={},
 pdfcreator={Emacs 28.2 (Org mode 9.8-pre)},
 pdflang={English}}

% Setup for code blocks [1/2]

\usepackage{fvextra}

\fvset{%
  commandchars=\\\{\},
  highlightcolor=white!95!black!80!blue,
  breaklines=true,
  breaksymbol=\color{white!60!black}\tiny\ensuremath{\hookrightarrow}}

% Make line numbers smaller and grey.
\renewcommand\theFancyVerbLine{\footnotesize\color{black!40!white}\arabic{FancyVerbLine}}

\usepackage{xcolor}

% In case engrave-faces-latex-gen-preamble has not been run.
\providecolor{EfD}{HTML}{f7f7f7}
\providecolor{EFD}{HTML}{28292e}

% Define a Code environment to prettily wrap the fontified code.
\usepackage[breakable,xparse]{tcolorbox}
\DeclareTColorBox[]{Code}{o}%
{colback=EfD!98!EFD, colframe=EfD!95!EFD,
  fontupper=\footnotesize\setlength{\fboxsep}{0pt},
  colupper=EFD,
  IfNoValueTF={#1}%
  {boxsep=2pt, arc=2.5pt, outer arc=2.5pt,
    boxrule=0.5pt, left=2pt}%
  {boxsep=2.5pt, arc=0pt, outer arc=0pt,
    boxrule=0pt, leftrule=1.5pt, left=0.5pt},
  right=2pt, top=1pt, bottom=0.5pt,
  breakable}

% Support listings with captions
\usepackage{float}
\floatstyle{plain}
\newfloat{listing}{htbp}{lst}
\newcommand{\listingsname}{Listing}
\floatname{listing}{\listingsname}
\newcommand{\listoflistingsname}{List of Listings}
\providecommand{\listoflistings}{\listof{listing}{\listoflistingsname}}


% Setup for code blocks [2/2]: syntax highlighting colors

\newcommand\efstrut{\vrule height 2.1ex depth 0.8ex width 0pt}
\definecolor{EFD}{HTML}{000000}
\definecolor{EfD}{HTML}{ffffff}
\newcommand{\EFD}[1]{\textcolor{EFD}{#1}} % default
\definecolor{EFvp}{HTML}{000000}
\newcommand{\EFvp}[1]{\textcolor{EFvp}{#1}} % variable-pitch
\definecolor{EFh}{HTML}{7f7f7f}
\newcommand{\EFh}[1]{\textcolor{EFh}{#1}} % shadow
\definecolor{EFsc}{HTML}{228b22}
\newcommand{\EFsc}[1]{\textcolor{EFsc}{\textbf{#1}}} % success
\definecolor{EFw}{HTML}{ff8e00}
\newcommand{\EFw}[1]{\textcolor{EFw}{\textbf{#1}}} % warning
\definecolor{EFe}{HTML}{ff0000}
\newcommand{\EFe}[1]{\textcolor{EFe}{\textbf{#1}}} % error
\definecolor{EFl}{HTML}{ff0000}
\newcommand{\EFl}[1]{\textcolor{EFl}{#1}} % link
\definecolor{EFlv}{HTML}{ff0000}
\newcommand{\EFlv}[1]{\textcolor{EFlv}{#1}} % link-visited
\definecolor{EFhi}{HTML}{ff0000}
\newcommand{\EFhi}[1]{\textcolor{EFhi}{#1}} % highlight
\definecolor{EFc}{HTML}{b22222}
\newcommand{\EFc}[1]{\textcolor{EFc}{#1}} % font-lock-comment-face
\definecolor{EFcd}{HTML}{b22222}
\newcommand{\EFcd}[1]{\textcolor{EFcd}{#1}} % font-lock-comment-delimiter-face
\definecolor{EFs}{HTML}{8b2252}
\newcommand{\EFs}[1]{\textcolor{EFs}{#1}} % font-lock-string-face
\definecolor{EFd}{HTML}{8b2252}
\newcommand{\EFd}[1]{\textcolor{EFd}{#1}} % font-lock-doc-face
\definecolor{EFm}{HTML}{008b8b}
\newcommand{\EFm}[1]{\textcolor{EFm}{#1}} % font-lock-doc-markup-face
\definecolor{EFk}{HTML}{9370db}
\newcommand{\EFk}[1]{\textcolor{EFk}{#1}} % font-lock-keyword-face
\definecolor{EFb}{HTML}{483d8b}
\newcommand{\EFb}[1]{\textcolor{EFb}{#1}} % font-lock-builtin-face
\definecolor{EFf}{HTML}{0000ff}
\newcommand{\EFf}[1]{\textcolor{EFf}{#1}} % font-lock-function-name-face
\definecolor{EFv}{HTML}{a0522d}
\newcommand{\EFv}[1]{\textcolor{EFv}{#1}} % font-lock-variable-name-face
\definecolor{EFt}{HTML}{228b22}
\newcommand{\EFt}[1]{\textcolor{EFt}{#1}} % font-lock-type-face
\definecolor{EFo}{HTML}{008b8b}
\newcommand{\EFo}[1]{\textcolor{EFo}{#1}} % font-lock-constant-face
\definecolor{EFwr}{HTML}{ff0000}
\newcommand{\EFwr}[1]{\textcolor{EFwr}{\textbf{#1}}} % font-lock-warning-face
\newcommand{\EFnc}[1]{#1} % font-lock-negation-char-face
\definecolor{EFpp}{HTML}{483d8b}
\newcommand{\EFpp}[1]{\textcolor{EFpp}{#1}} % font-lock-preprocessor-face
\newcommand{\EFrc}[1]{\textbf{#1}} % font-lock-regexp-grouping-construct
\newcommand{\EFrb}[1]{\textbf{#1}} % font-lock-regexp-grouping-backslash
\newcommand{\EFob}[1]{#1} % org-block
\newcommand{\EFobb}[1]{#1} % org-block-begin-line
\newcommand{\EFobe}[1]{#1} % org-block-end-line
\definecolor{EFOa}{HTML}{0000ff}
\newcommand{\EFOa}[1]{\textcolor{EFOa}{#1}} % outline-1
\definecolor{EFOb}{HTML}{a0522d}
\newcommand{\EFOb}[1]{\textcolor{EFOb}{#1}} % outline-2
\definecolor{EFOc}{HTML}{a020f0}
\newcommand{\EFOc}[1]{\textcolor{EFOc}{#1}} % outline-3
\definecolor{EFOd}{HTML}{b22222}
\newcommand{\EFOd}[1]{\textcolor{EFOd}{#1}} % outline-4
\definecolor{EFOe}{HTML}{228b22}
\newcommand{\EFOe}[1]{\textcolor{EFOe}{#1}} % outline-5
\definecolor{EFOf}{HTML}{008b8b}
\newcommand{\EFOf}[1]{\textcolor{EFOf}{#1}} % outline-6
\definecolor{EFOg}{HTML}{483d8b}
\newcommand{\EFOg}[1]{\textcolor{EFOg}{#1}} % outline-7
\definecolor{EFOh}{HTML}{8b2252}
\newcommand{\EFOh}[1]{\textcolor{EFOh}{#1}} % outline-8
\definecolor{EFhn}{HTML}{008b8b}
\newcommand{\EFhn}[1]{\textcolor{EFhn}{#1}} % highlight-numbers-number
\definecolor{EFhq}{HTML}{9370db}
\newcommand{\EFhq}[1]{\textcolor{EFhq}{#1}} % highlight-quoted-quote
\definecolor{EFhs}{HTML}{008b8b}
\newcommand{\EFhs}[1]{\textcolor{EFhs}{#1}} % highlight-quoted-symbol
\definecolor{EFrda}{HTML}{707183}
\newcommand{\EFrda}[1]{\textcolor{EFrda}{#1}} % rainbow-delimiters-depth-1-face
\definecolor{EFrdb}{HTML}{7388d6}
\newcommand{\EFrdb}[1]{\textcolor{EFrdb}{#1}} % rainbow-delimiters-depth-2-face
\definecolor{EFrdc}{HTML}{909183}
\newcommand{\EFrdc}[1]{\textcolor{EFrdc}{#1}} % rainbow-delimiters-depth-3-face
\definecolor{EFrdd}{HTML}{709870}
\newcommand{\EFrdd}[1]{\textcolor{EFrdd}{#1}} % rainbow-delimiters-depth-4-face
\definecolor{EFrde}{HTML}{907373}
\newcommand{\EFrde}[1]{\textcolor{EFrde}{#1}} % rainbow-delimiters-depth-5-face
\definecolor{EFrdf}{HTML}{6276ba}
\newcommand{\EFrdf}[1]{\textcolor{EFrdf}{#1}} % rainbow-delimiters-depth-6-face
\definecolor{EFrdg}{HTML}{858580}
\newcommand{\EFrdg}[1]{\textcolor{EFrdg}{#1}} % rainbow-delimiters-depth-7-face
\definecolor{EFrdh}{HTML}{80a880}
\newcommand{\EFrdh}[1]{\textcolor{EFrdh}{#1}} % rainbow-delimiters-depth-8-face
\definecolor{EFrdi}{HTML}{887070}
\newcommand{\EFrdi}[1]{\textcolor{EFrdi}{#1}} % rainbow-delimiters-depth-9-face
\definecolor{EFany}{HTML}{CDCD00}
\newcommand{\EFany}[1]{\textcolor{EFany}{#1}} % ansi-color-yellow
\definecolor{EFanr}{HTML}{CD0000}
\newcommand{\EFanr}[1]{\textcolor{EFanr}{#1}} % ansi-color-red
\definecolor{EFanb}{HTML}{000000}
\newcommand{\EFanb}[1]{\textcolor{EFanb}{#1}} % ansi-color-black
\definecolor{EFang}{HTML}{00CD00}
\newcommand{\EFang}[1]{\textcolor{EFang}{#1}} % ansi-color-green
\definecolor{EFanB}{HTML}{0000EE}
\newcommand{\EFanB}[1]{\textcolor{EFanB}{#1}} % ansi-color-blue
\definecolor{EFanc}{HTML}{00CDCD}
\newcommand{\EFanc}[1]{\textcolor{EFanc}{#1}} % ansi-color-cyan
\definecolor{EFanw}{HTML}{E5E5E5}
\newcommand{\EFanw}[1]{\textcolor{EFanw}{#1}} % ansi-color-white
\definecolor{EFanm}{HTML}{CD00CD}
\newcommand{\EFanm}[1]{\textcolor{EFanm}{#1}} % ansi-color-magenta
\definecolor{EFANy}{HTML}{EEEE00}
\newcommand{\EFANy}[1]{\textcolor{EFANy}{#1}} % ansi-color-bright-yellow
\definecolor{EFANr}{HTML}{EE0000}
\newcommand{\EFANr}[1]{\textcolor{EFANr}{#1}} % ansi-color-bright-red
\newcommand{\EFANb}[1]{#1} % ansi-color-bright-black
\definecolor{EFANg}{HTML}{00EE00}
\newcommand{\EFANg}[1]{\textcolor{EFANg}{#1}} % ansi-color-bright-green
\definecolor{EFANB}{HTML}{0000FF}
\newcommand{\EFANB}[1]{\textcolor{EFANB}{#1}} % ansi-color-bright-blue
\definecolor{EFANc}{HTML}{00EEEE}
\newcommand{\EFANc}[1]{\textcolor{EFANc}{#1}} % ansi-color-bright-cyan
\newcommand{\EFANw}[1]{#1} % ansi-color-bright-white
\newcommand{\EFANm}[1]{#1} % ansi-color-bright-magenta
\begin{document}

\maketitle
\begin{frame}[label={sec:orga87c7c1},fragile]{Topics}
 Wire-Cell Toolkit randomness

\begin{itemize}
\item The \texttt{IRandom} interface class
\item The \texttt{Random} implementation class
\item Random consumers
\item Reseeding issues
\item Proposed adaption to \emph{art}'s random
\item Multi-threading issues
\item Class of bugs leading to non-deterministic results
\end{itemize}
\end{frame}
\begin{frame}[label={sec:orgdd8f718},fragile]{The \texttt{IRandom} Interface Class}
 \begin{Code}
\begin{Verbatim}
\color{EFD}\EFk{class} \EFt{IRandom} : \EFk{public} \EFt{IComponent}<\EFt{IRandom}> \{
    \EFk{using} \EFt{int\_func} = \EFo{std}::\EFt{function}<\EFt{int}()>;

    \EFcd{///} \EFc{Sample a binomial distribution}
    \EFk{virtual} \EFt{int} \EFf{binomial}(\EFt{int} \EFv{max}, \EFt{double} \EFv{prob}) = 0;
    \EFk{virtual} \EFt{int\_func} \EFf{make\_binomial}(\EFt{int} \EFv{max}, \EFt{double} \EFv{prob});
    \EFcd{//} \EFc{...etc for other distributions}
\};
\end{Verbatim}
\end{Code}

\begin{itemize}
\item Direct \texttt{<distribution>()} methods return a random sample.

\item The \texttt{make\_<distribution>()} methods allow returning a closure over some
distribution object to avoid the overhead of calling its constructor for each
sample.
\end{itemize}
\end{frame}
\begin{frame}[label={sec:orgeb5fc74},fragile]{The \texttt{Random} implementation class}
 \begin{Code}
\begin{Verbatim}
\color{EFD}\EFk{class} \EFt{Random} : \EFk{public} \EFt{IRandom}, \EFk{public} \EFt{IConfigurable} \{
    \EFcd{//} \EFc{IConfigurable implementation...}
    \EFcd{//} \EFc{IRandom implementation....}
\};
\end{Verbatim}
\end{Code}

Configuration parameters accepted:

\begin{description}
\item[{\texttt{seeds}}] one or more integers, passed to C++ random engine.
\item[{\texttt{generator}}] name of a generator (C++'s default or \texttt{"twister"} uses \texttt{std::mt19937}).
\end{description}

Each \texttt{make\_<distribution>()} method returns a callable closure over an instance of
C++'s type: \texttt{std::<distribution>\_distribution}.
\end{frame}
\begin{frame}[label={sec:org5c724c9},fragile]{Getting and using an \texttt{IRandom}}
 Like all WCT interfaces, one gets an instance by \alert{type/name}.
\begin{itemize}
\item The type/name is almost always provided via configuration.
\item The \texttt{IRandom} type/name is usually provided by parameter \texttt{"rng"}.
\end{itemize}

\texttt{Drifter} example with a default type/name of \texttt{"Random"}:
\begin{Code}
\begin{Verbatim}
\color{EFD}\EFt{void} \EFo{Gen}::\EFo{Drifter}::\EFf{configure}(\EFk{const} \EFo{WireCell}::\EFt{Configuration}\& \EFv{cfg})
\{
    m\_rng\_tn = get(cfg, \EFs{"rng"}, m\_rng\_tn);
    m\_rng = \EFo{Factory}::find\_tn<IRandom>(m\_rng\_tn);
    \EFcd{//} \EFc{...}
\}
\end{Verbatim}
\end{Code}

\texttt{Drifter} example usage of \texttt{IRandom}:

\begin{Code}
\begin{Verbatim}
\color{EFD}dQ = sign * m\_rng->binomial((\EFt{int}) \EFo{std}::abs(Qi), absorbprob);
\end{Verbatim}
\end{Code}
\end{frame}
\begin{frame}[label={sec:orgba0b155}]{Issues for adding re-seeding to WCT}
\begin{block}{Re-seeding is very application-specific}
Re-seeding strategies are very application specific.  WCT does not even have the
idea of a ``event'' so any ``per-event reseeding'' has no meaning.
\end{block}
\begin{block}{No ``re-configure'' phase in WCT's life-cycle}
Re-seeding implies a novel ``re-configure'' phase would be needed.
\begin{itemize}
\item Can add one, in principle, but opens a can-of-worms.
\end{itemize}
\end{block}
\end{frame}
\begin{frame}[label={sec:org4f0415c},fragile]{A new \texttt{IRandom} implementation for \emph{art} in LArSoft's \texttt{larwirecell}}
 \begin{Code}
\begin{Verbatim}
\color{EFD}\EFcd{//} \EFc{Would be placed, eg, in larwirecell/Components/Random.\{h,cxx\}}
\EFk{namespace} \EFo{wcls} \{
  \EFk{class} \EFt{Random} : \EFk{public} \EFt{IArtEventVisitor}, \EFk{public} \EFt{IRandom} \{
    \EFcd{//} \EFc{... IArtEventVisitor methods ...}
    \EFk{virtual} \EFt{void} \EFf{visit}(\EFo{art}::\EFt{Event}\& \EFv{event});
    \EFcd{//} \EFc{... IRandom methods ...}
  \};
\}
\end{Verbatim}
\end{Code}

An example of a ``two-faced'' component with faces:
\begin{description}
\item[{WCT}] implements \texttt{IRandom}
\begin{itemize}
\item Called by WCT-side random consumers.
\item Hold/manage/use an \emph{art} pRNG in the standard \emph{art} way.
\end{itemize}
\item[{\emph{art}}] implements \texttt{IArtEventVisitor}
\begin{itemize}
\item Implement \texttt{visit()} to do per-event ``\emph{art} things''.
\begin{itemize}
\item \emph{eg} re-seed the held \emph{art} pRNG based ``event number''.
\end{itemize}
\end{itemize}
\end{description}
\end{frame}
\begin{frame}[label={sec:org85b9a76},fragile]{Multi-threading issues - CPU}
 \begin{center}
\includegraphics[width=0.7\textwidth]{graph-simspimg.pdf}

\scriptsize sim+sp+img for 6-APA PDSP
\end{center}

\begin{center}
\texttt{IRandom} users: \texttt{Drifter} (1), \texttt{DepoTransorm} (6) and \texttt{AddNoise} (6)
\end{center}

\begin{itemize}
\item Sharing a common \texttt{IRandom} + \texttt{TbbFlow}  = irreproducible results due to MT.
\item \alert{Expect} MT-reproducibility if we construct a unique \texttt{IRandom} for each consumer.
\begin{itemize}
\item Assuming the underlying pRNG instances do not share state.
\item Needs explicit validation, so far we have not tried this.
\end{itemize}
\end{itemize}
We can invent a Jsonnet function to ``bolt on'' unique \texttt{IRandom}'s to existing graph.
\end{frame}
\begin{frame}[label={sec:orgad81af9}]{Multi-threading issues - GPU}
Our general understanding is that GPU will induce non-reproducibility at some
level.

This impacts these Wire-Cell Toolkit features:

\begin{itemize}
\item DNNROI signal processing
\begin{itemize}
\item Other DL algs in development
\end{itemize}
\item FFT acceleration
\item Signal Processing Next Generation
\item Future simulation acceleration
\end{itemize}
\end{frame}
\begin{frame}[label={sec:org53c7285},fragile]{Unacceptable sources of randomness - pointer order}
 A common idiom (not just a WCT issue):

\begin{Code}
\begin{Verbatim}
\color{EFD}\EFo{std}::\EFt{map}<\EFt{Type}*, Value> \EFv{mymap} = ...;
\EFk{for} (\EFk{auto}\& [\EFo{t},\EFo{v}] : mymap) \{
    do\_something\_where\_order\_matters(t,v);
\}
\end{Verbatim}
\end{Code}

The order of the loop depends on \alert{pointer values} which are not deterministic.
\begin{itemize}
\item Reproducibility, itself, is not so reproducible in this case.
\begin{itemize}
\item \emph{eg} stable ordering until OS, compiler or compiler flags change.
\end{itemize}
\end{itemize}


In Wire-Cell Toolkit, this idiom is considered a \alert{bug that must be fixed}.  
\begin{itemize}
\item Recently, found \texttt{Pgraph} had it!  Fixed now.
\item No promise that there are no more of these bugs lurking.
\end{itemize}
I am curious how large this problem is outside of Wire-Cell Toolkit!  
\end{frame}
\begin{frame}[label={sec:orgbd63f1e},fragile]{Summary}
 All WCT consumers of randoms use \texttt{IRandom} interface
\begin{itemize}
\item Single implementation based on C++'s \texttt{<random>}.
\item Provides for seeding but not re-seeding.
\end{itemize}
Can add a new \texttt{IRandom} implementation in \texttt{larwirecell}.
\begin{itemize}
\item Can use \emph{art} pRNG internally.
\item Can re-seed before and/or after each ``event''.
\end{itemize}
Configure unique \texttt{IRandom} for each consumer for MT-reproduciblity.
\end{frame}
\end{document}
