#!/bin/bash
file_name=$(basename $0)
current_path=$(pwd)
cd /data/yixin/workspace/unl-graph-usenix
source activate /data/yixin/anaconda/unlg
# Define arguments
# dataset=
datasets=("IMDB-BINARY" "PROTEINS" "MUTAG" "ENZYMES" "IMDB-MULTI" )
models=("gcn" "gin" "gat" "sage")
# models=( "gat" )
entity="mib-nlp"
# timestamp=$(date +%s)
exp_name="combine-run-debug-v3"
seeds=("402" "0" "6178" )
batch_size=8
# methods=("clean" "rand"  "feat" "grad" "inject" "adv")
methods=( "clean" "rand"  "feat" "grad" "inject" "inject" "adv")
wd=1e-5

lr=0.01
es_patience=100
seed_default=0
optimizer="adam"
# clean rand feat grad inject adv
# method="adv"
# total_combinations=len(models)*len(methods)
# total_combinations=24
# current_count=0

max_gpu_utilization=80
total_aviable=24564
max_gpu_memory_gap=5000
budget=0.05
# device
available_devices=(3 6 7 8 9)
current_device_idx=0
sleeptime=40
cpu_mean_max=80
# update device idx, reset to 0 if out of range


# total_epoch=300
# max_steps=5000

total_epoch=2
max_steps=2

function update_device_idx {

    # while true; do
    #     cpu_mean=$(mpstat -P ALL 1 1 | awk '/Average:/ && $2 ~ /[0-9]/ { cpu_usage=100-$NF; total+=cpu_usage; count++ } END { print total/count }')
    #     # if currently cpu usage is less than the threshold, then break
    #     if [ $(echo "$cpu_mean < $cpu_mean_max" | bc) -eq 1 ]; then
    #         break
    #     else
    #         echo "cpu mean: $cpu_mean is greater than $cpu_mean_max, sleep 10 seconds"
    #         sleep 10
    #     fi
    # done;
    
    while true; do
        current_device_idx=$((current_device_idx+1))
        if [ $current_device_idx -ge ${#available_devices[@]} ]; then
            # reset 
            current_device_idx=0
        fi
        # check whether this device is fully booked using nvidia-smi
        # get the gpu current memory usage 
        useage=$(nvidia-smi --query-gpu=memory.used --format=csv,noheader,nounits -i ${available_devices[$current_device_idx]})
        utilization=$(nvidia-smi --query-gpu=utilization.gpu --format=csv,noheader,nounits -i ${available_devices[$current_device_idx]})
        
        if [ $useage -ge $((total_aviable-max_gpu_memory_gap)) ] || [ $utilization -ge $max_gpu_utilization ]; then
            echo "device ${available_devices[$current_device_idx]} is fully booked, try next one"
            continue
        else
            break
        fi
    done
    echo "current device: ${available_devices[$current_device_idx]}"
    device=${available_devices[$current_device_idx]}
}

# mkdir $current_path/l Respond with at most 250 words.
user