Latenzen durch die Verwendung von Richtlinien für kompakte Platzierung reduzieren
Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
In diesem Dokument wird beschrieben, wie Sie die Netzwerklatenz zwischen den VMs eines Jobs reduzieren, indem Sie eine Richtlinie für kompakte Platzierung angeben.
Wenn sich VM-Hosts innerhalb einer Zone physisch näher beieinander befinden, kann die Netzwerklatenz zwischen VMs reduziert werden. Dieser Leistungsvorteil kann besonders nützlich für Jobs mit eng gekoppelten Aufgaben sein, z. B. Aufgaben, die über MPI-Bibliotheken kommunizieren.
Wenn Sie einen Job erstellen, können Sie optional eine Richtlinie für kompakte Platzierung angeben, um die VMs des Jobs näher zueinander zu platzieren.
Andernfalls werden VMs bei der Standard-VM-Platzierung in der Regel auf separate Hosts verteilt, um die Auswirkungen von Stromausfällen zu minimieren.
Weitere Informationen zu Richtlinien für kompakte Platzierung finden Sie in der Compute Engine-Dokumentation unter Richtlinien für die Platzierung.
Bitten Sie Ihren Administrator, Ihnen die folgenden IAM-Rollen zuzuweisen, um die Berechtigungen zu erhalten, die Sie zum Erstellen eines Jobs benötigen:
Wenn Sie für einen Job eine Richtlinie für kompakte Platzierung angeben, wird von Batch automatisch eine Richtlinie für kompakte Platzierung für die VMs des Jobs erstellt und gelöscht. Sie können für einen Job keine vorhandene Richtlinie für kompakte Platzierung angeben.
Sie können keine Anzahl von VMs für die Richtlinie für kompakte Platzierung angeben.
Die Richtlinie für die kompakte Platzierung für einen Job gilt unabhängig davon, auf wie vielen VMs der Job ausgeführt wird.
Job erstellen und ausführen, der eine Platzierungsrichtlinie verwendet
Sie können festlegen, dass für einen Job eine Richtlinie für kompakte Platzierung verwendet wird, indem Sie das Feld collocationCOLLOCATED mit der gcloud CLI oder der Batch API festlegen.
gcloud
Erstellen Sie eine JSON-Datei, in der die Konfigurationsdetails des Jobs angegeben und das Feld collocation auf COLLOCATED festgelegt wird.
Wenn Sie beispielsweise einen einfachen Script-Job erstellen möchten, in dem eine kompakte Platzierungsrichtlinie angegeben wird, erstellen Sie eine JSON-Datei mit folgendem Inhalt:
{"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: der Pfad zu einer JSON-Datei mit den Konfigurationsdetails des Jobs.
API
Stellen Sie eine POST-Anfrage an die Methode jobs.create, in der das Feld collocation auf COLLOCATED festgelegt wird.
Wenn Sie beispielsweise einen einfachen Scriptjob erstellen möchten, der eine Richtlinie für kompakte Platzierung angibt, stellen Sie die folgende Anfrage:
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"}}
[[["Leicht verständlich","easyToUnderstand","thumb-up"],["Mein Problem wurde gelöst","solvedMyProblem","thumb-up"],["Sonstiges","otherUp","thumb-up"]],[["Schwer verständlich","hardToUnderstand","thumb-down"],["Informationen oder Beispielcode falsch","incorrectInformationOrSampleCode","thumb-down"],["Benötigte Informationen/Beispiele nicht gefunden","missingTheInformationSamplesINeed","thumb-down"],["Problem mit der Übersetzung","translationIssue","thumb-down"],["Sonstiges","otherDown","thumb-down"]],["Zuletzt aktualisiert: 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)."]]