cudaEventRecord() always throws error invalid resource handle

I have been trying to use event timers as per example code seen at

However no matter what I do I seem to always get an error invalid resource handle or worse, a SIGSEGV. This is somewhat baffling.

I have been trying over and over to reduce down to a minimal test case and the code is just a vector addition as can be seen here ~blastwave/bw: nvidia_cuda/event_timer/ - sourcehut git

I do select a GPU device with " int device_id = gpu_unit_min_number;" and then the call “cuda_err = cudaSetDevice(device_id);” which seems to work. Near as anyone can tell. After the device has been selected I then call “cudaEventCreate(&cuda_start);” for both the start and stop event timers. No matter what I do these blow up when I try to use them later with “cuda_err = cudaEventRecord(cuda_start, 0);”

Any insight would be greatly appreciated.

Why do you call cudaDeviceReset() ? This effectively invalidates everything which was done on the selected device, including the created cudaevents, which must not be used afterwards.

Good question !! That was my mistake. I reset the device and that wrecks the previous Event API calls. Wonderful. Thank you so much. I simply could not see that mistake.