I am trying to call a C function from Julia which expects pointers that will be modified. That function is a pre and pro-processing one. Those pointers are modified inside another C function (hcubature, in my case). The function call from Julia works, but the values are unnaffected.
Here is an example:
val = 0.;
err = 0.;
ccall(("integrate", "/home/pedro/codes/shared.so"), Int, (Ref{Float64}, Ref{Float64}), val, err)
The C shared library:
// shared.c
///CUBATUREPATH = /usr/local/include/cubature/
//gcc -fPIC -Wall -fno-exceptions -Werror -I. -I$(CUBATUREPATH) -c shared.c -L. -lm
//shared library built with:
//gcc -shared -o shared.so shared.o $(CUBATUREPATH)hcubature.o
#include <cubature.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
int f(unsigned ndim, const double *x, void *fdata, unsigned fdim, double *fval) {
double sigma = *((double *) fdata); // we can pass Ď via fdata argument
double sum = 0;
unsigned i;
for (i = 0; i < ndim; ++i) sum += x[i] * x[i];
// compute the output value: note that fdim should == 1 from below
fval[0] = exp(-sigma * sum);
return 0; // success*
}
int integrate(double* val, double* err)
{
double xmin[3] = {-2,-2,-2}, xmax[3] = {2,2,2}, sigma = 0.5;
hcubature(1, f, &sigma, 3, xmin, xmax, 0, 0, 1e-4, ERROR_INDIVIDUAL, val, err);
return 0;
}
I tested it in C, it works:
//testing.c
//gcc -g -Wall -fno-exceptions -Werror -I. -I$(CUBATUREPATH) -o testing testing.c shared.o $(CUBATUREPATH)hcubature.o -L. -lm
#include <stdio.h>
#include <math.h>
#include <cubature.h>
#include <shared.h>
int main()
{
double *val = malloc(sizeof(double));
double *err = malloc(sizeof(double));
integrate(val, err);
printf("----------------\nhcubature test\nComputed integral = %0.10g +/- %g\n", *val, *err);
free(val);
free(err);
return 0;
}
Can anyone point me what is happening and how can I workaround? Maybe it has something to do with memory layout, I donât know.
Thank you for your time,
Pedro