Calling Julia from Go

765 views
Skip to first unread message

mrec...@gmail.com

unread,
Aug 17, 2017, 12:46:57 PM8/17/17
to golang-nuts
Hi,

I would like to implement a valuation server. For the whole server infrastructure I would like to use Go, because it feels more natural than any other language for it. For the implementation of the valuations I would like to use Julia. Thus, using the aforementioned languages where (IMHO) their strengths are. Googling turned up nothing so far except for an old post on this list suggesting that it is indeed not too easy: https://groups.google.com/d/msg/golang-nuts/Tl7IMu6UnZ4/PBqA_v4rZhEJ
Is anyone aware of an existing solution for embedding Julia into Go?

Dan Kortschak

unread,
Aug 17, 2017, 7:17:43 PM8/17/17
to mrec...@gmail.com, golang-nuts
What is it that you want to do in Julia?

Konstantin Khomoutov

unread,
Aug 18, 2017, 3:28:14 AM8/18/17
to mrec...@gmail.com, golang-nuts
I'm not familiar with Julia, but I'd offer two possible points to
consider:

* If Julia code can be compiled to a library exposing C interface
(as Go makes possible on some platforms with the compiler's option
-buildmode=c-archive), you can do just that and the call it from Go
via the standard Go's cgo facility.

* You may consider an often overlooked approach to just not embed
anything to anything else and instead employ IPC (inter-process
communication).

Say, you could possibly compile your Julia program
so that it reads data in certain agreed-upon format on its stdin
and writes the results of processing them to its stdout.

Your Go server then merely starts your Julia program and wires two
pipes to those standard I/O streams of the process it created.
Communication then becomes a matter of writing stuff to one of those
pipes and then reading the result from another one.

If your Julia server need to be multithreaded, it's possible to
use "job identifiers" with each task submitted to that Julia process;
this would allow the latter to perform the jobs in parallel and
use those job IDs in its answers which then could come out the result
pipe in any order.

Certainly the latter approach is slower than the first but it's
definitely easier to implement and to reason about, and it fully
decouples both programs.

mrec...@gmail.com

unread,
Aug 22, 2017, 4:15:01 AM8/22/17
to golang-nuts, mrec...@gmail.com
I would like to implement a pricing server. The pricings will be delivered by quants and should be changeable (scripted) without affecting the server architecture/

mrec...@gmail.com

unread,
Aug 22, 2017, 4:21:25 AM8/22/17
to golang-nuts, mrec...@gmail.com
Unfortunately, Julia cannot be compiled to a library. At least not that I am aware of. What could be done is linking/loading the Julia runtime libraries. That is what the author of the linked article tried. Here he hit some road blocks due to how Julia and Go manipulate the stack - if I understood correctly. Since, the article is a few years old it might be something to try again.
IPC is actually my fallback scenario. It would like to avoid it for performance (serialize/deserialize) reasons.Might be the easiest/fastest way to get things started though.

Jason E. Aten

unread,
Dec 10, 2017, 3:48:22 AM12/10/17
to golang-nuts
Julia ships as a set of libraries and a thin repl prompt executable that loads them.

Here is a simple example of calling Julia from Go.

Reply all
Reply to author
Forward
0 new messages