2014-11-03 7 views
0

Когда я заменяю условие во время цикла в функции myatoi, ядро ​​зависает.Ядро 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 или моя ошибка.

Я проверил ту же самую функцию myatoi на автономной программе C и, похоже, там работает нормально. Я не могу понять проблему.

+2

С первого взгляда это должно быть '&&' вместо' || '... – Marco13

ответ

3

Вместо этого:

while((s[0]!=' ')||(s[0]!='\0')) 

Состояние должно быть:

while((s[0]!=' ')&&(s[0]!='\0')) 

Я уверен, что он также будет висеть, как standalone C program. Повторите проверку теста C program

Смежные вопросы