Skip to content

Fork safety #30

Closed
Closed

Description

@njsmith

The combination of thread pools + fork is unfortunate: it tends to lead to random freezes. Unfortunately, the best way to achieve high-level parallelism in Python is to use fork (via the multiprocessing module).

Fundamentally the problem is that if you spawn a thread pool, and then fork, then the child ends up thinking that it has a thread pool, and dispatching work to it, but there aren't actually any threads running. This doesn't end well.

When using OMP for threading, dealing with this is the responsibility of the OMP implementation, and out-of-scope for BLIS itself. But when using pthreads, this should be handled by using pthread_atfork to register a pre-fork callback that shuts down the thread pool.

The equivalent issue in OpenBLAS was fixed in OpenMathLib/OpenBLAS#343, specifically with this code (which should probably actually be called openblas_install_fork_handler...):

+void openblas_fork_handler()
+{
+  // This handler shuts down the OpenBLAS-managed PTHREAD pool when OpenBLAS is
+  // built with "make USE_OPENMP=0".
+  // Hanging can still happen when OpenBLAS is built against the libgomp
+  // implementation of OpenMP. The problem is tracked at:
+  //   http://gcc.gnu.org/bugzilla/show_bug.cgi?id=60035
+  // In the mean time build with USE_OPENMP=0 or link against another
+  // implementation of OpenMP.
+#ifndef OS_WINDOWS
+  int err;
+  err = pthread_atfork (BLASFUNC(blas_thread_shutdown), NULL, NULL);
+  if(err != 0)
+    openblas_warning(0, "OpenBLAS Warning ... cannot install fork handler. You may meet hang after fork.\n");
+#endif
+}

The attached test case can also probably be re-used with trivial tweaks to use the BLIS API instead of cblas: https://github.com/ogrisel/OpenBLAS/blob/49bd98f410369c9604031296f8ff47c5c20052bb/utest/test_fork.c

Activity

  1. devinamatthews commented on Nov 2, 2016

    @devinamatthews
    Member

    BLIS doesn't have it's own thread pool implementation at this point, so this isn't an issue. If we ever get around to implementing one we will take this into consideration, though.

  2. njsmith commented on Nov 2, 2016

    @njsmith
    Author

    Huh, interesting. Do you only support omp now, or how does that work?

    On Nov 2, 2016 11:34 AM, "Devin Matthews" [email protected] wrote:

    BLIS doesn't have it's own thread pool implementation at this point, so
    this isn't an issue. If we ever get around to implementing one we will take
    this into consideration, though.

    —
    You are receiving this because you authored the thread.
    Reply to this email directly, view it on GitHub
    #30 (comment), or mute
    the thread
    https://github.com/notifications/unsubscribe-auth/AAlOaJsf_M4wS0a9Vcf6tnyErXX-9cX3ks5q6NelgaJpZM4FYwtr
    .

  3. devinamatthews commented on Nov 2, 2016

    @devinamatthews
    Member

    We also support pthreads, but spawn new ones each time (yuck, I know).

    On 11/2/16 2:31 PM, Nathaniel J. Smith wrote:

    Huh, interesting. Do you only support omp now, or how does that work?

    On Nov 2, 2016 11:34 AM, "Devin Matthews" [email protected]
    wrote:

    BLIS doesn't have it's own thread pool implementation at this point, so
    this isn't an issue. If we ever get around to implementing one we
    will take
    this into consideration, though.

    —
    You are receiving this because you authored the thread.
    Reply to this email directly, view it on GitHub
    #30 (comment), or
    mute
    the thread

    https://github.com/notifications/unsubscribe-auth/AAlOaJsf_M4wS0a9Vcf6tnyErXX-9cX3ks5q6NelgaJpZM4FYwtr
    .

    —
    You are receiving this because you modified the open/close state.
    Reply to this email directly, view it on GitHub
    #30 (comment), or
    mute the thread
    https://github.com/notifications/unsubscribe-auth/AFAMoQ0aDTyeOJtsP-Zc_kmg0YbnBeNaks5q6OUrgaJpZM4FYwtr.

  4. jeffhammond commented on Nov 2, 2016

    @jeffhammond
    Member

    @njsmith git grep pthread_ is your friend 😉

    frame/thread/bli_thrcomm_pthreads.c:                    pthread_create( &pthreads[id], NULL, &bli_l3_thread_entry, &datas[id] );
    frame/thread/bli_thrcomm_pthreads.c:            pthread_join( pthreads[id], NULL );
    
  5. njsmith commented on Nov 2, 2016

    @njsmith
    Author

    I promise that my notebook is covered in little "njs + git grep" doodles
    with hearts around them, but in this case Devin's answer was more helpful
    :-)

    On Nov 2, 2016 12:43 PM, "Jeff Hammond" [email protected] wrote:

    @njsmith https://github.com/njsmith git grep pthread_ is your friend 😉

    frame/thread/bli_thrcomm_pthreads.c: pthread_t* pthreads = bli_malloc_intl( sizeof( pthread_t ) * n_threads );
    frame/thread/bli_thrcomm_pthreads.c: pthread_create( &pthreads[id], NULL, &bli_l3_thread_entry, &datas[id] );
    frame/thread/bli_thrcomm_pthreads.c: pthread_join( pthreads[id], NULL );

    —
    You are receiving this because you were mentioned.
    Reply to this email directly, view it on GitHub
    #30 (comment), or mute
    the thread
    https://github.com/notifications/unsubscribe-auth/AAlOaA_kOzPW8B8wmdcj-dOmevQTLuPLks5q6Of7gaJpZM4FYwtr
    .

  6. fgvanzee commented on Nov 2, 2016

    @fgvanzee
    Member

    Wait, there's a git-grep command? Where have I been?

  7. jeffhammond commented on Nov 2, 2016

    @jeffhammond
    Member

    @fgvanzee I initialize git repos in directories from tarballs for no other reason than the speedup associated with git grep.

  8. njsmith commented on Nov 2, 2016

    @njsmith
    Author

    @jeffhammond: getting off-topic, but you might want to check out ripgrep

  9. jeffhammond commented on Nov 2, 2016

    @jeffhammond
    Member

    @njsmith I think it's fine to digress on a closed ticket, because sitting four inches to the right of this box is a magically button that allows the uninterested to terminate notifications related to this thread 😃

    I read about ripgrep but I am satisfied with the performance of the default version, at least with the git prefix, since git magically elides a lot of the filesystem overhead (or something along those lines).

  10. added a commit that references this issue on Feb 25, 2022
  11. added a commit that references this issue on Apr 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions