C++ 在任何cuda调用中,CUDAERROUNKNOWN code=30
我已经安装了cuda工具包,可以毫无问题地运行示例。现在,我想在我的项目中使用cuda,在我的项目中使用cmake。所以,为了证明我的问题,我创建了一个简单的例子。我有3个文件,我的主文件是“teste.cpp”,一个cuda文件“hello_world.cu”,它的头文件。我的main唯一的功能是调用hello_world.cu中的函数,如下所示:C++ 在任何cuda调用中,CUDAERROUNKNOWN code=30,c++,cmake,cuda,C++,Cmake,Cuda,我已经安装了cuda工具包,可以毫无问题地运行示例。现在,我想在我的项目中使用cuda,在我的项目中使用cmake。所以,为了证明我的问题,我创建了一个简单的例子。我有3个文件,我的主文件是“teste.cpp”,一个cuda文件“hello_world.cu”,它的头文件。我的main唯一的功能是调用hello_world.cu中的函数,如下所示: #include <iostream> #include "hello_world.h" using namespace std;
#include <iostream>
#include "hello_world.h"
using namespace std;
int main(int argc, char** argv)
{
teste(argc, argv);
return 0;
}
// CUDA runtime
#include </usr/local/cuda-9.0/include/cuda_runtime.h>
// helper functions and utilities to work with CUDA
#include </usr/local/cuda-9.0/samples/common/inc/helper_functions.h>
#include </usr/local/cuda-9.0/samples/common/inc/helper_cuda.h>
#define NUM_BLOCKS 64
#define NUM_THREADS 256
__global__ static void timedReduction(const float *input, float *output, clock_t *timer)
{
// __shared__ float shared[2 * blockDim.x];
extern __shared__ float shared[];
const int tid = threadIdx.x;
const int bid = blockIdx.x;
if (tid == 0) timer[bid] = clock();
// Copy input.
shared[tid] = input[tid];
shared[tid + blockDim.x] = input[tid + blockDim.x];
// Perform reduction to find minimum.
for (int d = blockDim.x; d > 0; d /= 2)
{
__syncthreads();
if (tid < d)
{
float f0 = shared[tid];
float f1 = shared[tid + d];
if (f1 < f0)
{
shared[tid] = f1;
}
}
}
// Write result.
if (tid == 0) output[bid] = shared[0];
__syncthreads();
if (tid == 0) timer[bid+gridDim.x] = clock();
}
int teste(int argc, char** argv) {
printf("CUDA Clock sample\n");
// This will pick the best possible CUDA capable device
int dev = findCudaDevice(argc, (const char **)argv);
float *dinput = NULL;
float *doutput = NULL;
clock_t *dtimer = NULL;
clock_t timer[NUM_BLOCKS * 2];
float input[NUM_THREADS * 2];
for (int i = 0; i < NUM_THREADS * 2; i++)
{
input[i] = (float)i;
}
checkCudaErrors(cudaMalloc((void **)&dinput, sizeof(float) * NUM_THREADS * 2));
checkCudaErrors(cudaMalloc((void **)&dinput, sizeof(float) * NUM_THREADS * 2));
checkCudaErrors(cudaMalloc((void **)&doutput, sizeof(float) * NUM_BLOCKS));
checkCudaErrors(cudaMalloc((void **)&dtimer, sizeof(clock_t) * NUM_BLOCKS * 2));
checkCudaErrors(cudaMemcpy(dinput, input, sizeof(float) * NUM_THREADS * 2, cudaMemcpyHostToDevice));
timedReduction<<<NUM_BLOCKS, NUM_THREADS, sizeof(float) * 2 *NUM_THREADS>>>(dinput, doutput, dtimer);
checkCudaErrors(cudaMemcpy(timer, dtimer, sizeof(clock_t) * NUM_BLOCKS * 2, cudaMemcpyDeviceToHost));
checkCudaErrors(cudaFree(dinput));
checkCudaErrors(cudaFree(doutput));
checkCudaErrors(cudaFree(dtimer));
long double avgElapsedClocks = 0;
for (int i = 0; i < NUM_BLOCKS; i++)
{
avgElapsedClocks += (long double) (timer[i + NUM_BLOCKS] - timer[i]);
}
avgElapsedClocks = avgElapsedClocks/NUM_BLOCKS;
printf("Average clocks/block = %Lf\n", avgElapsedClocks);
return EXIT_SUCCESS;
}
cmake_minimum_required(VERSION 2.8)
set(CUDA_HOST_COMPILER /usr/bin/g++-4.9)
find_package(CUDA QUIET REQUIRED)
# Pass options to NVCC
set(
CUDA_NVCC_FLAGS
${CUDA_NVCC_FLAGS};
-O3 -std=c++11 -g
)
# For compilation ...
# Specify target & source files to compile it from
cuda_add_executable(
helloworld
teste.cpp
hello_world.cu
)
代码经过编译,运行时得到以下输出:
CUDA Clock sample
GPU Device 0: "GeForce GTX 950M" with compute capability 5.0
CUDA error at /home/cesar/Documents/cuda_teste/hello_world.cu:69 code=30(cudaErrorUnknown) "cudaMalloc((void **)&dinput, sizeof(float) * NUM_THREADS * 2)"
为什么我在这里得到这个错误,使用cmake?如果我转到samples目录并直接尝试“clock”示例,那么一切都很好。。那么这是我的CMakeLists.txt上的问题吗 我设法找到了解决办法
在我的CMakeLists.txt上,我需要在我的NVCC中添加一个带有“-arch=sm_50”的标志,在我的情况下,它是sm_50,因为我的图形卡具有计算能力5.0,如果任何一个有相同的错误并且想要尝试此操作,您必须检查您的计算能力我无法重现您的错误。如果我获取您的代码和Cmake文件并进行构建(只需进行一些路径更改),我将获得一个功能性可执行文件,并且不会出现运行时错误。您确定在您的构建机器上的多个CUDA版本或类似版本之间没有冲突吗?