Created
October 25, 2018 07:08
-
-
Save kuznetsovandrey76/967df4017e503761ebbe1827a7f2f70c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<cstdio> | |
#define N (1024*1024) | |
__global__ void kernel(float *dA, float *dB, float *dC, int size) | |
{ | |
int i = blockIdx.x * blockDim.x + threadIdx.x; | |
if (i < size) dC[i] = dA[i] + dB[i]; | |
} | |
int main() | |
{ | |
float timerVlueCPU, timerValueGPU; | |
cudaEvent_t start, stop; | |
cudaEventCreate(&start); | |
cudaEventCreate(&stop); | |
// TODO: объявление необходимых переменных. | |
int mem_size = N * sizeof(float); | |
float *dA, *dB, *dC, *hA, *hB, *hC; | |
hA = (float*)malloc(mem_size); | |
hB = (float*)malloc(mem_size); | |
hC = (float*)malloc(mem_size); | |
cudaMalloc((void**)&dA, mem_size); | |
cudaMalloc((void**)&dB, mem_size); | |
cudaMalloc((void**)&dC, mem_size); | |
for (int i = 0; i < N; ++i) | |
{ | |
hA[i] = (float)i; | |
hB[i] = (float)N - i; | |
} | |
// размеры сетки установить равными значениям из предыдущей лекции | |
int N_blocks = N / 512, N_threads = 512; | |
// старт таймера | |
cudaEventRecord(start, 0); | |
// копирование массивов | |
cudaMemcpy(dA, hA, mem_size, cudaMemcpyHostToDevice); | |
cudaMemcpy(dB, hB, mem_size, cudaMemcpyHostToDevice); | |
kernel << < N_blocks, N_threads >> >(dA, dB, dC, mem_size); | |
cudaMemcpy(hC, dC, mem_size, cudaMemcpyDeviceToHost); | |
cudaEventRecord(stop, 0); | |
cudaEventSynchronize(stop); | |
cudaEventElapsedTime(&timerValueGPU, start, stop); | |
printf("GPU time: %f ms\n", timerValueGPU); | |
// TODO: Аналогично для CPU | |
// {...} | |
printf("\n Rate: %f x\n", timerVlueCPU / timerValueGPU); | |
// TODO: освобождение памяти на host и device | |
// {...} | |
free(hA); | |
free(hB); | |
free(hC); | |
cudaFree(dA); | |
cudaFree(dB); | |
cudaFree(dC); | |
// уничтожение переменных событий | |
cudaEventDestroy(start); | |
cudaEventDestroy(stop); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment