Guest User

Untitled

a guest
Mar 15th, 2016
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.16 KB | None | 0 0
  1. #include <time.h>
  2. __global__ void slowKernel(float *output, bool write=false)
  3. {
  4.     float input = 5;
  5. #pragma unroll
  6.     for( int i = 0; i < 10000000; i++ ){
  7.         input = input * .9999999;
  8.     }
  9.     if (write) *output -= input;
  10. }
  11.  
  12. __global__ void fastKernel(float *output, bool write=false)
  13. {
  14.     float input = 5;
  15. #pragma unroll
  16.     for( int i = 0; i < 100000; i++ ){
  17.         input = input * .9999999;
  18.     }
  19.     if (write) *output -= input;
  20. }
  21.  
  22. void burntime(long val) {
  23.     struct timespec tv[] = {{0, val}};
  24.     nanosleep(tv, 0);
  25. }
  26.  
  27. void mallocSynchronize(){
  28.     cudaStream_t stream1, stream2;
  29.     cudaStreamCreate( &stream1 );
  30.     cudaStreamCreate( &stream2 );
  31.     const size_t sz = 1 << 21;
  32.     slowKernel <<<1, 1, 0, stream1 >>>((float *)(0));
  33.     burntime(500000000L); // 500ms wait - slowKernel around 1300ms
  34.     int *dev_a = 0;
  35.     for( int i = 0; i < 10; i++ ){
  36.         cudaMalloc( &dev_a, sz );
  37.         fastKernel <<<1, 1, 0, stream2 >>>((float *)(0));
  38.         burntime(1000000L); // 1ms wait - fastKernel around 15ms
  39.     }
  40. }
  41.  
  42. int main()
  43. {
  44.     mallocSynchronize();
  45.     cudaDeviceSynchronize();
  46.     cudaDeviceReset();
  47.     return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment