Skip to content

test_post_fork stuck with openmp #4017

Closed

Description

@haampie

We're building multiple flavors of OpenBLAS 0.3.23, some of which get stuck indefinitely, for example

https://gitlab.spack.io/spack/spack/-/jobs/6650547

cpu		: POWER9, altivec supported
'make' '-j1' '-s' 'CC=/builds/spack/spack/lib/spack/env/gcc/gcc' 'FC=/builds/spack/spack/lib/spack/env/gcc/gfortran' 'MAKE_NB_JOBS=0' 'ARCH=x86_64' 'DYNAMIC_ARCH=1' 'DYNAMIC_OLDER=1' 'TARGET=GENERIC' 'USE_LOCKING=1' 'USE_OPENMP=1' 'USE_THREAD=1' 'RANLIB=ranlib' 'all'

Using GCC 11.1.0.

Looks like there have been some changes to OpenMP # threads, so the only thing that stands out to me in that test is

#ifdef USE_OPENMP
    nthreads_omp = omp_get_max_threads();
    // Run with half the max OMP threads, the max threads and twice that
    for(i = (nthreads_omp + 1) / 2; i <= nthreads_omp * 2; i *= 2) {
        omp_set_num_threads(i);
#endif

i.e. calling omp_set_num_threads with double the number of threads reported available by omp_get_max_threads.

Activity

  1. martin-frbg commented on Apr 21, 2023

    @martin-frbg
    Collaborator

    Interesting. Requests for more than omp_get_max_threads() would get (silently) clamped to that limit until recently. Not sure why that would lead to failures on power9 specifically, and sadly our Travis CI seems to have gone dead again while
    still displaying the "all good" badge. (This may have masked the problem - though I'm fairly sure I tested this change manually in the gcc compile farm)

  2. martin-frbg commented on Apr 21, 2023

    @martin-frbg
    Collaborator

    Not reproducible with gcc 10.1 on power9, but will probably have to build my own if I want a newer compiler on that machine. Are there other architectures where the openblas_utest hangs for you ?

  3. haampie commented on Apr 21, 2023

    @haampie
    ContributorAuthor

    Hm, maybe the issue is just related to the build job using more cores than requested (it builds with -j1, probably omp_get_max_threads() returns something big inside the tests). I'll close for now.

  4. haampie commented on Apr 21, 2023

    @haampie
    ContributorAuthor

    @martin-frbg sorry for wasting your time (again 😆). I think the issue had to do with a small CI job getting scheduled to a large CPU, and although the build used -j1, the tests started to use nproc / 2, nproc, and 2 * nproc threads. Potentially cgroups were used to tame the build's CPU usage.

    Now I'm running make ... all -j1 with the environment variable OMP_NUM_THREADS=1 (which feels wrong, but well) and things are looking better.

  5. thrasibule commented on May 3, 2023

    @thrasibule
    Contributor

    I'm observing this as well with an old Xeon machine with 4 cores/8threads. And some user on the arch forum as well. https://aur.archlinux.org/packages/openblas-lapack. I've bisected it to your changes here: 9f9d001
    I can pass the test if I run it individually (with ./openblas_utest fork) which is a bit puzzling. I also get the test suite to pass if I run it with OPENBLAS_NUM_THREADS=8 openblas_utest. This could potentially make sense given your changes.

  6. martin-frbg commented on May 3, 2023

    @martin-frbg
    Collaborator

    Strange - declaring 8 threads should be no different from letting openblas figure it out . "Old Xeon" probably means Haswell target so nothing exotic (and the user report seems to be from quad-core Zen2, pretty much the same setup as far as openblas is concerned)

  7. thrasibule commented on May 3, 2023

    @thrasibule
    Contributor

    I've instrumented the test as follows:

    #ifdef USE_OPENMP
        nthreads_omp = omp_get_max_threads();
        printf("\nomp_threads:%d\topenblas_threads:%d\n", nthreads_omp, openblas_get_num_threads());
        // Run with half the max OMP threads, the max threads and twice that
        for(i = (nthreads_omp + 1) / 2; i <= nthreads_omp * 2; i *= 2) {
            printf("setting omp threads to: %d\n", i);
            omp_set_num_threads(i);
    #endif
    
            printf("omp_threads:%d\topenblas_threads:%d\n", nthreads_omp, openblas_get_num_threads());
            fork_pid = fork();
            if (fork_pid == -1) {
                CTEST_ERR("Failed to fork process.");
            } else if (fork_pid == 0) {
                // Just pretend to do something, e.g. call `uname`, then exit
                exit(0);
            } else {
                // Wait for the child to finish and check the exit code.
                int child_status = 0;
                pid_t wait_pid = wait(&child_status);
                ASSERT_EQUAL(wait_pid, fork_pid);
                ASSERT_EQUAL(0, WEXITSTATUS (child_status));
                // Now OpenBLAS has to work
                printf("after fork\n");
                printf("omp_threads:%d\topenblas_threads:%d\n", nthreads_omp, openblas_get_num_threads());
                check_dgemm(a, b, d, c, n);
                printf("after gemm\n");
                printf("omp_threads:%d\topenblas_threads:%d\n", nthreads_omp, openblas_get_num_threads());
    
            }
    

    This is how it looks before 9f9d001:

    TEST 37/37 fork:safety_after_fork_in_parent 
    omp_threads:8	openblas_threads:8
    setting omp threads to: 4
    omp_threads:8	openblas_threads:8
    after fork
    omp_threads:8	openblas_threads:8
    after gemm
    omp_threads:8	openblas_threads:4
    setting omp threads to: 8
    omp_threads:8	openblas_threads:4
    after fork
    omp_threads:8	openblas_threads:4
    after gemm
    omp_threads:8	openblas_threads:8
    setting omp threads to: 16
    omp_threads:8	openblas_threads:8
    after fork
    omp_threads:8	openblas_threads:8
    after gemm
    omp_threads:8	openblas_threads:16
    [OK]
    

    and after:

    TEST 1/1 fork:safety_after_fork_in_parent 
    omp_threads:8	openblas_threads:16
    setting omp threads to: 4
    omp_threads:8	openblas_threads:16
    after fork
    omp_threads:8	openblas_threads:16
    after gemm
    omp_threads:8	openblas_threads:4
    setting omp threads to: 8
    omp_threads:8	openblas_threads:4
    after fork
    omp_threads:8	openblas_threads:4
    after gemm
    omp_threads:8	openblas_threads:4
    setting omp threads to: 16
    omp_threads:8	openblas_threads:4
    after fork
    omp_threads:8	openblas_threads:4
    after gemm
    omp_threads:8	openblas_threads:4
    [OK]
    

    or if I try to run the whole test suite:

    omp_threads:8	openblas_threads:8
    setting omp threads to: 4
    omp_threads:8	openblas_threads:8
    after fork
    omp_threads:8	openblas_threads:8
    
    and then it gets stuck.
    
    So even when I can make it run, I think the behavior changed since setting omp num threads will permanently set openblas threads to a low number, which I'm not sure is a desired feature.
    
    I'm still trying to understand why would the test get stuck.
    
  8. martin-frbg commented on May 3, 2023

    @martin-frbg
    Collaborator

    This might be two separate issues - openblas thread count no longer getting unconditionally initialized to the omp num threads (note both "old" and "new" behavior have the omp num threads permanently affecting openblas thread count); and a race condition somewhere that may have been aggravated by the changed thread ratio.

  9. orlitzky commented on Jul 17, 2023

    @orlitzky

    Another data point: this hits an assertion with clang-16: llvm/llvm-project#63908

  10. martin-frbg commented on Jul 19, 2023

    @martin-frbg
    Collaborator

    I still cannot reproduce the hang, I can let my 6-core laptop run openblas_utest in a loop for hours, and I can also run it repeatedly on a 12-core Xeon in the gcc compile farm (though ofc I won't let it loop there for hours).
    The clang assertion appears to be triggered by running out of shared memory segments - there is some fallback code in the LLVM OpenMP that would try to use a temporary file instead, but the assert has been placed right before that.

  11. thrasibule commented on Jul 19, 2023

    @thrasibule
    Contributor

    The bug doesn't happen if I add openblas_set_num_threads(-1) here: https://github.com/xianyi/OpenBLAS/blob/develop/utest/test_post_fork.c#L102 (ie reset blas_num_threads_set to 0).
    I can consistently reproduce on a machine with 4 cores/8 threads.

  12. psimovec commented on Jul 20, 2023

    @psimovec

    I have most likely the same issue that test_post_fork gets stuck when trying to build new version 0.3.23 for Fedora, but only on s390x and ppl64le architectures. Works fine for x86_64, i386 and aarch64.

    I had the same issue with version 0.3.22, but the test passes with current version 0.3.21 of openblas on Fedora

    For ppc64le it gets stuck here:

    TEST 35/37 potrf:bug_695 [OK]
    TEST 36/37 kernel_regress:skx_avx [OK]
    

    for succesfull x86_64 build I see that these fork tests follow

    TEST 36/38 kernel_regress:skx_avx [OK]
    TEST 37/38 fork:safety [OK]
    TEST 38/38 fork:safety_after_fork_in_parent [OK]
    ...
    
  13. martin-frbg commented on Jul 20, 2023

    @martin-frbg
    Collaborator

    Still trying to reproduce this (builds without issue on pwr9 and z14 in CI too) but I think your successful x86_64 build is not using OpenMP...

  14. thrasibule commented on Jul 20, 2023

    @thrasibule
    Contributor

    I can trigger the lock with this simple test: https://gist.github.com/thrasibule/394425dee40b2b852513c47f1b737ad6. So just changing the number of omp threads between two calls of dgemm is enough to get a lock. I don't think it's a race issue, but something very basic with the number of treads that are different in two places in the code.
    I've identified what's different with my machine that triggers this vs my other machines. It's a Intel(R) Core(TM) i7-3820 CPU with 4 cores/8 threads. However sysconf(_SC_NPROCESSORS_CONF) returns 16 (sysconf(_SC_NPROCESSORS_ONLN) correctly returns 8). Because of this, just running dgemm is enough for blas_num_threads_set to be 1 (I've checked this), even without actually calling openblas_set_num_threads.

  15. thrasibule commented on Jul 31, 2023

    @thrasibule
    Contributor

    I can confirm that the above PR fixes the issue.

  16. martin-frbg commented on Jul 31, 2023

    @martin-frbg
    Collaborator

    Thank you. It appeared to do so on my 6-core AMD system as well, but a largish power10 system in the gcc compile farm (24 cores/192 threads) failed to reproduce the hang with or without the patch (though with the patch, the actual thread counts after openblas_set_num_threads make more sense)

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