Skip to content

revising optimization flags #300

Closed
Closed

Description

@loveshack

I noticed that several configurations, specifically the AMD ones, just use -O2, which means they'll have no vectorization past the optimized kernels -- just GEMM? (They also use -fomit-frame-pointer, which is redundant with gcc -O, but obviously harmless.)
It may be sensible generally to build non-critical routines with -O2 rather than O3, as O2 is generally better tested, may produce smaller code, and should build faster. However, -O3, or something like it, should presumably be in CRVECFLAGS, at least.
Also, with GCC, at least CRVECFLAGS should have -funsafe-loop-optimizations added to get the rest of the reference kernels vectorized (see #259) and minimize worries about correctness, i.e.

# Flags specific to reference kernels.
CROPTFLAGS     := $(CKOPTFLAGS) -O3
ifeq ($(CC_VENDOR),gcc)
CRVECFLAGS     := $(CKVECFLAGS) -funsafe-loop-optimizations
else
CRVECFLAGS     := $(CKVECFLAGS)
endif

in all the make_defs fragments, or perhaps somewhere common.
-funsafe-loop-optimizations vectorizes all the routines -ffast-math does (on haswell) apart from frame/util/bli_util_unb_var1.c.

I could contribute the changes if they seem sensible, depending on whether common gcc flags should be duplicated in the make fragments or be abstracted somewhere.

Activity

  1. fgvanzee commented on Feb 18, 2019

    @fgvanzee
    Member

    @loveshack I agree that the optimization flags for both optimized kernels and reference kernels can be raised to -O3 for all x86_64-based configurations. I would go further and say that I'm comfortable with -O3 for general framework code. In other words, I'm okay using -O3 wherever any optimization is permitted.

    I would also be open to including -funsafe-loop-optimizations when compiling reference kernels, as you have shown in your snippet above. I've played with it a little bit, combining the option with -Wunsafe-loop-optimizations and, at least with gcc 5.4.0, the only warnings come from packed and banded level-2 operations in the BLAS compatibility layer, which are currently implemented via f2c-based translations from Fortran (since BLIS does not natively implement these operations).

    -funsafe-loop-optimizations vectorizes all the routines -ffast-math does (on haswell) apart from frame/util/bli_util_unb_var1.c.

    Could you explain the above comment? I don't quite follow. Are you suggesting that no optimization flags be used when compiling bli_util_unb_var1.c? Or suggesting that that is already the case? (I can't find evidence in the top-level Makefile that this file is given any special treatment.)

  2. fgvanzee commented on Feb 19, 2019

    @fgvanzee
    Member

    Quick update: I'm having trouble getting the new flags through our Travis CI tests. I get the following error:

    +sde-external-8.16.0-2018-01-30-lin/sde64 -cpuid_in ./travis/cpuid/piledriver.def -- ./test_libblis.x
    TID 0 SDE-ERROR: Executed instruction not valid for specified chip (ICELAKE): 0x9172a5: bextr_xop rax, rcx, 0x103
    Image: /home/travis/build/flame/blis/test_libblis.x+0x5172a5 (in multi-region image, region# 0)
    Function: bli_dsetv_piledriver_ref
    Instruction bytes are: 8f ea f8 10 c1 03 01 00 00 
    

    I'm still trying to isolate exactly what is causing the problem. At first I figured it was the addition of -funsafe-loop-optimizations, but I backed out all of those changes and the error persists. I'm now making sure this isn't being caused by a change on Travis's side by rerunning the corresponding test from a recent commit that had previously passed. (And indeed it did pass again.) The only other (seemingly) relevant changes I made are the bump from -O2 to -O3 for some configurations and removal of -fomit-frame-pointer.

    @devinamatthews Any thoughts?

  3. devinamatthews commented on Feb 19, 2019

    @devinamatthews
    Member

    @fgvanzee it looks like an AMD "TBM" instruction whatever that is.

  4. fgvanzee commented on Feb 19, 2019

    @fgvanzee
    Member

    Thanks for sussing that out. gcc isolates the TBM instruction set via -mtbm, and so I can probably fix this nonsense with -mno-tbm. (And maybe disable XOP and LWP while I'm at it.)

  5. fgvanzee commented on Feb 19, 2019

    @fgvanzee
    Member

    That fixed it. Extra thanks, Devin.

  6. fgvanzee commented on Feb 19, 2019

    @fgvanzee
    Member

    @loveshack I've reverted back to using -funsafe-loop-optimizations. Feel free to review those changes while we do further testing in the lead up to merging back into master.

  7. jeffhammond commented on Feb 19, 2019

    @jeffhammond
    Member

    If you are going to use Intel SDE to try to emulate AMD processors, I recommend setting compiler flags that limit the code generation to the union ISA (Intel & AMD) and confirm any exceptions to this with the SDE documentation.
    The following seem like relevant references:

  8. loveshack commented on Feb 19, 2019

    @loveshack
    ContributorAuthor
  9. loveshack commented on Feb 19, 2019

    @loveshack
    ContributorAuthor
  10. fgvanzee commented on Feb 19, 2019

    @fgvanzee
    Member

    @jeffhammond I'm having trouble following your suggestion. Are you suggesting that we enable FMA4, XOP, TBM, and LWP, and then somehow ignore the invalid instruction errors that come out of the SDE?

    @loveshack It's okay, these things happen. I'm just glad I didn't spend too much time on it. :) As for using -funsafe-math-optimizations, I'm a bit more wary of these kinds of optimizations than those implied by -funsafe-loop-optimizations. Let me play with it a bit and see how the BLAS test drivers and BLIS testsuite respond to them.

    It occurs to me that changed flags should be checked with the testsuite on other architectures than x86_64; I don't know if you (can) do that.

    @loveshack I think our way of doing this is typically to let Travis CI chew on the new build (which uses the SDE as one of the tests) and see what happens. But I may be misunderstanding your comment.

  11. loveshack commented on Feb 20, 2019

    @loveshack
    ContributorAuthor
  12. fgvanzee commented on Feb 20, 2019

    @fgvanzee
    Member

    I think I said somewhere that I successfully ran the full testsuite with it (actually fast-math, I think) for Haswell.

    I must have missed that. (Sorry.) But please humor me. I am somewhat conservative when it comes to numerical issues, mostly because I am wary of issues lurking in that region just beyond my expertise. :)

    That sounds like multiple x86_64 micro-architectures as opposed to, say, aarch64, which is what I meant.

    Ohhh, I see what you mean. You meant other architectures such as ARM or Power. Yeah, our methods of testing changes of any sort for those non-x86_64 architectures is more limited.

  13. loveshack commented on Feb 20, 2019

    @loveshack
    ContributorAuthor
  14. 14 remaining items

  15. devinamatthews commented on Mar 17, 2019

    @devinamatthews
    Member

    Update: I compiled the following code with gcc 8.2, clang 7.0.0, and icc 19.0.1 on Godbolt,

    #include <complex.h>
    
    void zinvscal(int n, const double complex * restrict alpha,
                  double complex * restrict x, int incx)
    {
        const double complex ialpha = 1/(*alpha);
        for (int i = 0;i < n;i++) x[i] *= ialpha;
    }

    My assumption here is that for complex numbers, the approximation x/y = x*(1/y) is good enough, but 1/y = conj(y)/norm(y) is not. This means that the compiler should do something special to compute ialpha (i.e. gcc should use __divdc3), but the loop can be vectorized. Here are the results for different compilers and flags (all -O3 and targeting Haswell):

    Compiler \ Flags Default -funsafe-math-optimizations or -fp-model fast=1 + -ffinite-math-only -ffast-math or -fp-model fast=2
    gcc Uses __divdc3 and __muldc3 in loop Same Same __divdc3 optimized to conj(y)/norm(y)
    clang Same as gcc except checks for NaN outside of __muldc3 Loop vectorized but still checks for NaN Loop fully vectorized Same as gcc
    icc Uses __divdc3 (inlined), loop vectorized Same N/A Same as gcc

    So, it seems that according to the assumptions above, the "right" flags are -funsafe-math-optimizations -ffinite-math-only for clang and the default flags for icc, but there don't seem to be a single set of flags for gcc that do what I want.

    Unless of course people are OK with -fcx-limited-range and then -ffast-math or -fp-model fast=2 should be fine for BLIS.

  16. devinamatthews commented on Mar 17, 2019

    @devinamatthews
    Member

    I should also mention that if you change the inner loop to:

    for (int i = 0;i < n;i++) x[i] /= *alpha;

    then every compiler only vectorizes with the highest flags (i.e. they don't seem to separate the x/y = x*(1/y) and 1/y = conj(y)/norm(y) optimizations).

  17. fgvanzee commented on Mar 18, 2019

    @fgvanzee
    Member

    @devinamatthews Thanks for reporting these details, Devin. It sounds like your preliminary conclusion is that gcc flags should not be modified yet, but that we could begin using -funsafe-math-optimizations -ffinite-math-only for clang?

  18. devinamatthews commented on Mar 18, 2019

    @devinamatthews
    Member

    @fgvanzee do you have any comment of -fcx-limited-range? @loveshack do you have any suggestions for gcc flags?

  19. fgvanzee commented on Mar 18, 2019

    @fgvanzee
    Member

    @fgvanzee do you have any comment of -fcx-limited-range?

    I'm afraid I don't have anything super-intelligent to add at this time.

    Forgive me if I'm reading your table incorrectly, but it sounds like while there is no one set of flags that does what you want in gcc, -funsafe-math-optimizations seems to be a step in the right direction. Is the only thing missing the vectorization of the loop?

  20. devinamatthews commented on Mar 18, 2019

    @devinamatthews
    Member

    @fgvanzee Yes. Although, I suppose since BLIS uses level-0 macros we could implement complex multiplication in terms of real multiplication which would recover vectorizability with just -funsafe-math-optimizations.

  21. fgvanzee commented on Mar 18, 2019

    @fgvanzee
    Member

    since BLIS uses level-0 macros we could implement complex multiplication in terms of real multiplication which would recover vectorizability with just -funsafe-math-optimizations.

    Bingo. So perhaps the emerging consensus is:

    • gcc: -funsafe-math-optimizations
    • clang: -funsafe-math-optimizations -ffinite-math-only
    • icc: (no new flags)

    Should -ffinite-math-only be used for gcc as well?

  22. devinamatthews commented on Mar 18, 2019

    @devinamatthews
    Member

    The other thing that -ffinite-math-only affects is multiplying by zero. Most kernels special case this (e.g. beta=0), but for example if you fake real*complex as complex*complex with an imaginary zero component then this matters.

  23. fgvanzee commented on Mar 18, 2019

    @fgvanzee
    Member

    The other thing that -ffinite-math-only affects is multiplying by zero.

    Then it sounds like it's not that important in our case, for either gcc or clang. BLIS's level-0 scalar macros almost always manually optimize situations where you are multiplying by real x complex to avoid the zero imaginary component entirely.

  24. devinamatthews commented on Aug 6, 2020

    @devinamatthews
    Member

    I think -funsafe-math-optimizations and -O3 have percolated into all the configs.

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