将 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 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 集群中的主节点以及主要和辅助工作器节点,请填写 InstanceGroupConfig.AcceleratorConfig acceleratorTypeUriacceleratorCount 字段,作为 cluster.create API 请求的一部分。这些字段采用以下值:

  • 要挂接到节点的 GPU 的类型
  • 要挂接到节点的 GPU 的数量

控制台

如需将 GPU 挂接到 Managed Service for Apache Spark 集群中的主节点以及主要和辅助工作器节点,请执行以下步骤:

  1. 打开 Managed Service for Apache Spark 在 Compute Engine 上创建 Managed Service for Apache Spark 集群 页面。
  2. 选择配置节点 面板。
  3. 管理节点工作器节点辅助工作器节点部分中的 CPU 平台和 GPU > GPU 下,指定节点的 GPU 数量和 GPU 类型。

安装 GPU 驱动程序

利用挂接到 Managed Service for Apache Spark 节点的 GPU 需要使用 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 运行时会受益:

  1. 运行其中一个 Spark ML 示例
  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!")

后续步骤