【发布时间】:2014-01-26 15:53:57
【问题描述】:
如何修改此代码以获得 100% 的 GPU 负载?
#include <iostream>
using namespace std;
__global__ void saxpy_parallel(int n, float a, float *x, float *y)
{
// Get the unique ID of this kernel instance
int i = blockIdx.x * blockDim.x + threadIdx.x;
if (i < n)
{
y[i] = a*x[i] + y[i];
}
}
int main(int argc, char const *argv[])
{
// Tensors length
int const n = 100;
// Define tensors
float x[n], y[n];
for (int i = 0; i < n; ++i)
{
x[i] = 1.0f*i;
y[i] = 1.0f*i;
}
// Device pointers
float *d_x, *d_y;
cudaMalloc(&d_x, n*sizeof(float));
cudaMalloc(&d_y, n*sizeof(float));
if (cudaMemcpy(d_x, &x, n*sizeof(float), cudaMemcpyHostToDevice) != cudaSuccess)
{
printf("Memory Error!\n");
return 0;
}
if (cudaMemcpy(d_y, &y, n*sizeof(float), cudaMemcpyHostToDevice) != cudaSuccess)
{
printf("Memory Error!\n");
return 0;
}
// Run the kernel
saxpy_parallel<<<4096, 512>>>(n, 2.0, d_x, d_y);
// Retrieve results from the device memory
cudaMemcpy(&y, d_y, n*sizeof(float), cudaMemcpyDeviceToHost);
cudaFree(d_y);
cudaFree(d_x);
printf("%s\n",y[0]);
system("PAUSE");
return 0;
}
【问题讨论】:
-
定义“100% GPU 使用率”——什么意思?
-
@talonmies 对不起。我的意思是:legitreviews.com/images/reviews/1688/GPUzLoad.png看看声音“GPU Load”
-
您说的传感器参数GPU负载恐怕与CUDA编程无关。
-
我的意思正是@talonmies 在他的评论中的意思。我在 CUDA C Programming Guide 和 CUDA C Best Practices Guide 中都找不到 GPU load 的定义。所以我认为你必须向这个社区解释一些事情。如果我们不知道它的定义,如何最大化您的 GPU 负载 参数?投票结束。
-
我还没有说你链接的问题很清楚。 GPU 使用的定义对您来说似乎不是很清楚,因为您还不能提供它。说起来可能不愉快,但这应该会让你认为你正在处理的问题还没有以令人满意的方式正式制定。