Thursday, February 28, 2013

CUDA timing v2

So today I decided that I want to add some timing statements to my test code for some cuda methods. I have posted about timing CUDA code before, however I felt that it would be too cumbersome to add that code to the tests themselves. So as a solution I decided to use a method with a function pointer. The code snippet below demonstrates this.


void timeIt(void (*fn)(), string name){
 float elapsedTime=0.0;
 cudaEvent_t start, stop;
 cudaEventCreate(&start);
 cudaEventCreate(&stop);

 cudaEventRecord(start,0);
 fn();
 cudaEventRecord(stop, 0);
 cudaEventSynchronize(stop);//remember kernels run asynchronously
 cudaEventElapsedTime(&elapsedTime, start, stop);
 cout<<"Execution of "<<name<<" took "<<elapsedTime<<" milliseconds."<<endl;
}


The method 'timeIt' takes as its input a function pointer and a string with the name of the method to be called. Then the proper cuda events are created, and the method called while the timer runs. While this is straightfoward it is the first time I have actually used function pointers. A more detailed treatment of them can be found here

No comments:

Post a Comment