Hi Bob,

If you only need the digamma function, you could do this:

```
// fastbigoneheader.c, compile with `gcc -fPIC -shared fastbigoneheader.c
-o fboh.so`
#include "fastbigoneheader.h"
float fboh_digamma(float x) { return fastdigamma(x); }
```

and then in Julia:
```
fastgamma(x) = ccall( (:fboh_digamma, "fboh.so"), Float32, (Float32,), x)
```

But if you are doing this in a very tight loop there is a possibility that
the function execution could cost less than the calling overhead. So
porting this function to Julia might result in faster execution overall if
it ends up inlined.

(for other functions this may not be true, as I noticed that library uses
__m128 SIMD types in some places, which cannot yet be used from Julia).

Best,
Isaiah


On Wed, Feb 19, 2014 at 3:41 PM, Bob Quazar <[email protected]> wrote:

> Hi Ivar,
>
> Thanks for the reply. I'm interested in that particular header file
> because it contains a fast implementation of the "digamma" function. Julia
> has a built-in digamma function, but I suspect it's orders of magnitude
> slower than the one implemented in this header file---Julia's digamma is
> probably much more accurate than I need. (My program's run time is
> currently dominate by the calls to the digamma function, and there's no
> easy way to call digamma less often, such as through caching, etc. I'm
> implementing an optimization procedure for learning parameters a Bayesian
> model, using stochastic variational inference.)
>
> Indeed, I'm already using a version of Julia that I've compiled. I'm not
> sure yet whether I'll ultimately distribute my code---it would be nice to
> have the option to do so though. It sounds like it might be easier for me
> to port the fast digamma function from C to Julia, though I'm a bit
> disappointed it isn't more straightforward to call this C code from Julia.
> If there's another way, I'd love to hear it!
>
> Bob
>
>
>
> On Wednesday, February 19, 2014 11:49:50 AM UTC-8, Ivar Nesje wrote:
>>
>> I do not think you can use that C code (as is) from Julia. All the
>> functions are declared "static inline", and thus they need to be compiled
>> together with the code that calls them.
>>
>> I do not have a deep knowledge of C, linking, and shared libraries, but I
>> will attempt to give an outline on what steps you will need to google for.
>>
>>    1. Make the functions externally callable. This means that you will
>>    at least remove `static`, but likely you want to remove `inline also`. 
>> Just
>>    search for `static inline ` and replace by empty string
>>    2. Compile the c code and link it into the julia process. If you are
>>    on windows you might need to specially mark the symbols as exported. If 
>> you
>>    compile julia yourself, and do not plan to distribute the code, you might
>>    have success with just #include <fastonebigheader.h>, but ideally you
>>    should compile it as a shared library.
>>    3. Use the functions with `ccall` as normal. If you include the
>>    header in a julia, you will not need to specify a library.
>>
>> You ask a fairly open question that could actually be the basis of a
>> book. It would be easier to answer more specifically if you included a
>> paragraph about your project and why you want to use this code. If it is
>> not just curiosity about whether it is possible, you might instead get a
>> better suggestion for how to solve your problem.
>>
>> Ivar
>>
>> kl. 19:22:56 UTC+1 onsdag 19. februar 2014 skrev Bob Quazar følgende:
>>>
>>> I'm trying to call C code from Julia. The C code is just a big header
>>> file: http://fastapprox.googlecode.com/svn/trunk/fastapprox/src/
>>> fastonebigheader.h
>>> (There's no  corresponding .c file.) I've installed this header file in
>>> my "/usr/include/" directory, but according to the Julia web site, Julia
>>> only accesses C shared objects. I'm not sure what to do---wish I understood
>>> C/gcc better. I'd greatly appreciate any help.
>>>
>>> Thanks, Bob
>>>
>>

Reply via email to