

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 多佇列模式教學課程
<a name="tutorial-mqm"></a>



## AWS ParallelCluster 使用多個佇列模式在 上執行您的任務
<a name="tutorial-mqm-running-jobs"></a>

本教學課程將逐步引導您 AWS ParallelCluster 透過 在 上執行第一個 Hello World 任務[多佇列模式](queue-mode.md)。

**先決條件**
+ AWS ParallelCluster [已安裝](install.md) 。
+  AWS CLI [已安裝並設定 。](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html)
+ 您有 [EC2 金鑰對](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-key-pairs.html)。
+ 您有一個 IAM 角色，具有執行 CLI [`pcluster`](pcluster.md) 所需的[許可](iam.md#example-parallelcluser-policies)。

**注意**  
只有 2 AWS ParallelCluster .9.0 版或更新版本支援多個佇列模式。

### 設定叢集
<a name="tutorial-mqm-configure-cluster"></a>

首先，執行下列命令，確認 AWS ParallelCluster 已正確安裝。

```
$ pcluster version
```

如需有關 `pcluster version` 的詳細資訊，請參閱 [`pcluster version`](pcluster.version.md)。

此命令會傳回執行中的 版本 AWS ParallelCluster。

接下來，執行 `pcluster configure` 以產生基本組態檔案。遵循此命令的所有提示。

```
$ pcluster configure
```

如需 `pcluster configure` 命令的詳細資訊，請參閱[`pcluster configure`](pcluster.configure.md)。

完成此步驟後，您應該在 下有一個基本組態檔案`~/.parallelcluster/config`。此檔案應包含基本叢集組態和 VPC 區段。

教學課程的下一個部分概述如何修改新建立的組態，以及啟動具有多個佇列的叢集。

**注意**  
本教學中使用的某些執行個體不符合自由方案資格。

在本教學課程中，請使用下列組態。

```
[global]
update_check = true
sanity_check = true
cluster_template = multi-queue

[aws]
aws_region_name = <Your AWS 區域>

[scaling demo]
scaledown_idletime = 5              # optional, defaults to 10 minutes

[cluster multi-queue-special]
key_name = < Your key name >
base_os = alinux2                   # optional, defaults to alinux2
scheduler = slurm
master_instance_type = c5.xlarge    # optional, defaults to t2.micro
vpc_settings = <Your VPC section>
scaling_settings = demo             # optional, defaults to no custom scaling settings
queue_settings = efa,gpu

[cluster multi-queue]
key_name = <Your SSH key name>
base_os = alinux2                   # optional, defaults to alinux2
scheduler = slurm
master_instance_type = c5.xlarge    # optional, defaults to t2.micro
vpc_settings = <Your VPC section>
scaling_settings = demo
queue_settings = spot,ondemand

[queue spot]
compute_resource_settings = spot_i1,spot_i2
compute_type = spot                 # optional, defaults to ondemand

[compute_resource spot_i1]
instance_type = c5.xlarge
min_count = 0                       # optional, defaults to 0
max_count = 10                      # optional, defaults to 10

[compute_resource spot_i2]
instance_type = t2.micro
min_count = 1
initial_count = 2

[queue ondemand]
compute_resource_settings = ondemand_i1
disable_hyperthreading = true       # optional, defaults to false

[compute_resource ondemand_i1]
instance_type = c5.2xlarge
```

### 建立叢集
<a name="tutorial-mqm-creating-cluster"></a>

本節詳細說明如何建立多個佇列模式叢集。

首先，為您的叢集命名 `multi-queue-hello-world`，並根據上一節中定義的叢集區段建立`multi-queue`叢集。

```
$ pcluster create multi-queue-hello-world -t multi-queue
```

如需有關 `pcluster create` 的詳細資訊，請參閱 [`pcluster create`](pluster.create.md)。

建立叢集時，會顯示下列輸出：

```
Beginning cluster creation for cluster: multi-queue-hello-world
Creating stack named: parallelcluster-multi-queue-hello-world
Status: parallelcluster-multi-queue-hello-world - CREATE_COMPLETE
MasterPublicIP: 3.130.xxx.xx
ClusterUser: ec2-user
MasterPrivateIP: 172.31.xx.xx
```

訊息`CREATE_COMPLETE`指出叢集已成功建立。輸出也提供主機節點的公有和私有 IP 地址。

### 登入您的頭部節點
<a name="tutorial-mqm-log-into-head-node"></a>

使用您的私有 SSH 金鑰檔案登入您的頭部節點。

```
$ pcluster ssh multi-queue-hello-world -i ~/path/to/keyfile.pem
```

如需有關 `pcluster ssh` 的詳細資訊，請參閱 [`pcluster ssh`](pcluster.ssh.md)。

登入後，請執行 `sinfo`命令來驗證排程器佇列是否已設定。

如需 的詳細資訊`sinfo`，請參閱 *Slurm 文件*中的 [sinfo](https://slurm.schedmd.com/sinfo.html)。

```
$ sinfo
PARTITION AVAIL  TIMELIMIT  NODES  STATE NODELIST
ondemand     up   infinite     10  idle~ ondemand-dy-c52xlarge-[1-10]
spot*        up   infinite     18  idle~ spot-dy-c5xlarge-[1-10],spot-dy-t2micro-[2-9]
spot*        up   infinite      2   idle spot-dy-t2micro-1,spot-st-t2micro-1
```

輸出顯示您的叢集中有兩個`idle`處於 狀態的`t2.micro`運算節點。

**注意**  
`spot-st-t2micro-1` 是名稱`st`為 的靜態節點。此節點一律可用，並對應至叢集組態``min_count` = 1`中的 。
`spot-dy-t2micro-1` 是名稱`dy`為 的動態節點。此節點目前可供使用，因為它``initial_count` - `min_count` = 1`會根據您的叢集組態對應至 。此節點會在您的自訂 [`scaledown_idletime`](scaling-section.md#scaledown-idletime)5 分鐘後縮減。

其他節點都處於省電狀態，以節點狀態的尾碼顯示，沒有 EC2 `~` 執行個體支援它們。預設佇列由佇列名稱後面的`*`尾碼指定，因此您的預設任務佇列`spot`也是。

### 在多個佇列模式下執行任務
<a name="tutorial-mqm-running-job-mqm"></a>

接下來，嘗試執行任務以睡眠一段時間。任務稍後會輸出自己的主機名稱。請確定目前使用者可執行此指令碼。

```
$ cat hellojob.sh
#!/bin/bash
sleep 30
echo "Hello World from $(hostname)"

$ chmod +x hellojob.sh
$ ls -l hellojob.sh
-rwxrwxr-x 1 ec2-user ec2-user 57 Sep 23 21:57 hellojob.sh
```

使用 `sbatch`命令提交任務。使用 `-N 2`選項為此任務請求兩個節點，並確認任務已成功提交。如需 的詳細資訊`sbatch`，請參閱 *Slurm 文件*[https://slurm.schedmd.com/sbatch.html](https://slurm.schedmd.com/sbatch.html)中的 。

```
$ sbatch -N 2 --wrap "srun hellojob.sh"
Submitted batch job 2
```

您可以使用 `squeue`命令檢視佇列並檢查任務的狀態。請注意，因為您未指定特定佇列，所以會使用預設佇列 (`spot`)。如需 的詳細資訊`squeue`，請參閱 *Slurm文件*[https://slurm.schedmd.com/squeue.html](https://slurm.schedmd.com/squeue.html)中的 。

```
$ squeue
             JOBID PARTITION     NAME     USER ST       TIME  NODES NODELIST(REASON)
                 2      spot     wrap ec2-user  R       0:10      2 spot-dy-t2micro-1,spot-st-t2micro-1
```

輸出顯示任務目前處於執行中狀態。等待 30 秒讓任務完成，然後再次執行 `squeue`。

```
$ squeue
             JOBID PARTITION     NAME     USER ST       TIME  NODES NODELIST(REASON)
```

現在佇列中的任務都已完成，請在`slurm-2.out`目前的目錄中尋找輸出檔案。

```
$ cat slurm-2.out
Hello World from spot-dy-t2micro-1
Hello World from spot-st-t2micro-1
```

輸出也會顯示我們的任務在 `spot-st-t2micro-1`和 `spot-st-t2micro-2`節點上成功執行。

現在，使用以下命令指定特定執行個體的限制條件，以提交相同的任務。

```
$ sbatch -N 3 -p spot -C "[c5.xlarge*1&t2.micro*2]" --wrap "srun hellojob.sh"
Submitted batch job 3
```

您已將這些參數用於 `sbatch`。
+ `-N 3`– 請求三個節點
+ `-p spot`– 將任務提交至`spot`佇列。您也可以指定 ，將任務提交至`ondemand`佇列`-p ondemand`。
+ `-C "[c5.xlarge*1&t2.micro*2]"`– 指定此任務的特定節點限制條件。這會請求使用此任務的一 (1) 個`c5.xlarge`節點和兩 (2) 個`t2.micro`節點。

執行 `sinfo`命令以檢視節點和佇列。( 中的佇列 AWS ParallelCluster 在 中稱為分割區Slurm。)

```
$ sinfo
PARTITION AVAIL  TIMELIMIT  NODES  STATE NODELIST
ondemand     up   infinite     10  idle~ ondemand-dy-c52xlarge-[1-10]
spot*        up   infinite      1   mix# spot-dy-c5xlarge-1
spot*        up   infinite     17  idle~ spot-dy-c5xlarge-[2-10],spot-dy-t2micro-[2-9]
spot*        up   infinite      2  alloc spot-dy-t2micro-1,spot-st-t2micro-1
```

節點正在開機。這由節點狀態的字`#`尾表示。執行 squeue命令以檢視叢集中任務的相關資訊。

```
$ squeue
             JOBID PARTITION     NAME     USER ST       TIME  NODES NODELIST(REASON)
                 3      spot     wrap ec2-user CF       0:04      3 spot-dy-c5xlarge-1,spot-dy-t2micro-1,spot-st-t2micro-1
```

您的任務處於 `CF`(CONFIGURING) 狀態，正在等待執行個體擴展並加入叢集。

大約三分鐘後，節點應該可用，任務進入 `R`(RUNNING) 狀態。

```
$ sinfo
PARTITION AVAIL  TIMELIMIT  NODES  STATE NODELIST
ondemand     up   infinite     10  idle~ ondemand-dy-c52xlarge-[1-10]
spot*        up   infinite     17  idle~ spot-dy-c5xlarge-[2-10],spot-dy-t2micro-[2-9]
spot*        up   infinite      1    mix spot-dy-c5xlarge-1
spot*        up   infinite      2  alloc spot-dy-t2micro-1,spot-st-t2micro-1
$ squeue
             JOBID PARTITION     NAME     USER ST       TIME  NODES NODELIST(REASON)
                 3      spot     wrap ec2-user  R       0:04      3 spot-dy-c5xlarge-1,spot-dy-t2micro-1,spot-st-t2micro-1
```

任務完成，且所有三個節點都處於 `idle` 狀態。

```
$ squeue
             JOBID PARTITION     NAME     USER ST       TIME  NODES NODELIST(REASON)
$ sinfo
PARTITION AVAIL  TIMELIMIT  NODES  STATE NODELIST
ondemand     up   infinite     10  idle~ ondemand-dy-c52xlarge-[1-10]
spot*        up   infinite     17  idle~ spot-dy-c5xlarge-[2-10],spot-dy-t2micro-[2-9]
spot*        up   infinite      3   idle spot-dy-c5xlarge-1,spot-dy-t2micro-1,spot-st-t2micro-1
```

然後，在佇列中沒有剩餘的任務之後，您可以在`slurm-3.out`本機目錄中檢查 。

```
$ cat slurm-3.out 
Hello World from spot-dy-c5xlarge-1
Hello World from spot-st-t2micro-1
Hello World from spot-dy-t2micro-1
```

輸出也會顯示任務在對應的節點上成功執行。

您可以觀察縮減規模程序。在您指定的叢集組態中，自訂[`scaledown_idletime`](scaling-section.md#scaledown-idletime)為 5 分鐘。在閒置狀態五分鐘後，您的動態節點`spot-dy-c5xlarge-1``spot-dy-t2micro-1`會自動縮減規模並進入 `POWER_DOWN` 模式。請注意，靜態節點`spot-st-t2micro-1`不會縮減規模。

```
$ sinfo
PARTITION AVAIL  TIMELIMIT  NODES  STATE NODELIST
ondemand     up   infinite     10  idle~ ondemand-dy-c52xlarge-[1-10]
spot*        up   infinite      2  idle% spot-dy-c5xlarge-1,spot-dy-t2micro-1
spot*        up   infinite     17  idle~ spot-dy-c5xlarge-[2-10],spot-dy-t2micro-[2-9]
spot*        up   infinite      1   idle spot-st-t2micro-1
```

從上述程式碼中，您可以看到 `spot-dy-c5xlarge-1`和 `spot-dy-t2micro-1` 處於 `POWER_DOWN` 模式。這以`%`尾碼表示。對應的執行個體會立即終止，但節點會保持 `POWER_DOWN` 狀態，且無法使用 120 秒 （兩分鐘）。在此期間之後，節點會恢復省電，並再次可供使用。如需詳細資訊，請參閱[Slurm 適用於多個佇列模式的 指南](multiple-queue-mode-slurm-user-guide.md)。

這應該是叢集的最終狀態：

```
$ sinfo
PARTITION AVAIL  TIMELIMIT  NODES  STATE NODELIST
ondemand     up   infinite     10  idle~ ondemand-dy-c52xlarge-[1-10]
spot*        up   infinite     19  idle~ spot-dy-c5xlarge-[1-10],spot-dy-t2micro-[1-9]
spot*        up   infinite      1   idle spot-st-t2micro-1
```

登出叢集後，您可以執行 來清除 `pcluster delete`。如需 `pcluster list`和 的詳細資訊`pcluster delete`，請參閱 [`pcluster list`](pcluster.list.md)和 [`pcluster delete`](pcluster.delete.md)。

```
$ pcluster list
multi-queue CREATE_COMPLETE 2.11.9
$ pcluster delete multi-queue
Deleting: multi-queue
...
```

### 使用 EFA 和 GPU 執行個體在叢集上執行任務
<a name="tutorial-mqm-running-job-efa-gpu"></a>

教學課程的此部分詳細說明了如何修改組態，以及啟動包含具有 EFA 網路和 GPU 資源之執行個體的多個佇列的叢集。請注意，本教學中使用的執行個體是價格較高的執行個體。

**檢查您的 帳戶限制，以確保您已獲授權使用這些執行個體，然後再繼續進行本教學課程中所述的步驟。**

使用下列項目修改組態檔案。

```
[global]
update_check = true
sanity_check = true
cluster_template = multi-queue-special

[aws]
aws_region_name = <Your AWS 區域>

[scaling demo]
scaledown_idletime = 5

[cluster multi-queue-special]
key_name = <Your SSH key name>
base_os = alinux2                   # optional, defaults to alinux2
scheduler = slurm
master_instance_type = c5.xlarge    # optional, defaults to t2.micro
vpc_settings = <Your VPC section>
scaling_settings = demo
queue_settings = efa,gpu

[queue gpu]
compute_resource_settings = gpu_i1
disable_hyperthreading = true       # optional, defaults to false

[compute_resource gpu_i1]
instance_type = g3.8xlarge

[queue efa]
compute_resource_settings = efa_i1
enable_efa = true
placement_group = DYNAMIC           # optional, defaults to no placement group settings

[compute_resource efa_i1]
instance_type = c5n.18xlarge
max_count = 5
```

建立叢集

```
$ pcluster create multi-queue-special -t multi-queue-special
```

建立叢集之後，請使用您的私有 SSH 金鑰檔案登入您的頭部節點。

```
$ pcluster ssh multi-queue-special -i ~/path/to/keyfile.pem
```

這應該是叢集的初始狀態：

```
$ sinfo
PARTITION AVAIL  TIMELIMIT  NODES  STATE NODELIST
efa*         up   infinite      5  idle~ efa-dy-c5n18xlarge-[1-5]
gpu          up   infinite     10  idle~ gpu-dy-g38xlarge-[1-10]
```

本節說明如何提交一些任務，以檢查節點是否有 EFA 或 GPU 資源。

首先，撰寫任務指令碼。 `efa_job.sh`將休眠 30 秒。之後，請在 `lspci` 命令的輸出中尋找 EFA。 `gpu_job.sh`會休眠 30 秒。之後，執行 `nvidia-smi` 以顯示節點的 GPU 資訊。

```
$ cat efa_job.sh
#!/bin/bash

sleep 30
lspci | grep "EFA"

$ cat gpu_job.sh
#!/bin/bash

sleep 30
nvidia-smi

$ chmod +x efa_job.sh
$ chmod +x gpu_job.sh
```

使用 提交任務`sbatch`，

```
$ sbatch -p efa --wrap "srun efa_job.sh"
Submitted batch job 2
$ sbatch -p gpu --wrap "srun gpu_job.sh" -G 1
Submitted batch job 3 
$ squeue
             JOBID PARTITION     NAME     USER ST       TIME  NODES NODELIST(REASON)
                 2       efa     wrap ec2-user CF       0:32      1 efa-dy-c5n18xlarge-1
                 3       gpu     wrap ec2-user CF       0:20      1 gpu-dy-g38xlarge-1
$ sinfo
PARTITION AVAIL  TIMELIMIT  NODES  STATE NODELIST
efa*         up   infinite      1   mix# efa-dy-c5n18xlarge-1
efa*         up   infinite      4  idle~ efa-dy-c5n18xlarge-[2-5]
gpu          up   infinite      1   mix# gpu-dy-g38xlarge-1
gpu          up   infinite      9  idle~ gpu-dy-g38xlarge-[2-10]
```

幾分鐘後，您應該會在線上看到節點和正在執行的任務。

```
[ec2-user@ip-172-31-15-251 ~]$ sinfo
PARTITION AVAIL  TIMELIMIT  NODES  STATE NODELIST
efa*         up   infinite      4  idle~ efa-dy-c5n18xlarge-[2-5]
efa*         up   infinite      1    mix efa-dy-c5n18xlarge-1
gpu          up   infinite      9  idle~ gpu-dy-g38xlarge-[2-10]
gpu          up   infinite      1    mix gpu-dy-g38xlarge-1
[ec2-user@ip-172-31-15-251 ~]$ squeue
             JOBID PARTITION     NAME     USER ST       TIME  NODES NODELIST(REASON)
                 4       gpu     wrap ec2-user  R       0:06      1 gpu-dy-g38xlarge-1
                 5       efa     wrap ec2-user  R       0:01      1 efa-dy-c5n18xlarge-1
```

任務完成後，請檢查輸出。從 `slurm-2.out` 檔案中的輸出，您可以看到 EFA 存在於`efa-dy-c5n18xlarge-1`節點上。從 `slurm-3.out` 檔案中的輸出，您可以看到`nvidia-smi`輸出包含`gpu-dy-g38xlarge-1`節點的 GPU 資訊。

```
$ cat slurm-2.out
00:06.0 Ethernet controller: Amazon.com, Inc. Elastic Fabric Adapter (EFA)

$ cat slurm-3.out
Thu Oct  1 22:19:18 2020
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 450.51.05    Driver Version: 450.51.05    CUDA Version: 11.0     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|                               |                      |               MIG M. |
|===============================+======================+======================|
|   0  Tesla M60           Off  | 00000000:00:1D.0 Off |                    0 |
| N/A   28C    P0    38W / 150W |      0MiB /  7618MiB |      0%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+
|   1  Tesla M60           Off  | 00000000:00:1E.0 Off |                    0 |
| N/A   36C    P0    37W / 150W |      0MiB /  7618MiB |     98%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes:                                                                  |
|  GPU   GI   CI        PID   Type   Process name                  GPU Memory |
|        ID   ID                                                   Usage      |
|=============================================================================|
|  No running processes found                                                 |
+-----------------------------------------------------------------------------+
```

您可以觀察縮減規模程序。在叢集組態中，您先前已指定 5 分鐘[`scaledown_idletime`](scaling-section.md#scaledown-idletime)的自訂。因此，在閒置狀態五分鐘後，動態節點 `spot-dy-c5xlarge-1`和 `spot-dy-t2micro-1`會自動縮減規模並進入 `POWER_DOWN` 模式。最後，節點會進入省電模式，並再次可供使用。

```
$ sinfo
PARTITION AVAIL  TIMELIMIT  NODES  STATE NODELIST
efa*         up   infinite      1  idle% efa-dy-c5n18xlarge-1
efa*         up   infinite      4  idle~ efa-dy-c5n18xlarge-[2-5]
gpu          up   infinite      1  idle% gpu-dy-g38xlarge-1
gpu          up   infinite      9  idle~ gpu-dy-g38xlarge-[2-10]  

# After 120 seconds
$ sinfo
PARTITION AVAIL  TIMELIMIT  NODES  STATE NODELIST
efa*         up   infinite      5  idle~ efa-dy-c5n18xlarge-[1-5]
gpu          up   infinite     10  idle~ gpu-dy-g38xlarge-[1-10]
```

登出叢集後，您可以執行 來清除 ``pcluster delete` <cluster name>`。

```
$ pcluster list
multi-queue-special CREATE_COMPLETE 2.11.9
$ pcluster delete multi-queue-special
Deleting: multi-queue-special
...
```

 如需詳細資訊，請參閱[Slurm 適用於多個佇列模式的 指南](multiple-queue-mode-slurm-user-guide.md)。