import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from scipy.optimize import curve_fit
import matplotlib.cm as cm


def max_transfer_size(msg_size, np_procs, benchmark_type):
    if benchmark_type == 'Allgather':
        return (np_procs-72)*msg_size
    elif benchmark_type == 'Scatter':
        return (np_procs-72)*msg_size  # ?
    elif benchmark_type == 'Alltoall':
        return 72*(np_procs-72)*msg_size
    elif benchmark_type == 'Bcast':
        return msg_size
    elif benchmark_type == 'Gather':
        return (np_procs)*msg_size  # ?
    elif benchmark_type == 'Reduce_scatter':
        return 0.25*(np_procs-72)*(1/72)*msg_size  # ?
    elif benchmark_type == 'Allreduce':
        return 0.25*(np_procs-72)*(1/72)*msg_size
    elif benchmark_type == 'Reduce':
        return 0.25*(np_procs-72)*(1/72)*msg_size


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]
benchmarks = df_multinode_offdef['benchmark_type'].unique().tolist()
benchmarks = [x for x in benchmarks if x[-1] != 'v']
print(benchmarks)
df_multinode_offdef = df_multinode_offdef[df_multinode_offdef['benchmark_type'].isin(
    benchmarks)][df_multinode_offdef['msg_size_bytes'] > 1000]

df_multinode_offdef["max_transfer"] = df_multinode_offdef.apply(
    lambda row: max_transfer_size(
        msg_size=row["msg_size_bytes"],
        np_procs=row["proc_num"],
        benchmark_type=row["benchmark_type"]
    ),
    axis=1
)

df_multinode_offdef["bytes/usec"] = df_multinode_offdef["max_transfer"] / \
    df_multinode_offdef["t_avg_usec"]
df_multinode_offdef = df_multinode_offdef[df_multinode_offdef['benchmark_type']!='Allgather'][df_multinode_offdef['benchmark_type']!='Alltoall']
df_multinode_offdef = df_multinode_offdef[['benchmark_type','msg_size_bytes','t_avg_usec','proc_num']]

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.yscale("log")
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.show()
plt.savefig("./plots/benchmark_avg_time_barplot.png", dpi=300)
plt.close()

