私の実行時プログラムでは、cpuとgpuは非同期的には計算できますが、協力的に計算するのはなぜですか?私の実行時プログラムでは、cpuとgpuは非同期的に計算できますが、協調的にはなぜですか?
私はプログラムの時間を測定して、合計時間はCPUの計算時間とGPUの計算時間の合計時間です。視覚的なプロファイルを通して、CPUが完了するまでGPUが計算しないことがわかります。私の目的は、CPUがgpuと同じ時間に計算することです。
プラットフォーム: ウィンドウ10
CUDA 7.5
vs2013
デバッグモードでコンパイルされたコード(NO最適化)
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <stdio.h>
#include<time.h>
__global__ void addKernel()
{
int a ;
for (int i = 0; i < 10000;i++)
for (int j = 0; j < 10000;j++)
a = i;
}
void comput()
{
int a = 1;
for (int i = 0; i < 10000;i++)
for (int j = 0; j < 10000; j++)
{
for (int k = 0; k < 100;k++)
a = j;
}
}
int main()
{
cudaSetDevice(0);
cudaEvent_t start, stop1;
cudaEventCreate(&start);
cudaEventCreate(&stop1);
clock_t ss = clock();
cudaEventRecord(start,0);
addKernel<<<1,64>>>();
cudaEventRecord(stop1,0);
clock_t ct = clock();
comput();
clock_t ctt = clock();
cudaEventSynchronize(stop1);
cudaDeviceSynchronize();
clock_t sss = clock();
float t1;
cudaEventElapsedTime(&t1, start, stop1);
printf("clock GPU :%.4f s\n", t1/1000);
printf("clock cpu:%f s\n",(float) (ctt - ct)/CLOCKS_PER_SEC);
printf("clock total time: %f s\n", (float)(sss - ss)/CLOCKS_PER_SEC);
cudaEventDestroy(start);
cudaEventDestroy(stop1);
cudaDeviceReset();
}
これはドライバの問題です。私はcomput()を呼び出す前にcudaEventQuery(stop1)を呼び出して問題を解決しました。ありがとうございました。 – ZhangJiwei
はい、 'cudaEventQuery()'は現在のバッチをすぐに起動するのと同じプロパティを持ちます。これはあなたの問題を解決してうれしいです。 – tera