Skip to content

Commit

Permalink
Trying to call Julia from C
Browse files Browse the repository at this point in the history
  • Loading branch information
sverhoeven committed Oct 17, 2023
1 parent daeca16 commit 575b0f9
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 0 deletions.
25 changes: 25 additions & 0 deletions test/heat-images/c-julia/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
cmake_minimum_required(VERSION 3.0)
project(run_bmi_server C CXX)

set(_cflags "-ansi -Wall -Wundef -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -O2")
find_package(PkgConfig REQUIRED)
pkg_check_modules(BMIC REQUIRED IMPORTED_TARGET bmic)
message("-- bmic include - ${BMIC_INCLUDE_DIRS}")
include_directories(${BMIC_INCLUDE_DIRS})
pkg_check_modules(GRPC4BMI REQUIRED grpc4bmi)
# See https://github.com/barche/embedding-julia

find_package(Julia REQUIRED)
add_definitions(-DJULIA_ENABLE_THREADING)

add_executable(run_bmi_server run_bmi_server.cc)

target_include_directories(run_bmi_server PRIVATE "$<BUILD_INTERFACE:${Julia_INCLUDE_DIRS}>")

target_link_libraries(run_bmi_server
${GRPC4BMI_LINK_LIBRARIES}
$<BUILD_INTERFACE:${Julia_LIBRARY}>
)

install(TARGETS run_bmi_server
RUNTIME DESTINATION bin)
80 changes: 80 additions & 0 deletions test/heat-images/c-julia/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Build with
#
# docker build -t heat:c-julia -f test/heat-images/c-julia/Dockerfile .
#
# Run with
#
# ipython
# from grpc4bmi.bmi_client_docker import BmiClientDocker
# model = BmiClientDocker('heat:c-julia', work_dir='/tmp', delay=1)
# model.get_component_name()
# model.get_output_var_names()
# from pathlib import Path
# config = Path('/tmp/heat.config.txt')
# config.write_text('1.5, 8.0, 7, 6')
# model.initialize(str(config))
# model.update()
# import numpy as np
# model.get_value('plate_surface__temperature', np.zeros(42))
#
FROM julia:bullseye AS builder

# Install build deps
RUN apt-get update && apt-get install -qy git build-essential cmake autoconf libtool pkg-config libssl-dev

# Compile gRPC
WORKDIR /opt/grpc
ARG GRPC_VERSION=v1.51.1
RUN echo ${GRPC_VERSION}
RUN git clone -b ${GRPC_VERSION} --depth 1 https://github.com/grpc/grpc . && git submodule update --init
WORKDIR /opt/grpc/cmake/build
RUN cmake -DgRPC_INSTALL=ON -DgRPC_SSL_PROVIDER=package -DgRPC_BUILD_TESTS=OFF -DBUILD_SHARED_LIBS=ON ../.. \
&& make -j6 && make install && ldconfig

# Install bmi-cxx
ARG BMICXX_VERSION=v2.0
RUN git clone -b ${BMICXX_VERSION} https://github.com/csdms/bmi-cxx.git /opt/bmi-cxx
WORKDIR /opt/bmi-cxx/build
RUN cmake .. && make install

# Install bmi-c
ARG BMIC_VERSION=v2.1
RUN git clone -b ${BMIC_VERSION} https://github.com/csdms/bmi-c.git /opt/bmi-c
WORKDIR /opt/bmi-c/build
RUN cmake .. && make install

# Install heat-julia
ARG HEAT_VERSION=80c34b4f2217599e600fe9372b1bae50e1229edf
RUN git clone -b ${HEAT_VERSION} https://github.com/csdms/bmi-example-julia.git /opt/bmi-example-julia
WORKDIR /opt/bmi-example-julia/build
RUN cmake .. && make install && ldconfig

# Install grpc4bmi, use commit c5cc9b9bf33b6043e8db242a07c6fb92b9c63f66
# head of bmi2 branch, PR https://github.com/eWaterCycle/grpc4bmi/pull/124
ARG GRPC4BMI_VERSION=c5cc9b9bf33b6043e8db242a07c6fb92b9c63f66
RUN git clone https://github.com/eWaterCycle/grpc4bmi /opt/grpc4bmi \
&& cd /opt/grpc4bmi && git checkout ${GRPC4BMI_VERSION}
WORKDIR /opt/grpc4bmi/cpp/build
RUN cmake .. && make install

# Compile main
WORKDIR /opt/grpc4bmiheatc-julia/build
COPY test/heat-images/c-julia/run_bmi_server.cc /opt/grpc4bmiheatc-julia-julia
COPY test/heat-images/c-julia/CMakeLists.txt /opt/grpc4bmiheatc-julia
RUN cmake .. && make install

# run container
FROM julia:bullseye

# Install runtime deps
RUN apt-get update && apt-get install -qy libssl1.1 && rm -rf /var/lib/apt/lists/*

# Copy compiled and deps
COPY --from=builder /usr/local/bin/run_bmi_server /usr/local/bin/run_bmi_server
COPY --from=builder /usr/local/lib/ /usr/local/lib/

RUN ldconfig

ENV BMI_PORT=50051

ENTRYPOINT ["/usr/local/bin/run_bmi_server"]
23 changes: 23 additions & 0 deletions test/heat-images/c-julia/run_bmi_server.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// #include "bmi_grpc_server.h"
// #include <bmi.h>
// #include "bmi_heat.h"
#include <julia.h>

int main(int argc, char* argv[])
{
jl_init();
{
{
// Simple running Julia code

jl_eval_string("println(sqrt(2.0))");
}

// Bmi *model = (Bmi *) malloc(sizeof(Bmi));

// run_bmi_server(model, argc, argv);
}
int ret = 0;
jl_atexit_hook(ret);
return ret;
}

0 comments on commit 575b0f9

Please sign in to comment.