{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "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": [
    "# Alltoall "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1cc39aab",
   "metadata": {},
   "outputs": [
    {
     "ename": "",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31mnotebook controller is DISPOSED. \n",
      "\u001b[1;31mView Jupyter <a href='command:jupyter.viewOutput'>log</a> for further details."
     ]
    }
   ],
   "source": [
    "df_multinode = pd.read_csv(\"../data/data-multi-defand100cflag.csv\",delimiter = \",\")\n",
    "df_multinode['benchmark_type'].unique()\n",
    "df_gather = df_multinode[df_multinode[\"benchmark_type\"]==\"Bcast\"][df_multinode['msg_size_bytes']>1024][df_multinode['off_cache_flag']==-1]\n",
    "df_gather.columns.tolist()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4336d3c6",
   "metadata": {},
   "outputs": [
    {
     "ename": "",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31mnotebook controller is DISPOSED. \n",
      "\u001b[1;31mView Jupyter <a href='command:jupyter.viewOutput'>log</a> for further details."
     ]
    }
   ],
   "source": [
    "def model(proc_num, alpha, beta, msg_size):\n",
    "    return (alpha * msg_size * (proc_num - 72) * 72) / (12.5 * 1e3) + 1e6*beta\n",
    "\n",
    "results = []\n",
    "msg_sizes = sorted(df_gather['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_gather.groupby('msg_size_bytes')):\n",
    "    x = group['proc_num'].values.copy()\n",
    "    y = group['t_avg_usec'].values.copy()\n",
    "    sorted_indices = np.argsort(x)\n",
    "    x = x[sorted_indices]\n",
    "    y = y[sorted_indices]\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, 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.1, 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)*72*msg_size\n",
    "    min_data =(x[0]-72)*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('Alltoall Time Fit per Message Size\\nDots = Data Points | Dashed Lines = Fits\\n off_mem=-1', fontsize=14)\n",
    "fig.tight_layout(rect=[0, 0.03, 1, 0.95])\n",
    "# plt.savefig(\"plots/alltoall_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"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ce632d6f",
   "metadata": {},
   "outputs": [
    {
     "ename": "",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31mnotebook controller is DISPOSED. \n",
      "\u001b[1;31mView Jupyter <a href='command:jupyter.viewOutput'>log</a> for further details."
     ]
    }
   ],
   "source": [
    "df_gather[df_gather['msg_size_bytes']==1048576]"
   ]
  }
 ],
 "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
}
