Correct process to use julia code from C# or VB .NET

I guess your Julia package mainly does numerical computing, right? In that case, I believe that it is eaiser “to use a messaging protocol” through some communication mechanism like inter-process communication (IPC). Preferably, you may try the ZeroMQ library. You can find more discussion in this thread Running Julia from JAVA? What is crazier?. Just replace Java with C#.

In short, your Julia code may set up a server and listen for requests from the C# client, e.g., the request “svd” (along with data) may require the Julia code to perform some SVD on the given data. Then, the Julia server sends the solution back to C# client. MsgPack can be used to encode the message communicated by ZeroMQ.

2 Likes

Actually, I implemented a server-client logic using BSON.jl and Json.NET - Newtonsoft with some hacks to serialize-deserialize to the correct julia format, so you guessed right.

I leave this question open in case someone is interested in this.

PS : I also tested MsgPack, but the specification is a bit too restrictive for generic arrays. It works fine for simple arrays like vectors but other than that and it can get very tricky.

1 Like

Yes, you are correct. I posted a question regarding 2D arrays to the MessagePack-CSharp package. There is no specification for multidimenstional arrays for now. Different implementations usually have different standards.

So finally did you adopt the JSON string format for communcation between Julia and .NET? I guess you still have to define the array protocol by yourself, since C or C# follows the row major while Julia is column major.

Similarly, I think we can also transfer multi-dim arrays as a 1D array, but both the client and the server need to follow our own custom pack/unpack protocol. I will try it later and post the code to GitHub.

Actually, there is some discussion about ND array storage spec for MsgPack in https://github.com/msgpack/msgpack/pull/267, but this is still an open topic.
After discussing myself with the MsgPack-C# guys on their gitter, their point of view seems to be that they provide everything to go beyond the spec, but they wont implement such features in their package as long as the new spec is not merged (it has some drawbacks apparently).

Basically, I implemented a custom resolver / formatter that follows the Julia BSON format spec. I have to admit this was quite a bummer at first because whatever the serialization protocol used, you always get implementation specific features that you need to cope with.
JSON/BSON is very versatile, but you have to re-implement the formatter/resolver
while MsgPack is much more agnostic, but not yet ready for complex structured arrays.

In the end, I chose the solution where it was easier to adapt the code. Even though MsgPack.jl and MsgPack-CSharp are quite well, it felt easier (not much more however) to implement the required code in JSON.net because inferring the (de-)coding algorithm is easier when you don’t need to translate from binary to meaning of some property.

In my personal case, the messaging protocol is very much the tip of the iceberg. The core functionality of my package is really to

  1. receive parameters
  2. compute some complex math stuff that would be painful to implement in C# and that would have missed julia speed and ecosystem
  3. send results back to C# GUI

Overall, the heavy stuff is handled by Julia, bulky data is sent almost only once and results are reduced to their minimal. Therefore, performance / storage improvements of MsgPack, though probably crucial for some other stuff, is not that important here.

If you are interested in what I implemented, I will soon be working on the optimization / refactorization of my implementation of the C# part, which is fairly reduced to the bare minimals I need… I asked some questions on StackOverflow about potential improvements, but with little success so far:

1 Like

Regarding c# - (De)serialization of BSON data of potentially nested objects with specific structure - Stack Overflow did you want to pass a complicated structure between Julia and C#? If that is the case, I am afraid the paradigm may not be an ideal one. My personal feeling is that the ZeroMQ and MsgPack (or JSON etc.) mechanism is suitable if

  • Julia mainly does scientific computing;
  • The interaction between Julia and C# is infrequent
  • The data communicated are mainly arrays (1d or Nd) rather than complicated custom structures (like a full-fledged class in C#); otherwise, it is difficult to implement the pack/unpack operations in two different languages.

I just started working on a small project in which C# implements the UI (in WPF) and Julia provides the computing core (mainly optimization with JuMP). Your sharing helps a lot.

1 Like

Do you know this thread: Interoperability with .NET ?

It looks cool. Could you please explain the main idea under the interoperation between Julia and C#? It seems that the workhorse is jl_eval_string, but how can I pass an array to it (which may be built by the C# part via interaction with the user)?

Besides, I heard that eval in Julia is of low efficiency. Does it matter here?

Yes, actually, I started everything by reading what you did (you project doTimetable is cite above), though I did not see anything about pointers and other complex stuff…

@Shuhua In my current code, I duplicate all the custom structure definitions between the two implementations, and I create a custom JsonConverter that acts recusrively.
I will try to refactor a bit and post it here, or maybe create a github repo, I have to see…

I’ve been thinking of attempting this kind of thing for a while also (C# for front-end GUI and graphics and Julia for a back-end “computation and analytical engine”) but I’m really just starting out and nowhere close to where you have already gotten and/or oheil got with his doTimeTable application. There’s just not a lot out there on this so I applaud both of your persistence!

Just throwing this out there and not knowing too much about it myself yet, but what about leveraging the Arrow binary format to pass data back and forth? I believe the idea is it was designed exactly for this kind of passing binary data around and be an independent, language-agnostic format whereby binary Arrow data created in language A should “just work” to be read in by language B. It even looks to support streaming.

Although it looks like not every language fully supports all defined features yet according to this status page, Julia and C# do both appear to already support many of the most basic data types as well as Arrow file and stream formats.

Cheers,
Tom

Yep, that’s pretty much my conclusions too, there is nothing much out there about C# and julia. Thanks for the applause !

I personally checked Arrow myself, but currently neither the C# nor the Julia implementations support tensors so no vector or no matrix storage, which was my main concern.
I also tested MsgPack that worked fine until ND-arrays / tensors were involved.
The BSON was also a bummer at first but at least I found sources to develop my own C# formatter/resolver.

The overall message is JSON/BSON is very generic but only defines the grammar, understand only separators and basic syntax, but the specification does not consider specific objects such as tensors in a unified manner between implementations.
On the other side, Arrow or MsgPack degine very specific objects, but just do not currently support ND arrays as far as I can tell.

Is there some workaround possible for tensors/matrix, at least vectors with e.g. “List” that Julia and C# support (note “Large List” and “Fixed Size Binary” not supported by Julia, but not C#)?

At Implementation Status — Apache Arrow v9.0.0

I see Julia is the only language with full data type support for Arrow, even with one more than C++ (and @quinnj should be proud of making Julia (official) Arrow work). But (Sparse) Tensor, that neither language support is in another section “IPC Format” (where Julia still has next best support after C++, and C# least). I do see “the Python, R, Ruby and C/GLib libraries follow the C++ Arrow library.” Does that mean e.g. tensors too (for e.g. Python)? [Then for other sections after that one, Julia support drops off, I suppose all those also apply to e.g. Python.]

Often Julia prefers Julia-only code, and there was maybe a good reason for Arrow.jl. I suppose the C++ Arrow library could have been wrapper in Julia (and C#?), and still could, for as good support as for C++ and Python. Curious why wasn’t done.

I’m not sure if there’s anything to learn from e.g. this package with “The aim is to serialize only the numeric weights”, i.e. a vector, using Arrow.jl:

Hi, @BambOoxX and @tdierickx.

As inspired by the Interoperability with .NET, I have made some attempt to embed Julia inside a C# (or .NET in general) program. Though the raw C API is a little verbose, it seems feasible. I have prepared a series of brief tutorials for a beginner and for my own reference. Please check JuliaCSharp/Embedding at main · ShuhuaGao/JuliaCSharp · GitHub. Hope it helps. Any feedback is welcome. :grinning:

The plan next is to craft a .NET library that provides some wrapper classes to hide all these C API details. I have found a preliminary attempt here: GitHub - WangyuHello/JuliaSharp: C# Interop for Julia , which may serve as an initial reference.

5 Likes

Awesome! I’m looking forward to taking a closer look at these!

Interesting thread, I was just thinking about this. Especially since Microsoft is taking C# UIs cross-platform with MAUI. Also not a big fan of Julia-native UI solutions available right now. I’d like to replace a MATLAB-created GUI.

Hi @Shuhua

Perhaps we could merge some of our work together?

Here is my interop library: Interoperability with .NET - #21 by HyperSphereStudio

It provides interop from C# to Julia and Julia to C# (Julia → C# is far more primitive atm though).

I see you have some C interface backend, I have been working on the frontend as well if you want to take a look. Mind if I use some of your work for my JLArray etc?

1 Like

Hi, @HyperSphereStudio. Excellent work!

Please feel free to take the code in my repo. Regarding JLArray, maybe it is better to map a Julia array to math.net array or from the opposite side to leverage the matrix functions therein.

I would like to provide help, but currently I am working on a paper. I will join your efforts ASAP.

ps. Would you please enable the discussion panel in your repo? Previously I got some difficulty in automatic memory handling and would like to learn from you.

3 Likes

Hi, based on ur suggestion we took a first step like this way :slight_smile:

Akkugif

GitHub link : GitHub - PiResearchTech/Ayudha: Avalonia + Julia interface for cross platform Windows GUI

2 Likes

Obviously, there seems to be a growing requirement to have julia integrate with .NET !

1 Like

That is my main use case too !

1 Like

@Shuhua @HyperSphereStudio @oheil @BioTurboNick Quick poll, do you think it could be useful to ask for a repo in https://github.com/JuliaInterop so that such .NET / Julia interfaces gain more attention ?

1 Like