{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Patatrack full application throughput measurements"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This notebook needs python 3, matplotlib, scipy, abd tabulate."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 1\n",
    "%aimport plotting"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import json\n",
    "import statistics\n",
    "import matplotlib as mpl                                                                                                                                                                                                                                                                                                                                                 \n",
    "mpl.rcParams['figure.dpi'] = 100       \n",
    "\n",
    "from plotting import *\n",
    "setDefaultPath(\"CoriResults\")\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "import tabulate\n",
    "\n",
    "def moveLegendOut(plt):\n",
    "    plt.legend(bbox_to_anchor=(1.05,1), loc=\"upper left\")\n",
    "    return plt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Summary table"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table>\n",
       "<thead>\n",
       "<tr><th>Program                   </th><th>Setup              </th><th style=\"text-align: right;\">  Throughput mean</th><th style=\"text-align: right;\">   Std dev</th></tr>\n",
       "</thead>\n",
       "<tbody>\n",
       "<tr><td>CUDA                      </td><td>8 GPUs, 8 threads  </td><td style=\"text-align: right;\">        3991.37  </td><td style=\"text-align: right;\">18.3814   </td></tr>\n",
       "<tr><td>CUDA                      </td><td>1 GPU, 9 threads   </td><td style=\"text-align: right;\">        1839.13  </td><td style=\"text-align: right;\">15.9707   </td></tr>\n",
       "<tr><td>CUDA                      </td><td>1 GPU, 1 thread    </td><td style=\"text-align: right;\">         720.553 </td><td style=\"text-align: right;\">22.294    </td></tr>\n",
       "<tr><td>CUDA w/o caching allocator</td><td>1 GPU, 1 thread    </td><td style=\"text-align: right;\">         158.623 </td><td style=\"text-align: right;\"> 1.11681  </td></tr>\n",
       "<tr><td>CUDA UVM                  </td><td>1 GPU, 11 threads  </td><td style=\"text-align: right;\">         758.553 </td><td style=\"text-align: right;\"> 8.53624  </td></tr>\n",
       "<tr><td>CUDA UVM                  </td><td>1 GPU, 1 thread    </td><td style=\"text-align: right;\">         338.938 </td><td style=\"text-align: right;\"> 1.61886  </td></tr>\n",
       "<tr><td>cudacompat                </td><td>80 threads (node)  </td><td style=\"text-align: right;\">         898.932 </td><td style=\"text-align: right;\">10.8408   </td></tr>\n",
       "<tr><td>cudacompat                </td><td>40 threads (socket)</td><td style=\"text-align: right;\">         523.956 </td><td style=\"text-align: right;\"> 7.55801  </td></tr>\n",
       "<tr><td>cudacompat                </td><td>1 thread           </td><td style=\"text-align: right;\">          30.765 </td><td style=\"text-align: right;\"> 0.160149 </td></tr>\n",
       "<tr><td>Kokkos CUDA               </td><td>1 thread           </td><td style=\"text-align: right;\">         115.23  </td><td style=\"text-align: right;\"> 0.730507 </td></tr>\n",
       "<tr><td>Kokkos pthread            </td><td>18 threads         </td><td style=\"text-align: right;\">          45.0479</td><td style=\"text-align: right;\"> 0.180862 </td></tr>\n",
       "<tr><td>Kokkos pthread            </td><td>1 thread           </td><td style=\"text-align: right;\">          19.6687</td><td style=\"text-align: right;\"> 0.0928956</td></tr>\n",
       "<tr><td>Kokkos serial             </td><td>1 thread           </td><td style=\"text-align: right;\">          20.6847</td><td style=\"text-align: right;\"> 0.0625609</td></tr>\n",
       "</tbody>\n",
       "</table>"
      ],
      "text/plain": [
       "'<table>\\n<thead>\\n<tr><th>Program                   </th><th>Setup              </th><th style=\"text-align: right;\">  Throughput mean</th><th style=\"text-align: right;\">   Std dev</th></tr>\\n</thead>\\n<tbody>\\n<tr><td>CUDA                      </td><td>8 GPUs, 8 threads  </td><td style=\"text-align: right;\">        3991.37  </td><td style=\"text-align: right;\">18.3814   </td></tr>\\n<tr><td>CUDA                      </td><td>1 GPU, 9 threads   </td><td style=\"text-align: right;\">        1839.13  </td><td style=\"text-align: right;\">15.9707   </td></tr>\\n<tr><td>CUDA                      </td><td>1 GPU, 1 thread    </td><td style=\"text-align: right;\">         720.553 </td><td style=\"text-align: right;\">22.294    </td></tr>\\n<tr><td>CUDA w/o caching allocator</td><td>1 GPU, 1 thread    </td><td style=\"text-align: right;\">         158.623 </td><td style=\"text-align: right;\"> 1.11681  </td></tr>\\n<tr><td>CUDA UVM                  </td><td>1 GPU, 11 threads  </td><td style=\"text-align: right;\">         758.553 </td><td style=\"text-align: right;\"> 8.53624  </td></tr>\\n<tr><td>CUDA UVM                  </td><td>1 GPU, 1 thread    </td><td style=\"text-align: right;\">         338.938 </td><td style=\"text-align: right;\"> 1.61886  </td></tr>\\n<tr><td>cudacompat                </td><td>80 threads (node)  </td><td style=\"text-align: right;\">         898.932 </td><td style=\"text-align: right;\">10.8408   </td></tr>\\n<tr><td>cudacompat                </td><td>40 threads (socket)</td><td style=\"text-align: right;\">         523.956 </td><td style=\"text-align: right;\"> 7.55801  </td></tr>\\n<tr><td>cudacompat                </td><td>1 thread           </td><td style=\"text-align: right;\">          30.765 </td><td style=\"text-align: right;\"> 0.160149 </td></tr>\\n<tr><td>Kokkos CUDA               </td><td>1 thread           </td><td style=\"text-align: right;\">         115.23  </td><td style=\"text-align: right;\"> 0.730507 </td></tr>\\n<tr><td>Kokkos pthread            </td><td>18 threads         </td><td style=\"text-align: right;\">          45.0479</td><td style=\"text-align: right;\"> 0.180862 </td></tr>\\n<tr><td>Kokkos pthread            </td><td>1 thread           </td><td style=\"text-align: right;\">          19.6687</td><td style=\"text-align: right;\"> 0.0928956</td></tr>\\n<tr><td>Kokkos serial             </td><td>1 thread           </td><td style=\"text-align: right;\">          20.6847</td><td style=\"text-align: right;\"> 0.0625609</td></tr>\\n</tbody>\\n</table>'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "table = []\n",
    "headers = [\n",
    "    \"Program\",\n",
    "    \"Setup\",\n",
    "    \"Throughput mean\",\n",
    "    \"Std dev\"\n",
    "]\n",
    "def addRowMax(prog, setup, fname):\n",
    "    result = extract(fname, useThreads=True)\n",
    "    peak = max(result[1])\n",
    "    peak_nth = result[0][result[1].index(peak)]\n",
    "\n",
    "    table.append([\n",
    "        prog,\n",
    "        setup.format(peak_nth),\n",
    "        peak[0],\n",
    "        peak[1]\n",
    "    ])\n",
    "def addRowThreads(prog, setup, fname, nth):\n",
    "    result = extract(fname, useThreads=True)\n",
    "    ind = result[0].index(nth)\n",
    "\n",
    "    table.append([\n",
    "        prog,\n",
    "        setup,\n",
    "        result[1][ind][0],\n",
    "        result[1][ind][1],\n",
    "    ])\n",
    "\n",
    "cuda_pr = 129\n",
    "cudauvm_pr = \"162_rechit\"\n",
    "kokkos_pr = 166\n",
    "\n",
    "addRowMax(\"CUDA\", \"8 GPUs, {} threads\", \"result_cuda_pr{}_cuda11_ngpu8.json\".format(cuda_pr))\n",
    "addRowMax(\"CUDA\", \"1 GPU, {} threads\", \"result_cuda_pr{}_cuda11_ngpu1.json\".format(cuda_pr))\n",
    "addRowThreads(\"CUDA\", \"1 GPU, 1 thread\", \"result_cuda_pr{}_cuda11_ngpu1.json\".format(cuda_pr), 1)\n",
    "addRowThreads(\"CUDA w/o caching allocator\", \"1 GPU, 1 thread\", \"result_cuda_pr129_cuda11_noallocator.json\", 1)\n",
    "#\n",
    "addRowMax(\"CUDA UVM\", \"1 GPU, {} threads\", \"result_cudauvm_pr{}_ngpu1.json\".format(cudauvm_pr))\n",
    "addRowThreads(\"CUDA UVM\", \"1 GPU, 1 thread\", \"result_cudauvm_pr{}_ngpu1.json\".format(cudauvm_pr), 1)\n",
    "#\n",
    "addRowThreads(\"cudacompat\", \"80 threads (node)\", \"result_cudacompat_numactl.json\", 80)\n",
    "addRowThreads(\"cudacompat\", \"40 threads (socket)\", \"result_cudacompat_numactl.json\", 40)\n",
    "addRowThreads(\"cudacompat\", \"1 thread\", \"result_cudacompat_numactl.json\", 1)\n",
    "#\n",
    "addRowThreads(\"Kokkos CUDA\", \"1 thread\", \"result_kokkos_pr{}_cuda.json\".format(kokkos_pr), 1)\n",
    "addRowMax(\"Kokkos pthread\", \"{} threads\", \"result_kokkos_pr{}_pthread.json\".format(kokkos_pr))\n",
    "addRowThreads(\"Kokkos pthread\", \"1 thread\", \"result_kokkos_pr{}_pthread.json\".format(kokkos_pr), 1)\n",
    "addRowThreads(\"Kokkos serial\", \"1 thread\", \"result_kokkos_pr{}_serial.json\".format(kokkos_pr), 1)\n",
    "tabulate.tabulate(table, headers=headers, tablefmt='html')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
