3

How can I compile the VM and run Erlang programs on the Intel Xeon Phi coprocessor?

stpk
  • 2,015
  • 1
  • 16
  • 23

1 Answers1

3

Intel Xeon Phi is not a typical x86_64 architecture, therefore it's not possible to run the official Erlang VM on it. The only way to do it is to use cross-compilation and build the VM yourself on a different (host) machine where Erlang is supported, so that it runs on a target system (Phi in this case).

Xeon Phi also supports slightly different instruction set than a typical x86_64 architecture, so you also need to edit the code. If you just cross-compile the VM without touching the code, you will probably get an error:

/tmp/iccvaLP3vas_.s: Assembler messages:
/tmp/iccvaLP3vas_.s:25794: Error: `mfence' is not supported on `k1om'

So first of all you need to add #ifndef clauses around memory fence instructions, which are not supported on Phi (mfence, lfence and sfence). This boils down to opening the erts/include/internal/i386/ethr_membar.h file and adding following preprocessor directives:

#ifndef __MIC__
...
#endif

around __asm__ statements in funtions ethr_mfence__, ethr_sfence__ and ethr_lfence__ e.g.

static __inline__ void
ethr_mfence__(void)
{
#if ETHR_SIZEOF_PTR == 4
    if (ETHR_X86_RUNTIME_CONF_HAVE_NO_SSE2__)
        ETHR_NO_SSE2_MEMORY_BARRIER__;
    else
#endif
#ifndef __MIC__
    __asm__ __volatile__ ("mfence\n\t" : : : "memory");
#endif
}

Now you can try to cross-compile it. First download the sources (in my case Erlang VM 17.5), then run:

$ cd otp_src_17.5
$ export ERL_TOP=`pwd`;
$ ./configure \
    --host=k1om-unknown-linux-gnu \
    --build=x86_64-pc-linux-gnu \
    --without-termcap \
    --without-javac \
    --without-ssl \
    --prefix=/path/to/my/new_installation \
    CC=icc \
    CFLAGS=-mmic \
    LDFLAGS=-mmic \
    DED_LD=icc \
    DED_LDFLAGS="-mmic -shared -Wl,-Bsymbolic" \
    DED_LD_FLAG_RUNTIME_LIBRARY_PATH="-Wl,-R"
$ make
$ make install

icc is the official Intel Compiler and -mmic flag is required for compiling for Xeon Phi. The host and build flags are the systems/architectures of respectively the machine where you compile and the machine which you compile for (Phi). In my case they had following values, but if it's different for you, you might want to use the config.guess script which automatically detects your OS/CPU architecture.

$ ./config.guess
$ x86_64-pc-linux-gnu

And that's it! Now you should be able to ssh on your Phi and run Erlang.

$ ssh my-phi-coprocessor
$ cd /path/to/my/new_installation/
$ export PATH=`pwd`/bin:$PATH;
$ erl -version
Erlang (SMP,ASYNC_THREADS) (BEAM) emulator version 6.4
stpk
  • 2,015
  • 1
  • 16
  • 23
  • Although Knights Corner is an in-order machine, so loads and stores normally complete in order, you need at least a compiler-fence to ensure that the compiler hasn't moved those around. Therefore you ought to have an __asm__ volatile ("":::"memory") in the MIC side of your ifdef. Additionally, If you have code that uses NGO stores, you should insert a real mfence after those to ensure their completion, which you can emulate with __asm__ volatile("lock; addl $0,(%%rsp)"::"memory"). – Jim Cownie Sep 24 '15 at 08:17
  • Hey @stpk what version of ICC are you using? Composer, Pro, etc. I don't suppose I could talk you into a binary drop. I'd like to vet how well it performs on ICC versus an open source compiler with less robust support before figuring out if I need to shell out a grand and some change for parallel xe pro. I don't have a ton of free time to debug, etc. till later on in the year. So i'm leery of just signing up for the free trial until I have an eye on roughly how well things work. – Keith Brings Sep 28 '15 at 13:45
  • @JimCownie does ICC support `asm volatile ("":::"memory")` instructions? On [Wikipedia](https://en.wikipedia.org/wiki/Memory_ordering#Compile-time_memory_barrier_implementation) I found that one should use `__memory_barrier()`. Moreover, is there an easy way to check whether Erlang VM code uses any NGO stores? – stpk Oct 22 '15 at 18:53
  • @stpk The intel compiler (on unix-like OSes) supports the gcc inline asm syntax. I think it you use _mm_mfence() it will generate an mfence instruction which (as we have been discussing at some length) doesn't exist on KNC. – Jim Cownie Oct 23 '15 at 09:07
  • @stpk There are a number of ways to look for NGO stores, (execute the code under SDE https://software.intel.com/en-us/articles/intel-software-development-emulator BUT no runtime tool can ever certify that there are no instances of an instruction in the code that could be executed with some other input. So you'd be much better checking the source for the Erlang VM to see if it has any. (Ones in libraries like libc should be properly guarded anyway, so you don't need to worry about them). – Jim Cownie Oct 23 '15 at 09:18
  • @Keith Brings If you're doing open-source development you should be able to get the Intel compilers gratis. https://software.intel.com/en-us/qualify-for-free-software/opensourcecontributor – Jim Cownie Oct 23 '15 at 09:20