Mengurangi latensi dengan menggunakan kebijakan penempatan rapat
Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Dokumen ini menjelaskan cara mengurangi latensi jaringan di antara VM tugas dengan
menentukan kebijakan penempatan rapat.
Memiliki host VM yang secara fisik lebih berdekatan satu sama lain dalam zona dapat mengurangi latensi jaringan antar-VM. Manfaat performa ini sangat berguna untuk tugas dengan tugas yang terikat erat, seperti tugas yang berkomunikasi menggunakan library MPI.
Saat membuat tugas, Anda dapat secara opsional mewajibkan VM tugas ditempatkan
lebih berdekatan satu sama lain dengan menentukan kebijakan penempatan yang ringkas.
Jika tidak, penempatan VM default biasanya mendistribusikan VM di seluruh host terpisah untuk membantu meminimalkan dampak kegagalan daya.
Untuk mempelajari kebijakan penempatan ringkas lebih lanjut, lihat
Tentang kebijakan penempatan
dalam dokumentasi Compute Engine.
Saat Anda menentukan agar tugas menggunakan kebijakan penempatan yang ringkas, Batch akan otomatis membuat dan menghapus kebijakan penempatan yang ringkas untuk VM tugas. Anda tidak dapat menentukan
kebijakan penempatan compact yang ada untuk tugas.
Anda tidak dapat menentukan jumlah VM untuk kebijakan penempatan rapat.
Kebijakan penempatan ringkas untuk tugas berlaku terlepas dari jumlah VM yang menjalankan tugas tersebut.
Membuat dan menjalankan tugas yang menggunakan kebijakan penempatan
Anda dapat menentukan agar tugas menggunakan kebijakan penempatan ringkas dengan menetapkan
kolom collocation
ke COLLOCATED menggunakan gcloud CLI atau Batch API.
gcloud
Buat file JSON yang menentukan detail konfigurasi tugas dan menetapkan
kolom collocation ke COLLOCATED.
Misalnya, untuk membuat tugas skrip dasar yang menentukan kebijakan penempatan rapat, buat file JSON dengan konten berikut:
{"taskGroups":[{"taskSpec":{"runnables":[{"script":{"text":"echo Hello world from task ${BATCH_TASK_INDEX}"}}]},"taskCount":3}],"allocationPolicy":{"placement":{"collocation":"COLLOCATED"}},"logsPolicy":{"destination":"CLOUD_LOGGING"}}
JSON_CONFIGURATION_FILE: jalur untuk file
JSON dengan detail konfigurasi tugas.
API
Buat permintaan POST ke
metode jobs.create
yang menetapkan kolom collocation ke COLLOCATED.
Misalnya, untuk membuat tugas skrip dasar yang menentukan
kebijakan penempatan rapat, buat permintaan berikut:
POSThttps://batch.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/jobs?job_id=JOB_NAME{"taskGroups":[{"taskSpec":{"runnables":[{"script":{"text":"echo Hello world from task ${BATCH_TASK_INDEX}"}}]},"taskCount":3}],"allocationPolicy":{"placement":{"collocation":"COLLOCATED"}},"logsPolicy":{"destination":"CLOUD_LOGGING"}}
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 2025-08-12 UTC."],[[["\u003cp\u003eSpecifying a compact placement policy reduces network latency between a job's virtual machines (VMs) by ensuring they are located on physically closer hosts within a zone.\u003c/p\u003e\n"],["\u003cp\u003eCompact placement policies are beneficial for jobs with tightly coupled tasks, especially those that utilize MPI libraries for communication.\u003c/p\u003e\n"],["\u003cp\u003eBatch automatically creates and manages compact placement policies when specified, without the ability to use a pre-existing one or specifying the number of VMs.\u003c/p\u003e\n"],["\u003cp\u003eYou can set a compact placement policy for a job by setting the \u003ccode\u003ecollocation\u003c/code\u003e field to \u003ccode\u003eCOLLOCATED\u003c/code\u003e in the job configuration, either through the gcloud CLI or the Batch API.\u003c/p\u003e\n"],["\u003cp\u003eBefore creating a job, ensure you have the necessary permissions, which includes the Batch Job Editor role and the Service Account User role.\u003c/p\u003e\n"]]],[],null,["# Reduce latency by using compact placement policies\n\nThis document explains how to reduce network latency among a job's VMs by\nspecifying a compact placement policy.\n\nHaving VM hosts that are physically closer to each other within a\n[zone](/compute/docs/regions-zones#zones_and_clusters) can reduce\nnetwork latency between VMs. This performance benefit can be especially useful\nfor jobs with tightly coupled tasks, such as\n[tasks that communicate using MPI libraries](/batch/docs/create-run-job-mpi-library).\nWhen you create a job, you can optionally require a job's VMs to be located\ncloser to each other by specifying a compact placement policy.\nOtherwise, default VM placement typically distributes VMs across\nseparate hosts to help minimize the impact of power failures.\n\nTo learn more about compact placement policies, see\n[About placement policies](/compute/docs/instances/placement-policies-overview)\nin the Compute Engine documentation.\n\nBefore you begin\n----------------\n\n1. If you haven't used Batch before, review [Get started with Batch](/batch/docs/get-started) and enable Batch by completing the [prerequisites for projects and users](/batch/docs/get-started#prerequisites).\n2.\n\n To get the permissions that\n you need to create a job,\n\n ask your administrator to grant you the\n following IAM roles:\n\n - [Batch Job Editor](/iam/docs/roles-permissions/batch#batch.jobsEditor) (`roles/batch.jobsEditor`) on the project\n - [Service Account User](/iam/docs/roles-permissions/iam#iam.serviceAccountUser) (`roles/iam.serviceAccountUser`) on the [job's service account](/batch/docs/create-run-job-custom-service-account#create-job-service-account), which by default is the [default Compute Engine service account](/compute/docs/access/service-accounts#default_service_account)\n\n\n For more information about granting roles, see [Manage access to projects, folders, and organizations](/iam/docs/granting-changing-revoking-access).\n\n\n You might also be able to get\n the required permissions through [custom\n roles](/iam/docs/creating-custom-roles) or other [predefined\n roles](/iam/docs/roles-overview#predefined).\n\n\u003cbr /\u003e\n\nRestrictions\n------------\n\nIn addition to the\n[general restrictions for compact placement policies](/compute/docs/instances/placement-policies-overview#restrictions-compact-policies),\nBatch also has the following restrictions:\n\n- When you specify for a job to use a compact placement policy, Batch automatically creates and deletes a compact placement policy for the job's VMs. You can't specify an existing compact placement policy for a job.\n- You can't specify a number of VMs for the compact placement policy. The compact placement policy for a job applies regardless of how many VMs the job runs on.\n\nCreate and run a job that uses a placement policy\n-------------------------------------------------\n\nYou can specify for a job to use a compact placement policy by setting the\n[`collocation` field](/batch/docs/reference/rest/v1/projects.locations.jobs#PlacementPolicy.FIELDS.collocation)\nto `COLLOCATED` using the gcloud CLI or Batch API. \n\n### gcloud\n\n1. Create a JSON file that specifies the job's configuration details and sets\n the `collocation` field to `COLLOCATED`.\n\n For example, to create a basic script job that specifies a\n compact placement policy, create a JSON file with the following contents: \n\n {\n \"taskGroups\": [\n {\n \"taskSpec\": {\n \"runnables\": [\n {\n \"script\": {\n \"text\": \"echo Hello world from task ${BATCH_TASK_INDEX}\"\n }\n }\n ]\n },\n \"taskCount\": 3\n }\n ],\n \"allocationPolicy\": {\n \"placement\": {\n \"collocation\": \"COLLOCATED\"\n }\n },\n \"logsPolicy\": {\n \"destination\": \"CLOUD_LOGGING\"\n }\n }\n\n2. To create and run the job, use the\n [`gcloud batch jobs submit` command](/sdk/gcloud/reference/batch/jobs/submit):\n\n gcloud batch jobs submit \u003cvar translate=\"no\"\u003eJOB_NAME\u003c/var\u003e \\\n --location \u003cvar translate=\"no\"\u003eLOCATION\u003c/var\u003e \\\n --config \u003cvar translate=\"no\"\u003eJSON_CONFIGURATION_FILE\u003c/var\u003e\n\n Replace the following:\n - \u003cvar translate=\"no\"\u003eJOB_NAME\u003c/var\u003e: the name of the job.\n\n - \u003cvar translate=\"no\"\u003eLOCATION\u003c/var\u003e: the [location](/batch/docs/locations)\n of the job.\n\n - \u003cvar translate=\"no\"\u003eJSON_CONFIGURATION_FILE\u003c/var\u003e: the path for a JSON\n file with the job's configuration details.\n\n### API\n\nMake a `POST` request to the\n[`jobs.create` method](/batch/docs/reference/rest/v1/projects.locations.jobs/create)\nthat sets the `collocation` field to `COLLOCATED`.\n\nFor example, to create a basic script job that specifies a\ncompact placement policy, make the following request: \n\n POST https://batch.googleapis.com/v1/projects/\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e/locations/\u003cvar translate=\"no\"\u003eLOCATION\u003c/var\u003e/jobs?job_id=\u003cvar translate=\"no\"\u003eJOB_NAME\u003c/var\u003e\n\n {\n \"taskGroups\": [\n {\n \"taskSpec\": {\n \"runnables\": [\n {\n \"script\": {\n \"text\": \"echo Hello world from task ${BATCH_TASK_INDEX}\"\n }\n }\n ]\n },\n \"taskCount\": 3\n }\n ],\n \"allocationPolicy\": {\n \"placement\": {\n \"collocation\": \"COLLOCATED\"\n }\n },\n \"logsPolicy\": {\n \"destination\": \"CLOUD_LOGGING\"\n }\n }\n\nReplace the following:\n\n- \u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e: the\n [project ID](/resource-manager/docs/creating-managing-projects#identifying_projects)\n of your project.\n\n- \u003cvar translate=\"no\"\u003eLOCATION\u003c/var\u003e: the [location](/batch/docs/locations)\n of the job.\n\n- \u003cvar translate=\"no\"\u003eJOB_NAME\u003c/var\u003e: the name of the job.\n\nWhat's next\n-----------\n\n- If you have issues creating or running a job, see [Troubleshooting](/batch/docs/troubleshooting).\n- [View jobs and tasks](/batch/docs/view-jobs-tasks).\n- Learn about more [job creation options](/batch/docs/create-run-job#job-creation-options)."]]