Skip to content

Conversation

@dmikushin
Copy link
Contributor

Summary

This PR fixes an integer overflow bug in the GUPS benchmark that prevents running with problem size n = 2^31.

Changes

  • Changed size_t n = (size_t)(1 << logn) to size_t n = ((size_t)1) << logn in posts/gups/gups.cu:461
  • The issue was that 1 << logn performs left shift on an int (1) before casting to size_t, causing overflow when logn >= 31
  • With the fix, the cast to size_t happens first, allowing proper handling of large values

Testing

This fix is required to benchmark size n = 2^31 as mentioned in issue #57.

Fixes #57

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 NVIDIA-developer-blog#57
CUDA_RT_CALL(cudaFree(d_t));
return 0;
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 suggestion: ‏Add a newline to the end of the file.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix the EOF, then we can merge.

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.
- 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
@dmikushin
Copy link
Contributor Author

@harrism Hm, this is a tricky one 😅 The newline behavior is set by my git somewhere internally. Does it look fine for you if I add a newline manually?

@harrism
Copy link
Member

harrism commented Sep 18, 2025

You fixed it.

@harrism harrism merged commit 72d62ec into NVIDIA-developer-blog:master Sep 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GUPS: Fix overflow in type cast

2 participants