简体   繁体   English

CUDA 减少查找最大值

[英]CUDA reduce find max

I am trying to write my own implementation of reductive search for maximum number.我正在尝试编写自己的最大数量的还原搜索实现。 This is my first CUDA C program and I ran into some errors that I cannot understand yet.这是我的第一个 CUDA C 程序,我遇到了一些我还无法理解的错误。

my code:我的代码:

__global__
void reduce (box * d_in_data, int insize, box * d_out_data)
{
    /*
    for example:

    array size = 25200 (size variable, keep in d_in_data)
    gridDim = 252
    blockDim = 10
     */

    uint32_t tid = threadIdx.x;

    //__shared__ box sdata[insize / gridDim.x]; // 25200 / 252 = 100

    extern __shared__ box sdata[]; // 25200 / 252 = 100

    /*spt its 'size per thread' for filling sdata*/

    /* (25200 / 252) / 10 */ /* each thread should be copy 10 elements from d_in_data to sdata  */

    uint32_t spt = (insize / gridDim.x) / blockDim.x;

    box (* sh_ptr)[blockDim.x][spt] = (box (*) [blockDim.x][spt]) &sdata;

    for (int i = 0;i < spt;i++){

        (*sh_ptr)[tid][i] = d_in_data[blockIdx.x * (insize / gridDim.x) + (tid * blockDim.x + i)];

    }
    __syncthreads();
    box temp = (*sh_ptr)[tid][0];
    for (int i = 0;i < spt;i++){
        box i_box = (*sh_ptr)[tid][i];

        temp = temp.s < i_box.s ? i_box : temp;
    }

    (*sh_ptr)[tid][0] = temp;
    __syncthreads();
    box (* d_out_ptr)[gridDim.x][blockDim.x] =  (box (*)[gridDim.x][blockDim.x]) d_out_data;
    (*d_out_ptr)[blockIdx.x][tid]  = (*sh_ptr)[tid][0];
    
}
  1. __shared__ box sdata[insize / gridDim.x]; - why "here cannot be used as a constant" ? - 为什么“这里不能用作常量” how to set the size explicitly?如何明确设置大小?

  2. box (* sh_ptr)[blockDim.x][spt] = (box (*) [blockDim.x][spt]) &sdata; - "error: expression must have a constant value" , "note: attempt to access run-time storage" , "note: the value of variable "spt"" . - “错误:表达式必须有一个常量值”“注意:尝试访问运行时存储”“注意:变量“spt”的值” How to write correctly in my case?在我的情况下如何正确书写?

  3. uint32_t spt = (insize / gridDim.x) / blockDim.x; - "here cannot be used as a constant" - “这里不能用作常量”

  4. box (* d_out_ptr)[gridDim.x][blockDim.x] = (box (*)[gridDim.x][blockDim.x]) d_out_data; - "error: expression must have a constant value" , "note: attempt to access run-time storage" , It's the same here. - "error: expression must have a constant value" , "note: try to access run-time storage" ,这里也是一样。

Please tell me where I made a mistake in each paragraph.请告诉我我在每一段中哪里出错了。

Your errors have nothing to do with CUDA.您的错误与 CUDA 无关。

  • __shared__ box sdata[insize / gridDim.x]; - why "here cannot be used as a constant" ? - 为什么“这里不能用作常量”? how to set the size explicitly?如何明确设置大小?
    In C/C++ the array size must be known at compile time.在 C/C++ 中,必须在编译时知道数组大小。 Here, insize / gridDim.x would have to be known at compile time, but insize is certainly not.在这里,必须在编译时知道insize / gridDim.x ,但insize肯定不是。

     extern __shared__ box sdata[];

    looks completely OK to me.对我来说看起来完全没问题。 If you need the size of this array (you certainly do), you can compute it at runtime form function arguments etc.如果你需要这个数组的大小(你当然需要),你可以在运行时表单函数参数等中计算它。

  • box (* sh_ptr)[blockDim.x][spt] = (box (*) [blockDim.x][spt]) &sdata; - "error: expression must have a constant value" , "note: attempt to access run-time storage", "note: the value of variable "spt"". - “错误:表达式必须有一个常量值”,“注意:尝试访问运行时存储”,“注意:变量“spt”的值”。 How to write correctly in my case?在我的情况下如何正确书写?
    Here you try to define a pointer to a 2D array.在这里,您尝试定义一个指向二维数组的指针。 This is possible only if the last dimension of the array is known at compile time, but spt is not known.只有在编译时知道数组的最后一维,但不知道spt时,这才是可能的。 To circumvent it, you need to calculate the address into the shared memory ( sdata ) yourself.要绕过它,您需要自己计算共享内存( sdata )中的地址。 This is a 1D array and treat it this way.这是一个一维数组并以这种方式处理它。

  • Error number 3 seems to be a diagnostic message, not an error.错误编号 3 似乎是诊断消息,而不是错误。

  • Error number 4 is of the same type as Error nr 2.错误编号 4 与错误编号 2 属于同一类型。

For further reading see, for example, Pointer to Multidimensional Array in C?如需进一步阅读,请参见,例如, 指向 C 中的多维数组的指针?

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

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