Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fuse some mfiter loops #1100

Merged
merged 1 commit into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions unit_tests/wind_energy/actuator/test_act_utils.H
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,20 @@ inline void init_field(amr_wind::Field& fld)
const auto& dx = mesh.Geom(lev).CellSizeArray();
const auto& problo = mesh.Geom(lev).ProbLoArray();

for (amrex::MFIter mfi(fld(lev)); mfi.isValid(); ++mfi) {
auto bx = mfi.growntilebox();
const auto& farr = fld(lev).array(mfi);

amrex::ParallelFor(bx, [=] AMREX_GPU_DEVICE(int i, int j, int k) {
const auto& farrs = fld(lev).arrays();
amrex::ParallelFor(
fld(lev), fld.num_grow(),
[=] AMREX_GPU_DEVICE(int nbx, int i, int j, int k) noexcept {
const amrex::Real x = problo[0] + (i + offsetx) * dx[0];
const amrex::Real y = problo[1] + (j + offsety) * dx[1];
const amrex::Real z = problo[2] + (k + offsetz) * dx[2];

for (int d = 0; d < ncomp; d++) {
farr(i, j, k, d) = x + y + z;
farrs[nbx](i, j, k, d) = x + y + z;
}
});
}
}
amrex::Gpu::synchronize();
}

} // namespace amr_wind_tests
Expand Down
10 changes: 1 addition & 9 deletions unit_tests/wind_energy/test_abl_bc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,7 @@ void init_velocity(amr_wind::Field& fld, amrex::Real vval, int dir)
fld.setVal(0.0);

for (int lev = 0; lev < nlevels; ++lev) {

for (amrex::MFIter mfi(fld(lev)); mfi.isValid(); ++mfi) {
auto bx = mfi.growntilebox();
const auto& farr = fld(lev).array(mfi);
amrex::ParallelFor(bx, [=] AMREX_GPU_DEVICE(int i, int j, int k) {
// Initialize specified dir to specified value
farr(i, j, k, dir) = vval;
});
}
fld(lev).setVal(vval, dir, 1);
}
}
} // namespace
Expand Down
69 changes: 31 additions & 38 deletions unit_tests/wind_energy/test_abl_stats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,15 @@ void init_field1(amr_wind::Field& fld)
const int nlevels = fld.repo().num_active_levels();

for (int lev = 0; lev < nlevels; ++lev) {

for (amrex::MFIter mfi(fld(lev)); mfi.isValid(); ++mfi) {
auto bx = mfi.growntilebox();
const auto& farr = fld(lev).array(mfi);

// Give TKE gradient for nonzero diff term
amrex::ParallelFor(bx, [=] AMREX_GPU_DEVICE(int i, int j, int k) {
farr(i, j, k, 0) =
const auto& farrs = fld(lev).arrays();
amrex::ParallelFor(
fld(lev), amrex::IntVect(0),
[=] AMREX_GPU_DEVICE(int nbx, int i, int j, int k) noexcept {
farrs[nbx](i, j, k) =
std::sin(0.01 * i) + std::pow(k, 0.2) + std::cos(0.01 * j);
});
}
}
amrex::Gpu::synchronize();
}

amrex::Real test_new_tke(
Expand All @@ -42,24 +39,23 @@ amrex::Real test_new_tke(
for (int lev = 0; lev < tke.repo().num_active_levels(); ++lev) {

// Form tke estimate by adding to the old tke field
for (amrex::MFIter mfi(tkeold(lev)); mfi.isValid(); ++mfi) {
const auto& bx = mfi.tilebox();
const auto& tke_old_arr = tkeold(lev).array(mfi);
const auto& buoy_prod_arr = buoy_prod(lev).const_array(mfi);
const auto& shear_prod_arr = shear_prod(lev).const_array(mfi);
const auto& dissipation_arr = dissipation(lev).const_array(mfi);
const auto& diffusion_arr = diffusion(lev).const_array(mfi);
const auto& conv_arr = conv_term(lev).const_array(mfi);

amrex::ParallelFor(
bx, [=] AMREX_GPU_DEVICE(int i, int j, int k) noexcept {
tke_old_arr(i, j, k) +=
dt *
(conv_arr(i, j, k) + shear_prod_arr(i, j, k) +
buoy_prod_arr(i, j, k) - dissipation_arr(i, j, k) +
diffusion_arr(i, j, k));
});
}
const auto& tke_old_arrs = tkeold(lev).arrays();
const auto& buoy_prod_arrs = buoy_prod(lev).const_arrays();
const auto& shear_prod_arrs = shear_prod(lev).const_arrays();
const auto& dissipation_arrs = dissipation(lev).const_arrays();
const auto& diffusion_arrs = diffusion(lev).const_arrays();
const auto& conv_arrs = conv_term(lev).const_arrays();
amrex::ParallelFor(
tkeold(lev), amrex::IntVect(0),
[=] AMREX_GPU_DEVICE(int nbx, int i, int j, int k) noexcept {
tke_old_arrs[nbx](i, j, k) +=
dt *
(conv_arrs[nbx](i, j, k) + shear_prod_arrs[nbx](i, j, k) +
buoy_prod_arrs[nbx](i, j, k) -
dissipation_arrs[nbx](i, j, k) +
diffusion_arrs[nbx](i, j, k));
});
amrex::Gpu::synchronize();

// Difference between tke estimate and tke calculated by the code
error_total += amrex::ReduceSum(
Expand All @@ -84,19 +80,16 @@ amrex::Real test_new_tke(
void remove_nans(amr_wind::Field& field)
{
for (int lev = 0; lev < field.repo().num_active_levels(); ++lev) {

// Form tke estimate by adding to the old tke field
for (amrex::MFIter mfi(field(lev)); mfi.isValid(); ++mfi) {
const auto& bx = mfi.tilebox();
const auto& field_arr = field(lev).array(mfi);
amrex::ParallelFor(
bx, [=] AMREX_GPU_DEVICE(int i, int j, int k) noexcept {
field_arr(i, j, k) = std::isnan(field_arr(i, j, k))
const auto& farrs = field(lev).arrays();
amrex::ParallelFor(
field(lev), amrex::IntVect(0), field(lev).nComp(),
[=] AMREX_GPU_DEVICE(int nbx, int i, int j, int k, int n) noexcept {
farrs[nbx](i, j, k, n) = std::isnan(farrs[nbx](i, j, k, n))
? 0.0
: field_arr(i, j, k);
});
}
: farrs[nbx](i, j, k, n);
});
}
amrex::Gpu::synchronize();
}
} // namespace

Expand Down