Я новичок в cuda и попытался запустить простой векторный пример добавления, который я нашел в Интернете, чтобы начать. Я использую win10 64bit и 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», так как командная строка окна дает мне
nvcc fatal : Cannot find compiler 'cl.exe' in PATH
и онлайн-решения для меня не сработали. Затем я запустил эту команду (--compiler -options уже решил некоторые ошибки)
nvcc add.cu --compiler-options "-D _WIN64"
но компилятор все еще выдаёт мне ошибки
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
Я искал решения. Похоже, что у других людей с подобной проблемой были проблемы с их включенными заголовками, но мой пример кода загружен из Интернета, и у человека, который его загрузил, не было никаких проблем с ним, что заставляет меня запутаться в какой части программы есть проблема.
P.S .: Я не знаю, правильно ли установлена моя cuda. Подробности:
Я не смог установить cuda на мои окна, установка говорит мне, что установка не удалась. Тогда я нашел решение по этой ссылке в посте № 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
Кажется, все работает нормально, но я не знаю, была ли это проблема.