import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
from mpl_toolkits.mplot3d import Axes3D

data_file = "data/data-multi-defand100cflag.csv"
df_multinode = pd.read_csv(data_file, delimiter=',')
df_multinode_offdef = df_multinode[df_multinode['off_cache_flag'] == 100]
df_multinode_offdef = df_multinode_offdef[['benchmark_type','msg_size_bytes','t_avg_usec','proc_num']]

benchmarks = df_multinode_offdef['benchmark_type'].unique().tolist()
benchmarks = [x for x in benchmarks if x[-1] != 'v']
df_multinode_offdef = df_multinode_offdef[df_multinode_offdef['benchmark_type'].isin(
    benchmarks)][df_multinode_offdef['msg_size_bytes'] > 1000]
fast_benchmarks = ["Allreduce","Bcast","Reduce","Reduce_scatter"]
df_multinode_offdef = df_multinode_offdef[df_multinode_offdef["benchmark_type"].isin(fast_benchmarks)]

plt.figure(figsize=(16, 9))
sns.barplot(
    data=df_multinode_offdef,
    x="benchmark_type",
    y="t_avg_usec",
    dodge=True,
    hue=df_multinode_offdef["msg_size_bytes"].astype(str),
)

plt.ylim(0)
plt.title("Average Time (usec) per Benchmark Type and Message Size")
plt.ylabel("Average Time (usec)")
plt.xlabel("Benchmark Type")
plt.xticks(rotation=45)
plt.legend(title="Message Size (bytes)")
plt.tight_layout()
plt.savefig("./plots/fbenchmarks_avg_time_barplot.png", dpi=300)
plt.close()

df_allreduce= df_multinode_offdef[df_multinode_offdef["benchmark_type"]=="Allreduce"]
df_allreduce = df_allreduce[['msg_size_bytes','t_avg_usec','proc_num']]
df_allreduce = df_allreduce[df_allreduce['msg_size_bytes']>2**17]
pivot = df_allreduce.pivot(index="msg_size_bytes", columns="proc_num", values="t_avg_usec")
X = pivot.columns.values   # proc_num
Y = pivot.index.values     # msg_size_bytes
X, Y = np.meshgrid(X, Y)
Z = pivot.values

fig = plt.figure(figsize=(16, 9))
ax = fig.add_subplot(111, projection='3d')
surf = ax.plot_surface(X, Y, Z, cmap="viridis", edgecolor='k')
cbar = fig.colorbar(surf, ax=ax, shrink=0.6, pad=0.01, location='left')
cbar.set_label("Average Time (μs)")
ax.set_xlabel("Process Count")
ax.set_ylabel("Message Size (B)")
ax.set_zlabel("Average Time (μs)")
ax.set_title("Allreduce")
ax.set_xticks(pivot.columns.values)           # use the actual process count values
ax.set_xticklabels(pivot.columns.values)
ax.set_yticks(Y[:, 0])
ymin, ymax = ax.get_ylim()
ax.set_ylim(ymin*0.8, ymax)   # 30% more space at top
ax.set_yticklabels([f"$2^{{{int(np.log2(v))}}}$" for v in Y[:, 0]])
plt.tight_layout()
plt.savefig("./plots/allreduce_surface.png", dpi=300)
plt.close()
