[PATCH 096/144] Avoid loss precision in event time calculations
authornchristensen <11543181+nchristensen@users.noreply.github.com>
Sun, 5 Dec 2021 16:58:18 +0000 (16:58 +0000)
committerAndreas Beckmann <anbe@debian.org>
Fri, 7 Jan 2022 23:55:22 +0000 (23:55 +0000)
The current code implicitly converts `epoch` to a float before calculating `time_end` and `time_start` and converting back to a `cl_ulong`. A `float` only has seven or so digits of accuracy meaning this conversion throws away the least significant digits of `epoch` if `epoch` is larger than about 1e8. Conversely, the precision of `diff` is around 1e-3 so multiplying by 1e6 makes all of these digits > 1 and converting this number to a `cl_ulong` will not lose any  digits. The gist below illustrates this in Python.

https://gist.github.com/nchristensen/15ab1fd53ba099accaab780ab6dbad90

Gbp-Pq: Name 0096-Avoid-loss-precision-in-event-time-calculations.patch

lib/CL/devices/cuda/pocl-cuda.c

index e19d18ac2c626b8af97d6671a665e65030546712..c4bdd419575878d0fdfa6e4b37a824ee5ff6886c 100644 (file)
@@ -1552,14 +1552,14 @@ pocl_cuda_update_event (cl_device_id device, cl_event event)
           &diff, ((pocl_cuda_device_data_t *)device->data)->epoch_event,
           event_data->start);
       CUDA_CHECK (result, "cuEventElapsedTime");
-      event->time_start = (cl_ulong) (epoch + diff * 1e6);
+      event->time_start = epoch + (cl_ulong)(diff * 1e6);
       event->time_start = max (event->time_start, epoch + 1);
 
       result = cuEventElapsedTime (
           &diff, ((pocl_cuda_device_data_t *)device->data)->epoch_event,
           event_data->end);
       CUDA_CHECK (result, "cuEventElapsedTime");
-      event->time_end = (cl_ulong) (epoch + diff * 1e6);
+      event->time_end = epoch + (cl_ulong)(diff * 1e6);
       event->time_end = max (event->time_end, event->time_start + 1);
     }
 }