Giter Site home page Giter Site logo

hyperspherestudio / jdotnet Goto Github PK

View Code? Open in Web Editor NEW
31.0 4.0 6.0 2.6 MB

Julia-.NET Interface Library. Provides Interop between the .NET ecosystem and the Julia programming language

License: MIT License

C# 94.35% HTML 1.42% Batchfile 0.05% Julia 4.18%
csharp interoperability julia net6

jdotnet's Introduction

JdotNET

JdotNET is an API designed to go between .NET and the Julia Language. It utilizes C Intefaces of both languages to allow super efficient transfers between languages (type conversion overhead is expected in using nonspecialized (Any types for instance) functions). I removed the API that allowed Julia launch & interact with .NET. It was hard to maintain and I don't think it was used much compared to the other way around. I may add it back in the future.

Static Library Generator

In Development This project generates a .NET dll that statically creates a snapshot of a julia Module and all its submodules and allows for static analysis from csharp. This library will use the most efficient techniques (caching constants, specializing functions and using cfunctions when it can) to provide for the most efficient calls to julia.

Example using Base.dll:

   using Base;
   
   Base.println("My Function");   //This function will be statically available

Dynamics

In Development Some code may be dynamically generated in julia and thus cannot be represented using the static library generator. This project will provide an abstract layer to the underlying JdotNET net-julia interface to provide more seamless usage.

   dynamic Base = JPrimitive.BaseM;
   Base.println("My Function");  //JdotNET will dynamically resolve and cache this function. It is extremely recommended to locally cache functions outside loops.

##JdotNet Julia-Net Interface#

Launching Julia

JuliaOptions options = new JuliaOptions();
options.ThreadCount = 4;
Julia.Init(options);

Evaluation

Julia.Init();
int v = (int) Julia.Eval("2 * 2");
Julia.Exit(0); //Even if your program terminates after you should call this. It runs the finalizers and stuff 

Any

The Any class represents a boxed Julia value. There is many built in default conversions from native NET types to the Any type which can be utilized via the Any constructor.

   Julia.Init();
   var v25 = new Any(5) * 5; //The second 5 will be auto converted to Any then the operator '*' will be invoked on both arguments
   
   //Arrays are passed by pointer value so there is no copying cost. Keep in mind that if julia mutates the array, it will also be affected in c#
   var myNetArray = new []{1, 2, 3, 4, 5};
   var myJuliaArray = new Any(myNetArray);
   Julia.Eval("f(x) = x .* 2").Invoke(myJuliaArray);
   //myNetArray will now be {2, 4, 6, 8, 10}
   
   //GC
   Julia.GC_PUSH(myJuliaArray); //Will not be GC'd
   Julia.GC_POP();              //Can now be GC'd
    
   //Search for a Julia Module lets say its called Main.MyModule:
   var myModule = JPrimitive.MainM.GetGlobal("MyModule");
   
   //Search for Function add! in MyModule
   var add = myModule.GetFunction("add!");
   
   //You have several choices of invoking add
   
   //The first is using Invoke (SLOWEST). This is the safest way to invoke as it provides features like exception handling
   add.Invoke(2, 2);
   
   //The second is using Unsafe Invoke. This is unsafe and should only be used in time critcal code that is stable.
   add.UnsafeInvoke(2, 2);
   
   //Native Pointer of Specialized Methods. (FASTEST) This is unsafe so you should periodically check for exceptions
    var f = Julia.Eval(@"add(a, b) = a + b");   //Gets the julia function pointer add
    
    public delegate int Add(int a, int b); /*Declared somewhere*/
    
    Add add = f.GetDelegate<Add>();             //Compile f to a sharp function pointer Add
    var aAdd = f.GetDelegate<int, int, int>();  //Compile f to a sharp anonymous function pointer. Does not require a declared delegate
    
    int answer = add(3, 4);
    int answer2 = aAdd(4, 5);
   Julia.CheckExceptions();
   
   Julia.Exit(0);

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

Author

Library Written by Johnathan Bizzano

jdotnet's People

Contributors

hyperspherestudio avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

jdotnet's Issues

Cannot run the tests

Hi,

I want to use your library but I can't run the unit tests.
First upon initilization the library couldn't find the julia bin folder. I solved this for now by simply setting _juliaDir in the Julia.cs file like this:
private static string _JuliaDir= @"C:\Users\rreij\AppData\Local\Programs\Julia-1.8.0\bin";
But there has to be a better way. Should I set an environment variable in windows?

Next the initialization halted at the init_mods() method when running the line:
JULIAdotNET = Julia.Eval("JULIAdotNET");
When I open a Juia REPL in visual code and enter using JULIAdotNET I get the error at the end of this message.

I'm using Windows 11 and Julia 1.8.0.
Because I don't have .net 5 I also changed the project to .net 6.

Here's the error code when running using JULIAdotNET :
[ Info: Precompiling JULIAdotNET [6b20c3d1-d1a1-4518-91d2-6d987d94bff0] TESt!! ERROR: LoadError: ccall method definition: argument 2 type doesn't correspond to a C type Stacktrace: [1] top-level scope @ C:\Users\rreij\.julia\packages\JULIAdotNET\EITFN\src\julia\SharpForJulia\NetCore.jl:74 [2] include(mod::Module, _path::String) @ Base .\Base.jl:419 [3] include(x::String) @ JULIAdotNET.Sharp C:\Users\rreij\.julia\packages\JULIAdotNET\EITFN\src\julia\Sharp.jl:1 [4] top-level scope @ C:\Users\rreij\.julia\packages\JULIAdotNET\EITFN\src\julia\Sharp.jl:21 [5] include(mod::Module, _path::String) @ Base .\Base.jl:419 [6] include(x::String) @ JULIAdotNET C:\Users\rreij\.julia\packages\JULIAdotNET\EITFN\src\JULIAdotNET.jl:1 [7] top-level scope @ C:\Users\rreij\.julia\packages\JULIAdotNET\EITFN\src\JULIAdotNET.jl:5 [8] include @ .\Base.jl:419 [inlined] [9] include_package_for_output(pkg::Base.PkgId, input::String, depot_path::Vector{String}, dl_load_path::Vector{String}, load_path::Vector{String}, concrete_deps::Vector{Pair{Base.PkgId, UInt64}}, source::Nothing) @ Base .\loading.jl:1554 [10] top-level scope @ stdin:1 in expression starting at C:\Users\rreij\.julia\packages\JULIAdotNET\EITFN\src\julia\SharpForJulia\NetCore.jl:1 in expression starting at C:\Users\rreij\.julia\packages\JULIAdotNET\EITFN\src\julia\Sharp.jl:1 in expression starting at C:\Users\rreij\.julia\packages\JULIAdotNET\EITFN\src\JULIAdotNET.jl:1 in expression starting at stdin:1 ERROR: Failed to precompile JULIAdotNET [6b20c3d1-d1a1-4518-91d2-6d987d94bff0] to C:\Users\rreij\.julia\compiled\v1.8\JULIAdotNET\jl_9C01.tmp. Stacktrace: [1] error(s::String) @ Base .\error.jl:35 [2] compilecache(pkg::Base.PkgId, path::String, internal_stderr::IO, internal_stdout::IO, keep_loaded_modules::Bool) @ Base .\loading.jl:1705 [3] compilecache @ .\loading.jl:1649 [inlined] [4] _require(pkg::Base.PkgId) @ Base .\loading.jl:1337 [5] _require_prelocked(uuidkey::Base.PkgId) @ Base .\loading.jl:1200 [6] macro expansion @ .\loading.jl:1180 [inlined] [7] macro expansion @ .\lock.jl:223 [inlined] [8] require(into::Module, mod::Symbol) @ Base .\loading.jl:1144 [9] eval @ .\boot.jl:368 [inlined] [10] eval @ .\Base.jl:65 [inlined] [11] repleval(m::Module, code::Expr, #unused#::String) @ VSCodeServer c:\Users\rreij\.vscode\extensions\julialang.language-julia-1.7.10\scripts\packages\VSCodeServer\src\repl.jl:222 [12] (::VSCodeServer.var"#107#109"{Module, Expr, REPL.LineEditREPL, REPL.LineEdit.Prompt})() @ VSCodeServer c:\Users\rreij\.vscode\extensions\julialang.language-julia-1.7.10\scripts\packages\VSCodeServer\src\repl.jl:186 [13] with_logstate(f::Function, logstate::Any) @ Base.CoreLogging .\logging.jl:511 [14] with_logger @ .\logging.jl:623 [inlined] [15] (::VSCodeServer.var"#106#108"{Module, Expr, REPL.LineEditREPL, REPL.LineEdit.Prompt})() @ VSCodeServer c:\Users\rreij\.vscode\extensions\julialang.language-julia-1.7.10\scripts\packages\VSCodeServer\src\repl.jl:187 [16] #invokelatest#2 @ .\essentials.jl:729 [inlined] [17] invokelatest(::Any) @ Base .\essentials.jl:726 [18] macro expansion @ c:\Users\rreij\.vscode\extensions\julialang.language-julia-1.7.10\scripts\packages\VSCodeServer\src\eval.jl:34 [inlined] [19] (::VSCodeServer.var"#61#62")() @ VSCodeServer .\task.jl:484

LoadError on NetCore.jl

The unit tests, and also doing Pkg.precompile() fail with

ERROR: LoadError: ccall method definition: argument 2 type doesn't correspond to a C type
Stacktrace:
  [1] top-level scope
    @ C:\Users\mpinxte1\.julia\packages\JULIAdotNET\7zJUz\src\julia\SharpForJulia\NetCore.jl:74
  [2] include(mod::Module, _path::String)
    @ Base .\Base.jl:419
  [3] include(x::String)
    @ JULIAdotNET.Sharp C:\Users\mpinxte1\.julia\packages\JULIAdotNET\7zJUz\src\julia\Sharp.jl:1
  [4] top-level scope
    @ C:\Users\mpinxte1\.julia\packages\JULIAdotNET\7zJUz\src\julia\Sharp.jl:21

Could this be related to JuliaLang/julia#43953?

Other package with a similar issue: JuliaData/SentinelArrays.jl#69

I'm using Julia 1.8.2

`JuliaOptions` does not seem to be implemented in v2.0.0

Hi and thanks again for your effort. I just started testing this library.

While trying to reproduce you example in the github's frontpage, I could not find how to specify JuliaOptions. Was this renamed ?
Also it seems Julia.Init can only accept an optional string argument.

https://github.com/HyperSphereStudio/JULIA.Net/blob/4d1fbe4360cae909840641bb1f7e0b5537908acb/src/csharp/Julia.cs#L49-L71

Was the example implementation valid only in 1.x.x, or should it still work ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.