GRID: avoid integer overflow (#3927)

- On higher core-count systems, the adjusted expressions can overflow.
- For example (16 ranks, 384 threads), the lower bound was negative.
- Avoid subsequent OOB access.
This commit is contained in:
Hans Pabst 2025-02-02 10:59:21 +01:00 committed by GitHub
parent aac52debd2
commit b7bcddc135
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -379,10 +379,11 @@ static void collocate_one_grid_level(
const int actual_group_size = imin(group_size, nthreads - dest_thread);
// Parallelize summation by dividing grid points across group members.
const int rank = modulo(ithread, group_size); // position within the group
const int lb = (npts_local_total * rank) / actual_group_size;
const int ub = (npts_local_total * (rank + 1)) / actual_group_size;
const int64_t lb = ((int64_t)npts_local_total * rank) / actual_group_size;
const int64_t ub =
((int64_t)npts_local_total * (rank + 1)) / actual_group_size;
if (src_thread < nthreads) {
for (int i = lb; i < ub; i++) {
for (int i = (int)lb; i < (int)ub; i++) {
task_list->threadlocals[dest_thread][i] +=
task_list->threadlocals[src_thread][i];
}
@ -391,9 +392,9 @@ static void collocate_one_grid_level(
}
// Copy final result from first thread into shared grid.
const int lb = (npts_local_total * ithread) / nthreads;
const int ub = (npts_local_total * (ithread + 1)) / nthreads;
for (int i = lb; i < ub; i++) {
const int64_t lb = ((int64_t)npts_local_total * ithread) / nthreads;
const int64_t ub = ((int64_t)npts_local_total * (ithread + 1)) / nthreads;
for (int i = (int)lb; i < (int)ub; i++) {
grid->host_buffer[i] = task_list->threadlocals[0][i];
}