我想使用clock()來比較不同的內核實現。我試圖用一個簡單的SAXPY例子來實現它,但它導致零時鐘週期,這是不太可能的。CUDA時鐘()導致零時鐘週期
我已經找到了一些關於如何實現clock()的例子。 here和here。但不知何故轉移到我的代碼不起作用。
這裏是我使用的代碼:
/* SAXPY code example from https://devblogs.nvidia.com/parallelforall/easy-introduction-cuda-c-and-c/ */
#include <stdio.h>
// The declaration specifier __global__ defines a kernel. This code
// will be copied to the device and will be executed there in parallel
__global__
void saxpy(int n, float a, float *x, float *y, int *kernel_clock)
{
// The indexing of the single threads is done with the following
// code line
int i = blockIdx.x*blockDim.x + threadIdx.x;
clock_t start = clock();
// Each thread is executing just one position of the arrays
if (i < n) y[i] = a*x[i] + y[i];
clock_t stop = clock();
kernel_clock[i] = (int) (stop-start);
}
int main(void)
{
// Clock cycles of threads
int *kernel_clock;
int *d_kernel_clock;
// Creating a huge number
int N = 1<<20;
float *x, *y, *d_x, *d_y;
// Allocate an array on the *host* of the size of N
x = (float*)malloc(N*sizeof(float));
y = (float*)malloc(N*sizeof(float));
kernel_clock = (int*)malloc(N*sizeof(int));
// Allocate an array on the *device* of the size of N
cudaMalloc(&d_x, N*sizeof(float));
cudaMalloc(&d_y, N*sizeof(float));
cudaMalloc(&d_kernel_clock, N*sizeof(int));
// Filling the array of the host
for (int i = 0; i < N; i++) {
x[i] = 1.0f;
y[i] = 2.0f;
}
// Copy the host array to the device array
cudaMemcpy(d_x, x, N*sizeof(float), cudaMemcpyHostToDevice);
cudaMemcpy(d_y, y, N*sizeof(float), cudaMemcpyHostToDevice);
cudaMemcpy(d_kernel_clock, kernel_clock, N*sizeof(int), cudaMemcpyHostToDevice);
// Perform SAXPY on 1M elements. The triple chevrons dedicates how
// the threads are grouped on the device
saxpy<<<(N+255)/256, 256>>>(N, 2.0f, d_x, d_y, d_kernel_clock);
cudaDeviceSynchronize();
// Copy the result from the device to the host
cudaMemcpy(y, d_y, N*sizeof(float), cudaMemcpyDeviceToHost);
cudaMemcpy(kernel_clock, d_kernel_clock, N*sizeof(int), cudaMemcpyDeviceToHost);
// Calculate average clock time
float average_clock = 0;
for (int i = 0; i < N; i++) {
average_clock += (float) (kernel_clock[i]);
}
average_clock /= N;
// Display the time to the screen
printf ("Kernel clock cycles: %.4f\n", average_clock);
// Free the memory on the host and device
free(x);
free(y);
free(kernel_clock);
cudaFree(d_x);
cudaFree(d_y);
cudaFree(d_kernel_clock);
}
此代碼示例導致:
Kernel clock cycles: 0.0000
我不知道我做錯了。所以我的問題是:我怎樣才能得到一個合理的結果?
我沒有看到任何錯誤檢查。如果你用'cuda-memcheck'運行你的代碼會發生什麼? –
'cuda-memcheck'提供0錯誤 '========錯誤摘要:0錯誤' – stebran