Stream: helpdesk (published)

Topic: Moving data between Julia and C


view this post on Zulip Jeffrey Vasel (Oct 23 2021 at 18:52):

Hi Everyone. This is my first time using this forum....

view this post on Zulip Jeffrey Vasel (Oct 23 2021 at 18:54):

Here is my current situation:
When I ccall my C function from Julia, the C function has to do some initial work that takes a lot of time. Then it processes the data Julia sent to it.
A very short time later, Julia has some new data. Julia then ccall the C function again. The C function has to do the same initial work again causing a delay before it can process the new data Julia sent to it.
Ideally, I would like to be able to send new data from Julia to the C program without having to restart the C program with another ccall.
So, I thought perhaps I can have C "callback" a Julia function and retrieve data from the Julia function back into the C program.
What are your thoughts?

view this post on Zulip Mason Protter (Oct 23 2021 at 19:11):

Hey Jeffrey, welcome.

Have you considered just setting a flag in the C function that determines if you repeat the setup work? E.g. you could make it so that it caches the setup work somewhere, and then when Julia calls the C function, it either calls the version that does the setup work, or it calls the version that fetches the setup work from last time

view this post on Zulip Mason Protter (Oct 23 2021 at 19:11):

But I think doing mutual callbacks would probably work too

view this post on Zulip Jeffrey Vasel (Oct 23 2021 at 19:18):

Hi Mason!

The mutual callback sounds interesting. I have C sending Julia data via a callback but is it possible for Julia to send data to C via this same callback?

view this post on Zulip Mosè Giordano (Oct 24 2021 at 02:04):

https://julialang.org/blog/2013/05/callback/

view this post on Zulip Gunnar Farnebäck (Oct 24 2021 at 10:10):

I have an example of the classic mutual fibonacci recurrence in https://github.com/GunnarFarneback/DynamicallyLoadedEmbedding.jl/blob/2df94e871b4967f96fe41427fcecd529685f1fb0/c/main.c#L6-L19 and https://github.com/GunnarFarneback/DynamicallyLoadedEmbedding.jl/blob/2df94e871b4967f96fe41427fcecd529685f1fb0/src/julia_cfunctions.jl#L3-L14.

view this post on Zulip j-fu (Oct 24 2021 at 17:44):

Also see here for a refinement callback for triangulations:
https://github.com/JuliaGeometry/Triangulate.jl/blob/master/src/ctriangulateio.jl and https://github.com/JuliaGeometry/Triangulate.jl/blob/master/deps/src/triwrap.c

view this post on Zulip Jeffrey Vasel (Oct 24 2021 at 21:56):

Wow!!! Lots of good material...I need a bit more time to delve into it...I will circle back with you all soon though...thx!


Last updated: Oct 02 2023 at 04:34 UTC