From 64ff74caf9a9c54e21be15c15391ae01da0ffb43 Mon Sep 17 00:00:00 2001 From: Dmitry Mikushin Date: Wed, 17 Sep 2025 22:02:08 +0200 Subject: [PATCH 1/4] Fix overflow in type cast for GUPS benchmark Changed size_t n = (size_t)(1 << logn) to size_t n = ((size_t)1) << logn to prevent integer overflow when logn >= 31. The original code would perform the left shift on an int (1) before casting to size_t, causing overflow. Now the cast happens first, allowing proper handling of large values like n = 2^31. Fixes #57 --- posts/gups/gups.cu | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/posts/gups/gups.cu b/posts/gups/gups.cu index 5636a32..8ef8fa0 100644 --- a/posts/gups/gups.cu +++ b/posts/gups/gups.cu @@ -458,7 +458,7 @@ int main(int argc, char* argv[]) prop.sharedMemPerBlockOptin)); } - size_t n = (size_t)(1 << logn); + size_t n = ((size_t)1) << logn; size_t working_set = (size_t)n * 100 / occupancy; if (accesses_per_elem == -1) { if (test_type == UPDATE || test_type == UPDATE_NO_LOOP @@ -595,4 +595,4 @@ int main(int argc, char* argv[]) if (!shared_mem) CUDA_RT_CALL(cudaFree(d_t)); return 0; -} +} \ No newline at end of file From 278a59a4379da1e1af19818b6400431a3d55dd12 Mon Sep 17 00:00:00 2001 From: Dmitry Mikushin Date: Thu, 18 Sep 2025 06:03:33 +0200 Subject: [PATCH 2/4] Use 1UL instead of C-style cast as suggested in review Using unsigned long literal (1UL) is cleaner and more idiomatic C++ than C-style cast. This achieves the same result of avoiding integer overflow while being more readable. --- posts/gups/gups.cu | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/posts/gups/gups.cu b/posts/gups/gups.cu index 8ef8fa0..15f5daf 100644 --- a/posts/gups/gups.cu +++ b/posts/gups/gups.cu @@ -458,7 +458,7 @@ int main(int argc, char* argv[]) prop.sharedMemPerBlockOptin)); } - size_t n = ((size_t)1) << logn; + size_t n = 1UL << logn; size_t working_set = (size_t)n * 100 / occupancy; if (accesses_per_elem == -1) { if (test_type == UPDATE || test_type == UPDATE_NO_LOOP From 8b558db6bd011b81ac241feaa34f404d81e1baec Mon Sep 17 00:00:00 2001 From: Dmitry Mikushin Date: Thu, 18 Sep 2025 06:17:17 +0200 Subject: [PATCH 3/4] Remove redundant C-style casts - Remove redundant cast on line 462 where n is already size_t - Fix similar pattern on line 393 using 1UL instead of C-style cast --- posts/gups/gups.cu | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/posts/gups/gups.cu b/posts/gups/gups.cu index 15f5daf..df68178 100644 --- a/posts/gups/gups.cu +++ b/posts/gups/gups.cu @@ -390,7 +390,7 @@ int main(int argc, char* argv[]) } } - size_t n_shmem = (size_t)1 << logn_shmem; + size_t n_shmem = 1UL << logn_shmem; int ndev; CUDA_RT_CALL(cudaGetDeviceCount(&ndev)); @@ -459,7 +459,7 @@ int main(int argc, char* argv[]) } size_t n = 1UL << logn; - size_t working_set = (size_t)n * 100 / occupancy; + size_t working_set = n * 100 / occupancy; if (accesses_per_elem == -1) { if (test_type == UPDATE || test_type == UPDATE_NO_LOOP || test_type == READ_WRITE) { From f0f1e141f38dab5bc7216aba5d0ad7e4e712000b Mon Sep 17 00:00:00 2001 From: Dmitry Mikushin Date: Thu, 18 Sep 2025 07:31:27 +0200 Subject: [PATCH 4/4] Ensure file ends with newline character --- posts/gups/gups.cu | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/posts/gups/gups.cu b/posts/gups/gups.cu index df68178..fb96f5e 100644 --- a/posts/gups/gups.cu +++ b/posts/gups/gups.cu @@ -595,4 +595,5 @@ int main(int argc, char* argv[]) if (!shared_mem) CUDA_RT_CALL(cudaFree(d_t)); return 0; -} \ No newline at end of file +} +