Visual Studio 2019 在 Windows 10 上构建 Cuda 静态库时出错

Error on building a Cuda static library from Visual Studio 2019 on Windows 10

我正在尝试使用 Visual Studio 2019 构建一个 cuda 静态库。我的最终目标是从 Unreal Engine C++ 脚本 link 这个 .lib 文件。

预期结果是 x64/Release 文件夹中的 .lib 文件,但我在构建时遇到以下错误:

下载Cuda 11.6后,我在VS2019中使用Cuda模板创建了一个项目。我正在按照此 blog 中的步骤操作。我把默认的kernel.cu拆分成了cuda_ue.cu & cuda_ue.h 如下:

cuda_ue.cu

#include "cuda_ue.h"

__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, std::string* error_message)
{
    int* dev_a = 0;
    int* dev_b = 0;
    int* dev_c = 0;
    cudaError_t cuda_status;

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

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

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

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

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

    cuda_status = cudaMemcpy(dev_b, b, size * sizeof(int), cudaMemcpyHostToDevice);
    if (cuda_status != cudaSuccess) {
        *error_message = "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
    cuda_status = cudaGetLastError();
    if (cuda_status != cudaSuccess) {
        *error_message = "addKernel launch failed: " + std::string(cudaGetErrorString(cuda_status));
        goto Error;
    }

    // cudaDeviceSynchronize waits for the kernel to finish, and returns
    // any errors encountered during the launch.
    cuda_status = cudaDeviceSynchronize();
    if (cuda_status != cudaSuccess) {
        *error_message = "cudaDeviceSynchronize returned error code " + std::to_string(cuda_status) + " after launching addKernel!";
        goto Error;
    }

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

Error:
    cudaFree(dev_c);
    cudaFree(dev_a);
    cudaFree(dev_b);

    return cuda_status;
}

cuda_ue.h

#include <string>
#include <stdio.h>
#include "cuda_runtime.h"
#include "device_launch_parameters.h"

cudaError_t addWithCuda(int* c, const int* a, const int* b, unsigned int size, std::string* error_message);

根据博客,我将调试更改为发布,并将配置类型更改为“静态库”。我无法从该错误消息中了解任何有关原因的信息。

如何构建配置为 Release 的 cuda 静态库?

我还包括了 VS2019 输出,详细级别为 'Normal':

Build started...
1>------ Build started: Project: cuda_ue_vs, Configuration: Release x64 ------
1>Build started 2/9/2022 5:24:40 AM.
1>Target InitializeBuildStatus:
1>  Touching "x64\Release\cuda_ue_vs.tlog\unsuccessfulbuild".
1>Target CudaBuild:
1>  Target CudaBuildCore:
1>    Compiling CUDA source file cuda_ue.cu...
1>
1>    C:\Users\htic\Documents\Unreal Projects\cuda_ue_vs>"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.6\bin\nvcc.exe" -gencode=arch=compute_52,code=\"sm_52,compute_52\" --use-local-env -ccbin "C:\Program Files (x86)\Microsoft Visual Studio19\Community\VC\Tools\MSVC.29.30133\bin\HostX86\x64" -x cu   -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.6\include" -I"C:\vcpkg\installed\x64-windows\include" -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.6\include"     --keep-dir x64\Release  -maxrregcount=0  --machine 64 --compile -cudart static    -DWIN32 -DWIN64 -DNDEBUG -D_CONSOLE -D_MBCS -Xcompiler "/EHsc /W3 /nologo /O2 /Fd"C:\Users\htic\Documents\Unreal Projects\cuda_ue_vs\x64\Release\cuda_ue_vs.pdb" /FS   /MD " -o x64\Release\cuda_ue.cu.obj "C:\Users\htic\Documents\Unreal Projects\cuda_ue_vs\cuda_ue.cu"
1>    nvcc fatal   : A single input file is required for a non-link phase when an outputfile is specified
1>    C:\Program Files (x86)\Microsoft Visual Studio19\Community\MSBuild\Microsoft\VC\v160\BuildCustomizations\CUDA 11.6.targets(790,9): error MSB3721: The command ""C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.6\bin\nvcc.exe" -gencode=arch=compute_52,code=\"sm_52,compute_52\" --use-local-env -ccbin "C:\Program Files (x86)\Microsoft Visual Studio19\Community\VC\Tools\MSVC.29.30133\bin\HostX86\x64" -x cu   -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.6\include" -I"C:\vcpkg\installed\x64-windows\include" -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.6\include"     --keep-dir x64\Release  -maxrregcount=0  --machine 64 --compile -cudart static    -DWIN32 -DWIN64 -DNDEBUG -D_CONSOLE -D_MBCS -Xcompiler "/EHsc /W3 /nologo /O2 /Fd"C:\Users\htic\Documents\Unreal Projects\cuda_ue_vs\x64\Release\cuda_ue_vs.pdb" /FS   /MD " -o x64\Release\cuda_ue.cu.obj "C:\Users\htic\Documents\Unreal Projects\cuda_ue_vs\cuda_ue.cu"" exited with code 1.
1>  Done building target "CudaBuildCore" in project "cuda_ue_vs.vcxproj" -- FAILED.
1>
1>  Done building project "cuda_ue_vs.vcxproj" -- FAILED.
1>
1>Build FAILED.
1>
1>C:\Program Files (x86)\Microsoft Visual Studio19\Community\MSBuild\Microsoft\VC\v160\BuildCustomizations\CUDA 11.6.targets(790,9): error MSB3721: The command ""C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.6\bin\nvcc.exe" -gencode=arch=compute_52,code=\"sm_52,compute_52\" --use-local-env -ccbin "C:\Program Files (x86)\Microsoft Visual Studio19\Community\VC\Tools\MSVC.29.30133\bin\HostX86\x64" -x cu   -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.6\include" -I"C:\vcpkg\installed\x64-windows\include" -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.6\include"     --keep-dir x64\Release  -maxrregcount=0  --machine 64 --compile -cudart static    -DWIN32 -DWIN64 -DNDEBUG -D_CONSOLE -D_MBCS -Xcompiler "/EHsc /W3 /nologo /O2 /Fd"C:\Users\htic\Documents\Unreal Projects\cuda_ue_vs\x64\Release\cuda_ue_vs.pdb" /FS   /MD " -o x64\Release\cuda_ue.cu.obj "C:\Users\htic\Documents\Unreal Projects\cuda_ue_vs\cuda_ue.cu"" exited with code 1.
1>    0 Warning(s)
1>    1 Error(s)
1>
1>Time Elapsed 00:00:00.65
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

自己解决了: 这是一个愚蠢的修复,文件夹名称不应该有 space。好像nvcc 就当成两个文件了。