Skip to content

Commit

Permalink
Calling sqrt in julia from c works, now try to call https://github.co…
Browse files Browse the repository at this point in the history
  • Loading branch information
sverhoeven committed Oct 17, 2023
1 parent 575b0f9 commit fe42fc7
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 13 deletions.
2 changes: 0 additions & 2 deletions grpc4bmi/run_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,6 @@ def main(argv=sys.argv[1:]):
model = build_julia(names[0], names[1])
else:
model = build_julia(names[0])
# model.initialize('t')
# print(model.get_component_name())
else:
model = build(args.name, path)

Expand Down
8 changes: 7 additions & 1 deletion test/heat-images/c-julia/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
cmake_minimum_required(VERSION 3.0)
project(run_bmi_server C CXX)

set(CMAKE_MACOSX_RPATH 1)
set(CMAKE_SKIP_BUILD_RPATH FALSE)
set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)

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

# See https://github.com/barche/embedding-julia
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR})
find_package(Julia REQUIRED)

add_definitions(-DJULIA_ENABLE_THREADING)

add_executable(run_bmi_server run_bmi_server.cc)
Expand Down
17 changes: 9 additions & 8 deletions test/heat-images/c-julia/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,6 @@ 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
Expand All @@ -57,9 +51,11 @@ RUN git clone https://github.com/eWaterCycle/grpc4bmi /opt/grpc4bmi \
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/FindJulia.cmake /opt/grpc4bmiheatc-julia
COPY test/heat-images/c-julia/run_bmi_server.cc /opt/grpc4bmiheatc-julia
COPY test/heat-images/c-julia/CMakeLists.txt /opt/grpc4bmiheatc-julia
RUN cmake .. && make install

Expand All @@ -73,7 +69,12 @@ RUN apt-get update && apt-get install -qy libssl1.1 && rm -rf /var/lib/apt/lists
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
# Install heat-julia
RUN julia -e 'using Pkg; Pkg.add(url="https://github.com/csdms/bmi-example-julia.git",rev="80c34b4f2217599e600fe9372b1bae50e1229edf")'

RUN curl -L -O /usr/local/share/bmi/heat.toml https://github.com/csdms/bmi-example-julia/raw/main/example/heat.toml

RUN echo '/usr/local/julia/lib' > /etc/ld.so.conf.d/julia.conf && ldconfig

ENV BMI_PORT=50051

Expand Down
133 changes: 133 additions & 0 deletions test/heat-images/c-julia/FindJulia.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@

if(Julia_FOUND)
return()
endif()

####################
# Julia Executable #
####################

find_program(Julia_EXECUTABLE julia DOC "Julia executable")
MESSAGE(STATUS "Julia_EXECUTABLE: ${Julia_EXECUTABLE}")

#################
# Julia Version #
#################

execute_process(
COMMAND "${Julia_EXECUTABLE}" --startup-file=no --version
OUTPUT_VARIABLE Julia_VERSION_STRING
)

string(
REGEX REPLACE ".*([0-9]+\\.[0-9]+\\.[0-9]+).*" "\\1"
Julia_VERSION_STRING "${Julia_VERSION_STRING}"
)

MESSAGE(STATUS "Julia_VERSION_STRING: ${Julia_VERSION_STRING}")

##################
# Julia Includes #
##################

if(DEFINED ENV{JULIA_INCLUDE_DIRS})
set(Julia_INCLUDE_DIRS $ENV{JULIA_INCLUDE_DIRS}
CACHE STRING "Location of Julia include files")
else()
execute_process(
COMMAND ${Julia_EXECUTABLE} --startup-file=no -E "julia_include_dir = joinpath(match(r\"(.*)(bin)\",Sys.BINDIR).captures[1],\"include\",\"julia\")\n
if !isdir(julia_include_dir) # then we're running directly from build\n
julia_base_dir_aux = splitdir(splitdir(Sys.BINDIR)[1])[1] # useful for running-from-build\n
julia_include_dir = joinpath(julia_base_dir_aux, \"usr\", \"include\" )\n
julia_include_dir *= \";\" * joinpath(julia_base_dir_aux, \"src\", \"support\" )\n
julia_include_dir *= \";\" * joinpath(julia_base_dir_aux, \"src\" )\n
end\n
julia_include_dir"
OUTPUT_VARIABLE Julia_INCLUDE_DIRS
)

string(REGEX REPLACE "\"" "" Julia_INCLUDE_DIRS "${Julia_INCLUDE_DIRS}")
string(REGEX REPLACE "\n" "" Julia_INCLUDE_DIRS "${Julia_INCLUDE_DIRS}")
set(Julia_INCLUDE_DIRS ${Julia_INCLUDE_DIRS}
CACHE PATH "Location of Julia include files")
endif()
MESSAGE(STATUS "Julia_INCLUDE_DIRS: ${Julia_INCLUDE_DIRS}")

###################
# Julia Libraries #
###################

execute_process(
COMMAND ${Julia_EXECUTABLE} --startup-file=no -E "using Libdl; abspath(dirname(Libdl.dlpath(\"libjulia\")))"
OUTPUT_VARIABLE Julia_LIBRARY_DIR
)

string(REGEX REPLACE "\"" "" Julia_LIBRARY_DIR "${Julia_LIBRARY_DIR}")
string(REGEX REPLACE "\n" "" Julia_LIBRARY_DIR "${Julia_LIBRARY_DIR}")

string(STRIP "${Julia_LIBRARY_DIR}" Julia_LIBRARY_DIR)
set(Julia_LIBRARY_DIR "${Julia_LIBRARY_DIR}"
CACHE PATH "Julia library directory")

if(WIN32)
set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES} .a)
find_library(Julia_LIBRARY
NAMES libjulia.dll.a
PATHS ${Julia_LIBRARY_DIR}//..//lib
NO_DEFAULT_PATH
)
else()
find_library(Julia_LIBRARY
NAMES julia libjulia
PATHS ${Julia_LIBRARY_DIR}
NO_DEFAULT_PATH
)
endif()

MESSAGE(STATUS "Julia_LIBRARY_DIR: ${Julia_LIBRARY_DIR}")
MESSAGE(STATUS "Julia_LIBRARY: ${Julia_LIBRARY}")

##############
# Sys.BINDIR #
##############

execute_process(
COMMAND ${Julia_EXECUTABLE} --startup-file=no -E "Sys.BINDIR"
OUTPUT_VARIABLE Sys.BINDIR
)

string(REGEX REPLACE "\"" "" Sys.BINDIR "${Sys.BINDIR}")
string(REGEX REPLACE "\n" "" Sys.BINDIR "${Sys.BINDIR}")

MESSAGE(STATUS "Sys.BINDIR: ${Sys.BINDIR}")

###################
# libLLVM version #
###################

execute_process(
COMMAND ${Julia_EXECUTABLE} --startup-file=no -E "Base.libllvm_version"
OUTPUT_VARIABLE Julia_LLVM_VERSION
)

string(REGEX REPLACE "\"" "" Julia_LLVM_VERSION "${Julia_LLVM_VERSION}")
string(REGEX REPLACE "\n" "" Julia_LLVM_VERSION "${Julia_LLVM_VERSION}")

##################################
# Check for Existence of Headers #
##################################

find_path(Julia_MAIN_HEADER julia.h HINTS ${Julia_INCLUDE_DIRS})

MESSAGE(STATUS "Julia_LLVM_VERSION: ${Julia_LLVM_VERSION}")

###########################
# FindPackage Boilerplate #
###########################

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(Julia
REQUIRED_VARS Julia_LIBRARY Julia_LIBRARY_DIR Julia_INCLUDE_DIRS Julia_MAIN_HEADER
VERSION_VAR Julia_VERSION_STRING
FAIL_MESSAGE "Julia not found"
)
18 changes: 16 additions & 2 deletions test/heat-images/c-julia/run_bmi_server.cc
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
// #include "bmi_grpc_server.h"
// #include <bmi.h>
// #include "bmi_heat.h"
#include <iostream>
#include <julia.h>

using namespace std;

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

jl_eval_string("println(sqrt(2.0))");
jl_eval_string("x = sqrt(2.0)");
jl_eval_string("print(x)");
}
{
jl_eval_string("import Heat");
}
{
jl_function_t *initialize = jl_get_function(jl_main_module, "Heat.BMI.initialize");
jl_value_t* path = jl_cstr_to_string("/usr/local/share/bmi/heat.toml");
jl_value_t *model = jl_call1(initialize, path);
jl_function_t *get_component_name = jl_get_function(jl_main_module, "Heat.BMI.get_component_name");
jl_value_t *name = jl_call1(get_component_name, model);
cout << jl_string_ptr(name) << endl;
}

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

0 comments on commit fe42fc7

Please sign in to comment.