In iOS, what is the possible minimum amount of threads in a threadGroup

Question

I can check for the maximum via the code below, but how can I check for the minimum?

let maxThreads = mFunctionPSO.maxTotalThreadsPerThreadgroup

I saw that the minimum amount of threads depends on the chip architecture, but I could not find it in the metal documentation. Of course it makes no sense to make for example one thread in a threadGroup, but I keep running into the error below when the data inside the buffer is not long enough.

Setup

I test on an iPhone 8 running iOS 13.5.

var message: [UInt8] = [UInt8]("ABC".utf8)
threadAmount = message.count
        
mBufferResult = self.device.makeBuffer(
     bytes: &message, 
     length: MemoryLayout<UInt8>.stride * threadAmount, 
     options: MTLResourceOptions.storageModeShared)

mCommandBuffer = mCommandQueue.makeCommandBuffer()

mEncoder = mCommandBuffer?.makeComputeCommandEncoder()

mEncoder?.setBuffer(mBufferResult, offset: 0, index: 0)
        
let grid = MTLSize(width: threadAmount, height: 1, depth: 1)
let maxThreads = mFunctionPSO.maxTotalThreadsPerThreadgroup
let threadGroupSize = MTLSize(width: maxThreads, height: 1, depth: 1)
        
mEncoder?.setComputePipelineState(mFunctionPSO)

mEncoder?.dispatchThreads(grid, threadsPerThreadgroup: threadGroupSize)

Problem

threadAmount will be 3, then this is the error I get on dispatchThreads... what does this error even mean? If i change the message to ABCD then the error disappears.

Compute Function(example): argument result[0] from buffer(0) with offset(0) and length(3) has space for 3 bytes, but argument has a length(4).

This is the metal shader code, just a basic test setup:

kernel void example(device uint* result,
                       uint index [[thread_position_in_grid]])
{
   //some code
}
Answered by Ceylo in 747944022

Your kernel takes a uint* result. uint is 4 bytes, but your Swift code gives a buffer of 3 UInt8 aka 3 bytes.

Accepted Answer

Your kernel takes a uint* result. uint is 4 bytes, but your Swift code gives a buffer of 3 UInt8 aka 3 bytes.

In iOS, what is the possible minimum amount of threads in a threadGroup
 
 
Q