Bash script for GPU monitoring

با استفاده از اسکریپت زیر باید بتوانید میزان CPU و RAM اشغال شده بر روی جی‌پی‌یو(ها)یی که با استفاده از اسلرم گرفته‌اید، در فایلی که این در پوشه home شما ایجاد می‌کند، ثبت کنید:

#!/bin/bash

# Define the output file
output_file="gpu_usage_""${SLURM_JOB_ID}"".log"

# Function to get GPU memory usage and GPU utilization for a specific GPU
get_gpu_usage() {
    local gpu_id=$1

    # Get GPU memory usage
    gpu_memory_usage=$(nvidia-smi --id=$gpu_id --query-gpu=memory.used --format=csv,noheader,nounits)
    gpu_memory_total=$(nvidia-smi --id=$gpu_id --query-gpu=memory.total --format=csv,noheader,nounits)

    # Get GPU utilization percentage
    gpu_utilization=$(nvidia-smi --id=$gpu_id --query-gpu=utilization.gpu --format=csv,noheader,nounits)

    # Convert the memory usage, total memory, and GPU utilization to integers
    gpu_memory_usage=$(echo $gpu_memory_usage | awk '{print $1}')
    gpu_memory_total=$(echo $gpu_memory_total | awk '{print $1}')
    gpu_utilization=$(echo $gpu_utilization | awk '{print $1}')

    # Calculate the percentage of memory used
    gpu_memory_percentage=$(echo "scale=2; $gpu_memory_usage / $gpu_memory_total * 100" | bc)

    # Get the current date and time
    current_time=$(date '+%Y-%m-%d %H:%M:%S')

    # Print the results with date and time and append to the output file
    echo "[$current_time] GPU $gpu_id: Usage: $gpu_utilization%, Memory Usage: $gpu_memory_usage MB / $gpu_memory_total MB ($gpu_memory_percentage%)" >> "${HOME}/""${output_file}"
}

# Get the number of GPUs
num_gpus=$(nvidia-smi --query-gpu=count --format=csv,noheader,nounits | head -1)

# Loop to print GPU usage and memory usage every 10 seconds
while true; do
    for ((gpu_id=0; gpu_id<num_gpus; gpu_id++)); do
        get_gpu_usage $gpu_id
    done
    sleep 5  # Wait for 5 seconds before the next iteration
done

این اسکریپت را در فایلی به نام gpu-stat.sh ذخیره کنید. سپس می‌توانید آن را برای راحتی در پوشه bin خود قرار دهید.

mkdir -p ~/bin
cp gpu-stat.sh ~/bin
chmod +x ~/bin/gpu-stat.sh

بعد از اینکه جاب گرفتید، در نود gpu ای که به شما اختصاص داده شده و نه در لاگین نود یا ترمینال اصلی پرتال، این اسکریپت را اجرا کنید.

gpu-stat.sh

ضمن عرض سلام و قدر دانی بابت زحمات دکتر حسینی راد
بنده اسکریپتی که زحمت کشیده بودید را در یک فایل notepad با فرمت .sh روی سیستم خودم ذخیره کردم. اما متوجه نشدم چطور باید آن را در پوشه bin که فرمودید قرار بدهم.
سعی کردم دستور ذخیره را در بخش panthera Shell access از پورتال اجرا کنم ولی گویا به فایل ذخیره شده روی سیستم من دسترسی ندارد:
cp: cannot stat ‘gpu-stat.sh’: No such file or directory
اگر ممکن است توضیح بیشتری راجع به نحوه ذخیره فایل و نحوه اجرای آن در نود GPU اختصاص داده شده بفرمائید.
از لطف شما سپاس گزارم

سلام. فکر می‌کنم فیلم آموزشی زیر بتواند راهنمایی‌تان کند.