Skip to content

Commit ab0e899

Browse files
committed
Replace arrays of ColumnView with a 2D view. Use template parameters instead of raw pointers. Replace the single loop with two nested parallel loops.
1 parent 34c4d5c commit ab0e899

File tree

7 files changed

+33
-33
lines changed

7 files changed

+33
-33
lines changed

src/mam4xx/gas_chem.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,15 @@ void imp_slv_inti(Real epsilon[clscnt4]) {
7777
epsilon[i] = rel_err;
7878
}
7979
}
80-
80+
template <typename VectorType>
8181
KOKKOS_INLINE_FUNCTION
8282
void newton_raphson_iter(const Real dti, const Real lin_jac[nzcnt],
8383
const Real lrxt[rxntot],
8484
const Real lhet[gas_pcnst], // in
8585
const Real iter_invariant[clscnt4], // in
8686
const bool factor[itermax],
8787
const int permute_4[gas_pcnst],
88-
const int clsmap_4[gas_pcnst], Real lsol[gas_pcnst],
88+
const int clsmap_4[gas_pcnst], VectorType& lsol,
8989
Real solution[clscnt4], // inout
9090
bool converged[clscnt4], bool &convergence, // out
9191
Real prod[clscnt4], Real loss[clscnt4],
@@ -239,9 +239,9 @@ void newton_raphson_iter(const Real dti, const Real lin_jac[nzcnt],
239239
} // end if (nr_iter > 0)
240240
} // end nr_iter loop
241241
} // newton_raphson_iter() function
242-
242+
template <typename VectorType>
243243
KOKKOS_INLINE_FUNCTION
244-
void imp_sol(Real base_sol[gas_pcnst], // inout - species mixing ratios [vmr]
244+
void imp_sol(VectorType& base_sol, // inout - species mixing ratios [vmr]
245245
const Real reaction_rates[rxntot], const Real het_rates[gas_pcnst],
246246
const Real extfrc[extcnt], const Real &delt,
247247
const int permute_4[gas_pcnst], const int clsmap_4[gas_pcnst],

src/mam4xx/gas_chem_mechanism.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,9 @@ void adjrxt(Real rate[rxntot], const Real inv[nfs], const Real m) {
8484
// TODO: the lines of concern *kind of* bear resemblance to the similarly
8585
// concerning lines in linmat(), though it's difficult to tell if that results
8686
// in consistent units
87+
template <typename VectorType>
8788
KOKKOS_INLINE_FUNCTION
88-
void imp_prod_loss(Real prod[clscnt4], Real loss[clscnt4], Real y[gas_pcnst],
89+
void imp_prod_loss(Real prod[clscnt4], Real loss[clscnt4], VectorType& y,
8990
const Real rxt[rxntot], const Real het_rates[gas_pcnst]) {
9091
const Real zero = 0;
9192
loss[0] = (+het_rates[1] + rxt[0] + rxt[2]) * (+y[1]);

src/mam4xx/gas_phase_chemistry.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ using mam4::gas_chemistry::indexm;
3333

3434
// performs gas phase chemistry calculations on a single level of a single
3535
// atmospheric column
36+
template <typename VectorType>
3637
KOKKOS_INLINE_FUNCTION
3738
void gas_phase_chemistry(
3839
// in
@@ -41,7 +42,7 @@ void gas_phase_chemistry(
4142
const Real invariants[nfs], const int (&clsmap_4)[gas_pcnst],
4243
const int (&permute_4)[gas_pcnst], const Real het_rates[gas_pcnst],
4344
// out
44-
Real (&qq)[gas_pcnst]) {
45+
VectorType &qq) {
4546
//=====================================================================
4647
// ... set rates for "tabular" and user specified reactions
4748
//=====================================================================

src/mam4xx/mam4_amicphys.hpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2113,15 +2113,15 @@ void get_gcm_tend_diags_from_subareas(
21132113

21142114
//--------------------------------------------------------------------------------
21152115
//--------------------------------------------------------------------------------
2116-
2116+
template<typename VectorType, typename VectorTypeModes>
21172117
KOKKOS_INLINE_FUNCTION
21182118
void modal_aero_amicphys_intr(
21192119
// in
21202120
const AmicPhysConfig &config, const Real deltat, const Real temp,
21212121
const Real pmid, const Real pdel, const Real zm, const Real pblh,
21222122
const Real qv, const Real cld,
21232123
// in/out
2124-
Real (&qq)[gas_pcnst], Real (&qqcw)[gas_pcnst],
2124+
VectorType &qq, VectorType &qqcw,
21252125
// Diagnostics (out)
21262126
const int kk, // level info needed for diagnistics output
21272127
const View2D &gas_aero_exchange_condensation,
@@ -2130,11 +2130,12 @@ void modal_aero_amicphys_intr(
21302130
const View2D &gas_aero_exchange_coagulation,
21312131
const View2D &gas_aero_exchange_renaming_cloud_borne,
21322132
// in
2133-
const Real (&q_pregaschem)[gas_pcnst],
2134-
const Real (&q_precldchem)[gas_pcnst],
2135-
const Real (&qqcw_precldchem)[gas_pcnst], const Real (&dgncur_a)[num_modes],
2136-
const Real (&dgncur_awet)[num_modes],
2137-
const Real (&wetdens_host)[num_modes]) {
2133+
const VectorType& q_pregaschem,
2134+
const VectorType& q_precldchem,
2135+
const VectorType& qqcw_precldchem,
2136+
const VectorTypeModes& dgncur_a,
2137+
const VectorTypeModes& dgncur_awet,
2138+
const VectorTypeModes& wetdens_host) {
21382139
// deltat: time step
21392140
// qq(ncol,pver,pcnst): current tracer mixing ratios (TMRs)
21402141
// these values are updated (so out /= in)

src/mam4xx/mo_gas_phase_chemdr.hpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ void mmr2vmr_col(const ThreadTeam &team, const haero::Atmosphere &atm,
3434
const mam4::Prognostics &progs,
3535
const Real adv_mass_kg_per_moles[gas_pcnst],
3636
const int offset_aerosol,
37-
const ColumnView vmr_col[gas_pcnst]) {
37+
const View2D vmr_col) {
3838
// Make a local copy of nlev to avoid the identifier "mam4::nlev" being
3939
// undefined in device code.
4040
constexpr int nlev_local = nlev;
@@ -50,7 +50,7 @@ void mmr2vmr_col(const ThreadTeam &team, const haero::Atmosphere &atm,
5050
// output (vmr)
5151
mam4::microphysics::mmr2vmr(qq, adv_mass_kg_per_moles, vmr);
5252
for (int i = 0; i < gas_pcnst; ++i) {
53-
vmr_col[i](kk) = vmr[i];
53+
vmr_col(kk,i) = vmr[i];
5454
}
5555
});
5656
}
@@ -204,11 +204,7 @@ void perform_atmospheric_chemistry_and_microphysics(
204204
work_set_het_ptr += nlev * gas_pcnst;
205205

206206
// vmr0 stores mixing ratios before chemistry changes the mixing
207-
ColumnView vmr_col[gas_pcnst];
208-
for (int i = 0; i < gas_pcnst; ++i) {
209-
vmr_col[i] = ColumnView(work_set_het_ptr, nlev);
210-
work_set_het_ptr += nlev;
211-
}
207+
const auto vmr_col = View2D(work_set_het_ptr, nlev, gas_pcnst);
212208
const int sethet_work_len = mam4::mo_sethet::get_work_len_sethet();
213209
const auto work_sethet_call = View1D(work_set_het_ptr, sethet_work_len);
214210
work_set_het_ptr += sethet_work_len;

src/mam4xx/mo_photo.hpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -173,16 +173,17 @@ void set_ub_col(Real &o3_col_delta,
173173
KOKKOS_INLINE_FUNCTION
174174
void setcol(const ThreadTeam &team, const Real o3_col_deltas[mam4::nlev + 1],
175175
ColumnView &o3_col_dens) {
176-
// we can probably accelerate this with a parallel_scan, but let's just do
177-
// a simple loop for now
178176
constexpr int nlev = mam4::nlev;
179-
Kokkos::single(Kokkos::PerTeam(team), [=]() {
180-
o3_col_dens(0) = 0.5 * (o3_col_deltas[0] + o3_col_deltas[1]);
181-
for (int k = 1; k < nlev; ++k) {
182-
o3_col_dens(k) =
183-
o3_col_dens(k - 1) + 0.5 * (o3_col_deltas[k] + o3_col_deltas[k + 1]);
184-
}
185-
});
177+
Kokkos::parallel_for(
178+
Kokkos::TeamThreadRange(team, nlev),
179+
[&](int kk) {
180+
Kokkos::parallel_reduce(
181+
Kokkos::ThreadVectorRange(team, kk+1),
182+
[&](int i, Real &lsum) {
183+
lsum += 0.5 * (o3_col_deltas[i] + o3_col_deltas[i+1]);
184+
},
185+
o3_col_dens(kk));
186+
});
186187
}
187188

188189
KOKKOS_INLINE_FUNCTION

src/mam4xx/mo_sethet.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ void sethet_detail(
215215
const ConstColumnView &nevapr, // evaporation [kg/kg/s] //in
216216
const Real delt, // time step [s] //in
217217
const View2D &invariants, // total atms density [cm^-3] //in
218-
const ColumnView qin[gas_pcnst], // xported species [vmr] //in
218+
const View2D qin, // xported species [vmr] //in
219219
// working variables
220220
const ColumnView
221221
&t_factor, // temperature factor to calculate henry's law parameters
@@ -330,8 +330,8 @@ void sethet_detail(
330330
rain(kk) = mass_air * precip(kk) * invariants(kk, indexm) / mass_h2o;
331331
xliq(kk) =
332332
precip(kk) * delt * invariants(kk, indexm) / avo * mass_air * m3_2_cm3;
333-
xh2o2(kk) = qin[spc_h2o2_ndx](kk) * invariants(kk, indexm);
334-
xso2(kk) = qin[spc_so2_ndx](kk) * invariants(kk, indexm);
333+
xh2o2(kk) = qin(kk,spc_h2o2_ndx) * invariants(kk, indexm);
334+
xso2(kk) = qin(kk,spc_so2_ndx) * invariants(kk, indexm);
335335
});
336336
zsurf = m2km * phis * rga;
337337

@@ -501,7 +501,7 @@ void sethet(
501501
const ConstColumnView &nevapr, // evaporation [kg/kg/s] //in
502502
const Real dt, // time step [s] //in
503503
const View2D &invariants, //
504-
const ColumnView vmr[gas_pcnst], // xported species [vmr] //in
504+
const View2D& vmr, // xported species [vmr] //in
505505
// working variables
506506
const View1D &work) {
507507

0 commit comments

Comments
 (0)