【发布时间】:2020-12-08 21:11:03
【问题描述】:
我正在尝试编译并运行以下名为 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 comunity 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
【问题讨论】:
-
您的代码没有问题。如果您遇到问题,我建议您使用proper CUDA error checking。关于“预期的表达式错误”,它来自 VS intellisense,并不是代码中的实际错误。 Intellisense 不理解 CUDA 语法。如果您想了解有关此常见主题的更多信息,可以在 Google 上搜索“CUDA 红色下划线”以获取大量信息。
-
godbolt.org/z/r6xKcG -- 代码编译。您似乎将智能感知错误与运行时问题(可能是 CUDA 安装损坏)混为一谈。
-
不幸的是,VS 没有为它不理解的语言关闭 IntelliSense,而是一直在尖叫“我不明白!”尽可能大声。也许这种行为是为了让它看起来更人性化。
-
正如其他人所说,这不是错误。 VS 有一个预先检测错误的系统,因此它们在编译之前就被捕获,称为 IntelliSense。简单地说,IntelliSense 支持 C++ 但不支持 CUDA,因此所有 CUDA 代码看起来都很奇怪。所以 VS 将某些东西标记为错误并不意味着它不会编译。
-
这是 100% 的错误。程序没有输出应有的内容,并且程序从不进入“添加”功能
标签: c++ visual-studio cuda visual-studio-2019 nvidia