I am trying to time the thrust sort function . Present, i am using cuda events. but i was curious if cuda events will give me the wrong value. This is because, on my computer, thrust is sorting 2 million floats in GPU in 34 ms. But this seems too fast
I tried both CPU and GPU times and got following:
CPU ( takes around 36 ms)
__int64 ctr1 = 0 , ctr2 = 0 , freq = 0 ;
QueryPerformanceFrequency((LARGE_INTEGER *) &freq);
QueryPerformanceCounter((LARGE_INTEGER *) &ctr1);
thrust::sort(D.begin(),D.end());
// transfer data back to host
thrust::copy(D.begin(), D.end(), H.begin());
cudaThreadSynchronize(); // block until kernel is finished
QueryPerformanceCounter((LARGE_INTEGER *)&ctr2);
double ans = ((ctr2 - ctr1) * 1.0 / freq);
printf("The time elapsed in milliseconds is %f\n",(ans*1000));
GPU
cudaEvent_t start, stop;
cudaEventCreate(&start);
cudaEventCreate(&stop);
cudaEventRecord(start, 0);
thrust::sort(D.begin(),D.end());
thrust::copy(D.begin(), D.end(), H.begin());
cudaEventRecord(stop, 0);
cudaEventSynchronize(stop);
float elapsedTime;
cudaEventElapsedTime(&elapsedTime , start, stop);
printf("time is %f ms", elapsedTime);
Please let me know which timing is correct
Thanks
Both timings are correct from different aspects. The CPU timing will include the overhead caused by the API calls and the synchronization. If you are interested in this overhead you should use the CPU timer.
The event based timing is isolating the timing on the GPU and gives you the time of the GPU execution.
Other differences between CPU and Event Timing is that if the thrust::sort() is the first call to the GPU from the current thread the call to will need to setup the CUDA context and give you a timing which includes the context setup. You will not get this problem if you use the event based timing because the context will be setup when calling cudaEventCreate().
If you want to time GPU algorithms to in order to get a performance figure, the best way to do this is to use event based timing but also run the algorithms in a loop a number of times.