Metal - Threads和ThreadGroups

时间:2016-09-23 13:47:03

标签: multithreading metal

我正在学习Metal并尝试理解以下几行:

let threadGroupCount = MTLSizeMake(8, 8, 1) ///line 1
let threadGroups = MTLSizeMake(drawable.texture.width / threadGroupCount.width, drawable.texture.height / threadGroupCount.height, 1) ///line 2

command_encoder.dispatchThreadgroups(threadGroups, threadsPerThreadgroup: threadGroupCount) ///line 3
  1. line 1,3个整数代表什么?我的猜测是分配在进程中使用的线程数,但哪个是哪个?

  2. line 1和'第2行'之间有什么不同?我再次猜测线程和线程组之间的差异。但我不确定什么是根本区别以及何时使用什么。

2 个答案:

答案 0 :(得分:7)

在将工作项网格分派给计算内核时,您有责任将网格划分为名为 threadgroups 的子集,每个子​​集都有一个线程总数(width * height *深度)小于相应计算管道状态的maxTotalThreadsPerThreadgroup

threadsPerThreadgroup大小表示"形状"网格的每个子集(即每个网格维度中的线程数)。 threadgroupsPerGrid参数指示构成整个网格的线程组的数量。在代码中,通常是纹理的尺寸除以您选择的线程组尺寸的尺寸。

一个性能注释:每个计算管道状态都有一个threadExecutionWidth值,表示一个线程组将由GPU一起调度和执行多少个线程。因此,最佳线程组大小将始终是threadExecutionWidth的倍数。在开发过程中,只需调度当前正在进行的小方格网即可。

答案 1 :(得分:1)

第一行为您提供每组的线程数(在本例中为二维8x8),而第二行为您提供每个网格的组数。然后第三行的dispatchThreadgroups(_:threadsPerThreadgroup:)函数使用这两个数字。可以省略组的数量,在这种情况下,它默认使用一个组。