Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mtune and march when compiling in a docker image

When compiling in a docker image (i.e. in the dockerfile), what should march and mtune be set to?

Note this is not about compiling in a running container, but compiling when the container is being built (e.g. building tools from source when the image is run).

For example, currently when I run docker build and install R packages from source I get loads of (could be g++/gcc/f95 ...):

g++ -std=gnu++14 [...] -O3 -march=native -mtune=native -fPIC [...]

If I use native in an image built by Dockerhub, I guess this will use the spec of the machine used by Dockerhub, and this will impact the image binary available for download?

This is related to this similar question about VMs but containers aren't VMs.

like image 232
asachet Avatar asked Jan 04 '19 12:01

asachet


1 Answers

If I use native in an image built by Dockerhub, I guess this will use the spec of the machine used by Dockerhub, and this will impact the image binary available for download?

That's true. When the docker image is built, it is done on the host machine and using its resources, so -march=native and -mtune=native will take the specs of the host machine.

For building docker images that may be used by a wide audience, and making them work as on many (X86) targets as possible, it's best to use a common instruction set. If you need to specify march and mtune, these would probably be the safest choice:

-march=x86-64 -mtune=generic

There may be some performance hits compared to -march=native -mtune=native in certain cases, but fortunately, on most applications, this change could go almost unnoticed (specific applications may be more affected, especially if they depend on a small piece of kernel functions that GCC is able to optimize well, for example by utilizing the CPU vector instruction sets).

For reference, check this detailed benchmark comparison by Phoronix:
GCC Compiler Tests At A Variety Of Optimization Levels Using Clear Linux

It compares about a dozen benchmarks with GCC 6.3 using different optimization flags. Benchmarks run on an Intel Core-I7 6800K machine, which supports modern Intel instruction sets including SSE, AVX, BMI, etc. (see here for the complete list). Specifically, -O3 vs. -O3 -march=native is the interesting metric. You could see that in most benchmarks, the advantage of -O3 -march=native over -O3 is minor to negligible (and in one case, -O3 wins...).

To conclude, -march=x86-64 -mtune=generic is a decent choice for Docker images and should provide good portability and a typically minor performance hit.

like image 76
valiano Avatar answered Sep 18 '22 16:09

valiano