CUDA内核挂起

时间:2014-11-03 16:48:14

标签: cuda nvcc

当我在myatoi函数中替换while循环中的条件时,内核会挂起。

#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'))
而不是     while((s [0]!='')) 然后内核挂起。我无法弄清楚,如果这是一个CUDA错误或我的错误。

我在一个独立的C程序上验证了相同的myatoi功能,它似乎在那里工作正常。我无法弄清楚问题。

1 个答案:

答案 0 :(得分:3)

而不是:

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

条件应该是:

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

我确定它也会挂起standalone C program。请重新检查您的测试C program

相关问题