Metal - 线程和线程组
Metal - Threads and ThreadGroups
我现在正在学习 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
对于line 1
,这3个整数代表什么?我的猜测是分配进程中要使用的线程数,但哪个是哪个?
line 1
和'line 2'有什么区别?我再次猜测是线程和线程组之间的不同。但我不确定根本区别是什么以及何时使用什么。
第一行给出每组的线程数(在本例中为二维 8x8),而第二行给出每个网格的组数。然后第三行的 dispatchThreadgroups(_:threadsPerThreadgroup:) 函数使用这两个数字。组数可以省略,默认使用一组。
将工作项网格分派到计算内核时,您有责任将网格划分为称为 threadgroups 的子集,每个子集都有一定数量的线程(width * height * depth) 小于相应计算管道状态的 maxTotalThreadsPerThreadgroup
。
threadsPerThreadgroup
大小表示网格每个子集的"shape"(即每个网格维度中的线程数)。 threadgroupsPerGrid
参数表示有多少个线程组组成了整个网格。正如在您的代码中一样,它通常是纹理的尺寸除以您选择的线程组大小的尺寸。
一个性能说明:每个计算管道状态都有一个 threadExecutionWidth
值,表示一个线程组中有多少线程将由 GPU 一起调度和执行。因此,最佳线程组大小将始终是 threadExecutionWidth
的倍数。在开发过程中,像您目前所做的那样只发送一个小方格是完全可以接受的。
我现在正在学习 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
对于
line 1
,这3个整数代表什么?我的猜测是分配进程中要使用的线程数,但哪个是哪个?line 1
和'line 2'有什么区别?我再次猜测是线程和线程组之间的不同。但我不确定根本区别是什么以及何时使用什么。
第一行给出每组的线程数(在本例中为二维 8x8),而第二行给出每个网格的组数。然后第三行的 dispatchThreadgroups(_:threadsPerThreadgroup:) 函数使用这两个数字。组数可以省略,默认使用一组。
将工作项网格分派到计算内核时,您有责任将网格划分为称为 threadgroups 的子集,每个子集都有一定数量的线程(width * height * depth) 小于相应计算管道状态的 maxTotalThreadsPerThreadgroup
。
threadsPerThreadgroup
大小表示网格每个子集的"shape"(即每个网格维度中的线程数)。 threadgroupsPerGrid
参数表示有多少个线程组组成了整个网格。正如在您的代码中一样,它通常是纹理的尺寸除以您选择的线程组大小的尺寸。
一个性能说明:每个计算管道状态都有一个 threadExecutionWidth
值,表示一个线程组中有多少线程将由 GPU 一起调度和执行。因此,最佳线程组大小将始终是 threadExecutionWidth
的倍数。在开发过程中,像您目前所做的那样只发送一个小方格是完全可以接受的。