CUDA:访问冲突写入位置0x0000000000000000

时间:2018-10-01 18:15:27

标签: c++ c cuda

我的代码:

int main() {

cudaError_t err = cudaSuccess;

FILE *fp2 = fopen("key.txt", "r");
size_t len = 256;
char *line = (char *)malloc(sizeof(char) * len);

int icount = 0;
char **m_keyword;
cudaMallocManaged(&m_keyword, len *550000 * sizeof(char *));
while (fgets(line, len, fp2) != NULL) {
    line[strlen(line) - 1] = '\0';

    err = cudaMallocManaged(&(m_keyword[icount]), sizeof(line) / sizeof(char *) * sizeof(char));

    if (err != cudaSuccess)
    {
        fprintf(stderr, "(error code %s)!\n", cudaGetErrorString(err));
    }
    strcpy(m_keyword[icount], line);    // Access violation writing location
    icount++;
}
free(line);

kern_2D << < 55000, 1 >> > (m_keyword, icount);
cudaDeviceSynchronize();

return 0;
}

我正在编写代码以读取文本文件,其内容如下:

motorcycle ckd
new apsonic
ckd 2017
ckd 2018
motorcycle apsonic
new motorcycle apsonic

如果我运行的文件有2000行,一切都很好。但是,如果我超过26000行,则会收到错误消息“访问冲突写入位置0x0000000000000000”。某些东西可以运行,有些东西出错了。 请帮助我。

1 个答案:

答案 0 :(得分:1)

在调用cudaMallocManaged()m_keyword[icount]分配内存时,您使用sizeof(line) / sizeof(char*) * sizeof(char)作为字节长度,这是错误的(4/4 * 1 = 1字节!)。您需要改用strlen(line)+1

实际上,即使您第一次拨打cudaMallocManaged()也是错误的。您根本不应该将char*指针的数量乘以len。您分配的字节数比实际需要多256倍。

话虽这么说,如果cudaMallocManaged()(或其他任何失败)失败,您根本不会停止程序。并且应该限制while循环,以确保icount不会超过您为其分配空间的char*指针的数量。

请尝试以下类似操作:

int main()
{    
    FILE *fp2 = fopen("key.txt", "r");
    if (!fp2)
    {
        fprintf(stderr, "Error opening file!\n");
        return 1;
    }

    const size_t max_lines = 55000; 
    const size_t max_line_len = 256;

    char line[max_line_len];
    size_t line_len;

    char **m_keyword = NULL;
    int icount = 0;

    cudaError_t err = cudaMallocManaged((void**)&m_keyword, max_lines * sizeof(char*));
    if (err != cudaSuccess)
    {
        fprintf(stderr, "Error allocating memory for m_keyword! %s\n", cudaGetErrorString(err));
        fclose(fp2);
        return 1;
    }

    do
    {
        if (!fgets(line, max_line_len, fp2))
        {
            if (ferror(fp2) && !feof(fp2))
            {
                fprintf(stderr, "Error reading from file!\n");
                fclose(fp2);
                return 1;
            }
            break;
        }

        line_len = strlen(line);
        if ((line_len > 0) && (line[line_len - 1] == '\n'))
        {
            line[line_len - 1] = '\0';
            --line_len;
        }

        err = cudaMallocManaged((void**)&(m_keyword[icount]), line_len + 1);
        if (err != cudaSuccess)
        {
            fprintf(stderr, "Error allocating memory for m_keyword[%d]! %s\n", icount, cudaGetErrorString(err));
            fclose(fp2);
            return 1;
        }

        strcpy(m_keyword[icount], line);
        ++icount;
    }
    while (icount < max_lines);

    fclose(fp2);

    kern_2D << < max_lines, 1 >> > (m_keyword, icount);
    cudaDeviceSynchronize();

    return 0;
}