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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With