Calculate time with cudaEvent_t

I am trying to calculate time in c++ cuda program

But I received 0.000 for timeProd variable. (It can't be zero, because operation is not small)

What I did wrong? This line cudaEventRecord(stopProd, 0); returns error

CUDA Error: Assign rnorm = d : 33 (invalid resource handle).

Me code

float timeProd;
  cudaEvent_t startProd, stopProd;
  cudaEventCreate(&startProd);
  cudaEventCreate(&stopProd);
  cudasafe(cudaEventRecord(startProd, 0), "Assign rnorm = d", __FILE__, __LINE__);
  //rr_dot_product.record_start();

  for (int i = 0; i < cudaGetDeviceCountVal; i++) {
    cudaSetDevice(i);
    gpu_dot(r[i], r[i], endGpu[i] - startGpu[i], buff[i], rho[i]);
  }
  cudasafe(cudaEventRecord(stopProd, 0), "Assign rnorm = d", __FILE__, __LINE__);
  cudasafe(cudaEventSynchronize(startProd), "Assign rnorm = d", __FILE__, __LINE__);
  cudasafe(cudaEventSynchronize(stopProd), "Assign rnorm = d", __FILE__, __LINE__);
  cudasafe(cudaEventElapsedTime(&timeProd, startProd, stopProd), "Assign rnorm = d", __FILE__, __LINE__);
  printf("timeProd %.20f\n", timeProd);

1 answer

  • answered 2019-12-15 12:58 talonmies

    Events are tied to specific GPU and you need to ensure that the events only used with the device on which they are created. I would expect this modification to work:

      float timeProd;
      cudaEvent_t startProd, stopProd;
      cudaSetDevice(0); // Explicit device selection for event
      cudaEventCreate(&startProd);
      cudaEventCreate(&stopProd);
      cudasafe(cudaEventRecord(startProd, 0), "Assign rnorm = d", __FILE__, __LINE__);
    
      for (int i = 0; i < cudaGetDeviceCountVal; i++) {
        cudaSetDevice(i);
        gpu_dot(r[i], r[i], endGpu[i] - startGpu[i], buff[i], rho[i]);
      }
    
      cudaSetDevice(0); // Change back to device which owns event before continuing
      cudasafe(cudaEventRecord(stopProd, 0), "Assign rnorm = d", __FILE__, __LINE__);
      cudasafe(cudaEventSynchronize(startProd), "Assign rnorm = d", __FILE__, __LINE__);
      cudasafe(cudaEventSynchronize(stopProd), "Assign rnorm = d", __FILE__, __LINE__);
      cudasafe(cudaEventElapsedTime(&timeProd, startProd, stopProd), "Assign rnorm = d", __FILE__, __LINE__);
      printf("timeProd %.20f\n", timeProd);