Skip to content

Race condition in usage of ar, and incorrect dependency resolution of tmglib #1682

Closed

Description

@staticfloat

When compiling OpenBLAS on a very fast machine and a warm ccache using a parallel build, it is very easy to get two ar processes modifying libopenblas64_p-r0.3.0.dev.a. This manifests in symbols randomly "disappearing" from libopenblas for some builds, because two ar processes read in the .a file, they both modify it to add in their object files, then both save it.

This most often happens for the lapack-netlib/TESTING/MATGEN symbols, which are not the most critical of symbols (and so sometimes go unnoticed when they are left undefined), but which always fails on OSX due to the symbol renaming that we do on that platform. This manifests itself in messages that look like the following:

/Users/sabae/src/julia/deps/scratch/objconv/objconv @objconv.def ../libopenblas64_p-r0.3.0.dev.a ../libopenblas64_p-r0.3.0.dev.a.osx.renamed
Warning 1106: Symbol %s not found. Modification of this symbol failed_clahilb_
Warning 1106: Symbol %s not found. Modification of this symbol failed_clakf2_
Warning 1106: Symbol %s not found. Modification of this symbol failed_clarge_
Warning 1106: Symbol %s not found. Modification of this symbol failed_clarnd_
Warning 1106: Symbol %s not found. Modification of this symbol failed_claror_
Warning 1106: Symbol %s not found. Modification of this symbol failed_clarot_
Warning 1106: Symbol %s not found. Modification of this symbol failed_clatm1_
Warning 1106: Symbol %s not found. Modification of this symbol failed_clatm2_
Warning 1106: Symbol %s not found. Modification of this symbol failed_clatm3_
Warning 1106: Symbol %s not found. Modification of this symbol failed_clatm5_
Warning 1106: Symbol %s not found. Modification of this symbol failed_clatm6_
Warning 1106: Symbol %s not found. Modification of this symbol failed_clatme_
Warning 1106: Symbol %s not found. Modification of this symbol failed_clatmr_
Warning 1106: Symbol %s not found. Modification of this symbol failed_clatmt_
Warning 1106: Symbol %s not found. Modification of this symbol failed_dlahilb_
Warning 1106: Symbol %s not found. Modification of this symbol failed_dlakf2_
Warning 1106: Symbol %s not found. Modification of this symbol failed_dlaran_
Warning 1106: Symbol %s not found. Modification of this symbol failed_dlarge_
Warning 1106: Symbol %s not found. Modification of this symbol failed_dlarnd_
Warning 1106: Symbol %s not found. Modification of this symbol failed_dlaror_
Warning 1106: Symbol %s not found. Modification of this symbol failed_dlarot_
Warning 1106: Symbol %s not found. Modification of this symbol failed_dlatm1_
Warning 1106: Symbol %s not found. Modification of this symbol failed_dlatm2_
Warning 1106: Symbol %s not found. Modification of this symbol failed_dlatm3_
...

Followed by this linker error:

ccache gfortran -mmacosx-version-min=10.8 -march=x86-64 -Wa,-q -m64 -O2 -fPIC -O2 -m128bit-long-double -Wall -m64 -fdefault-integer-8 -fPIC -all_load -headerpad_max_install_names -install_name "/Users/sabae/src/julia/deps/scratch/openblas-939452ea9dcb57abdcc3f1278c6db68a4690465/exports/../libopenblas64_p-r0.3.0.dev.dylib" -dynamiclib -o ../libopenblas64_p-r0.3.0.dev.dylib ../libopenblas64_p-r0.3.0.dev.a.osx.renamed -Wl,-exported_symbols_list,osx.def  -L/usr/local/Cellar/gcc/8.1.0/lib/gcc/8/gcc/x86_64-apple-darwin17.5.0/8.1.0 -L/usr/local/Cellar/gcc/8.1.0/lib/gcc/8/gcc/x86_64-apple-darwin17.5.0/8.1.0/../../.. -L/usr/local/Cellar/gcc/8.1.0/lib/gcc/8/gcc/x86_64-apple-darwin17.5.0/8.1.0 -L/usr/local/Cellar/gcc/8.1.0/lib/gcc/8/gcc/x86_64-apple-darwin17.5.0/8.1.0/../../..  -lgfortran -lSystem -lquadmath -lm -lSystem -lgfortran -lSystem -lquadmath -lm -lSystem
Undefined symbols for architecture x86_64:
  "_clagge_64_", referenced from:
      _LAPACKE_clagge_work64_ in libopenblas64_p-r0.3.0.dev.a.osx.renamed(lapacke_clagge_work.o)
  "_claghe_64_", referenced from:
      _LAPACKE_claghe_work64_ in libopenblas64_p-r0.3.0.dev.a.osx.renamed(lapacke_claghe_work.o)
  "_clagsy_64_", referenced from:
      _LAPACKE_clagsy_work64_ in libopenblas64_p-r0.3.0.dev.a.osx.renamed(lapacke_clagsy_work.o)
  "_clahilb_64_", referenced from:
     -exported_symbol[s_list] command line option
  "_clakf2_64_", referenced from:
     -exported_symbol[s_list] command line option
....

I think it is necessary to do something to prevent the Makefiles from running these ar invocations simultaneously; perhaps by forcing an order-only dependency chain between them.

This is further exacerbated by lapack-netlib/TESTING/MATGEN/Makefile, which has the following logic:

../../$(TMGLIB): $(ALLOBJ)
    $(ARCH) $(ARCHFLAGS) $@ $^
    $(RANLIB) $@

$(TMGLIB) in this case is libopenblas64_p-r0.3.0.dev.a, and so this says that if libopenblas64_p-r0.3.0.dev.a already exists and is newer than $(ALLOBJ), we don't need to run this ar invocation. This means that if I notice some symbols are missing, and try to run make -C lapack-netlib tmglib, it just says "nothing to do".

Activity

  1. staticfloat commented on Jul 13, 2018

    @staticfloat
    ContributorAuthor

    I'm getting around the second problem by setting those targets that perform ar commands as .PHONY, but that's not necessarily the correct way; I would argue the correct way would be to have each ar invocation output a separate .a file, then have one giant ar command to glom them all together.

  2. staticfloat commented on Jul 14, 2018

    @staticfloat
    ContributorAuthor

    I should note that the .PHONY fix must be applied to multiple Makefiles:

    • lapack-netlib/TESTING/MATGEN: .PHONY: ../../$(TMGLIB)
    • lapack-netlib/LAPACKE/utils/Makefile: .PHONY: lib
    • lapack-netlib/LAPACKE/src/Makefile: .PHONY: ../../$(LAPACKELIB)
    • lapack-netlib/SRC/Makefile: .PHONY: ../$(LAPACKLIB)
  3. martin-frbg commented on Jul 14, 2018

    @martin-frbg
    Collaborator

    If I understand you correctly, wouldn't that be a bug in (parallel) make rather than the Makefile ?

  4. martin-frbg commented on Jul 14, 2018

    @martin-frbg
    Collaborator

    On Linux a stopgap solution could be to put an flock on the relevant AR invocations, but that utility (though not the system call of the same name) does not seem to be available elsewhere. Creating separate .a files with a final ar run at the end is probably the best option (and might fix other deficiencies mentioned earlier, like #705)

  5. staticfloat commented on Jul 17, 2018

    @staticfloat
    ContributorAuthor

    Creating separate .a files with a final ar run at the end is probably the best option

    Agreed. Yes, it looks like Tony's issue is very much related. To be honest, I'm amazed this has worked as well as it has for so long. It must be that we're finally getting machines that can trigger these race conditions more reliably as they get faster and faster. :P

  6. martin-frbg commented on Jul 17, 2018

    @martin-frbg
    Collaborator

    Looks like it, yes. I wonder if adding netlib to the existing .NOTPARALLEL line in the Makefile would be enough for a quick-and-dirty fix. The main problem is probably that OpenBLAS sets all of LAPACKLIB, LAPACKELIB and TMGLIB to itself in make.inc, making it impossible for make to notice the potential conflict.

  7. martin-frbg commented on Jul 19, 2018

    @martin-frbg
    Collaborator

    Unfortunately there does not appear to be a portable way to have one giant ar command to glom them all together (except by first unpacking them all again, which does not look that attractive to me).

  8. staticfloat commented on Jul 19, 2018

    @staticfloat
    ContributorAuthor

    Yeah, you're right. I would have thought this would be easier, but I guess not. I agree, unpacking everything seems like a large annoyance.

    How hard would it be to have a single ar command that just reads from all dependent .o files and assembles the one giant .a file directly?

  9. martin-frbg commented on Jul 19, 2018

    @martin-frbg
    Collaborator

    That would probably expand to a huge command line, possibly causing new problems (of the "argument list too long" kind) and we'd probably still need intermediate .a files to run the tests.

  10. staticfloat commented on Jul 19, 2018

    @staticfloat
    ContributorAuthor

    If we can expand the required toolset to include libtool, it looks like the following can do all the work for us:

    libtool -static -o new.a old1.a old2.a
    

    That program is pretty common on OSX and Linux; what kinds of other platforms do we need to care about? For windows, I'm assuming you're using a toolchain similar to mingw32, so libtool may not be too hard to come by....

  11. martin-frbg commented on Jul 19, 2018

    @martin-frbg
    Collaborator

    Android, possibly AIX, the various BSD and SunOS/Solaris derivatives ... on Windows mingw is not a given either, as there is (limited) support for a pure MSVC build if I am not mistaken. So I am not sure if the libtool solution would be more portable than ar -M with MRI commands ?

  12. staticfloat commented on Jul 19, 2018

    @staticfloat
    ContributorAuthor

    So I am not sure if the libtool solution would be more portable than ar -M with MRI commands ?

    The problem with MRI commands is that the default ar on OSX is the BSD ar which doesn't support MRI commands. If we require llvm-ar, that does seem to support MRI commands, but it doesn't come by default on OSX machines (or FreeBSD, I believe). :(

    That would probably expand to a huge command line, possibly causing new problems (of the "argument list too long" kind) and we'd probably still need intermediate .a files to run the tests.

    The way this is usually done is to do ar -r big.a one.o, ar -r big.a two.o, etc.... That's actually what we do now, the problem is just that we have multiple targets that are dumping into the same big.a.

    I think we can have a surprisingly simple solution actually; we create separate .a files out of each subproject and have those separate .a files depend on the .o files in each subproject, so that we have a clean dependency chain that causes rebuilds of the larger .a files properly. Then, we have the main overall giant library depend on those .a files to setup the dependency chain properly, but then we don't actually use the .a files themselves, but just directly have a bunch of ar -r invocations. E.g.:

    libopenblas.a: subproject1.a subproject2.a ...
        ar -r libopenblas.a <.o files for subproject 1...>
        ar -r libopenblas.a <.o files for subproject 2...>

    This way, we ensure that any time we need to modify libopenblas.a we relink everything so we never get into the weird situation where libopenblas.a contains only a subset of subprojects, and also we ensure that the dependency chain is clean such that changing foo.c will cause foo.o to be rebuilt, which will cause subproject_foo.a to be rebuilt, which will finally cause libopenblas.a to be rebuilt, which is what we need.

  13. dev-zero commented on Oct 1, 2018

    @dev-zero
    Contributor

    Since I've been looking into this when encountering #1213: I fail to see how the ar process can run in parallel since OpenBLAS' main Makefile (in 0.3.3) explicitly runs the targets after each other:

    ifeq ($(NOFORTRAN), $(filter 0,$(NOFORTRAN)))
        @$(MAKE) -C $(NETLIB_LAPACK_DIR) lapacklib
        @$(MAKE) -C $(NETLIB_LAPACK_DIR) tmglib
    endif
    ifndef NO_LAPACKE
        @$(MAKE) -C $(NETLIB_LAPACK_DIR) lapackelib
    endif
    endif
  14. staticfloat commented on Oct 1, 2018

    @staticfloat
    ContributorAuthor

    Within each of those targets however, is the chance for parallelism. Such as here: https://github.com/xianyi/OpenBLAS/blob/develop/lapack-netlib/TESTING/MATGEN/Makefile#L60-L78

  15. dev-zero commented on Oct 1, 2018

    @dev-zero
    Contributor

    @staticfloat not really, as far as I understand make: OpenBLAS calls that makefile (actually, it calls one intermediate makefile, but with the same effect) without a specific target, so the first target will be picked, which is all, which depends on ../../$(TMGLIB). As a prerequisite for ../../$(TMGLIB), it builds everything in $(ALLOBJ), then make checks whether or not any of the just built prerequisites is newer than ../../$(TMGLIB) and executes $(ARCH) $(ARCHFLAGS) $@ $^ if they are. If you have a fast system and a filesystem with no sub-second resolution in the timestamps, ../../$(TMGLIB) and the objects in $(ALLOBJ) might have the same mtime and therefore ../../$(TMGLIB) does not get updated. But the single, complex, etc. are never executed.

  16. martin-frbg commented on Oct 2, 2018

    @martin-frbg
    Collaborator

    @dev-zero while the single,complex etc. sub-targets are not executed, wouldn't make be able to build the CMATGEN, DMATGEN,.. parts of ALLOBJ in parallel ? (Else I am not sure how/why disabling parallel make would be able to solve this)

  17. dev-zero commented on Oct 2, 2018

    @dev-zero
    Contributor

    @martin-frbg as far as I understood make, it will first resolve the variables, such that we have the equivalent of:

    ../../$(TMGLIB): slatms.o ... clatms.o ... slatm1.o ...
    	$(ARCH) $(ARCHFLAGS) $@ $^
    	$(RANLIB) $@

    the objects slatms.o, ... are then built in parallel, but this does not matter at all since they have different names. And only after all of them are built, make will check their timestamps against the target and based on that run the commands to update the target.

    The error messages here are slightly different due to the tools being used for linking being different, but I would say this is the same issue as in #1213. In fact, OpenBLAS is not alone on this and it seems that Apple's APFS or HFS+ is to blame: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81797 ... APFS has a bug and HFS+ has only 1s time stamp resolution (see https://gcc.gnu.org/ml/gcc-bugs/2017-08/msg01319.html).

    The solution: disable parallel builds on macOS or make all the ../../$(TMGLIB): ... targets .PHONY to force make to always update them (as already proposed). Since we explicitly run those targets in serial in OpenBLAS Makefile, we actually know that there will never be more than one process updating ../../$(TMGLIB) at the same time, so that should be safe.

  18. staticfloat commented on Oct 2, 2018

    @staticfloat
    ContributorAuthor

    OpenBLAS calls that makefile (actually, it calls one intermediate makefile, but with the same effect) without a specific target, so the first target will be picked, which is all, which depends on ../../$(TMGLIB).

    Ah, my bad. Thanks @dev-zero. It's been a while since I looked at this so I don't remember precisely which targets are being built in parallel.

    as far as I understood make, it will first resolve the variables, such that we have the equivalent of:

    Your understanding is correct.

    The solution: disable parallel builds on macOS

    This will greatly increase compile times for us. I'd rather not do that.

    make all the ../../$(TMGLIB): ... targets .PHONY

    This should work, as long as ar is smart enough to not complain when you are trying to add symbols into an archive that already exist. Additionally, this means that when we run make on OSX, it will never do nothing; it will always re-run ar.

  19. martin-frbg commented on Oct 2, 2018

    @martin-frbg
    Collaborator

    IIRC all ar invocations are with flags set to -r or probably -ru so ar should not complain (?)

  20. added a commit that references this issue on Oct 6, 2018
    79ea839
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