<!--
{
  "availability" : [
    "iOS: 8.0.0 -",
    "iPadOS: 8.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.11.0 -",
    "tvOS: -",
    "visionOS: 1.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Metal",
  "identifier" : "/documentation/Metal/MTLComputeCommandEncoder/dispatchThreadgroups(_:threadsPerThreadgroup:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Metal"
    ],
    "preciseIdentifier" : "c:objc(pl)MTLComputeCommandEncoder(im)dispatchThreadgroups:threadsPerThreadgroup:"
  },
  "title" : "dispatchThreadgroups(_:threadsPerThreadgroup:)"
}
-->

# dispatchThreadgroups(_:threadsPerThreadgroup:)

Encodes a compute dispatch command using a grid aligned to threadgroup boundaries.

```
func dispatchThreadgroups(_ threadgroupsPerGrid: MTLSize, threadsPerThreadgroup: MTLSize)
```

## Parameters

`threadgroupsPerGrid`

An [`MTLSize`](/documentation/Metal/MTLSize) instance that represents the number of threads for each grid dimension.

`threadsPerThreadgroup`

An [`MTLSize`](/documentation/Metal/MTLSize) instance that represents the number of threads in a threadgroup.

## Discussion> Tip:
> Prefer using dispatchThreads for your kernel calls on `Apple4` and later Apple GPUs. See [Metal Feature Set Tables (PDF)](https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf) for information on hardware support.

Metal calculates the number of threads in a grid by multiplying `threadsPerThreadgroup` by `threadgroupsPerGrid`.

If the size of your data doesn’t match the size of the grid, perform boundary checks in your compute function to avoid accessing data out of bounds. See [Calculating threadgroup and grid sizes](/documentation/Metal/calculating-threadgroup-and-grid-sizes) for an example.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)