CUDA ProgrammingcontinuedITCS 4145/5145 Nov 24, 2010 © Barry WilkinsonCUDA-3
2Error ReportingcontinuedCUDA SDK toolkit has some “safety check routines:•cutilSafeCall( ... ); // check for error return...
3Error ReportingcontinuedBook by Sanders and Kandrot* uses a macro called HANDLE_ERROR() tosurround CUDA calls, e.g.:HANDL...
4Timing ExecutionCUDA SDK timerint timer =0;cutCreateTimer (& timer);cutStartTimer (timer);...cutStopTimer (timer);cutGetT...
5TimingIf program uses synchronous cudaMemcpy, canuse clock():#include <time.h>…start = clock();cudaMemcpy… // kernel call...
6Monte Carlo ComputationsEmbarrassingly parallel computations that are attractive for GPUs.Use random numbers to make rand...
7Generating random numbersPossible solutions:1.Call rand() in the CPU code and copy the random numbers acrossto the GPU (n...
Questions
Upcoming SlideShare
Loading in …5
×

Cuda 3

443 views

Published on

Published in: Technology, Sports
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
443
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
9
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Cuda 3

  1. 1. CUDA ProgrammingcontinuedITCS 4145/5145 Nov 24, 2010 © Barry WilkinsonCUDA-3
  2. 2. 2Error ReportingcontinuedCUDA SDK toolkit has some “safety check routines:•cutilSafeCall( ... ); // check for error return codes:•cutilCheckMsg( ... ); // check for failure messages:ExamplecutilSafeCall( cudaMalloc(… ) ); // allocate GPU memorymyKernel<<<nblocks,nthreads>>>( … ) ; // execute kernelcutilCheckMsg("myKernel failedn");cutilSafeCall( cudaMemcpy(…); // copy results backcutilSafeCall(cudaFree( … ); // free memoryNeed details of these routines!
  3. 3. 3Error ReportingcontinuedBook by Sanders and Kandrot* uses a macro called HANDLE_ERROR() tosurround CUDA calls, e.g.:HANDLE_ERROR( cudaMalloc( … ));HANDLE_ERROR detects that call has returned an error code, prints anassociated error message, and exist the application with an EXIT_FAILUREcode:static void HandleError( cudaError_t err, const char *file, int line ) {if (err != cudaSuccess) {printf( "%s in %s at line %dn", cudaGetErrorString( err ), file, line );exit( EXIT_FAILURE );}}#define HANDLE_ERROR( err ) (HandleError( err, __FILE__, __LINE__ ))* “CUDA By Example An Introduction to General-Purpose GPU Programming” by JasonSanders and Edward Kandrot, Addison-Wesley, Upper Saddle River, NJ, 2011
  4. 4. 4Timing ExecutionCUDA SDK timerint timer =0;cutCreateTimer (& timer);cutStartTimer (timer);...cutStopTimer (timer);cutGetTimerValue (timer);cutDeleteTimer (timer);Avoid including time of first kernel launch which will be moretiming consuming that subsequent launches because ofinitializationUse events instead of above for asynchronous functionsNeed details of these routines!
  5. 5. 5TimingIf program uses synchronous cudaMemcpy, canuse clock():#include <time.h>…start = clock();cudaMemcpy… // kernel callcudaMemcpystop = clock();printf("GPU pi calculated in %f s.n",(stop-start)/(float)CLOCKS_PER_SEC);
  6. 6. 6Monte Carlo ComputationsEmbarrassingly parallel computations that are attractive for GPUs.Use random numbers to make random selections that are thenused in the computation.Many application areas: numerical integration, physical simulations,business models, finance, …Principle issue is how to generate (pseudo) random sequences.Cannot call rand() or any other C library function from within aCUDA kernel.* http://developer.download.nvidia.com/compute/cuda/3_2_prod/toolkit/docs/CURAND_Library.pdf
  7. 7. 7Generating random numbersPossible solutions:1.Call rand() in the CPU code and copy the random numbers acrossto the GPU (not the best way)2.Use the NVIDIA CUDA CURAND library*3.Hand-code the rand() function in kernel.Common random number generator formula is:xi+1 = (a * xi + c) mod m.Good values for a, c, and m are a = 16807, c = 0, and m = 231-1 (a prime number).Will need to use long ints because of the size of numbers.* http://developer.download.nvidia.com/compute/cuda/3_2_prod/toolkit/docs/CURAND_Library.pdf
  8. 8. Questions

×