將 GPU 附加至叢集

Managed Service for Apache Spark 可讓您將圖形處理器 (GPU) 附加至 Managed Service for Apache Spark 叢集中的主要節點及工作站 Compute Engine 節點。您可以使用這些 GPU 加速處理執行個體上的特定工作負載,例如機器學習和資料處理。

如要進一步瞭解您可以使用 GPU 執行哪些作業,以及 GPU 硬體類型,請閱讀 Compute Engine 上的 GPU

事前準備

  • GPU 需要特殊的驅動程式和軟體。這些項目已預先安裝在 Managed Service for Apache Spark -ml 映像檔中 (建議使用 -ml 映像檔),如有需要,也可以手動安裝。
  • 請閱讀 Compute Engine 上的 GPU 定價,瞭解在執行個體中使用 GPU 所需費用。
  • 請閱讀具有 GPU 的執行個體的限制,瞭解這些執行個體的運作方式與非 GPU 執行個體有何不同。
  • 請查看專案的配額頁面,確保專案有足夠的 GPU 配額 (NVIDIA_T4_GPUSNVIDIA_P100_GPUSNVIDIA_V100_GPUS)。如果配額頁面未列出 GPU,或您需要額外的 GPU 配額,請要求增加配額

GPU 類型

Managed Service for Apache Spark 節點支援下列 GPU 類型。將 GPU 附加至 Managed Service for Apache Spark 叢集時,必須指定 GPU 類型。

  • nvidia-tesla-l4 - NVIDIA® Tesla® L4
  • nvidia-tesla-a100 - NVIDIA® Tesla® A100
  • nvidia-tesla-p100 - NVIDIA® Tesla® P100
  • nvidia-tesla-v100 - NVIDIA® Tesla® V100
  • nvidia-tesla-p4 - NVIDIA® Tesla® P4
  • nvidia-tesla-t4 - NVIDIA® Tesla® T4
  • nvidia-rtx-pro-6000 - NVIDIA® RTX® PRO 6000
  • nvidia-tesla-p100-vws - NVIDIA® Tesla® P100 虛擬工作站
  • nvidia-tesla-p4-vws - NVIDIA® Tesla® P4 虛擬工作站
  • nvidia-tesla-t4-vws - NVIDIA® Tesla® T4 虛擬工作站

將 GPU 附加至叢集

如要將 GPU 附加至 Managed Service for Apache Spark 叢集,建立叢集時必須指定 -ml 映像檔 (建議),或使用初始化動作安裝 GPU 驅動程式。下列範例會指定或假設建立叢集時使用 2.3-ml-ubuntu 映像檔。

Google Cloud 控制台

如要將 GPU 附加至 Managed Service for Apache Spark 叢集,請執行下列步驟:

  1. 開啟「建立叢集頁面。
  2. 在「工作站設定」部分,按一下「GPU」分頁標籤,然後指定 GPU 類型和數量。
  3. 根據預設,驅動程式 (主要) 節點設定與主要 worker 設定相同。 在「Additional configuration」(其他設定) 下,您可以點選「driver 節點」,取消勾選「Default driver node to the same as primary worker」(將 driver 節點預設為與主要 worker 相同) 核取方塊,然後指定 driver 節點的機型。

gcloud CLI

如要將 GPU 附加至 Managed Service for Apache Spark 叢集中的主要、主要和次要工作站節點,請使用 gcloud dataproc clusters create ‑‑master-accelerator‑‑worker-accelerator‑‑secondary-worker-accelerator 旗標建立叢集。這些標記採用下列值:

  • 要附加至節點的 GPU 類型
  • 要附加至節點的 GPU 數量

GPU 類型是必填欄位,而 GPU 數量是選填欄位 (預設值是 1 個 GPU)。

範例:

gcloud dataproc clusters create cluster-name \
    --image-version=2.3-ml-ubuntu \
    --region=region \
    --master-accelerator type=nvidia-tesla-t4 \
    --worker-accelerator type=nvidia-tesla-t4,count=4 \
    --secondary-worker-accelerator type=nvidia-tesla-t4,count=4 \
    ... other flags

REST API

如要將 GPU 連接至 Managed Service for Apache Spark 叢集中的主要執行個體、主要和次要 worker 節點,請在 cluster.create API 要求中,填寫 InstanceGroupConfig.AcceleratorConfig acceleratorTypeUriacceleratorCount 欄位。這些欄位採用下列值:

  • 要附加至節點的 GPU 類型
  • 要附加至節點的 GPU 數量

安裝 GPU 驅動程式

您必須安裝 GPU 驅動程式,才能使用附加至 Managed Service for Apache Spark 節點的 GPU。除了使用 Managed Service for Apache Spark -ml 映像檔中安裝的 GPU 驅動程式,您也可以在建立叢集時,使用下列初始化動作安裝 GPU 驅動程式:

確認 GPU 驅動程式安裝成功

如要驗證叢集上的 GPU 驅動程式安裝作業,請使用 SSH 連線至叢集主要節點,然後執行下列指令:

nvidia-smi

如果驅動程式正常運作,輸出會顯示驅動程式版本和 GPU 統計資料 (請參閱驗證 GPU 驅動程式安裝)。

Spark 設定

將工作提交至 Spark 時,您可以使用 spark.executorEnv Spark 設定 runtime environment 屬性,搭配 LD_PRELOAD 環境變數預先載入所需程式庫。

範例:

gcloud dataproc jobs submit spark --cluster=CLUSTER_NAME \
  --region=REGION \
  --class=org.apache.spark.examples.SparkPi \
  --jars=file:///usr/lib/spark/examples/jars/spark-examples.jar \
  --properties=spark.executorEnv.LD_PRELOAD=libnvblas.so,spark.task.resource.gpu.amount=1,spark.executor.resource.gpu.amount=1,spark.executor.resource.gpu.discoveryScript=/usr/lib/spark/scripts/gpu/getGpusResources.sh

範例 GPU 工作

您可以執行下列任何工作來測試 Managed Service for Apache Spark 上的 GPU,這些工作在使用 GPU 時會增加效能:

  1. 執行一個 Spark 機器學習範例
  2. 使用 spark-shell 執行下列範例,以執行矩陣運算:
import org.apache.spark.mllib.linalg._
import org.apache.spark.mllib.linalg.distributed._
import java.util.Random

def makeRandomSquareBlockMatrix(rowsPerBlock: Int, nBlocks: Int): BlockMatrix = {
  val range = sc.parallelize(1 to nBlocks)
  val indices = range.cartesian(range)
  return new BlockMatrix(
      indices.map(
          ij => (ij, Matrices.rand(rowsPerBlock, rowsPerBlock, new Random()))),
      rowsPerBlock, rowsPerBlock, 0, 0)
}

val N = 1024 * 4
val n = 2
val mat1 = makeRandomSquareBlockMatrix(N, n)
val mat2 = makeRandomSquareBlockMatrix(N, n)
val mat3 = mat1.multiply(mat2)
mat3.blocks.persist.count
println("Processing complete!")

後續步驟