Libgomp.so.1: version `GOMP_4.0' not found (Building Libtorch from source gcc 7.3.1 and using gcc 4.8.2 at runtime)

username: Marouane_Maatouk
cooked: <p>Hi,<br>
I am trying to build Libtorch from source using gcc 7.3.0, and then at runtime i am required to use a gcc 4.8.2.<br>
Thus i face an error due to OpenMP library: <code>libgomp.so.1: version `GOMP_4.0' not found </code>;<br>
even after disabling OpenMP by <code>USE_OPENMP=0</code>.</p>
<p>Is there a way to solve this issue,</p>
<ul>
<li>A way to use OPENMP 3.1 (compatible with gcc 4.8.2) ?</li>
<li>A way to effectively disable openmp (if it won’t affect dramatically the execution time)</li>
</ul>
<hr>
<p>Build (Centos 7):</p>
<pre><code class="lang-auto">conda activate pytorch-build-3
#devtoolset gcc
yum install -y devtoolset-7-gcc devtoolset-7-gcc-c++ devtoolset-7-gcc-gfortran devtoolset-7-binutils
export PATH=/opt/rh/devtoolset-7/root/usr/bin:$PATH
export LD_LIBRARY_PATH_BACKUP=$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/opt/rh/devtoolset-7/root/usr/lib64:/opt/rh/devtoolset-7/root/usr/lib:$LD_PRELOAD:$LD_LIBRARY_PATH

# pytorch env variables
export BUILD_CAFFE2_MOBILE=OFF BUILD_TEST=OFF USE_OPENMP=0 USE_CUDA=0
export BUILD_PYTHONLESS=1 LIBTORCH_VARIANT=shared-with-deps
export PYTORCH_BUILD_VERSION=1.6.0 PYTORCH_BUILD_NUMBER=0
export _GLIBCXX_USE_CXX11_ABI=0 # Disabling the new ABI

# Installing MKL
MKL_VERSION=2020.1
MKL_BUILD=217
mkdir -p ~/intel/lib
curl -fsSL https://anaconda.org/intel/mkl-static/${MKL_VERSION}/download/linux-64/mkl-static-${MKL_VERSION}-intel_${MKL_BUILD}.tar.bz2 | tar xjv
mv lib/* ~/intel/lib/
curl -fsSL https://anaconda.org/intel/mkl-include/${MKL_VERSION}/download/linux-64/mkl-include-${MKL_VERSION}-intel_${MKL_BUILD}.tar.bz2 | tar xjv
mv include ~/intel/

export CMAKE_PREFIX_PATH=${CONDA_PREFIX:-"$(dirname $(which conda))/../"}
export CMAKE_LIBRARY_PATH="~/intel/lib:/lib:$CMAKE_LIBRARY_PATH"
export CMAKE_INCLUDE_PATH="~/intel/include:$CMAKE_INCLUDE_PATH"

# Clone the repository
git clone https://github.com/pytorch/pytorch
cd ~/Repos/pytorch
git checkout v1.6.0
git submodule update --init --recursive

# Build libtorch
python setup.py clean

mkdir -p build_libtorch &amp;&amp; cd build_libtorch 
python ../tools/build_libtorch.py
</code></pre>
<p>From the logs of the build:</p>
<pre><code class="lang-auto">-- MKL libraries: /.../intel/lib/libmkl_intel_lp64.a;/.../intel/lib/libmkl_gnu_thread.a;/.../intel/lib/libmkl_core.a;-fopenmp;/lib64/libpthread.so;/lib64/libm.so;/lib64/libdl.so
-- MKL include directory: /.../intel/include
-- MKL OpenMP type: GNU
-- MKL OpenMP library: -fopenmp
</code></pre>
<p>After switching to gcc 4.8.2, <code>ldd libtorch.so</code></p>
<pre><code class="lang-auto">./libtorch.so: /.../gcc/4.8.2/lib64/libgomp.so.1: version 'GOMP_4.0' not found (required by /.../libtorch/lib/./libtorch_cpu.so)
	linux-vdso.so.1 =&gt;  (0x00002aaaaaaae000)
	libtorch_cpu.so =&gt; /.../libtorch/lib/./libtorch_cpu.so (0x00002aaaaacc7000)
	libgcc_s.so.1 =&gt; /.../gcc/4.8.2/lib64/libgcc_s.so.1 (0x00002aaaba75f000)
	libpthread.so.0 =&gt; /lib64/libpthread.so.0 (0x00002aaaba984000)
	libc.so.6 =&gt; /lib64/libc.so.6 (0x00002aaababa1000)
	libtensorpipe.so =&gt; /.../libtorch/lib/./libtensorpipe.so (0x00002aaabaf64000)
	librt.so.1 =&gt; /lib64/librt.so.1 (0x00002aaabb3f8000)
	libdl.so.2 =&gt; /lib64/libdl.so.2 (0x00002aaabb601000)
	libm.so.6 =&gt; /lib64/libm.so.6 (0x00002aaabb805000)
	libc10.so =&gt; /.../libtorch/lib/./libc10.so (0x00002aaabbb07000)
	libstdc++.so.6 =&gt; /.../gcc/4.8.2/lib64/libstdc++.so.6 (0x00002aaabbd7c000)
	libgomp.so.1 =&gt; /.../gcc/4.8.2/lib64/libgomp.so.1 (0x00002aaabc085000)
	/lib64/ld-linux-x86-64.so.2 (0x0000555555554000)

</code></pre>
<p>Thanks,</p>

username: Marouane_Maatouk
cooked: <p>Found the solution for disabling OpenMP: <code>export MKL_THREADING=TBB USE_TBB=1</code> will use another library which doesn’t throw any issue with gcc 4.8.2</p>
<aside class="onebox githubblob">
  <header class="source">
      <a href="https://github.com/pytorch/pytorch/blob/master/setup.py#L105-L106" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>
  <article class="onebox-body">
    <h4><a href="https://github.com/pytorch/pytorch/blob/master/setup.py#L105-L106" target="_blank" rel="noopener nofollow ugc">pytorch/pytorch/blob/master/setup.py#L105-L106</a></h4>
<pre class="onebox"><code class="lang-py"><ol class="start lines" start="105" style="counter-reset: li-counter 104 ;">
<li>#   MKL_THREADING</li>
<li>#     MKL threading mode: SEQ, TBB or OMP (default)</li>
</ol></code></pre>


  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>