我试图通过使用常量参数来分配共享内存,但是得到了一个错误。我的内核看起来如下:
__global__ void Kernel(const int count)
{
    __shared__ int a[count];
}我说错了
错误:表达式必须有一个常量值
伯爵是康斯特!我为什么要犯这个错误?我怎么才能绕过这一切呢?
发布于 2011-04-03 17:36:06
const的意思不是“常量”,而是“只读”。
常量表达式是编译器在编译时知道其值的东西.
发布于 2011-04-03 18:40:42
CUDA支持动态共享内存分配。如果您像这样定义内核:
__global__ void Kernel(const int count)
{
    extern __shared__ int a[];
},然后传递内核启动的第三个参数所需的字节数。
Kernel<<< gridDim, blockDim, a_size >>>(count)然后可以在运行时对其进行调整。请注意,运行时只支持每个块的单个动态声明的分配。如果需要更多信息,则需要在单个分配中使用指向偏移量的指针。此外,当使用指针时,共享内存使用32位字,所有分配必须是32位字对齐,无论共享内存分配的类型。
发布于 2011-04-04 16:57:39
选项一:用常量声明共享内存(与const不同)
__global__ void Kernel(int count_a, int count_b)
{
    __shared__ int a[100];
    __shared__ int b[4];
}选项二:在内核启动配置中动态声明共享内存:
__global__ void Kernel(int count_a, int count_b)
{
    extern __shared__ int *shared;
    int *a = &shared[0]; //a is manually set at the beginning of shared
    int *b = &shared[count_a]; //b is manually set at the end of a
}
sharedMemory = count_a*size(int) + size_b*size(int);
Kernel <<<numBlocks, threadsPerBlock, sharedMemory>>> (count_a, count_b);注意:指向动态共享内存的指针是,所有都具有相同的地址。我使用两个共享内存数组来说明如何在共享内存中手动设置两个数组。
https://stackoverflow.com/questions/5531247
复制相似问题