Skip to content

Commit

Permalink
Moving SPIO I/O type utils inside scorpio nspace
Browse files Browse the repository at this point in the history
Moving SCORPIO I/O type utils inside the SCORPIO namespace. Also
adding a new enum for all supported SCORPIO I/O types.
  • Loading branch information
jayeshkrishna committed Mar 12, 2024
1 parent 9a17bda commit 86181df
Show file tree
Hide file tree
Showing 17 changed files with 68 additions and 57 deletions.
4 changes: 2 additions & 2 deletions components/eamxx/src/control/intensive_observation_period.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void read_dimensionless_variable_from_file(const std::string& filename,
int ncid, varid, err1, err2;
bool was_open = scorpio::is_file_open_c2f(filename.c_str(),-1);
if (not was_open) {
scorpio::register_file(filename,scorpio::FileMode::Read,0);
scorpio::register_file(filename,scorpio::FileMode::Read,scorpio::IOType::DefaultIOType);
}
ncid = scorpio::get_file_ncid_c2f (filename.c_str());
err1 = PIOc_inq_varid(ncid,varname.c_str(),&varid);
Expand Down Expand Up @@ -87,7 +87,7 @@ void read_variable_from_file(const std::string& filename,
}

// Read into data
scorpio::register_file(filename, scorpio::FileMode::Read,0);
scorpio::register_file(filename, scorpio::FileMode::Read,scorpio::IOType::DefaultIOType);
std::string io_decomp_tag = varname+","+filename;
scorpio::register_variable(filename, varname, varname, dimnames, vartype, io_decomp_tag);
std::vector<scorpio::offset_t> dof_offsets(data_size);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ TEST_CASE("create_map_file")
// Existing dofs are "copied", added dofs are averaged from neighbors
const int nnz = ngdofs_src + 2*(ngdofs_src-1);

scorpio::register_file(filename, scorpio::FileMode::Write);
scorpio::register_file(filename, scorpio::FileMode::Write, scorpio::IOType::DefaultIOType);

scorpio::register_dimension(filename, "n_a", "n_a", ngdofs_src, false);
scorpio::register_dimension(filename, "n_b", "n_b", ngdofs_tgt, false);
Expand Down
2 changes: 1 addition & 1 deletion components/eamxx/src/physics/spa/spa_functions_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ create_horiz_remapper (
{
using namespace ShortFieldTagsNames;

scorpio::register_file(spa_data_file,scorpio::Read,0);
scorpio::register_file(spa_data_file,scorpio::Read,scorpio::DefaultIOType);
const int nlevs_data = scorpio::get_dimlen(spa_data_file,"lev");
const int ncols_data = scorpio::get_dimlen(spa_data_file,"ncol");
const int nswbands = scorpio::get_dimlen(spa_data_file,"swband");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ get_my_triplets (const std::string& map_file) const
using namespace ShortFieldTagsNames;

// 1. Load the map file chunking it evenly across all ranks
scorpio::register_file(map_file,scorpio::FileMode::Read,0);
scorpio::register_file(map_file,scorpio::FileMode::Read,scorpio::IOType::DefaultIOType);

// 1.1 Create a "helper" grid, with as many dofs as the number
// of triplets in the map file, and divided linearly across ranks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ VerticalRemapper (const grid_ptr_type& src_grid,
// remapping the target field will be defined on the same DOFs
// as the source field, but will have a different number of
// vertical levels.
scorpio::register_file(map_file,scorpio::FileMode::Read,0);
scorpio::register_file(map_file,scorpio::FileMode::Read,scorpio::IOType::DefaultIOType);
m_num_remap_levs = scorpio::get_dimlen(map_file,"lev");

auto tgt_grid = src_grid->clone("vertical_remap_tgt_grid",true);
Expand Down
4 changes: 2 additions & 2 deletions components/eamxx/src/share/io/scorpio_input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,10 +350,10 @@ void AtmosphereInput::finalize()
/* ---------------------------------------------------------- */
void AtmosphereInput::init_scorpio_structures()
{
int iotype = str2iotype("default");
int iotype = scorpio::str2iotype("default");
if(m_params.isParameter("iotype")){
std::string iotype_str = m_params.get<std::string>("iotype");
iotype = str2iotype(iotype_str);
iotype = scorpio::str2iotype(iotype_str);
}

scorpio::register_file(m_filename,scorpio::Read,iotype);
Expand Down
34 changes: 0 additions & 34 deletions components/eamxx/src/share/io/scream_io_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,40 +59,6 @@ inline OutputAvgType str2avg (const std::string& s) {
return OAT::Invalid;
}

inline int str2iotype(const std::string &str)
{
if(str == "default"){
return 0;
}
else if(str == "netcdf"){
return 1;
}
else if(str == "pnetcdf"){
return 2;
}
else if(str == "adios"){
return 3;
}
else if(str == "hdf5"){
return 4;
}
else{
return 0;
}
}

inline std::string iotype2str(int iotype)
{
switch(iotype){
case 0: return "default";
case 1: return "netcdf";
case 2: return "pnetcdf";
case 3: return "adios";
case 4: return "hdf5";
default: return "default";
}
}

std::string find_filename_in_rpointer (
const std::string& casename,
const bool model_restart,
Expand Down
4 changes: 2 additions & 2 deletions components/eamxx/src/share/io/scream_output_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -679,8 +679,8 @@ set_params (const ekat::ParameterList& params,
if(m_params.isParameter("iotype")){
iotype = m_params.get<std::string>("iotype");
}
m_output_file_specs.iotype = str2iotype(iotype);
m_checkpoint_file_specs.iotype = str2iotype(iotype);
m_output_file_specs.iotype = scorpio::str2iotype(iotype);
m_checkpoint_file_specs.iotype = scorpio::str2iotype(iotype);
}

/*===============================================================================================*/
Expand Down
10 changes: 5 additions & 5 deletions components/eamxx/src/share/io/scream_scorpio_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ int get_dimlen(const std::string& filename, const std::string& dimname)

bool was_open = is_file_open_c2f(filename.c_str(),-1);
if (not was_open) {
register_file(filename,Read,0);
register_file(filename,Read,DefaultIOType);
}

ncid = get_file_ncid_c2f (filename.c_str());
Expand Down Expand Up @@ -154,7 +154,7 @@ bool has_dim (const std::string& filename, const std::string& dimname)

bool was_open = is_file_open_c2f(filename.c_str(),-1);
if (not was_open) {
register_file(filename,Read,0);
register_file(filename,Read,DefaultIOType);
}

ncid = get_file_ncid_c2f (filename.c_str());
Expand All @@ -181,7 +181,7 @@ bool has_variable (const std::string& filename, const std::string& varname)

bool was_open = is_file_open_c2f(filename.c_str(),-1);
if (not was_open) {
register_file(filename,Read,0);
register_file(filename,Read,DefaultIOType);
}

ncid = get_file_ncid_c2f (filename.c_str());
Expand Down Expand Up @@ -212,7 +212,7 @@ bool has_attribute (const std::string& filename, const std::string& varname, con

bool was_open = is_file_open_c2f(filename.c_str(),-1);
if (not was_open) {
register_file(filename,Read,0);
register_file(filename,Read,DefaultIOType);
}

// Get file id
Expand Down Expand Up @@ -513,7 +513,7 @@ ekat::any get_any_attribute (const std::string& filename, const std::string& att
}
/* ----------------------------------------------------------------- */
ekat::any get_any_attribute (const std::string& filename, const std::string& var_name, const std::string& att_name) {
register_file(filename,Read,0);
register_file(filename,Read,DefaultIOType);
auto ncid = get_file_ncid_c2f (filename.c_str());
EKAT_REQUIRE_MSG (ncid>=0,
"[get_any_attribute] Error! Could not retrieve file ncid.\n"
Expand Down
45 changes: 45 additions & 0 deletions components/eamxx/src/share/io/scream_scorpio_interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,51 @@ namespace scorpio {
Append = 2,
Write = 4
};

// I/O types supported
enum IOType {
// Default I/O type is used to let the code choose I/O type as needed (via CIME)
DefaultIOType = 0,
NetCDF,
PnetCDF,
Adios,
Hdf5
};

inline int str2iotype(const std::string &str)
{
if(str == "default"){
return static_cast<int>(IOType::DefaultIOType);
}
else if(str == "netcdf"){
return static_cast<int>(IOType::NetCDF);
}
else if(str == "pnetcdf"){
return static_cast<int>(IOType::PnetCDF);
}
else if(str == "adios"){
return static_cast<int>(IOType::Adios);
}
else if(str == "hdf5"){
return static_cast<int>(IOType::Hdf5);
}
else{
return static_cast<int>(IOType::DefaultIOType);
}
}

inline std::string iotype2str(int iotype)
{
switch(iotype){
case static_cast<int>(IOType::DefaultIOType): return "default";
case static_cast<int>(IOType::NetCDF): return "netcdf";
case static_cast<int>(IOType::PnetCDF): return "pnetcdf";
case static_cast<int>(IOType::Adios): return "adios";
case static_cast<int>(IOType::Hdf5): return "hdf5";
default: return "default";
}
}

/* All scorpio usage requires that the pio_subsystem is initialized. Happens only once per simulation */
void eam_init_pio_subsystem(const ekat::Comm& comm);
void eam_init_pio_subsystem(const int mpicom, const int atm_id = 0);
Expand Down
2 changes: 1 addition & 1 deletion components/eamxx/src/share/io/tests/io_remap_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ TEST_CASE("io_remap_test","io_remap_test")

// Write remap data to file
const std::string remap_filename = "remap_weights_np"+std::to_string(io_comm.size())+".nc";
scorpio::register_file(remap_filename, scorpio::FileMode::Write,0);
scorpio::register_file(remap_filename, scorpio::FileMode::Write, scorpio::IOType::DefaultIOType);

scorpio::register_dimension(remap_filename,"n_a", "n_a", ncols_src, true);
scorpio::register_dimension(remap_filename,"n_b", "n_b", ncols_tgt, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ void create_remap_file(const std::string& filename, const int ngdofs_tgt)
const int ngdofs_src = ngdofs_tgt + 1;
const int nnz = 2*ngdofs_tgt;

scorpio::register_file(filename, scorpio::FileMode::Write,0);
scorpio::register_file(filename, scorpio::FileMode::Write, scorpio::IOType::DefaultIOType);

scorpio::register_dimension(filename,"n_a", "n_a", ngdofs_src, true);
scorpio::register_dimension(filename,"n_b", "n_b", ngdofs_tgt, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ void write_map_file (const std::string& filename, const int ngdofs_src) {
// Existing dofs are "copied", added dofs are averaged from neighbors
const int nnz = ngdofs_src + 2*(ngdofs_src-1);

scorpio::register_file(filename, scorpio::FileMode::Write,0);
scorpio::register_file(filename, scorpio::FileMode::Write, scorpio::IOType::DefaultIOType);

scorpio::register_dimension(filename, "n_a", "n_a", ngdofs_src, false);
scorpio::register_dimension(filename, "n_b", "n_b", ngdofs_tgt, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ void write_map_file (const std::string& filename, const int ngdofs_src) {
// Existing dofs are "copied", added dofs are averaged from neighbors
const int nnz = ngdofs_src + 2*(ngdofs_src-1);

scorpio::register_file(filename, scorpio::FileMode::Write);
scorpio::register_file(filename, scorpio::FileMode::Write, scorpio::IOType::DefaultIOType);

scorpio::register_dimension(filename, "n_a", "n_a", ngdofs_src, false);
scorpio::register_dimension(filename, "n_b", "n_b", ngdofs_tgt, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ Real data_func(const int col, const int vec, const Real pres) {
void create_remap_file(const std::string& filename, const int nlevs, const std::vector<std::int64_t>& dofs_p, const std::vector<Real>& p_tgt)
{

scorpio::register_file(filename, scorpio::FileMode::Write,0);
scorpio::register_file(filename, scorpio::FileMode::Write, scorpio::IOType::DefaultIOType);

scorpio::register_dimension(filename,"lev","lev",nlevs, false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ void TimeInterpolation::set_file_data_triplets(const vos_type& list_of_files) {
if (ii==0) {
ts_ref = ts_file_start;
}
scorpio::register_file(filename,scorpio::Read,0);
scorpio::register_file(filename,scorpio::Read,scorpio::DefaultIOType);
const int ntime = scorpio::get_dimlen(filename,"time");
for (int tt=0; tt<ntime; tt++) {
auto time_snap = scorpio::read_time_at_index_c2f(filename.c_str(),tt+1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void create_vert_remap() {

std::string remap_filename = "vertical_remap.nc";

scorpio::register_file(remap_filename, scorpio::FileMode::Write);
scorpio::register_file(remap_filename, scorpio::FileMode::Write, scorpio::IOType::DefaultIOType);
scorpio::register_dimension(remap_filename,"lev", "lev", nlevs, false);
scorpio::register_variable(remap_filename,"p_levs","p_levs","none",{"lev"},"real","real","Real-lev");
scorpio::set_dof(remap_filename,"p_levs",dofs_levs.size(),dofs_levs.data());
Expand Down Expand Up @@ -64,7 +64,7 @@ void create_nudging_weights_ncfile(int ntimes, int ncols, int nlevs, const std::
}
}

scorpio::register_file(filename, scorpio::FileMode::Write);
scorpio::register_file(filename, scorpio::FileMode::Write, scorpio::IOType::DefaultIOType);
scorpio::register_dimension(filename,"ncol", "ncol", ncols, false);
scorpio::register_dimension(filename,"lev", "lev", nlevs, false);
scorpio::register_dimension(filename,"time","time",ntimes, false);
Expand Down

0 comments on commit 86181df

Please sign in to comment.