CUDA <<< X,X >>>给出预期的表达式错误

问题描述

我正在尝试编译并运行以下名为test.cu的程序:

#include <iostream>
#include <math.h>
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
// Kernel function to add the elements of two arrays
__global__
void add(int n,float* x,float* y)
{
    int index = threadIdx.x;
    int stride = blockDim.x;
    for (int i = index; i < n; i += stride)
        y[i] = x[i] + y[i];
}

int main(void)
{
    int N = 1 << 20;
    float* x,* y;

    // Allocate Unified Memory – accessible from CPU or GPU
    cudaMallocManaged(&x,N * sizeof(float));
    cudaMallocManaged(&y,N * sizeof(float));

    // initialize x and y arrays on the host
    for (int i = 0; i < N; i++) {
        x[i] = 2.0f;
        y[i] = 1.0f;
    }

    // Run kernel on 1M elements on the GPU
    add <<<1,256>>> (N,x,y);

    // Wait for GPU to finish before accessing on host
    cudaDeviceSynchronize();

    // Check for errors (all values should be 3.0f)
    for (int i = 0; i < 10; i++)
        std::cout << y[i] << std::endl;

    // Free memory
    cudaFree(x);
    cudaFree(y);

    return 0;
}

我正在使用Visual Studio社区2019,它标记为“添加>>(N,x,y);”预期表达错误。我尝试编译它,并且以某种方式正确编译,但是运行.exe文件时,它输出一堆“ 1”而不是预期的“ 3”。

我也尝试使用“ nvcc test.cu”进行编译,但是最初它说“ nvcc fatal : Cannot find compiler 'cl.exe' in PATH”,所以我添加了“ C:\ Program Files(x86)\ Microsoft Visual Studio \ 2019 \ Community \ VC \ Tools \ MSVC \ 14.27.29110 \ bin \ Hostx64 \ x64“移至路径,现在使用nvcc进行编译与使用Visual Studio进行编译时会出现相同的错误。

在两种情况下,程序都不会输入“添加”功能。

我非常确定代码正确,并且问题与安装有关,但是我已经尝试过重新安装cuda工具包并修复MCVS,但这没有用。

在Visual Studio中使用cuda启动新项目时出现的kernel.cu示例也不起作用。运行时,输出“没有可用的内核映像在设备上执行”。

如何解决这个问题?

nvcc版本,如果有帮助的话:

nvcc -V
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2020 NVIDIA Corporation
Built on Wed_Jul_22_19:09:35_Pacific_Daylight_Time_2020
Cuda compilation tools,release 11.0,V11.0.221
Build cuda_11.0_bu.relgpu_drvr445TC445_37.28845127_0

解决方法

Visual Studio提供了IntelliSense C ++。在C ++语言中,尖括号的正确解析很麻烦。对于模板,您要<<<>>>。这使Intellisense难以正常工作。在CUDA中获取完整IntelliSense的方法是从运行时API切换到驱动程序API。 C ++只是C ++,而CUDA仍然是C ++(某种),对于语言解析而言,不必费劲>>。

您可以看一下matrixMul和matrixMulDrv之间的区别。 <<<>>>语法实际上是由编译器处理的,只是吐出了调用Driver API调用的代码。您将链接到cuda.lib而不是cudart.lib,并且如果仅使用CUDA-RT库,则可能必须处理“混合模式”程序。您可以参考此link以获得更多信息。

此外,此link还介绍了如何在VS中为CUDA添加Intellisense。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...