#!/usr/bin/env python3
import subprocess


def slurm_gpu(file, version, ntasks, mem_per_cpu, time):
    return f"""#!/bin/sh

#SBATCH --job-name={file[0]}.out
#SBATCH --output={file[0]}-{version}-{ntasks}.out.o%j
#SBATCH --ntasks={ntasks}
#SBATCH --cpus-per-task=1
#SBATCH --partition=shared-gpu
#SBATCH --gpus=ampere:{min(ntasks, 2)}
#SBATCH --mem-per-cpu={mem_per_cpu}
#SBATCH --time={time}

module load CUDA

srun {file[1]}_{version}_bench {min(ntasks, 8)} {file[2]} {file[2]} {file[2]}
"""


def slurm_cpu(file, version, ntasks, mem_per_cpu, time):
    return f"""#!/bin/sh

#SBATCH --job-name={file[0]}.out
#SBATCH --output={file[0]}-{version}-{ntasks}.out.o%j
#SBATCH --ntasks={ntasks}
#SBATCH --cpus-per-task=1
#SBATCH --partition=shared-cpu
#SBATCH --mem-per-cpu={mem_per_cpu}
#SBATCH --time={time}

srun {file[1]}_{version}_bench 1 {file[2]} {file[2]} {file[2]}
"""


FILES = [
    ["elem"], ["/home/users/c/coudrayb/projet-de-bachelor/elementary/cmake-build-release/elementary", 300000000],
    ["gol"], ["/home/users/c/coudrayb/projet-de-bachelor/game_of_life/cmake-build-release/game_of_life", 30000],
    ["lbm"], ["/home/users/c/coudrayb/projet-de-bachelor/lattice_boltzmann/cmake-build-release/lattice_boltzmann", 300]
]
HOURS = ["12:00:00", "06:00:00", "03:00:00", "02:00:00", "01:00:00", "01:00:00", "01:00:00"]


def main():
    for version in ["cuda"]:
        for i in range(0, 8):
            ntasks = 2 ** i
            mem_per_cpu = 13000
            file = FILES[0]
            time = HOURS[i]
            if version == "seq" or version == "mc":
                content = slurm_cpu(file, version, ntasks, mem_per_cpu, time)
            else:
                content = slurm_gpu(file, version, ntasks, mem_per_cpu, time)
            filename = f"srun_{version}_{ntasks}.sh"
            with open(filename, "w") as fh:
                fh.write(content)
            subprocess.run(["/usr/bin/sbatch", filename])


if __name__ == '__main__':
    main()