Skip to content

Segmentation fault when calling C code using GMP initialization together with OpenMP #33223

Closed
@ederc

Description

@ederc

When calling C code from julia I figured out a segmentation fault which appears when using in the C code GMP together with OpenMP and letting the code run in parallel. If I let the code run from the C library without calling it from julia, this error does not appear. Note that the error occurs only if I set num_threads greater than 1 in the OpenMP pragma.

Here is a minimal example:

#include <gmp.h>
#include <omp.h>
void min_example()
{
    int i;
#pragma omp parallel for num_threads(4) private(i)
    for (i = 0; i < 4; ++i) {
        mpz_t x;
        mpz_init(x);
        mpz_clear(x);
    }
}

Running the C code directly no error appears, there is no memory issue, I even tried it with valgrind.
Doing now a ccall like

function test()
    lib = Libdl.dlopen(clib)
    sym = Libdl.dlsym(lib, :min_example)
    ccall(sym, Nothing, ())
end

the call of test throws a segmentation fault:

#0  0x00007ffff7b36871 in jl_gc_counted_malloc () from /usr/bin/../lib/libjulia.so.1
#1  0x00007fffe64fb008 in __gmpz_init () from /usr/bin/../lib/julia/libgmp.so

The error always appears when calling mpz_init(), also in my bigger real example where I have encountered the problem at first.
Again, if num_threads(1) is given, no such error appears, but once the value of num_threads is greater than 1 and the computation is done in parallel I run into this error. Of course, x in the above example is private to each thread.

Here is the versioninfo:

Julia Version 1.2.0
Commit c6da87ff4b (2019-08-20 00:03 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Core(TM) i7-5557U CPU @ 3.10GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.1 (ORCJIT, broadwell)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bignumsBigInt and BigFloatmultithreadingBase.Threads and related functionality

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions