Managed Service for Apache Spark 可讓您將圖形處理單元 (GPU) 附加至 Managed Service for Apache Spark 叢集中的主要執行個體和 worker 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_GPUS、NVIDIA_P100_GPUS或NVIDIA_V100_GPUS)。如果配額頁面未列出 GPU,或您需要更多 GPU 配額,請要求提高配額。
GPU 類型
Managed Service for Apache Spark 節點支援下列 GPU 類型。將 GPU 附加至 Managed Service for Apache Spark 叢集時,必須指定 GPU 類型。
nvidia-tesla-l4- NVIDIA® Tesla® L4nvidia-tesla-a100- NVIDIA® Tesla® A100nvidia-tesla-p100- NVIDIA® Tesla® P100nvidia-tesla-v100- NVIDIA® Tesla® V100nvidia-tesla-p4- NVIDIA® Tesla® P4nvidia-tesla-t4- NVIDIA® Tesla® T4nvidia-rtx-pro-6000- NVIDIA® RTX® PRO 6000nvidia-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 映像檔。
Cloud de Confiance 控制台
如要將 GPU 附加至 Managed Service for Apache Spark 叢集,請按照下列步驟操作:
- 開啟「Create cluster」(建立叢集) 頁面。
- 在「Worker configuration」(Worker 設定) 區段中,按一下「GPUs」(GPU) 分頁標籤,然後指定 GPU 的類型和數量。
- 根據預設,驅動程式 (主要) 節點設定與主要 worker 設定相同。在「更多設定」部分,您可以點選「driver 節點」,清除「將 driver 節點預設為與主要 worker 相同」核取方塊,並指定 driver 節點的機型。
gcloud CLI
如要將 GPU 附加至 Managed Service for Apache Spark 叢集中的主要、主要 worker 和次要 worker 節點,請使用 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 的 acceleratorTypeUri 和 acceleratorCount 欄位。這些欄位會採用下列值:
- 要附加至節點的 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 設定執行階段環境屬性和 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 執行時會更有效率:
- 執行一個 Spark 機器學習範例。
- 使用
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!")
後續步驟
- 瞭解如何建立附加 GPU 的 Compute Engine 執行個體。
- 進一步瞭解 GPU 機型。