0
當我在myatoi函數中替換while循環中的條件時,內核掛起。CUDA內核掛起
#include<stdio.h>
#include<string.h>
#define INFILE "sentences.txt"
#define MAX_BUF_SIZE 6000
#define MAX_LINE_LEN 128
#define MAX_SHINGLES_PER_LINE 30
#define MAX_ALLOWABLE_MEMORY 256*1024*1024
struct sentence
{
int id;
char line[MAX_LINE_LEN];
int shingles;
int hash[MAX_SHINGLES_PER_LINE];
};
__device__ int myatoi(char *s)
{
int ret=0;
printf("s=%s\n",s);
while((s[0]!=' '))
{
ret=10*ret+(*s)-'0';
s++;
}
return ret;
}
__global__
void sentence_hasher(struct sentence *s)
{
s[threadIdx.x].id=myatoi(s[threadIdx.x].line);
}
int main()
{
int len=0,cx=0;
char buffer[MAX_BUF_SIZE]={0};
struct sentence *sentences = NULL;
struct sentence *sd=NULL;
int num_sentences = 0,id,i;
int ssize;
int blocksize=2;
printf("%d\n",sizeof(sentence));
//calculate the number of documents which can be fit in 1 batch
num_sentences = MAX_ALLOWABLE_MEMORY/sizeof(struct sentence);
num_sentences=2;
ssize = num_sentences * sizeof(struct sentence);
printf("Allocating memory on the host %d\n",ssize);
sentences = (struct sentence *)malloc(ssize);
if(!sentences)
{
printf("Memory allocation failure\n");
exit(1);
}
//Initialise the sentences with 2 small sentences
strcpy(sentences[0].line,"0 how to create property");
strcpy(sentences[1].line,"11 a capable ship which meets the requirements");
cx=2;
//Allocate memory on cuda
printf("Allocating memory on device\n");
cudaMalloc((void **)&sd,ssize);
printf("Allowable sentences per batch=%d\n",num_sentences);
//print the buffer
for(i=0;i<cx;i++)
{
printf("i=%d line=%s\n",i,sentences[i].line);
}
printf("Copying data to device\n");
cudaMemcpy(sd, sentences, ssize, cudaMemcpyHostToDevice);
dim3 dimBlock(blocksize, 1);
dim3 dimGrid(1, 1);
printf("Running kernel\n");
sentence_hasher<<<dimGrid, dimBlock>>>(sd);
cudaMemcpy(sentences, sd, ssize, cudaMemcpyDeviceToHost);
printf("Kernel complete\n");
//print the buffer
for(i=0;i<cx;i++)
{
printf("i=%d id=%d line=%s\n",i,sentences[i].id,sentences[i].line);
}
free(sentences);
cudaFree(sd);
}
我實現我的版本的atoi功能遇到空格或\ 0字符串結束應儘快停止。在myatoi功能,如果我包括此線
while((s[0]!=' ')||(s[0]!='\0'))
代替 而((S [0]!=」「)) 那麼內核掛起。我無法弄清楚,如果這是一個CUDA錯誤或我的錯誤。
我在一個獨立的C程序上驗證了同樣的myatoi函數,它似乎在那裏工作得很好。我無法弄清楚這個問題。
乍一看,這應該是'&&'而不是'||'... – Marco13 2014-11-03 16:54:37