行行都行 2024-04-06 21:26 采纳率: 0%
浏览 181

VS2022中初始CUDA示例项目中的核函数调用时报错E0029

在Visual Studio 2022中新建了一个CUDA项目,系统自动生成了一段示例代码,功能是将向量a和向量b相加得到向量c,但是运行的时候出现了3个错误,分别是E0029、C1083和MSB3721,实在是没看懂为什么这样一段示例代码会报错,希望大家指点!
完整的示例代码如下:


#include "cuda_runtime.h"
#include "device_launch_parameters.h"

#include <stdio.h>

cudaError_t addWithCuda(int *c, const int *a, const int *b, unsigned int size);

__global__ void addKernel(int *c, const int *a, const int *b)
{
    int i = threadIdx.x;
    c[i] = a[i] + b[i];
}

int main()
{
    const int arraySize = 5;
    const int a[arraySize] = { 1, 2, 3, 4, 5 };
    const int b[arraySize] = { 10, 20, 30, 40, 50 };
    int c[arraySize] = { 0 };

    // Add vectors in parallel.
    cudaError_t cudaStatus = addWithCuda(c, a, b, arraySize);
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "addWithCuda failed!");
        return 1;
    }

    printf("{1,2,3,4,5} + {10,20,30,40,50} = {%d,%d,%d,%d,%d}\n",
        c[0], c[1], c[2], c[3], c[4]);

    // cudaDeviceReset must be called before exiting in order for profiling and
    // tracing tools such as Nsight and Visual Profiler to show complete traces.
    cudaStatus = cudaDeviceReset();
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaDeviceReset failed!");
        return 1;
    }

    return 0;
}

// Helper function for using CUDA to add vectors in parallel.
cudaError_t addWithCuda(int *c, const int *a, const int *b, unsigned int size)
{
    int *dev_a = 0;
    int *dev_b = 0;
    int *dev_c = 0;
    cudaError_t cudaStatus;

    // Choose which GPU to run on, change this on a multi-GPU system.
    cudaStatus = cudaSetDevice(0);
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaSetDevice failed!  Do you have a CUDA-capable GPU installed?");
        goto Error;
    }

    // Allocate GPU buffers for three vectors (two input, one output)    .
    cudaStatus = cudaMalloc((void**)&dev_c, size * sizeof(int));
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaMalloc failed!");
        goto Error;
    }

    cudaStatus = cudaMalloc((void**)&dev_a, size * sizeof(int));
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaMalloc failed!");
        goto Error;
    }

    cudaStatus = cudaMalloc((void**)&dev_b, size * sizeof(int));
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaMalloc failed!");
        goto Error;
    }

    // Copy input vectors from host memory to GPU buffers.
    cudaStatus = cudaMemcpy(dev_a, a, size * sizeof(int), cudaMemcpyHostToDevice);
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaMemcpy failed!");
        goto Error;
    }

    cudaStatus = cudaMemcpy(dev_b, b, size * sizeof(int), cudaMemcpyHostToDevice);
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaMemcpy failed!");
        goto Error;
    }

    // Launch a kernel on the GPU with one thread for each element.
    addKernel<<<1, size>>>(dev_c, dev_a, dev_b);

    // Check for any errors launching the kernel
    cudaStatus = cudaGetLastError();
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "addKernel launch failed: %s\n", cudaGetErrorString(cudaStatus));
        goto Error;
    }
    
    // cudaDeviceSynchronize waits for the kernel to finish, and returns
    // any errors encountered during the launch.
    cudaStatus = cudaDeviceSynchronize();
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaDeviceSynchronize returned error code %d after launching addKernel!\n", cudaStatus);
        goto Error;
    }

    // Copy output vector from GPU buffer to host memory.
    cudaStatus = cudaMemcpy(c, dev_c, size * sizeof(int), cudaMemcpyDeviceToHost);
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaMemcpy failed!");
        goto Error;
    }

Error:
    cudaFree(dev_c);
    cudaFree(dev_a);
    cudaFree(dev_b);
    
    return cudaStatus;
}

报错显示的是第91行应输入表达式:

img


想问问大家这个E0029报错应该如何解决啊,然后下面两个c1083和MSB3721也不是特别清楚该怎么改,望指点!

  • 写回答

4条回答 默认 最新

  • 关注

    晚上好🌙🌙🌙
    本答案参考ChatGPT-3.5

    针对在VS2022中初始CUDA示例项目中的核函数调用时报错E0029,C1083和MSB3721,有如下解决方案:

    E0029错误:

    • E0029错误常见于内核函数的参数传递错误,表明在执行核函数时出现了类型不匹配的问题。
    • 根据代码可知,在addKernel函数中的线程索引i表示的是当前线程的唯一标识符,类型应该是unsigned int,而不是int类型。因此,在函数定义时,将类型unsigned int替换int类型即可。
    • 修改后的代码如下:
    __global__ void addKernel(int *c, const int *a, const int *b)
    {
        unsigned int i = threadIdx.x;
        c[i] = a[i] + b[i];
    }
    

    C1083错误:

    • C1083错误通常表示找不到指定的头文件,即cuda_runtime.h或device_launch_parameters.h文件。这种情况可能是没有安装CUDA工具包或CUDA工具包版本太低导致的问题。
    • 如果确实没有安装CUDA工具包,需要先安装CUDA工具包。
    • 如果已经安装了CUDA工具包,则需要确保VS2022中的CUDA工具包版本与安装的CUDA工具包版本匹配,或者安装更高版本的CUDA工具包。
    • 再次检查是否正确包含了cuda_runtime.h和device_launch_parameters.h头文件。

    MSB3721错误:

    • MSB3721错误通常表示编译器或工具集出现了问题,并且无法生成输出文件。
    • 这种情况可能是由于编译器本身损坏或其他未知错误导致的。
    • 可以尝试在命令行中使用nvcc编译器,而不是使用Visual Studio编译器。或重新安装CUDA工具包和Visual Studio,并确保版本兼容性。
    • 如果以上解决方案仍无法解决问题,可以尝试通过卸载并重新安装Visual Studio的方式来解决问题。
    评论

报告相同问题?

问题事件

  • 创建了问题 4月6日

悬赏问题

  • ¥30 孪生网络模型,当训练集与测试集共用一个数据集时,训练准确率为100%,而测试准确率仍在50%左右浮动(正常来说测试的都是学习过的数据,准确率应为100%)。【不要GPT及任何大模型回答】
  • ¥15 怎么在c语言上输出对应数据类型的内存大小以及数值范围
  • ¥30 河流的geojson数据为什么放到mapshaper网站中全部是长方形
  • ¥15 谁能介绍一个可以搜索大部分单词的,每个单词有词根词缀记忆方法的电子书和配套软件吗?给你报酬,你可以给电子书和配套软件给我吗?可以的话加我微信:15218392686
  • ¥20 ANSYS fluent烟雾扩散仿真
  • ¥15 新建vitis工程时,显示创建失败,需要查看vitis log
  • ¥15 java 在同一包下无法跨文件引入自己写的类,也无法导包过去
  • ¥15 求帮生成一个lattice diamond的许可证
  • ¥15 大一前端新生求教学解答
  • ¥15 如何制作一个可以查看“网游有序列的装备词条”的软件/插件