简体   繁体   English

对于使用CUDA的嵌套循环

[英]For nested loops with CUDA

I'm having a problem with some for nested loops that I have to convert from C/C++ into CUDA. 我遇到一些嵌套循环的问题,我必须从C / C ++转换为CUDA。 Basically I have 4 for nested loops which are sharing the same array and making bit shift operations. 基本上我有4个嵌套循环,它们共享相同的数组并进行位移操作。

#define N 65536

// ----------------------------------------------------------------------------------

int a1,a2,a3,a4, i1,i2,i3,i4;

int Bit4CBitmapLookUp[16] = {0, 1, 3, 3, 7, 7, 7, 7, 15, 15, 15, 15, 15, 15, 15, 15};

int _cBitmapLookupTable[N];

int s = 0;  // index into the cBitmapLookupTable

for (i1 = 0; i1 < 16; i1++)
{
    // first customer
    a1 = Bit4CBitmapLookUp[i1] << 12;

    for (i2 = 0; i2 < 16; i2++)
    {
        // second customer
        a2 = Bit4CBitmapLookUp[i2] << 8;

        for (i3 = 0; i3 < 16; i3++)
        {
            // third customer
            a3 = Bit4CBitmapLookUp[i3] << 4;

            for (i4 = 0;i4 < 16;i4++)
            {
                // fourth customer
                a4 = Bit4CBitmapLookUp[i4];

                // now actually set the sBitmapLookupTable value
                _cBitmapLookupTable[s] = a1 | a2 | a3 | a4;

                s++;

            } // for i4
        } // for i3
    } // for i2
} // for i1

This is the code that I should convert into CUDA. 这是我应该转换为CUDA的代码。 I tried different ways but everytime i having the wrong output. 我尝试了不同的方法,但每次输出错误。 Here i post my version of CUDA conversion (the piece from kernel's part) 在这里,我发布我的CUDA转换版本(来自内核的部分)

#define N 16

//----------------------------------------------------------------------------------

// index for the GPU
int i1 = blockDim.x * blockIdx.x + threadIdx.x;
int i2 = blockDim.y * blockIdx.y + threadIdx.y;
int i3 = i1;
int i4 = i2;

__syncthreads();
for(i1 = i2 = 0; i1 < N, i2 < N; i1++, i2++)
{
    // first customer
    a1 = Bit4CBitmapLookUp_device[i1] << 12;

    // second customer
    a2 = Bit4CBitmapLookUp_device[i2] << 8;

    for(i3 = i4 = 0; i3 < N, i4 < N; i3++, i4++){
        // third customer
        a3 = Bit4CBitmapLookUp_device[i3] << 4;

        // fourth customer
        a4 = Bit4CBitmapLookUp_device[i4];

        // now actually set the sBitmapLookupTable value
        _cBitmapLookupTable[s] = a1 | a2 | a3 | a4;
        s++;
    }
} 

I'm brand new in CUDA and I'm still learning, but really i can't find a solution for those for nested loops. 我是CUDA的新手,我还在学习,但实际上我找不到嵌套循环的解决方案。 Thank you in advance. 先感谢您。

As leftaroundabout already indicated there's a problem with the initialization. 由于左下角已经表明初始化存在问题。 What I would recommend is that you rewrite the program as follows 我建议您重写程序如下

int i1 = blockDim.x * blockIdx.x + threadIdx.x;
int i2 = blockDim.y * blockIdx.y + threadIdx.y;
int i3;
int i4;

while(i1 < N && i2 < N){
  a1 = ..;
  a2 = ..;
  for(i3 = i4 = 0; i3 < N, i4 < N; i3++, i4++){
    // third customer
    a3 = Bit4CBitmapLookUp_device[i3] << 4;

    // fourth customer
    a4 = Bit4CBitmapLookUp_device[i4];

    // now actually set the sBitmapLookupTable value
    _cBitmapLookupTable[s] = a1 | a2 | a3 | a4;
    s ++;
  }
  s += blockDim.x*gridDim.x*blockDim.y*gridDim.y;
  i1 += blockDim.x*gridDim.x;
  i2 += blockDim.y*gridDim.y;
}

I haven't tested it, so I can't guarantee that the indices are correct. 我没有测试过,所以我不能保证索引是正确的。 I'll leave that to you. 我会留给你的。

A bit more explanation: In the code above only the loops over i1 and i2 are parallelized. 更多解释:在上面的代码中,只有i1和i2上的循环是并行化的。 This assumes that N**2 is large enough compared to the number of cores you have on your GPU. 这假设N ** 2与GPU上的核心数相比足够大。 If this is not the case. 如果不是这样的话。 All four loops need to be parallelized in order to obtain an efficient program. 所有四个循环都需要并行化以获得有效的程序。 The approach would then be a bit different. 这种方法会有所不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM