在ubuntu上运行cuda -它是否需要一个本地设备驱动程序(使用nvidia显卡)
run cuda on ubuntu - does it need a native device driver (with nvidia graphics card)?
我正在使用GeForce GTX 660M显卡在Ubuntu 14上运行cuda/opencl GPU计算的基本入门示例。
尽管我设法编译和运行示例代码,但似乎GPU没有计算任何东西或cudamemcpy操作不起作用,因为我的结果值在调用内核并执行devicetohost复制操作后没有更新。
我想知道,为了使用cuda或opencl,我是否需要在Ubuntu上安装某个来自nvidia的本地驱动程序。
这是我的基本入门代码(用于cuda):
#include <iostream>
using namespace std;
// global constants
#define THREADS 4
const int N = 100;
int fill_content = 1;
__global__ void sum(int* a, int* b, int* c)
{
int i = blockIdx.x * blockDim.x * threadIdx.x;
c[i] = a[i] + b[i];
}
void check( int* a, int N )
{
cout << endl;
for(int i = 0; i < N; ++i)
{
int num = a[i];
cout << i << ": " << num << endl;
}
cout << endl;
}
void fill_vectors(int*p , int size)
{
for(int i = 0; i < size; ++i)
{
p[i] = fill_content;
}
}
int main(int argc, char **argv)
{
int host_a[N], host_b[N], host_c[N];
size_t s_a,s_b,s_c;
s_a = s_b = s_c = sizeof(int) * N;
int *dev_a, *dev_b, *dev_c;
// allocate memory on the device for calculation input and results
cudaMalloc(&dev_a, s_a);
cudaMalloc(&dev_b, s_b);
cudaMalloc(&dev_c, s_c);
fill_content = 1;
fill_vectors(host_a, N);
fill_content = 2;
fill_vectors(host_b, N);
fill_content = 0;
fill_vectors(host_c, N);
// copy the input values to the gpu-memory
cudaMemcpy(dev_a, host_a, s_a, cudaMemcpyHostToDevice);
cudaMemcpy(dev_b, host_b, s_b, cudaMemcpyHostToDevice);
// invokes kernel-method sum on device using device-memory dev_a, dev_b, dev_c
//sum<<<N/THREADS, THREADS,1>>>(dev_a, dev_b, dev_c);
// copy the result values back from the device_memory to the host-memory
cudaMemcpy(host_c, dev_c, s_c, cudaMemcpyDeviceToHost);
// free memory allocated on device (for input and result values)
cudaFree(dev_a); cudaFree(dev_b); cudaFree(dev_c);
// expected to print out 3
check(host_c,N);
}
我用:
nvcc -o vector-sum2 vector-sum2.cu
安装了nvidia-cuda-toolkit
:
就像上面解释的那样,它只为每个数组元素输出0
0: 0
1: 0
2: 0
3: 0
4: 0
5: 0
…继续。
你知道我需要改变什么才能使这个例子工作吗?
首先,你的内核调用被注释掉了:
//sum<<<N/THREADS, THREADS,1>>>(dev_a, dev_b, dev_c);
所以你的输出都是零,因为你没有实际运行内核。
如果取消对内核的注释,就会出现问题。任何时候你遇到CUDA代码的问题,你应该使用适当的CUDA错误检查和运行你的代码与cuda-memcheck
。
取消内核注释并使用cuda-memcheck
运行会发现内核有大量的越界访问。这些最终是由于这行代码:
int i = blockIdx.x * blockDim.x * threadIdx.x;
这不是创建唯一线程索引的正确方法。相反,我们想要:
int i = blockIdx.x * blockDim.x + threadIdx.x;
有了这些改变,你的代码就可以为我正确运行了。如果它仍然不适合你,你可能是有问题的机器设置,在这种情况下,适当的cuda错误检查可能会给你一些线索。
相关文章:
- 是否可以在 c++ 中创建一个文件并为其提供属性以将其隐藏?(在 Ubuntu 上)
- 在 Ubuntu(linux OS)中将一个巨大的文件读入C++向量
- 如何获得一个使用 XWindows 在 Windows 上的 Ubuntu 上的 Bash 上构建的 C++ 程序?
- 如何在Ubuntu中将内容从一个文件传输到另一个文件
- 在 Ubuntu 12.04 上运行,一个在 Windows 上的 MS Visual Studio 中用 c++11
- 如何编写一个程序,我可以像 Ubuntu 中的命令一样添加信息(例如:程序 -u "Hello World" )
- 如何在 ubuntu 上将可执行文件注入另一个可执行文件
- 如何在Ubuntu上的G 中编译一个程序,具有多个源文件
- 我是一个菜鸟,但是我可以编写一个带有gui的c ++程序,并让它在Visual Studio 2010 express和ubuntu中的终端上运行吗?
- 两个 C++ 可执行对象之间的实时数据通信,一个是 Windows 和另一个在虚拟机 Ubuntu 中
- 从Ubuntu到Debian运行一个可执行文件
- JNI 中的 Java 线程C++仅使用一个内核的环境.Arm 处理器和 Ubuntu
- 在 Ubuntu 中创建一个文件作为 C 中的可执行程序
- 引导一个程序,该程序将选择一个特定的 Ubuntu iso 并从中引导
- 如何在QtCreator(Linux Ubuntu)中编译和运行一个随机的单个C++文件
- 如何在ubuntu中创建和运行一个脚本,该脚本每5分钟运行一次并关闭一个c代码
- 在ubuntu上运行cuda -它是否需要一个本地设备驱动程序(使用nvidia显卡)
- 为Ubuntu创建一个deb包
- Qt:在ubuntu中使用crontab定期启动一个消息框
- 如何让 cmake 使用另一个 opencv 目录,而不是 ubuntu 中的系统目录?