title: CUDA Time Events
body: <p>I am timing how long it takes my CUDA program to calculate matrices of a certain size.  For example, 10x10, 100x100, 500x500,100x1000.  </p>
  
  <p>However, the results are not at all what I was expecting.  The numbers for the graph are not at what is expected.  With the increase in size of the matrices, the computational time decreases.  </p>
  
  <p>For example, here is the average time (from 1000 runs):
  10x10: 0.032768s
  100x100: 0.068960s
  500x500: 0.006336s
  1000x1000: 0.018400s</p>
  
  <p>The time goes down, then up again at 1000.  What is going on?  Shouldn't the numbers peak off at a certain point?  Why is it going in a roller coaster like this?</p>
  
  <p>Here is how the actual timing code is being run:</p>
  
  <pre><code>int blocksNeeded=0;
  cudaError_t cudaStatus;
  blocksNeeded=(size/MAXTHREADS)+1;
  int threadsPerBlock = MAXTHREADS/blocksNeeded+1;
  cudaEvent_t start, stop;
  float elapsedtime;
  .
  .
  .
  .
  .
  cudaEventCreate(&amp;start);
  cudaEventCreate(&amp;stop); 
  cudaEventRecord(start, 0);
  addKernel&lt;&lt;&lt;blocksNeeded, size&gt;&gt;&gt;(dev_c, dev_a, dev_b,size);
  cudaStatus = cudaDeviceSynchronize();
  cudaEventRecord(stop, 0); 
  cudaEventSynchronize(stop); 
  cudaEventElapsedTime(&amp;elapsedtime, start, stop);
  cudaEventDestroy(start);
  cudaEventDestroy(stop);
  </code></pre>
  
  <p>where MAXTHREADS are 1024 and, size is the amount of elements I have in the matrix. I.E. 10x10 matrix will have 100 elements which is the size. </p>
  
  <p>Updated with kernel:</p>
  
  <pre><code>__global__ void addKernel(float *c, float *a, float *b,int size)
  {
      int idx = blockDim.x * blockIdx.x + threadIdx.x;
      if(idx &lt; size) 
          c[idx] = a[idx] + b[idx];
  
  }
  </code></pre>