c++ - Cuda:访问冲突写入位置 0x0000000000000000

标签 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”。有些东西可以运行,有些东西出错了。 请帮助我。

最佳答案

当您调用 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;
}

关于c++ - Cuda:访问冲突写入位置 0x0000000000000000,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52596662/

相关文章:

检查具体的scanf格式

c++ - 函数返回后,指向字符串文字的指针是否仍然有效?

c++ - Crypto++ 5.6.3rc5 GenerateBlock 未实现

c - MMX SSE 到 C 代码转换时图像质量下降

java - 使用 JCuda 计算肤色未给出正确的百分比

ffmpeg 编译因 cuda 失败,找不到 libnpp

c++ - 如何使用 GPU 绘制 OpenGL 像素

c++ - C++ 中的 3D 运动场

c++ - 为什么 vector::push_back 和 emplace_back 调用 value_type::constructor 两次?

C函数参数可以设置变量吗?