Dynamically adjust the process cores number

动态调整进程核心数

Bash
Author

Jie Hua

Published

Jan 1, 2025

Modified

Jan 1, 2025

引言 / Introduction

在高性能计算或脚本环境中,根据一天中的时间动态调整所使用的线程数往往很有用。

In high-performance computing or scripting environments, it’s often useful to dynamically adjust the number of threads used based on the time of day.

例如,在工作时段,我们可能希望使用较少的线程,以免压垮共享系统;而在夜间,则可以充分利用可用资源。

For example, during working hours, we might want to use fewer threads to avoid overwhelming shared systems, while at night, we can take full advantage of available resources.

本教程演示如何使用一个简单的 Bash 脚本判断当前时间,并据此设置线程数。

This tutorial demonstrates how to use a simple Bash script to determine the current time and set the number of threads accordingly.

Bash 脚本 / Bash Script

下面的脚本使用 date 命令获取当前小时数,然后依据预设规则设置线程数:

The following script uses the date command to get the current hour and then sets the number of threads based on predefined rules:

Code
# Get the current hour in 24-hour format (00-23)
hour=$(date +"%H")

# Set thread number based on the time of day
if [ "$hour" -ge 0 ] && [ "$hour" -lt 6 ]; then
    threads=16  # Early morning: high resource usage allowed
elif [ "$hour" -ge 6 ] && [ "$hour" -lt 12 ]; then
    threads=8   # Morning: moderate usage
elif [ "$hour" -ge 12 ] && [ "$hour" -lt 18 ]; then
    threads=4   # Afternoon: light usage
else
    threads=12  # Evening: high usage again
fi

echo "Current hour: $hour"
echo "Using $threads threads"

说明 / Notes

你可以根据自己的系统和策略自定义这些阈值与线程取值。

You can customize the thresholds and thread values based on your system and policy.

这段逻辑可以很容易地嵌入 SLURM 作业脚本或任何基于 shell 的流程中。

This logic can be easily embedded in SLURM job scripts or any shell-based pipeline.

结论 / Conclusion

这种简单方法让你在一日之中不同的使用模式下都能兼顾资源与响应。它是一种轻量级方案,可在任何类 Unix(Unix-like)环境中运行。

This simple method allows you to be resource-conscious and responsive to different usage patterns throughout the day. It’s a lightweight solution that works in any Unix-like environment.