Skip to content

Detect AVX2 support at runtime #71

Open
@jamesjer

Description

@jamesjer

Linux distributions must build for the lowest common denominator CPU. For the Fedora Linux distribution, the original x86_64 is still supported, meaning we cannot build msolve with AVX2 support. Would you consider detecting AVX2 support at runtime instead of at compile time?

One way that could be done is to add this code somewhere in src/neogb:

/* check for AVX2 availability */
#ifdef __amd64__
#include <cpuid.h>
int have_avx2;

static void __attribute__((constructor))
set_avx2_flag(void)
{
  unsigned int eax, ebx, ecx, edx;
  have_avx2 = __get_cpuid(7, &eax, &ebx, &ecx, &edx) && (ebx & bit_AVX2) != 0;
}
#endif

That works for gcc and clang. If you want to support other compilers, the code might get a little more complex. With have_avx2 available, then code like this:

#ifdef HAVE_AVX2
  foo;
#else
  bar;
#endif

would be transformed into this:

#ifdef __amd64__
  if (have_avx2) {
    foo;
  } else
#endif
  {
    bar;
  }

On x86_64 platforms, then -mavx could be passed to the compiler always, since the AVX2 code is not executed if __get_cpuid indicates the CPU doesn't support AVX2. That would let you throw away most or all of several files in the m4 directory.

I can open a PR if you like the idea.

Activity

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