To add a GPU to a Compute Engine instance in GCP, you need to first ensure that you are using a compatible machine type and zone that supports GPUs. Here are the steps for each method:
1. Using GCP Console:
a. Go to the GCP Console: https://console.cloud.google.com/
b. Navigate to Compute Engine > VM instances.
c. Click on the name of the instance you want to modify.
d. Click the “Stop” button and wait for the instance to be stopped.
e. Click the “Edit” button at the top of the page.
f. Under “Machine configuration,” select a compatible machine type that supports GPUs.
g. Under “GPUs,” click “Add GPU.” Select the GPU type and the number of GPUs you want to attach to the instance.
h. Click the “Save” button at the bottom of the page.
i. Click the “Start” button to restart the instance with the added GPU.
2. Using gcloud CLI:
a. Install the Google Cloud SDK and configure authentication: https://cloud.google.com/sdk/docs/install
b. Stop the instance using the gcloud compute instances stop
command:
gcloud compute instances stop INSTANCE_NAME --zone ZONE
Replace INSTANCE_NAME
and ZONE
with appropriate values.
c. Update the instance by adding a GPU using the gcloud compute instances update
command:
gcloud compute instances update INSTANCE_NAME --zone ZONE --machine-type MACHINE_TYPE --accelerator type=GPU_TYPE,count=GPU_COUNT
Replace INSTANCE_NAME
, ZONE
, MACHINE_TYPE
, GPU_TYPE
, and GPU_COUNT
with appropriate values.
d. Start the instance using the gcloud compute instances start
command:
gcloud compute instances start INSTANCE_NAME --zone ZONE
Replace INSTANCE_NAME
and ZONE
with appropriate values.
3. Using Terraform:
a. Update the main.tf
file to add a GPU to the instance:
resource "google_compute_instance" "example" {
name = "example-instance"
machine_type = "MACHINE_TYPE" # Use a compatible machine type that supports GPUs
zone = "ZONE"
# ... other settings ...
attached_disk {
source = google_compute_disk.example.self_link
device_name = "example-disk"
}
guest_accelerator {
type = "GPU_TYPE"
count = GPU_COUNT
}
# ... other settings ...
}
Replace MACHINE_TYPE
, ZONE
, GPU_TYPE
, and GPU_COUNT
with appropriate values.
b. Run the following commands to apply the changes:
terraform init
terraform plan
terraform apply
By following these steps, you can add a GPU to a Compute Engine instance in GCP using the GCP Console, gcloud CLI, or Terraform. Adding a GPU can provide significant performance improvements for specific workloads, such as machine learning, gaming, and graphics rendering. However, keep in mind that adding a GPU will also increase the cost of the instance, so ensure that it is necessary for your use case.
Leave a Reply