Skip to content

Commit

Permalink
Update to Bmi class in bmipy 2.0 (#62)
Browse files Browse the repository at this point in the history
* Update proto to Bmi class in bmipy 2.0

See https://github.com/csdms/bmi-python/blob/v2.0/bmipy/bmi.py

* Pin to v1.27.2 version of grpc

Otherwise v1.28.0rc3 is installed, which is a pre-release

* Grpc install instructions changed from make to cmake

As make was deprecated, see https://github.com/grpc/grpc/blob/master/BUILDING.md#building-with-make-on-unix-systems-deprecated

* Regenerated Python gRPC code

* Update Python client/server to updated interface

* Update tests for client and server

Also switched to unstructured grid model based on https://bmi.readthedocs.io/en/latest/model_grids.html#unstructured-grids

* Replaced https://github.com/csdms/bmi Git submodule with bmi-c and bmi-cxx ones

* Sync c++ server with proto file

* Trying to sync our c++ code to updated bmi.h and bmi.hxx

* Debugging Travis-CI build

* Fixed FindType

* undef bmi_success, bmi_failure so bmi.h and bmi.hx can both be imported

* Implement C version of some bmi functions

* Correct C wrapper header file

* Correct signatures of C wrapper implementation

* More corrections to C wrapper implementation

* Correct server implementation

* Sync C++ test code to updated interface

* Build grpc with shared libs

* Verbose make

* Update ld config now that grpc is build with shared libs

* Sync test_bmi_grpc_server.cc with new interface

* Skip notebook test when on Travis

* pytest skipif expects bool

* Fixed compile error

* Fixed failing tests due to mismatch in ctest name - switch string

* Fixed bug in input_var_names server implementation

Co-authored-by: gvdoord <[email protected]>
  • Loading branch information
sverhoeven and goord authored Mar 12, 2020
1 parent cc4e057 commit 0f5d8d4
Show file tree
Hide file tree
Showing 29 changed files with 836 additions and 673 deletions.
9 changes: 6 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "bmi"]
path = bmi
url = https://github.com/csdms/bmi.git
[submodule "cpp/bmi-cxx"]
path = cpp/bmi-cxx
url = https://github.com/csdms/bmi-cxx.git
[submodule "cpp/bmi-c"]
path = cpp/bmi-c
url = https://github.com/csdms/bmi-c.git
21 changes: 12 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ matrix:
dist: xenial
python: '3.6'
install:
- pip install -U pip
- pip install -r requirements.txt
- python setup.py install
- docker pull ewatercycle/walrus-grpc4bmi:v0.3.1
Expand Down Expand Up @@ -46,26 +47,28 @@ matrix:
before_install:
- git submodule update --init
- sudo apt-get update
- sudo apt-get install -y build-essential autoconf libtool pkg-config
- sudo apt-get install -y build-essential autoconf libtool pkg-config libunwind-dev
- |
if [[ ! -f /usr/local/bin/protoc || ! -f /usr/local/bin/grpc_cpp_plugin ]]; then
git clone -b v1.25.0 https://github.com/grpc/grpc
git clone -b v1.27.2 https://github.com/grpc/grpc
cd grpc
git submodule update --init --recursive
cd third_party/protobuf
./autogen.sh
./configure
sudo make -j4 install
cd ../..
wget -q -O cmake-linux.sh https://github.com/Kitware/CMake/releases/download/v3.16.5/cmake-3.16.5-Linux-x86_64.sh
sudo sh cmake-linux.sh -- --skip-license --prefix=/usr/local
rm cmake-linux.sh
mkdir cmake/build
cd cmake/build
/usr/local/bin/cmake ../.. -DgRPC_INSTALL=ON -DgRPC_SSL_PROVIDER=package -DgRPC_BUILD_TESTS=OFF -DBUILD_SHARED_LIBS=ON
sudo make -j4 install
sudo ldconfig
cd ${TRAVIS_BUILD_DIR}
else
echo "using cached builds of protobuf and grpc"
fi
script:
- mkdir -p ${TRAVIS_BUILD_DIR}/cpp/build && cd ${TRAVIS_BUILD_DIR}/cpp/build
- cmake ..
- make
- /usr/local/bin/cmake ..
- make VERBOSE=1
- ctest -V
cache:
directories:
Expand Down
1 change: 0 additions & 1 deletion bmi
Submodule bmi deleted from 564742
3 changes: 1 addition & 2 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ set(GRPC_SRC_FILES ${CMAKE_CURRENT_BINARY_DIR}/bmi.pb.cc ${CMAKE_CURRENT_BINARY_
set(GRPC_HDR_FILES ${CMAKE_CURRENT_BINARY_DIR}/bmi.pb.h ${CMAKE_CURRENT_BINARY_DIR}/bmi.grpc.pb.h)
link_libraries(${PROTOBUF_LIBRARY} ${GRPCPP_UNSECURE} ${GRPC} ${BMIC})
include_directories(${CMAKE_SOURCE_DIR})
include_directories(${CMAKE_SOURCE_DIR}/../bmi)
include_directories(${CMAKE_CURRENT_BINARY_DIR})
add_library (grpc4bmi SHARED ${CMAKE_SOURCE_DIR}/../bmi/bmi.h ${CMAKE_SOURCE_DIR}/../bmi/bmi.hxx bmi_cpp_extension.h bmi_cpp_extension.cc
add_library (grpc4bmi SHARED bmi-c/bmi.h bmi-cxx/bmi.hxx bmi_cpp_extension.h bmi_cpp_extension.cc
bmi_c_wrapper.h bmi_c_wrapper.cc bmi_grpc_server.cc bmi_grpc_server.h ${GRPC_SRC_FILES} ${GRPC_HDR_FILES})
set_target_properties(grpc4bmi PROPERTIES PUBLIC_HEADER "bmi_cpp_extension.h;bmi_c_wrapper.h;bmi_grpc_server.h;${GRPC_HDR_FILES}")
add_subdirectory (test)
Expand Down
1 change: 1 addition & 0 deletions cpp/bmi-c
Submodule bmi-c added at 9b5b34
1 change: 1 addition & 0 deletions cpp/bmi-cxx
Submodule bmi-cxx added at 46cec3
85 changes: 50 additions & 35 deletions cpp/bmi_c_wrapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,98 +7,110 @@ BmiCWrapper::~BmiCWrapper(){}

void checkStatus(int status)
{
if(status == BMI_FAILURE)
if(status == bmi::BMI_FAILURE)
{
throw std::runtime_error("BMI failure from C intercepted");
}
}

void BmiCWrapper::Initialize(const char* configfile)
void BmiCWrapper::Initialize(std::string configfile)
{
checkStatus(this->model->initialize(this->model->self, const_cast<char*>(configfile)));
checkStatus(this->model->initialize(this->model->self, const_cast<char*>(configfile.c_str())));
}

void BmiCWrapper::Update()
{
double curtime, timestep;
checkStatus(this->model->get_current_time(this->model->self, &curtime));
checkStatus(this->model->get_time_step(this->model->self, &timestep));
checkStatus(this->model->update(this->model->self, curtime + timestep));
checkStatus(this->model->update(this->model->self));
}

void BmiCWrapper::UpdateUntil(double time)
{
checkStatus(this->model->update(this->model->self, time));
checkStatus(this->model->update_until(this->model->self, time));
}

void BmiCWrapper::Finalize()
{
checkStatus(this->model->finalize(this->model->self));
}

void BmiCWrapper::GetComponentName(char* const dest)
std::string BmiCWrapper::GetComponentName()
{
char* dest;
checkStatus(this->model->get_component_name(this->model->self, dest));
return dest;
}

int BmiCWrapper::GetInputVarNameCount()
int BmiCWrapper::GetInputItemCount()
{
int dest;
checkStatus(this->model->get_input_item_count(this->model->self, &dest));
return dest;
}

int BmiCWrapper::GetOutputVarNameCount()
int BmiCWrapper::GetOutputItemCount()
{
int dest;
checkStatus(this->model->get_output_item_count(this->model->self, &dest));
return dest;
}

void BmiCWrapper::GetInputVarNames(char** dest)
std::vector<std::string> BmiCWrapper::GetInputVarNames()
{
char** dest;
checkStatus(this->model->get_input_var_names(this->model->self, dest));
// TODO convert dest to vector of strings
}

void BmiCWrapper::GetOutputVarNames(char** dest)
std::vector<std::string> BmiCWrapper::GetOutputVarNames()
{
char** dest;
checkStatus(this->model->get_output_var_names(this->model->self, dest));
// TODO convert dest to vector of strings
}

int BmiCWrapper::GetVarGrid(const char* name)
int BmiCWrapper::GetVarGrid(std::string name)
{
int dest;
checkStatus(this->model->get_var_grid(this->model->self, const_cast<char*>(name), &dest));
checkStatus(this->model->get_var_grid(this->model->self, const_cast<char*>(name.c_str()), &dest));
return dest;
}

void BmiCWrapper::GetVarType(const char* name, char* vtype)
std::string BmiCWrapper::GetVarType(std::string name)
{
checkStatus(this->model->get_var_type(this->model->self, const_cast<char*>(name), vtype));
char* vtype;
checkStatus(this->model->get_var_type(this->model->self, const_cast<char*>(name.c_str()), vtype));
return vtype;
}

int BmiCWrapper::GetVarItemsize(const char* name)
int BmiCWrapper::GetVarItemsize(std::string name)
{
int dest;
checkStatus(this->model->get_var_itemsize(this->model->self, const_cast<char*>(name), &dest));
checkStatus(this->model->get_var_itemsize(this->model->self, const_cast<char*>(name.c_str()), &dest));
return dest;
}

void BmiCWrapper::GetVarUnits(const char* name, char* const dest)
std::string BmiCWrapper::GetVarUnits(std::string name)
{
checkStatus(this->model->get_var_units(this->model->self, const_cast<char*>(name), dest));
char* dest;
checkStatus(this->model->get_var_units(this->model->self, const_cast<char*>(name.c_str()), dest));
return dest;
}

int BmiCWrapper::GetVarNbytes(const char* name)
int BmiCWrapper::GetVarNbytes(std::string name)
{
int dest;
checkStatus(this->model->get_var_nbytes(this->model->self, const_cast<char*>(name), &dest));
checkStatus(this->model->get_var_nbytes(this->model->self, const_cast<char*>(name.c_str()), &dest));
return dest;
}

void BmiCWrapper::GetVarLocation(const char* name, char* location)
std::string BmiCWrapper::GetVarLocation(std::string name)
{
checkStatus(this->model->get_var_location(this->model->self, const_cast<char*>(name), location));
char* location;
checkStatus(this->model->get_var_location(this->model->self, const_cast<char*>(name.c_str()), location));
return location;
}

double BmiCWrapper::GetCurrentTime()
Expand All @@ -122,9 +134,11 @@ double BmiCWrapper::GetEndTime()
return dest;
}

void BmiCWrapper::GetTimeUnits(char* dest)
std::string BmiCWrapper::GetTimeUnits()
{
char* dest;
checkStatus(this->model->get_time_units(this->model->self, dest));
return dest;
}

double BmiCWrapper::GetTimeStep()
Expand All @@ -134,32 +148,31 @@ double BmiCWrapper::GetTimeStep()
return dest;
}

void BmiCWrapper::GetValue(const char* name, void* dest)
void BmiCWrapper::GetValue(std::string name, void* dest)
{
checkStatus(this->model->get_value(this->model->self, const_cast<char*>(name), dest));
checkStatus(this->model->get_value(this->model->self, const_cast<char*>(name.c_str()), dest));
}

void* BmiCWrapper::GetValuePtr(const char* name)
void* BmiCWrapper::GetValuePtr(std::string name)
{
void* dest;
checkStatus(this->model->get_value_ptr(this->model->self, const_cast<char*>(name), &dest));
checkStatus(this->model->get_value_ptr(this->model->self, const_cast<char*>(name.c_str()), &dest));
return dest;
}

void* BmiCWrapper::GetValueAtIndices(const char* name, void* dest, int* pts, int numpts)
void BmiCWrapper::GetValueAtIndices(std::string name, void* dest, int* pts, int numpts)
{
checkStatus(this->model->get_value_at_indices(this->model->self, const_cast<char*>(name), dest, const_cast<int*>(pts), numpts));
return dest; // Is this the idea?
checkStatus(this->model->get_value_at_indices(this->model->self, const_cast<char*>(name.c_str()), dest, const_cast<int*>(pts), numpts));
}

void BmiCWrapper::SetValue(const char* name, void* src)
void BmiCWrapper::SetValue(std::string name, void* src)
{
checkStatus(this->model->set_value(this->model->self, const_cast<char*>(name), src));
checkStatus(this->model->set_value(this->model->self, const_cast<char*>(name.c_str()), src));
}

void BmiCWrapper::SetValueAtIndices(const char* name, void* values, int* pts, int numpts)
void BmiCWrapper::SetValueAtIndices(std::string name, int* pts, int numpts, void* values)
{
checkStatus(this->model->set_value_at_indices(this->model->self, const_cast<char*>(name), values, pts, numpts));
checkStatus(this->model->set_value_at_indices(this->model->self, const_cast<char*>(name.c_str()), pts, numpts, values));
}

int BmiCWrapper::GetGridSize(int id)
Expand All @@ -176,9 +189,11 @@ int BmiCWrapper::GetGridRank(int id)
return dest;
}

void BmiCWrapper::GetGridType(int id, char* dest)
std::string BmiCWrapper::GetGridType(int id)
{
char* dest;
checkStatus(this->model->get_grid_type(this->model->self, id, dest));
return dest;
}

void BmiCWrapper::GetGridShape(int id, int* dest)
Expand Down
47 changes: 26 additions & 21 deletions cpp/bmi_c_wrapper.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
#ifndef BMI_C_WRAPPER_H_INCLUDED
#define BMI_C_WRAPPER_H_INCLUDED

#include <string>
#include <vector>

#ifndef BMI_INCLUDED
#define BMI_INCLUDED
#include "bmi.h"
#include "bmi.hxx"
#include "bmi-c/bmi.h"
#undef BMI_SUCCESS
#undef BMI_FAILURE
#include "bmi-cxx/bmi.hxx"
#endif

typedef Bmi BMIModel;
Expand All @@ -16,45 +21,45 @@ class BmiCWrapper: public bmi::Bmi
~BmiCWrapper();

// Model control functions.
virtual void Initialize(const char *config_file) override;
virtual void Initialize(std::string config_file) override;
virtual void Update() override;
virtual void UpdateUntil(double time) override;
virtual void Finalize() override;

// Model information functions.
virtual void GetComponentName(char * const name) override;
virtual int GetInputVarNameCount(void) override;
virtual int GetOutputVarNameCount(void) override;
virtual void GetInputVarNames(char **names) override;
virtual void GetOutputVarNames(char **names) override;
virtual std::string GetComponentName() override;
virtual int GetInputItemCount(void) override;
virtual int GetOutputItemCount(void) override;
virtual std::vector<std::string> GetInputVarNames() override;
virtual std::vector<std::string> GetOutputVarNames() override;

// Variable information functions
virtual int GetVarGrid(const char *name) override;
virtual void GetVarType(const char *name, char *vtype) override;
virtual void GetVarUnits (const char *name, char *units) override;
virtual int GetVarItemsize(const char *name) override;
virtual int GetVarNbytes(const char *name) override;
virtual void GetVarLocation(const char *name, char *location) override;
virtual int GetVarGrid(std::string name) override;
virtual std::string GetVarType(std::string name) override;
virtual std::string GetVarUnits (std::string name) override;
virtual int GetVarItemsize(std::string name) override;
virtual int GetVarNbytes(std::string name) override;
virtual std::string GetVarLocation(std::string name) override;

virtual double GetCurrentTime(void) override;
virtual double GetStartTime(void) override;
virtual double GetEndTime(void) override;
virtual void GetTimeUnits(char *units) override;
virtual std::string GetTimeUnits() override;
virtual double GetTimeStep(void) override;

// Variable getters
virtual void GetValue(const char *name, void *dest) override;
virtual void *GetValuePtr(const char *name) override;
virtual void *GetValueAtIndices(const char *name, void *dest, int *inds, int count) override;
virtual void GetValue(std::string name, void *dest) override;
virtual void *GetValuePtr(std::string name) override;
virtual void GetValueAtIndices(std::string name, void *dest, int *inds, int count) override;

// Variable setters
virtual void SetValue(const char *name, void *values) override;
virtual void SetValueAtIndices(const char *name, void *values, int *inds, int count) override;
virtual void SetValue(std::string name, void *values) override;
virtual void SetValueAtIndices(std::string name, int *inds, int count, void *values) override;

// Grid information functions
virtual int GetGridRank(const int grid) override;
virtual int GetGridSize(const int grid) override;
virtual void GetGridType(const int grid, char *gtype) override;
virtual std::string GetGridType(const int grid) override;

virtual void GetGridShape(const int grid, int *shape) override;
virtual void GetGridSpacing(const int grid, double *spacing) override;
Expand Down
Loading

0 comments on commit 0f5d8d4

Please sign in to comment.