Generate Index using CUDA-C

只谈情不闲聊 提交于 2019-12-13 04:31:50

问题


I am trying to generate set of indices below:

I have a cuda block that consists of 20 blocks(blockIdx:from 0 -19) with each block subdivided into 4 blocks (sub block Idx: 0,1,2 and 3).

I am trying to generate index pattern like this :

threadIdx (tid),SubBlockIdxA(SA),SubBlockIdxB(SB), BlockIdxA(BA),BlockIdxB(BB)

            Required                           Obtained         
   tid  SBA SBB BA  BB    SBA  SBB BA  BB
    0   0   1   0   0       0   1   0   0
    1   1   0   0   1       1   0   0   1
    2   0   1   1   1       0   1   1   1
    3   1   0   1   2       1   0   1   2
    4   0   1   2   2       0   1   2   2
    5   1   0   2   3       1   0   2   3
    6   0   1   3   3       0   1   3   3
    7   1   0   3   4       1   0   3   4
    8   2   3   0   0       2   3   0   0
    9   3   2   0   1       3   2   0   1
    10  2   3   1   1       2   3   1   1
    11  3   2   1   2       3   2   1   2
    12  2   3   2   2       2   3   2   2
    13  3   2   2   3       3   2   2   3
    14  2   3   3   3       2   3   3   3
    15  3   2   3   4       3   2   3   4
    16  0   1   5   5       0   1   5   5
    17  1   0   5   6       1   0   5   6
    18  0   1   6   6       0   1   6   6
    19  1   0   6   7       1   0   6   7
    20  0   1   7   7       0   1   7   7
    21  1   0   7   8       1   0   7   8
    22  0   1   8   8       0   1   8   8
    23  1   0   8   9       1   0   8   9
    24  0   1   10  10      2   3   5   5
    25  1   0   10  11      3   2   5   6
    26  0   1   11  11      2   3   6   6
    27  1   0   11  12      3   2   6   7
    28  0   1   12  12      2   3   7   7
    29  1   0   12  13      3   2   7   8
    30  0   1   13  13      2   3   8   8
    31  1   0   13  14      3   2   8   9
    32  2   3   10  10      0   1   10  10
    33  3   2   10  11      1   0   10  11
    34  2   3   11  11      0   1   11  11
    35  3   2   11  12      1   0   11  12
    36  2   3   12  12      0   1   12  12
    37  3   2   12  13      1   0   12  13
    38  2   3   13  13      0   1   13  13
    39  3   2   13  14      1   0   13  14
    40  0   1   15  15      2   3   10  10
    41  1   0   15  16      3   2   10  11
    42  0   1   16  16      2   3   11  11
    43  1   0   16  17      3   2   11  12
    44  0   1   17  17      2   3   12  12
    45  1   0   17  18      3   2   12  13
    46  0   1   18  18      2   3   13  13
    47  1   0   18  19      3   2   13  14

Please see my code below:

static __device__ void function()
    {
        uint16 uBlockIdxA, uBlockIdxB, uSubBlockIdxA, usubBlockIdxB;
        if threadIdx.x < 48)
        {
            uint16 uY = threadIdx.x / 8;
            uint16 uX = threadIdx.x - (uY * 8);    
            uSubBlockIdxA = ((uY & 0x01) << 1)  + (uX & 0x01); 
            uSubBlockIdxB = ((uY & 0x01) << 1)  + ((uX + 1) & 0x01);  
            uBlockIdxB = (uY >> 1) * 5  + ((1 + uX) >> 1);
            uBlockIdxA = (uY >> 1) * 5  + ((0 + uX) >> 1);    
            func (uBlockIdxA, uBlockIdxB, uSubBlockIdxA, uSubBlockIdxB);
        }
    }

I am trying to think the logic to achieve what i am looking for. I am not right but not sure what i am missing.

logic as to how to generate this will be helpful. Code is appreciated. Please help.

Thanks in advance.


回答1:


code to generate the indices is below:

Split the threads into half since the indices from first half can be used for the second half with an offset of 10 and minor tweaks.

static __device__ void function()
{
    uint16 uBlockIdxA, uBlockIdxB, uSubBlockIdxA, usubBlockIdxB;

    if(threadIdx.x < 48)
    {
        uint16 uY = threadIdx.x / 8;
        uint16 uX = threadIdx.x - (uY * 8);

        if (threadIdx.x < 24)
        {

            uSubBlockIdxA = ((uY & 0x01) << 1)  + (uX & 0x01); 
            uSubBlockIdxB = ((uY & 0x01) << 1)  + ((uX + 1) & 0x01);  
            uBlockIdxB = (uY >> 1) * 5  + ((1 + uX) >> 1);
            uBlockIdxA = (uY >> 1) * 5  + ((0 + uX) >> 1);    

        }
        else if
        {
            uSubBlockIdxA = (((uY - 3) & 0x01) << 1)  + (uX & 0x01); 
            uSubBlockIdxB = (((uY - 3) & 0x01) << 1)  + ((uX + 1) & 0x01);  
            uBlockIdxB = ((uY - 3) >> 1) * 5  + ((1 + uX) >> 1) + 10;
            uBlockIdxA = ((uY - 3) >> 1) * 5  + ((0 + uX) >> 1) + 10;
        }
    }
    func (uBlockIdxA, uBlockIdxB, uSubBlockIdxA, uSubBlockIdxB);        
}


来源:https://stackoverflow.com/questions/17334625/generate-index-using-cuda-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!