Skip to content

Commit a22c3d2

Browse files
committed
EAMxx: fix usage of std::any for restart data in atm procs
1 parent 64bd1f0 commit a22c3d2

File tree

5 files changed

+17
-18
lines changed

5 files changed

+17
-18
lines changed

components/eamxx/src/control/atmosphere_driver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -955,7 +955,7 @@ void AtmosphereDriver::restart_model ()
955955

956956
for (auto& it : m_atm_process_group->get_restart_extra_data()) {
957957
const auto& name = it.first;
958-
auto& any = it.second;
958+
auto& any = *it.second;
959959

960960
if (any.type()==typeid(int)) {
961961
std::any_cast<int&>(any) = scorpio::get_attribute<int>(filename,"GLOBAL",name);

components/eamxx/src/dynamics/homme/eamxx_homme_process_interface.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ HommeDynamics::HommeDynamics (const ekat::Comm& comm, const ekat::ParameterList&
5252
// This class needs Homme's context, so register as a user
5353
HommeContextUser::singleton().add_user();
5454

55-
m_restart_extra_data["homme_nsteps"] = std::make_any<int>(-1);
55+
m_restart_extra_data["homme_nsteps"] = std::make_shared<std::any>(std::make_any<int>(-1));
5656

5757
if (!is_parallel_inited_f90()) {
5858
// While we're here, we can init homme's parallel session
@@ -505,8 +505,7 @@ void HommeDynamics::run_impl (const double dt)
505505

506506
// Update nstep in the restart extra data, so it can be written to restart if needed.
507507
const auto& tl = c.get<Homme::TimeLevel>();
508-
auto& nstep = std::any_cast<int&>(m_restart_extra_data["homme_nsteps"]);
509-
nstep = tl.nstep;
508+
std::any_cast<int&>(*m_restart_extra_data["homme_nsteps"]) = tl.nstep;
510509

511510
// Post process Homme's output, to produce what the rest of Atm expects
512511
Kokkos::fence();
@@ -940,7 +939,7 @@ void HommeDynamics::restart_homme_state () {
940939
auto& tl = c.get<Homme::TimeLevel>();
941940

942941
// For BFB restarts, set nstep counter in Homme's TimeLevel to match the restarted value.
943-
const auto& nstep = std::any_cast<int>(m_restart_extra_data["homme_nsteps"]);
942+
const auto& nstep = std::any_cast<const int&>(*m_restart_extra_data["homme_nsteps"]);
944943
tl.nstep = nstep;
945944
set_homme_param("num_steps",nstep);
946945

@@ -1119,7 +1118,7 @@ void HommeDynamics::initialize_homme_state () {
11191118
const int n0 = tl.n0;
11201119
const int n0_qdp = tl.n0_qdp;
11211120

1122-
std::any_cast<int&>(m_restart_extra_data["homme_nsteps"]) = tl.nstep;
1121+
std::any_cast<int&>(*m_restart_extra_data["homme_nsteps"]) = tl.nstep;
11231122

11241123
const auto phis_dyn_view = m_helper_fields.at("phis_dyn").get_view<const Real***>();
11251124
const auto phi_int_view = m_helper_fields.at("phi_int_dyn").get_view<Pack*****>();

components/eamxx/src/share/atm_process/atmosphere_process.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,8 @@ class AtmosphereProcess : public std::enable_shared_from_this<AtmosphereProcess>
266266
// - these maps are: data_name -> std::any
267267
// - the data_name is unique across the whole atm
268268
// The AD will take care of ensuring these are written/read to/from restart files.
269-
const strmap_t<std::any>& get_restart_extra_data () const { return m_restart_extra_data; }
270-
strmap_t<std::any>& get_restart_extra_data () { return m_restart_extra_data; }
269+
const strmap_t<std::shared_ptr<std::any>>& get_restart_extra_data () const { return m_restart_extra_data; }
270+
strmap_t<std::shared_ptr<std::any>>& get_restart_extra_data () { return m_restart_extra_data; }
271271

272272
// Boolean that dictates whether or not the conservation checks are run for this process
273273
bool has_column_conservation_check () { return m_column_conservation_check_data.has_check; }
@@ -506,7 +506,7 @@ class AtmosphereProcess : public std::enable_shared_from_this<AtmosphereProcess>
506506
std::shared_ptr<logger_t> m_atm_logger;
507507

508508
// Extra data needed for restart
509-
strmap_t<std::any> m_restart_extra_data;
509+
strmap_t<std::shared_ptr<std::any>> m_restart_extra_data;
510510

511511
// Use at your own risk. Motivation: Free up device memory for a field that is
512512
// no longer used, such as a field read in the ICs used only to initialize

components/eamxx/src/share/io/eamxx_output_manager.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ setup (const std::shared_ptr<fm_type>& field_mgr,
296296
}
297297

298298
void OutputManager::
299-
add_global (const std::string& name, const std::any& global) {
299+
add_global (const std::string& name, const std::shared_ptr<std::any>& global) {
300300
EKAT_REQUIRE_MSG (m_globals.find(name)==m_globals.end(),
301301
"Error! Global attribute was already set in this output manager.\n"
302302
" - global att name: " + name + "\n");
@@ -540,17 +540,17 @@ void OutputManager::run(const util::TimeStamp& timestamp)
540540
// Write all stored globals
541541
for (const auto& it : m_globals) {
542542
const auto& name = it.first;
543-
const auto& any = it.second;
543+
const auto& any = *it.second;
544544
if (any.type()==typeid(int)) {
545-
set_attribute(filespecs.filename,"GLOBAL",name,std::any_cast<int>(any));
545+
set_attribute(filespecs.filename,"GLOBAL",name,std::any_cast<const int&>(any));
546546
} else if (any.type()==typeid(std::int64_t)) {
547-
set_attribute(filespecs.filename,"GLOBAL",name,std::any_cast<std::int64_t>(any));
547+
set_attribute(filespecs.filename,"GLOBAL",name,std::any_cast<const std::int64_t&>(any));
548548
} else if (any.type()==typeid(float)) {
549-
set_attribute(filespecs.filename,"GLOBAL",name,std::any_cast<float>(any));
549+
set_attribute(filespecs.filename,"GLOBAL",name,std::any_cast<const float&>(any));
550550
} else if (any.type()==typeid(double)) {
551-
set_attribute(filespecs.filename,"GLOBAL",name,std::any_cast<double>(any));
551+
set_attribute(filespecs.filename,"GLOBAL",name,std::any_cast<const double&>(any));
552552
} else if (any.type()==typeid(std::string)) {
553-
set_attribute(filespecs.filename,"GLOBAL",name,std::any_cast<std::string>(any));
553+
set_attribute(filespecs.filename,"GLOBAL",name,std::any_cast<const std::string&>(any));
554554
} else {
555555
EKAT_ERROR_MSG (
556556
"Error! Invalid concrete type for IO global.\n"

components/eamxx/src/share/io/eamxx_output_manager.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class OutputManager
6363
public:
6464
using fm_type = FieldManager;
6565
using gm_type = GridsManager;
66-
using globals_map_t = std::map<std::string,std::any>;
66+
using globals_map_t = std::map<std::string,std::shared_ptr<std::any>>;
6767

6868
// Constructor(s) & Destructor
6969
OutputManager() = default;
@@ -105,7 +105,7 @@ class OutputManager
105105
void set_logger(const std::shared_ptr<ekat::logger::LoggerBase>& atm_logger) {
106106
m_atm_logger = atm_logger;
107107
}
108-
void add_global (const std::string& name, const std::any& global);
108+
void add_global (const std::string& name, const std::shared_ptr<std::any>& global);
109109

110110
void init_timestep (const util::TimeStamp& start_of_step, const Real dt);
111111
void run (const util::TimeStamp& current_ts);

0 commit comments

Comments
 (0)