【问题标题】:Threads hierarchy design in CUDA for my code我的代码在 CUDA 中的线程层次结构设计
【发布时间】:2015-06-30 02:49:10
【问题描述】:

我想将我以前的 c++ 代码转换为 CUDA

for(int x=0 ; x < 100; x++)
{
    for(int y=0 ; y < 100; y++)
    {
        for(int w=0 ; w < 100; w++)
        {
            for(int z=0 ; z < 100; z++)
            {
              ........
            }
        }
    }
}

这些循环组合成一个新的 int 值。

如果我想使用 CUDA,我必须在构建之前设计线程层次结构 内核代码。

那么我该如何设计层次结构呢?

取决于每个循环,我认为它会是这样的:

100*100*100*100 = 100000000 个线程。

你能帮我吗

谢谢

我的 CUDA 规范:

CUDA 设备 #0

主要修订号:1

次要修订号:1

名称:GeForce G 105M

全局内存总量:536870912

每个块的总共享内存:16384

每个块的寄存器总数:8192

经纱尺寸:32

最大内存间距:2147483647

每个块的最大线程数:512

块的最大维度1:512

块的最大维度2:512

块的最大维度 3:64

网格最大维度1:65535

网格最大维度2:65535

网格的最大维度 3:1

时钟频率:1600000

总常量内存:65536

纹理对齐:256

并发复制和执行:否

多处理器数量:1

内核执行超时:是

【问题讨论】:

    标签: cuda


    【解决方案1】:

    100000000 个线程(或块)对于 GPU 来说并不算多。

    您的 GPU 具有 1.1 的计算能力,因此在前两个网格维度(x 和 y)中的每一个中限制为 65535 个块。由于 100*100 = 10000,我们可以在前两个网格维度中的每一个中启动 10000 个块,以覆盖整个 for 循环范围。这将在每次 for 循环迭代中启动一个块(xyzw 的唯一组合)并假设您将使用块中的线程来满足您的 for-循环计算代码:

    __global__ void mykernel(...){
    
      int idx = blockIdx.x;
      int idy = blockIdx.y;
    
      int w = idx/100;
      int z = idx%100;
      int x = idy/100;
      int y = idy%100;
    
      int tx = threadIdx.x;
    
     // (the body of your for-loop code here...
    
    }
    

    启动:

    dim3 blocks(10000, 10000);
    dim3 threads(...); // can use any number here up to 512 for your device
    mykernel<<<blocks, threads>>>(...);
    

    如果相反,您想为 for 循环的每个内部 z 迭代分配一个线程(可能有用/更高的性能取决于您正在做什么和您的数据组织),您可以做这样的事情:

    __global__ void mykernel(...){
    
      int idx = blockIdx.x;
      int idy = blockIdx.y;
    
      int w = idx/100;
      int x = idx%100;
      int y = idy;
    
      int z = threadIdx.x;
    
     // (the body of your for-loop code here...
    
    }
    

    启动:

    dim3 blocks(10000, 100);
    dim3 threads(100); 
    mykernel<<<blocks, threads>>>(...);
    

    以上所有内容都假设您的 for 循环迭代是独立的。如果您的 for 循环迭代相互依赖(依赖于执行顺序),那么这些简单的答案将不起作用,并且您没有在问题中提供足够的信息来讨论合理的策略。

    【讨论】:

    • 非常感谢您的精彩回复。
    猜你喜欢
    • 1970-01-01
    • 2018-01-08
    • 2014-12-06
    • 1970-01-01
    • 2016-10-16
    • 2013-11-21
    • 2011-05-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多