Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bmi_Fortran_Adapter: Add MPI-specific constructor
Browse files Browse the repository at this point in the history
PhilMiller committed Aug 23, 2024
1 parent 56783a3 commit 23725d1
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions include/bmi/Bmi_Fortran_Adapter.hpp
Original file line number Diff line number Diff line change
@@ -11,6 +11,10 @@
#include "State_Exception.hpp"
#include "utilities/ExternalIntegrationException.hpp"

#if NGEN_WITH_MPI
#include <mpi.h>
#endif // NGEN_WITH_MPI

// Forward declaration to provide access to protected items in testing
class Bmi_Fortran_Adapter_Test;

@@ -76,6 +80,46 @@ namespace models {
}
}

#if NGEN_WITH_MPI
// Special case constructor for formulation modules that
// implement the NGen BMI MPI protocol, in which the MPI
// Communicator is set through SetValue() between
// construction and Initialize()
Bmi_Fortran_Adapter(const std::string &type_name,
std::string library_file_path,
std::string bmi_init_config,
bool has_fixed_time_step,
std::string registration_func,
MPI_Comm comm)
: AbstractCLibBmiAdapter(type_name,
library_file_path,
bmi_init_config,
has_fixed_time_step,
registration_func
)
{
try {
bmi_model = std::make_unique<Bmi_Fortran_Handle_Wrapper>(Bmi_Fortran_Handle_Wrapper());
dynamic_library_load();
execModuleRegistration();

MPI_Fint comm_fortran = MPI_Comm_c2f(comm);
inner_set_value_int("bmi_mpi_comm_handle", &comm_fortran);

int init_result = initialize(&bmi_model->handle, bmi_init_config.c_str());
if (init_result != BMI_SUCCESS) {
init_exception_msg = "Failure when attempting to initialize " + model_name;
throw models::external::State_Exception(init_exception_msg);
}
model_initialized = true;
}
catch (...) {
model_initialized = true;
throw;
}
}
#endif // NGEN_WITH_MPI

Bmi_Fortran_Adapter(Bmi_Fortran_Adapter const&) = delete;
Bmi_Fortran_Adapter(Bmi_Fortran_Adapter&&) = delete;

0 comments on commit 23725d1

Please sign in to comment.