---
title: "dispatchThreadgroups(_:threadsPerThreadgroup:)"
framework: metal
role: symbol
role_heading: Instance Method
path: "metal/mtlcomputecommandencoder/dispatchthreadgroups(_:threadsperthreadgroup:)"
---

# dispatchThreadgroups(_:threadsPerThreadgroup:)

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

## Declaration

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

## Parameters

- `threadgroupsPerGrid`: An doc://com.apple.metal/documentation/Metal/MTLSize instance that represents the number of threads for each grid dimension.
- `threadsPerThreadgroup`: An doc://com.apple.metal/documentation/Metal/MTLSize instance that represents the number of threads in a threadgroup.

## Mentioned in

Calculating threadgroup and grid sizes

## Discussion

Discussion tip: Prefer using dispatchThreads for your kernel calls on Apple4 and later Apple GPUs. See 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 for an example.

## See Also

### Dispatching kernel calls directly

- [dispatchThreads(_:threadsPerThreadgroup:)](metal/mtlcomputecommandencoder/dispatchthreads(_:threadsperthreadgroup:).md)
