#!/bin/bash
# Simple init script for Python on DTU HPC
# Patrick M. Jensen, patmjen@dtu.dk, 2022

# Configuration
# This is what you should change for your setup
PYTHON_VERSION=3.9.14  # Python version
CUDA_VERSION=11.6  # CUDA version
VENV_DIR=.  # Where to store your virtualenv
VENV_NAME=venv  # Name of your virtualenv

# Load modules
module load python3/$PYTHON_VERSION
module load $(module avail -o modulepath -t -C "python-${PYTHON_VERSION}" | grep "numpy/")
module load $(module avail -o modulepath -t -C "python-${PYTHON_VERSION}" | grep "scipy/")
module load $(module avail -o modulepath -t -C "python-${PYTHON_VERSION}" | grep "matplotlib/")
module load $(module avail -o modulepath -t -C "python-${PYTHON_VERSION}" | grep "pandas/")
module load cuda/$CUDA_VERSION
CUDNN_MOD=$(module avail -o modulepath -t cudnn | grep "cuda-${CUDA_VERSION}" | sort | tail -n1)
if [[ ${CUDNN_MOD} ]]
then
    module load ${CUDNN_MOD}
fi

# Create virtualenv if needed and activate it
if [ ! -d "${VENV_DIR}/${VENV_NAME}" ]
then
    echo INFO: Did not find virtualenv. Creating...
    virtualenv ${VENV_DIR}/${VENV_NAME}
fi
source ${VENV_DIR}/${VENV_NAME}/bin/activate

# Make all GPUs visible
if command -v nvidia-smi &> /dev/null
then
    export CUDA_VISIBLE_DEVICES=$(nvidia-smi --query-gpu=index --format=csv,noheader | tr '\n' ',')
    echo CUDA_VISIBLE_DEVICES=${CUDA_VISIBLE_DEVICES}
fi

