Skip to content

Commit 72d62ec

Browse files
authored
Fix overflow in type cast for GUPS benchmark (#58)
* 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
1 parent 52b16fa commit 72d62ec

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

posts/gups/gups.cu

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ int main(int argc, char* argv[])
390390
}
391391
}
392392

393-
size_t n_shmem = (size_t)1 << logn_shmem;
393+
size_t n_shmem = 1UL << logn_shmem;
394394

395395
int ndev;
396396
CUDA_RT_CALL(cudaGetDeviceCount(&ndev));
@@ -458,8 +458,8 @@ int main(int argc, char* argv[])
458458
prop.sharedMemPerBlockOptin));
459459
}
460460

461-
size_t n = (size_t)(1 << logn);
462-
size_t working_set = (size_t)n * 100 / occupancy;
461+
size_t n = 1UL << logn;
462+
size_t working_set = n * 100 / occupancy;
463463
if (accesses_per_elem == -1) {
464464
if (test_type == UPDATE || test_type == UPDATE_NO_LOOP
465465
|| test_type == READ_WRITE) {
@@ -596,3 +596,4 @@ int main(int argc, char* argv[])
596596
CUDA_RT_CALL(cudaFree(d_t));
597597
return 0;
598598
}
599+

0 commit comments

Comments
 (0)