다른 커널 구현을 비교하기 위해 clock()을 사용하고 싶습니다. 간단한 SAXPY 예제에서 구현하려고 시도했지만 제로 클록주기가 발생하지만 이는 거의 없습니다.CUDA clock()은 제로 클럭 사이클을 유도합니다.
이미 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);
}
이 코드 예제는 리드 (Lead) : 내가 잘못하고있는 무슨 확실하지 않다
Kernel clock cycles: 0.0000
여기
내가 사용하고있는 코드입니다. 그래서 제 질문은 : 실제로 합리적인 결과를 얻으려면 어떻게해야합니까? 당신이 당신의 질문에 링크 된 답변 중 하나에서 인용
오류 검사가 표시되지 않습니다. 'cuda-memcheck'로 코드를 실행하면 어떻게됩니까? –
'cuda-memcheck'는 0 에러를냅니다. '======== 에러 요약 : 0 errors' – stebran