Tutorial for creating a manual MPI cluster on Google Cloud using Terraform and gcloud CLI.
- gcloud CLI installed and configured
- Terraform installed
- Google Cloud project ID configured in
.envfile
Create a .env file in the project root with your configuration:
cp .env.example .envEdit .env with your values, then load the variables:
source .envGCP_PROJECT_ID- Your Google Cloud Project IDGCP_ZONE- Google Cloud Zone (e.g., us-west1-a)GCP_REGION- Google Cloud Region (e.g., us-west1)CLUSTER_NAME- Cluster name tagMPIUSER- MPI user nameMASTER_NODE- Master node nameWORKER_NODES- Comma-separated worker node namesNUM_WORKERS- Number of worker nodesMACHINE_TYPE- VM machine type (e.g., e2-micro)
cd terraform
terraform init
terraform apply -auto-approveWait 60 seconds for the startup scripts to complete and the cluster to be ready.
gcloud compute scp /path/to/local/dir mpiuser@master:/home/mpiuser/ --zone=$GCP_ZONE --project=$GCP_PROJECT_ID --recursegcloud compute ssh mpiuser@master --zone=$GCP_ZONE --project=$GCP_PROJECT_IDOn the master node, compile your MPI program:
mpicc -o /home/mpiuser/<your_program> /home/mpiuser/<your_program>.cFrom the master, copy the compiled executable to all worker nodes:
for i in 1 2 3; do
scp /home/mpiuser/<your_program> worker-$i:/home/mpiuser/
doneExecute your MPI program across the cluster:
mpirun -np 2 --hostfile /home/mpiuser/hfile /home/mpiuser/<your_program>Where:
-np 2specifies the number of processes--hostfile /home/mpiuser/hfilespecifies the host file with cluster nodes/home/mpiuser/<your_program>is the path to your compiled executable
To destroy the cluster and free resources:
cd terraform
terraform destroy -auto-approve