2014-11-03 5 views
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 기능을 확인했으며 제대로 작동하는 것 같습니다. 문제를 파악할 수 없습니다. 대신이의

+2

언뜻보기에 이것은'||'대신에'&&'이어야합니다 ... – Marco13

답변

3

:

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

이 조건이 있어야한다 :

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

나는 그것이 또한 standalone C program으로 중단됩니다 확신합니다. 테스트를 다시 확인하십시오 C program