CUDA编译错误:类模板已被定义 [英] Cuda compilation error: class template has already been defined

查看:107
本文介绍了CUDA编译错误:类模板已被定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是cuda的新手,并尝试运行一个简单的vector add示例,我在网上找到了该示例以开始使用.我正在使用Win10 64位和Visual Studio 2017.

I am new to cuda and tried to run a simple vector add sample that I found online to get started. I am using win10 64bit and visual studio 2017.

#include "cuda_runtime.h"
#include "cuda.h"
#include "device_launch_parameters.h"
#include <iostream>


#include <math.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] = 1.0f;
        y[i] = 2.0f;
    }

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

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

    // Check for errors (all values should be 3.0f)
    float maxError = 0.0f;
    for (int i = 0; i < N; i++)
        maxError = fmax(maxError, fabs(y[i] - 3.0f));
    std::cout << "Max error: " << maxError << std::endl;

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

    return 0;
}

我使用了"VS2017开发人员命令提示符",因为窗口的命令提示符给了我

I used the "Developer Command Prompt for VS2017" since the window's command prompt is giving me

nvcc fatal : Cannot find compiler 'cl.exe' in PATH

,在线解决方案对我不起作用.然后我运行了这个命令(--compiler -options已经解决了一些错误)

and the online solutions didn't work for me. Then I ran this command(the --compiler -options solved some of the errors already)

nvcc add.cu --compiler-options "-D _WIN64"

但是编译器仍然给我错误

but the compiler is still giving me errors

C:\Programming\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\type_traits(95): error: class template "std::_Is_function" has already been defined

C:\Programming\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\type_traits(95): error: class template "std::_Is_function" has already been defined

C:\Programming\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\type_traits(95): error: class template "std::_Is_function" has already been defined

C:\Programming\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\type_traits(95): error: class template "std::_Is_function" has already been defined

C:\Programming\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\type_traits(95): error: class template "std::_Is_function" has already been defined

C:\Programming\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\type_traits(95): error: class template "std::_Is_function" has already been defined

C:\Programming\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\type_traits(95): error: class template "std::_Is_function" has already been defined

C:\Programming\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\type_traits(95): error: class template "std::_Is_function" has already been defined

C:\Programming\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\type_traits(95): error: class template "std::_Is_function" has already been defined

C:\Programming\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\type_traits(95): error: class template "std::_Is_function" has already been defined

C:\Programming\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\type_traits(95): error: class template "std::_Is_function" has already been defined

C:\Programming\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\type_traits(95): error: class template "std::_Is_function" has already been defined

C:\Programming\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\type_traits(95): error: class template "std::_Is_function" has already been defined

C:\Programming\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\type_traits(95): error: class template "std::_Is_function" has already been defined

C:\Programming\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\type_traits(95): error: class template "std::_Is_function" has already been defined

C:\Programming\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\type_traits(95): error: class template "std::_Is_function" has already been defined

C:\Programming\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\type_traits(95): error: class template "std::_Is_function" has already been defined

C:\Programming\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\type_traits(95): error: class template "std::_Is_function" has already been defined

C:\Programming\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\type_traits(95): error: class template "std::_Is_function" has already been defined

C:\Programming\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\type_traits(95): error: class template "std::_Is_function" has already been defined

C:\Programming\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\type_traits(95): error: class template "std::_Is_function" has already been defined

C:\Programming\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\type_traits(95): error: class template "std::_Is_function" has already been defined

C:\Programming\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\type_traits(95): error: class template "std::_Is_function" has already been defined

C:\Programming\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\type_traits(95): error: class template "std::_Is_function" has already been defined

C:\Programming\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\type_traits(139): error: class template "std::_Is_memfunptr" has already been defined

C:\Programming\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\type_traits(139): error: class template "std::_Is_memfunptr" has already been defined

C:\Programming\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\type_traits(139): error: class template "std::_Is_memfunptr" has already been defined

C:\Programming\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\type_traits(139): error: class template "std::_Is_memfunptr" has already been defined

C:\Programming\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\type_traits(139): error: class template "std::_Is_memfunptr" has already been defined

C:\Programming\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\type_traits(139): error: class template "std::_Is_memfunptr" has already been defined

C:\Programming\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\type_traits(139): error: class template "std::_Is_memfunptr" has already been defined

C:\Programming\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\type_traits(139): error: class template "std::_Is_memfunptr" has already been defined

C:\Programming\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\type_traits(139): error: class template "std::_Is_memfunptr" has already been defined

C:\Programming\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\type_traits(139): error: class template "std::_Is_memfunptr" has already been defined

C:\Programming\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\type_traits(139): error: class template "std::_Is_memfunptr" has already been defined

C:\Programming\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\type_traits(139): error: class template "std::_Is_memfunptr" has already been defined

C:\Programming\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\type_traits(139): error: class template "std::_Is_memfunptr" has already been defined

C:\Programming\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\type_traits(139): error: class template "std::_Is_memfunptr" has already been defined

C:\Programming\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\type_traits(139): error: class template "std::_Is_memfunptr" has already been defined

C:\Programming\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\type_traits(139): error: class template "std::_Is_memfunptr" has already been defined

C:\Programming\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\type_traits(139): error: class template "std::_Is_memfunptr" has already been defined

C:\Programming\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\type_traits(139): error: class template "std::_Is_memfunptr" has already been defined

C:\Programming\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\type_traits(139): error: class template "std::_Is_memfunptr" has already been defined

C:\Programming\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\type_traits(139): error: class template "std::_Is_memfunptr" has already been defined

C:\Programming\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\type_traits(139): error: class template "std::_Is_memfunptr" has already been defined

C:\Programming\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\type_traits(139): error: class template "std::_Is_memfunptr" has already been defined

C:\Programming\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\type_traits(139): error: class template "std::_Is_memfunptr" has already been defined

C:\Programming\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\type_traits(139): error: class template "std::_Is_memfunptr" has already been defined

C:\Programming\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\type_traits(139): error: class template "std::_Is_memfunptr" has already been defined

C:\Programming\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\type_traits(139): error: class template "std::_Is_memfunptr" has already been defined

C:\Programming\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\type_traits(139): error: class template "std::_Is_memfunptr" has already been defined

C:\Programming\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\type_traits(139): error: class template "std::_Is_memfunptr" has already been defined

C:\Programming\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\type_traits(139): error: class template "std::_Is_memfunptr" has already been defined

C:\Programming\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\type_traits(139): error: class template "std::_Is_memfunptr" has already been defined

C:\Programming\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\type_traits(139): error: class template "std::_Is_memfunptr" has already been defined

C:\Programming\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\type_traits(139): error: class template "std::_Is_memfunptr" has already been defined

C:\Programming\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\type_traits(139): error: class template "std::_Is_memfunptr" has already been defined

C:\Programming\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\type_traits(139): error: class template "std::_Is_memfunptr" has already been defined

C:\Programming\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\type_traits(139): error: class template "std::_Is_memfunptr" has already been defined

C:\Programming\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\type_traits(139): error: class template "std::_Is_memfunptr" has already been defined

C:\Programming\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\type_traits(1824): error: class template "std::result_of" has already been defined

C:\Programming\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.14.26428\include\type_traits(1824): error: class template "std::result_of" has already been defined

我一直在寻找解决方案.似乎其他遇到类似问题的人的头文件也有问题,但是我的示例代码是从互联网上下载的,上传它的人对此没有任何问题,这使我对程序的哪个部分感到困惑有问题.

I have been looking for solutions. Seems like the other people with the similar problem were having issues with their included headers but my sample code is downloaded from the internet and the person who uploaded it didn't have any problem with it, which makes me confused on what part of the program have the problem.

P.S .:我不知道自己的cuda是否安装正确.细节:我无法在Windows上安装cuda,安装过程不断告诉我安装失败.然后,我在第19条帖子的链接中找到了一种解决方案:

P.S.: I don't know if my cuda is installed properly. Details: I wasn't able to install cuda on my windows, the installation keeps telling me installation failed. Then I found a solution in this link at post #19: https://devtalk.nvidia.com/default/topic/1035535/cuda-setup-and-installation/cuda-9-2-does-not-work-with-visual-studio-2017-15-7-1/2 It seems to work fine but I don't know if it was the problem.

推荐答案

我已经在Nvidia CUDA论坛上发布了相同的帖子:

I have posted the same post in Nvidia CUDA forum: Link Here

我使用帖子中的方法重新安装了多次,但仍然存在相同的类模板"问题.

I reinstalled multiple times with the method from the post but still having the same "class template" problems.

然后我也使用相同的方法重新安装了CUDA 9.1和VS2017 ver 15.6.7,并且最终可以正常工作.

Then I reinstalled CUDA 9.1 and VS2017 ver 15.6.7 also with the same method and it finally works.

帖子中也遇到了我遇到的其他问题.

Further problems that I encountered is in the post too.

这篇关于CUDA编译错误:类模板已被定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆