我有一个CUDA内核,我正在编译一个没有任何特殊标志的cubin文件:
nvcc text.cu -cubin
Run Code Online (Sandbox Code Playgroud)
它编译,但有这条消息:
顾问:假设全局内存空间,无法分辨指针指向的内容
和一些临时cpp文件中的行的引用.我可以通过评论一些对我来说毫无意义的看似随意的代码来解决这个问题.
内核如下:
__global__ void string_search(char** texts, int* lengths, char* symbol, int* matches, int symbolLength)
{
int localMatches = 0;
int blockId = blockIdx.x + blockIdx.y * gridDim.x;
int threadId = threadIdx.x + threadIdx.y * blockDim.x;
int blockThreads = blockDim.x * blockDim.y;
__shared__ int localMatchCounts[32];
bool breaking = false;
for(int i = 0; i < (lengths[blockId] - (symbolLength - 1)); i += blockThreads)
{
if(texts[blockId][i] == symbol[0])
{
for(int j = 1; j < symbolLength; j++)
{
if(texts[blockId][i + j] != symbol[j])
{
breaking = true;
break;
}
}
if (breaking) continue;
localMatches++;
}
}
localMatchCounts[threadId] = localMatches;
__syncthreads();
if(threadId == 0)
{
int sum = 0;
for(int i = 0; i < 32; i++)
{
sum += localMatchCounts[i];
}
matches[blockId] = sum;
}
}
Run Code Online (Sandbox Code Playgroud)
如果我更换线
localMatchCounts[threadId] = localMatches;
Run Code Online (Sandbox Code Playgroud)
在第一个for循环后用这一行
localMatchCounts[threadId] = 5;
Run Code Online (Sandbox Code Playgroud)
它编译没有通知.这也可以通过注释掉线上方的循环的看似随机的部分来实现.我也尝试用普通数组替换本地内存数组无效.谁能告诉我这是什么问题?
该系统是Vista 64bit,它的价值.
编辑:我修复了代码,所以它实际上工作,虽然它仍然产生编译器通知.似乎警告不是问题,至少在正确性方面(可能会影响性能).
像 char** 这样的指针数组在内核中是有问题的,因为内核无法访问主机的内存。
最好分配一个连续的缓冲区,并以支持并行访问的方式对其进行划分。
在这种情况下,我将定义一个一维数组,其中包含依次定位的所有字符串和另一个一维数组,大小为 2*numberOfStrings,其中包含第一个数组中每个字符串的偏移量及其长度:
内核准备:
char* 缓冲区 = st[0] + st[1] + st[2] + ....; int* 元数据 = new int[numberOfStrings * 2]; int 最后位置 = 0; for (int cnt = 0; cnt < 2* numberOfStrings; cnt+=2) { 元数据[cnt] = 最后位置; 最后位置 += 长度(st[cnt]); 元数据[cnt] = 长度(st[cnt]); }在内核中:
currentIndex = threadId + blockId * numberOfBlocks; char* currentString = buffer + 元数据[2 * currentIndex]; int currentStringLength = 元数据[2 * currentIndex + 1];