From 90ef44fc4054f20ad6b31038d2b8d73eb20df901 Mon Sep 17 00:00:00 2001 From: nchristensen <11543181+nchristensen@users.noreply.github.com> Date: Sun, 5 Dec 2021 16:58:18 +0000 Subject: [PATCH] [PATCH 096/144] Avoid loss precision in event time calculations 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 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/CL/devices/cuda/pocl-cuda.c b/lib/CL/devices/cuda/pocl-cuda.c index e19d18a..c4bdd41 100644 --- a/lib/CL/devices/cuda/pocl-cuda.c +++ b/lib/CL/devices/cuda/pocl-cuda.c @@ -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); } } -- 2.30.2