{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "da7c16b4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "from scipy.optimize import curve_fit\n",
    "from matplotlib.cm import get_cmap"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "47341b1d",
   "metadata": {},
   "source": [
    "# Allgather "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "1cc39aab",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_10800/1470334786.py:3: UserWarning: Boolean Series key will be reindexed to match DataFrame index.\n",
      "  df_allgather = df_multinode[df_multinode[\"benchmark_type\"]==\"Allgather\"][df_multinode['off_cache_flag']==-1][df_multinode['msg_size_bytes']>1024][df_multinode['proc_num']>0]\n",
      "/tmp/ipykernel_10800/1470334786.py:3: UserWarning: Boolean Series key will be reindexed to match DataFrame index.\n",
      "  df_allgather = df_multinode[df_multinode[\"benchmark_type\"]==\"Allgather\"][df_multinode['off_cache_flag']==-1][df_multinode['msg_size_bytes']>1024][df_multinode['proc_num']>0]\n",
      "/tmp/ipykernel_10800/1470334786.py:3: UserWarning: Boolean Series key will be reindexed to match DataFrame index.\n",
      "  df_allgather = df_multinode[df_multinode[\"benchmark_type\"]==\"Allgather\"][df_multinode['off_cache_flag']==-1][df_multinode['msg_size_bytes']>1024][df_multinode['proc_num']>0]\n"
     ]
    }
   ],
   "source": [
    "df_multinode = pd.read_csv(\"data/data-multi-defand100cflag.csv\",delimiter = \",\")\n",
    "df_multinode['benchmark_type'].unique()\n",
    "df_allgather = df_multinode[df_multinode[\"benchmark_type\"]==\"Allgather\"][df_multinode['off_cache_flag']==-1][df_multinode['msg_size_bytes']>1024][df_multinode['proc_num']>0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "4336d3c6",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_10800/119527997.py:9: MatplotlibDeprecationWarning: The get_cmap function was deprecated in Matplotlib 3.7 and will be removed in 3.11. Use ``matplotlib.colormaps[name]`` or ``matplotlib.colormaps.get_cmap()`` or ``pyplot.get_cmap()`` instead.\n",
      "  cmap = get_cmap('tab10')\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 431684 characters]",
      "text/plain": [
       "<Figure size 1500x1600 with 12 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    msg_size_bytes     alpha      beta  inv_alpha\n",
      "0             2048  1.552236  0.000150   0.644232\n",
      "1             4096  1.000317  0.000317   0.999684\n",
      "2             8192  1.000374  0.000374   0.999626\n",
      "3            16384  1.000750  0.000750   0.999250\n",
      "4            32768  1.001409  0.001408   0.998593\n",
      "5            65536  1.002759  0.002759   0.997249\n",
      "6           131072  1.005332  0.005332   0.994696\n",
      "7           262144  1.429840  0.010007   0.699379\n",
      "8           524288  1.016241  0.016955   0.984018\n",
      "9          1048576  1.079597  0.038522   0.926272\n",
      "10         2097152  1.089258  0.089258   0.918056\n",
      "11         4194304  1.171323  0.171323   0.853735\n"
     ]
    }
   ],
   "source": [
    "\n",
    "def model(proc_num, alpha, beta, msg_size):\n",
    "   return (proc_num/72-1)*((alpha*(msg_size)*72)/(12500)+1e6*beta) \n",
    "\n",
    "results = []\n",
    "msg_sizes = sorted(df_allgather['msg_size_bytes'].unique())\n",
    "n_rows = int(np.ceil(len(msg_sizes) / 3))\n",
    "n_cols = min(len(msg_sizes), 3)\n",
    "fig, axes = plt.subplots(n_rows, n_cols, figsize=(5*n_cols, 4*n_rows), squeeze=False)\n",
    "cmap = get_cmap('tab10')\n",
    "\n",
    "for idx, (msg_size, group) in enumerate(df_allgather.groupby('msg_size_bytes')):\n",
    "    x = group['proc_num'].values\n",
    "    y = group['t_avg_usec'].values\n",
    "\n",
    "    fit_func = lambda proc_num, alpha, beta: model(proc_num, alpha, beta, msg_size)\n",
    "    popt, _ = curve_fit(fit_func, x, y, bounds=([1, 0], [np.inf, np.inf]))\n",
    "    alpha, beta = popt\n",
    "    results.append({'msg_size_bytes': msg_size, 'alpha': alpha, 'beta': beta})\n",
    "\n",
    "    x_fit = np.linspace(min(x), max(x), 100)\n",
    "    y_fit = fit_func(x_fit, alpha, beta)\n",
    "    y_speed = model(x_fit,1,0,msg_size)\n",
    "    row, col = divmod(idx, n_cols)\n",
    "    ax = axes[row][col]\n",
    "\n",
    "    color = cmap(idx % 10)\n",
    "    ax.scatter(x, y/1e6, label='Data', color=color)\n",
    "    ax.plot(x_fit, y_fit/1e6, linestyle='--', color=color, alpha=0.5, label='Fit')\n",
    "    ax.plot(x_fit, y_speed/1e6, linestyle='--', color='red', alpha=0.2, label='Fit')\n",
    "    ax.set_title(f'msg_size: {msg_size} bytes')\n",
    "    ax.set_xlabel('num. proc.')\n",
    "    ax.set_ylabel('Average Time [s]')\n",
    "    ax.set_xticks(x)\n",
    "    ax.grid(True)\n",
    "    max_data =(x[-1]-72)*msg_size\n",
    "    min_data =(x[0]-72)*msg_size\n",
    "\n",
    "    textstr = \"\"\n",
    "    if(max_data > 1e9):\n",
    "        textstr+=f\"max data = {max_data/1e9:0.2f}GB\\n\" \n",
    "    else:\n",
    "        textstr+=f\"max data = {max_data/1e6:0.2f}MB\\n\" \n",
    "\n",
    "    if(min_data > 1e9):\n",
    "        textstr+=f\"min data = {min_data/1e9:0.2f}GB\\n\" \n",
    "    else:\n",
    "        textstr+=f\"min data = {min_data/1e6:0.2f}MB\\n\" \n",
    "    textstr += r\"$\\alpha$\" +f\"= {alpha:.3e}\\n\"+r\"$b_{eff}=$\"+f\"{12.5/alpha:0.3f}Gbps\\n\"+\\\n",
    "               r\"$\\beta$\"+f\"= {beta:.3e} s\"\n",
    "    ax.text(0.95, 0.05, textstr, transform=ax.transAxes,\n",
    "            fontsize=10, verticalalignment='bottom',\n",
    "            horizontalalignment='right',\n",
    "            bbox=dict(boxstyle='round', facecolor='white', alpha=0.8))\n",
    "\n",
    "fig.suptitle('Allgather Time Fit per Message Size\\nDots = Data Points | Dashed Lines = Fits | off_mem=-1', fontsize=14)\n",
    "fig.tight_layout(rect=[0, 0.03, 1, 0.95])\n",
    "plt.savefig(\"plots/allgather_analysis.png\",dpi=300)\n",
    "plt.show()\n",
    "\n",
    "fit_results = pd.DataFrame(results)\n",
    "fit_results['inv_alpha'] = 1 / fit_results['alpha']\n",
    "print(fit_results)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "ce632d6f",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_10800/695385123.py:2: UserWarning: Boolean Series key will be reindexed to match DataFrame index.\n",
      "  df_allgather_2 = df_multinode_2[df_multinode_2[\"benchmark_type\"]==\"Allgather\"][df_multinode_2['off_cache_flag']==100][df_multinode_2['msg_size_bytes']>1024][df_multinode_2['proc_num']>0]\n",
      "/tmp/ipykernel_10800/695385123.py:2: UserWarning: Boolean Series key will be reindexed to match DataFrame index.\n",
      "  df_allgather_2 = df_multinode_2[df_multinode_2[\"benchmark_type\"]==\"Allgather\"][df_multinode_2['off_cache_flag']==100][df_multinode_2['msg_size_bytes']>1024][df_multinode_2['proc_num']>0]\n",
      "/tmp/ipykernel_10800/695385123.py:2: UserWarning: Boolean Series key will be reindexed to match DataFrame index.\n",
      "  df_allgather_2 = df_multinode_2[df_multinode_2[\"benchmark_type\"]==\"Allgather\"][df_multinode_2['off_cache_flag']==100][df_multinode_2['msg_size_bytes']>1024][df_multinode_2['proc_num']>0]\n"
     ]
    }
   ],
   "source": [
    "df_multinode_2 = pd.read_csv(\"data/data-multi-MPIF-100cflag-complete.csv\",delimiter = \",\")\n",
    "df_allgather_2 = df_multinode_2[df_multinode_2[\"benchmark_type\"]==\"Allgather\"][df_multinode_2['off_cache_flag']==100][df_multinode_2['msg_size_bytes']>1024][df_multinode_2['proc_num']>0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "3532b67e",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_10800/1198317908.py:9: MatplotlibDeprecationWarning: The get_cmap function was deprecated in Matplotlib 3.7 and will be removed in 3.11. Use ``matplotlib.colormaps[name]`` or ``matplotlib.colormaps.get_cmap()`` or ``pyplot.get_cmap()`` instead.\n",
      "  cmap = get_cmap('tab10')\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 452600 characters]",
      "text/plain": [
       "<Figure size 1500x1600 with 12 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    msg_size_bytes     alpha      beta  inv_alpha\n",
      "0             2048  1.000157  0.000157   0.999843\n",
      "1             4096  1.000257  0.000257   0.999743\n",
      "2             8192  1.000389  0.000389   0.999611\n",
      "3            16384  1.000718  0.000718   0.999283\n",
      "4            32768  1.001514  0.001514   0.998488\n",
      "5            65536  1.000996  0.002762   0.999005\n",
      "6           131072  1.005062  0.005062   0.994964\n",
      "7           262144  1.012598  0.009937   0.987559\n",
      "8           524288  1.020122  0.020125   0.980275\n",
      "9          1048576  1.062741  0.062742   0.940963\n",
      "10         2097152  1.178951  0.179023   0.848211\n",
      "11         4194304  1.537594  0.537920   0.650367\n"
     ]
    }
   ],
   "source": [
    "\n",
    "def model_2(proc_num, alpha, beta, msg_size):\n",
    "   return (proc_num/72-1)*((alpha*(msg_size)*72)/(12500)+1e6*beta) \n",
    "\n",
    "results = []\n",
    "msg_sizes = sorted(df_allgather_2['msg_size_bytes'].unique())\n",
    "n_rows = int(np.ceil(len(msg_sizes) / 3))\n",
    "n_cols = min(len(msg_sizes), 3)\n",
    "fig, axes = plt.subplots(n_rows, n_cols, figsize=(5*n_cols, 4*n_rows), squeeze=False)\n",
    "cmap = get_cmap('tab10')\n",
    "\n",
    "for idx, (msg_size, group) in enumerate(df_allgather_2.groupby('msg_size_bytes')):\n",
    "    x = group['proc_num'].values\n",
    "    y = group['t_avg_usec'].values\n",
    "\n",
    "    fit_func = lambda proc_num, alpha, beta: model_2(proc_num, alpha, beta, msg_size)\n",
    "    popt, _ = curve_fit(fit_func, x, y, bounds=([1, 0], [np.inf, np.inf]))\n",
    "    alpha, beta = popt\n",
    "    results.append({'msg_size_bytes': msg_size, 'alpha': alpha, 'beta': beta})\n",
    "\n",
    "    x_fit = np.linspace(min(x), max(x), 100)\n",
    "    y_fit = fit_func(x_fit, alpha, beta)\n",
    "    y_speed = model_2(x_fit,1,0,msg_size)\n",
    "    row, col = divmod(idx, n_cols)\n",
    "    ax = axes[row][col]\n",
    "\n",
    "    color = cmap(idx % 10)\n",
    "    ax.scatter(x, y/1e6, label='Data', color=color)\n",
    "    ax.plot(x_fit, y_fit/1e6, linestyle='--', color=color, alpha=0.5, label='Fit')\n",
    "    ax.plot(x_fit, y_speed/1e6, linestyle='--', color='red', alpha=0.2, label='Fit')\n",
    "    ax.set_title(f'msg_size: {msg_size} bytes')\n",
    "    ax.set_xlabel('num. proc.')\n",
    "    ax.set_ylabel('Average Time [s]')\n",
    "    ax.set_xticks(x)\n",
    "    ax.grid(True)\n",
    "    max_data =(x[-1]-72)*msg_size\n",
    "    min_data =(x[0]-72)*msg_size\n",
    "\n",
    "    textstr = \"\"\n",
    "    if(max_data > 1e9):\n",
    "        textstr+=f\"max data = {max_data/1e9:0.2f}GB\\n\" \n",
    "    else:\n",
    "        textstr+=f\"max data = {max_data/1e6:0.2f}MB\\n\" \n",
    "\n",
    "    if(min_data > 1e9):\n",
    "        textstr+=f\"min data = {min_data/1e9:0.2f}GB\\n\" \n",
    "    else:\n",
    "        textstr+=f\"min data = {min_data/1e6:0.2f}MB\\n\" \n",
    "    textstr += r\"$\\alpha$\" +f\"= {alpha:.3e}\\n\"+r\"$b_{eff}=$\"+f\"{12.5/alpha:0.3f}Gbps\\n\"+\\\n",
    "               r\"$\\beta$\"+f\"= {beta:.3e} s\"\n",
    "    ax.text(0.95, 0.05, textstr, transform=ax.transAxes,\n",
    "            fontsize=10, verticalalignment='bottom',\n",
    "            horizontalalignment='right',\n",
    "            bbox=dict(boxstyle='round', facecolor='white', alpha=0.8))\n",
    "\n",
    "\n",
    "fig.suptitle('Allgather Time Fit per Message Size\\nDots = Data Points | Dashed Lines = Fits \\n off_mem = 100 |I_MPI_TUNING=on | I_MPI_TUNING_MODE=auto', fontsize=14)\n",
    "fig.tight_layout(rect=[0, 0.03, 1, 0.95])\n",
    "plt.savefig(\"plots/allgather2_analysis.png\",dpi=300)\n",
    "plt.show()\n",
    "\n",
    "fit_results = pd.DataFrame(results)\n",
    "fit_results['inv_alpha'] = 1 / fit_results['alpha']\n",
    "print(fit_results)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "data",
   "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.13.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
