Skip to content

Commit

Permalink
In allocateSlice, handle empty list of boxes (#3808)
Browse files Browse the repository at this point in the history
## Summary
In `allocateSlice` (which is called by `get_slice_data` in
`Src/Base/AMReX_MultiFabUtil.cpp`) handle the case when no intersection
is found between the slice and the box array.

## Additional background
When no intersection is found, the `boxes` array will be empty which
would cause a segfault if the `data` would be accessed. The fix is to
return `nullptr` in this case.
Also, `get_slice_data` is modified to return `nullptr` in this case as
well.

## Checklist

The proposed changes:
- [x] fix a bug or incorrect behavior in AMReX
- [ ] add new capabilities to AMReX
- [ ] changes answers in the test suite to more than roundoff level
- [ ] are likely to significantly affect the results of downstream AMReX
users
- [ ] include documentation in the code and/or rst files, if appropriate
  • Loading branch information
dpgrote committed Mar 16, 2024
1 parent a1c1525 commit 54e438a
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions Src/Base/AMReX_MultiFabUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,15 @@ namespace {
boxes.push_back(is.second);
slice_to_full_ba_map.push_back(is.first);
}
BoxArray slice_ba(boxes.data(), static_cast<int>(boxes.size()));
DistributionMapping slice_dmap(std::move(procs));

return std::make_unique<MultiFab>(slice_ba, slice_dmap, ncomp, 0,
MFInfo(), FArrayBoxFactory());
if (!boxes.empty()) {
BoxArray slice_ba(boxes.data(), static_cast<int>(boxes.size()));
DistributionMapping slice_dmap(std::move(procs));

return std::make_unique<MultiFab>(slice_ba, slice_dmap, ncomp, 0,
MFInfo(), FArrayBoxFactory());
} else {
return nullptr;
}
}
}

Expand Down Expand Up @@ -560,6 +564,10 @@ namespace amrex
Vector<int> slice_to_full_ba_map;
std::unique_ptr<MultiFab> slice = allocateSlice(dir, cc, ncomp, geom, coord, slice_to_full_ba_map);

if (!slice) {
return nullptr;
}

#ifdef AMREX_USE_OMP
#pragma omp parallel if (Gpu::notInLaunchRegion())
#endif
Expand Down

0 comments on commit 54e438a

Please sign in to comment.