将 Julia 脚本嵌入到 C# 中:jl_get_function 不存在?

Embedding Julia Script into C#: jl_get_function doesn't exist?

目前我正在做一个项目,我必须在 Unity 中从 C# 调用其他人编写的一些 Julia 脚本。我一直在尝试做一些基本的例子,看看哪些有效,哪些无效。在 Julia 文档中,它说使用函数:jl_get_function 获取指向带有 julia 模块的函数的指针。但是,我在 libjulia.dll 中得到一个 EntryPointNotFound,当我在计算机上使用 DependencyWalker 打开 dll 时,我找不到调用它的函数。我是疯了还是安装了一些奇怪的东西? jl_eval_stringjl_unbox_float64 等其他东西工作正常。

此外,我不完全确定如何获取 jl_get_function 模块的指针。我想过从内存映射文件对象中获取指针,或者从 jl_eval_string(include([module name in directory])); 中获取 IntPtr,但我不确定。

这是我在 Julia 中用于此测试的代码。

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

这里是我用 C# 编写的代码,被删减了一些

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();
    }
}

}

您可以看到我在代码中尝试使用 jl_eval_string 获取指向函数的指针。这是异常前的一个例子运行:

25
1.5977136277678E-314
1.08223857600744E-314

我一直 运行 遇到各种各样的问题,我只是想知道是否有人能够帮助我。我对这个话题不是很熟悉,大约一个星期前我了解了P/Invoke

jl_get_function 是一个内联函数。您可以使用 jl_get_global.

另请注意,您的代码随时可能崩溃。所有需要在对 julia runtime/functions 的调用中使用的 jl_value_t* 都必须是根目录。请参阅嵌入文档中的内存管理部分。我不知道如何将其转换为 C#。