Julia's Script embedding in C #: jl_get_function doesn't exist?
I am currently working on a project where I need to get some Julia scripts written by other people to be called from C # in Unity. I tried to do some basic examples to see what works and what doesn't. Julia's documentation says to use the: function jl_get_function
to grab a function pointer with the julia module. However, I get EntryPointNotFound
in libjulia.dll
, and when I open a DLL on my computer with DependencyWalker, I cannot find a function called that. Am I crazy or have I installed something strange? other things like jl_eval_string
and jl_unbox_float64
work fine.
Also, I'm not really sure how to get the module pointer for jl_get_function
. I thought about grabbing a pointer from an object with a Memory Mapped file or grabbing IntPtr
from jl_eval_string(include([module name in directory]));
, but I'm not sure.
Here is my Julie code for this test.
module TestModule
export calculate
function calculate(a::Float64,b::Float64)::Float64
return 3a+b^2
end
function calcMore(a,b)
return ones(a,b)::Array{Float64,2};
end
function moreTest(a::Float64, b::Float64)
return (a+b)::Float64;
end
end
and here's my code in C # which was cut a bit
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
namespace TestCInCSharp
{
class Program
{
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool SetDllDirectory(string lpPathName);
[DllImport("libjulia.dll", SetLastError = true)]
public static extern void jl_init(string path);
[DllImport("libjulia.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr jl_eval_string(string input);
[DllImport("libjulia.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr jl_box_float64(float value);
[DllImport("libjulia.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern double jl_unbox_float64(IntPtr value);
[DllImport("libjulia.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr jl_get_function(IntPtr func, string name);
[DllImport("libjulia.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr jl_call2(IntPtr func, IntPtr v1, IntPtr v2);
[DllImport("libjulia.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void jl_atexit_hook(int a);
static void Main(string[] args)
{
string p = @"C:\Users\schulk4\Documents\Programming\TestJuliaSim\Assets\test_julia.jl";
string julia_path = @"C:\Users\schulk4\AppData\Local\Julia-0.5.2\bin";
IntPtr module, module2;
IntPtr a, b, c;
SetDllDirectory(julia_path);
jl_init(julia_path);
p = @"C:\\Users\\schulk4\\Documents\\Programming\\TestJuliaSim\\Assets\\test_julia.jl";
p = "include(\"" + p + "\")";
module = jl_eval_string(p); //holds module pointer?
a = jl_eval_string("TestModule.calculate(3.0,4.0)");
double d = jl_unbox_float64(a);
Console.WriteLine(d);
a = jl_eval_string("TestModule.calculate");
b = jl_box_float64(3.0f);
c = jl_box_float64(4.0f);
module2 = jl_call2(a, b, c);
d = jl_unbox_float64(module2);
Console.WriteLine(d);
a = jl_eval_string("TestModule.moreTest");
b = jl_box_float64(12.0f);
c = jl_box_float64(13.0f);
module2 = jl_call2(a, b, c);
d = jl_unbox_float64(module2);
Console.WriteLine(d);
IntPtr f = jl_get_function(module, "calculate"); //EntryPointNotFoundException
jl_atexit_hook(0);
Console.ReadLine();
}
}
}
You can see my attempts to get the function pointer from jl_eval_string
in the code. This is an example to run before the exception:
25
1.5977136277678E-314
1.08223857600744E-314
I was having all sorts of problems, I'm just wondering if anyone can help me. I am not very familiar with this topic, I found out about P/Invoke
about a week ago.
source to share
jl_get_function
is a built-in function. You can use jl_get_global
.
Also note that your code may fail at any time. Anything jl_value_t*
that needs to be used during the julia runtime / functions call needs to be injected. See the section on memory management in the implementation document. I don't know how you can translate this to C #.
source to share