I know how to call c function with rust. However I'm interesting by use multithreaded library written in C and I need your help. Suppose I want to call the following function (which use OpenMP):
double parallel()
{
int const n = 1000;
double A[n][n];
double B[n][n];
double P[n][n];
int i, j, k;
double somme;
for (i = 0; i<n; i++)
{
for (j = 0; j<n; j++)
{
A[i][j] = 1.0/(i*j +1 );
B[i][j] = 1.0/(i*j +1);
}
}
#pragma omp parallel for
for (i = 0; i<n; i++)
{
for (j = 0; j<n; j++)
{
somme = 0.0;
for (k = 0; k<n; k++)
{
somme = somme + A[i][k]*B[k][j];
}
P[i][j] = somme;
}
}
return P[n-1][n-1];
}
If I remove the flag -fopenmp (or the line #pragma omp parallel for and this flag) this work. But in this situation I have a problem with the parallel features. rustc message :
parallel.c:(.text+0x505) : undefined reference to « GOMP_parallel »
C compilers automagically add OpenMP runtime libraries when you use -fopenmp flag, but Rust doesn't know you use OpenMP, or that OpenMP even exists, so it doesn't add the GOMP libraries for you.
Alternatively, you could try rewriting the C function into Rust and parallelize it using rayon. Rayon is quite fast and similar in capabilities to OpenMP. Just keep in mind that in Rust [i] can be quite slow. For C-like performance you need to use either Iterators or .get_unchecked(i).
You haven't described what doesn't work. Does the openmp-sys crate print any warnings? Try cargo build --verbose and see if correct libraries are passed to the linker.
BTW, you can use the cc crate to compile the C project in build.rs, so that you don't have to manually invoke ar, set RUSTFLAGS, etc.
openmp-sys does not require cc. It's designed to work around problems that #[link(name = "gomp")] creates, such as link errors with gcc installations that put libgomp in a private directory, as well as work around portability issues, because gomp is GCC-only.
I don't know why it's not working for you. You haven't elaborated beyond "don't work".