Search code examples
tensorflowbuildbazelopenmpi

Building TensorFlow: import MPI headers from outside of the Bazel root


I want to build TensorFlow 1.3 (not 1.13) on Ubuntu 16.04, with support for MPI (instead of the default gRPC). I installed the package libopenmpi-dev from the Ubuntu repos. I have supplied /usr/lib/openmpi as the MPI toolkit directory when running the configure script.

I use this command to launch the build:

$ bazel build --config=opt --config=cuda //tensorflow/tools/pip_package:build_pip_package

But there is a header inclusion problem:

  1. The file tensorflow/contrib/mpi/mpi_utils.cc includes tensorflow/contrib/mpi/mpi_utils.h
  2. This mpi_utils.h includes third_party/mpi/mpi.h
  3. This mpi.h is a symlink to /usr/lib/openmpi/include/mpi.h
  4. This actual mpi.h contains this line:
#include "openmpi/ompi/mpi/cxx/mpicxx.h"
  1. And mpicxx.h is in the folder /usr/lib/openmpi/include/openmpi/ompi/mpi/cxx/, which is not in the include path.

I have "fixed" this by creating a symlink to the right folder:

$ ln -s /usr/lib/openmpi/include/openmpi third_party/mpi/openmpi

Now mpicxx.h is found, but it wants to include mpi.h, which fails since mpi.h is not in the same folder:

$ bazel build --config=opt --config=cuda //tensorflow/tools/pip_package:build_pip_package
WARNING: /home/arno/tensorflow/tensorflow/contrib/learn/BUILD:15:1: in py_library rule //tensorflow/contrib/learn:learn: target '//tensorflow/contrib/learn:learn' depends on deprecated target '//tensorflow/contrib/session_bundle:exporter': No longer supported. Switch to SavedModel immediately.
WARNING: /home/arno/tensorflow/tensorflow/contrib/learn/BUILD:15:1: in py_library rule //tensorflow/contrib/learn:learn: target '//tensorflow/contrib/learn:learn' depends on deprecated target '//tensorflow/contrib/session_bundle:gc': No longer supported. Switch to SavedModel immediately.
INFO: Found 1 target...
ERROR: /home/arno/tensorflow/tensorflow/contrib/mpi/BUILD:60:1: C++ compilation of rule '//tensorflow/contrib/mpi:mpi_rendezvous_mgr' failed: crosstool_wrapper_driver_is_not_gcc failed: error executing command external/local_config_cuda/crosstool/clang/bin/crosstool_wrapper_driver_is_not_gcc -U_FORTIFY_SOURCE '-D_FORTIFY_SOURCE=1' -fstack-protector -fPIE -Wall -Wunused-but-set-parameter ... (remaining 151 argument(s) skipped): com.google.devtools.build.lib.shell.BadExitStatusException: Process exited with status 1.
In file included from ./third_party/mpi/mpi.h:2673:0,
                 from ./tensorflow/contrib/mpi/mpi_utils.h:27,
                 from ./tensorflow/contrib/mpi/mpi_rendezvous_mgr.h:33,
                 from tensorflow/contrib/mpi/mpi_rendezvous_mgr.cc:18:
./third_party/mpi/openmpi/ompi/mpi/cxx/mpicxx.h:35:17: fatal error: mpi.h: No such file or directory
compilation terminated.
Target //tensorflow/tools/pip_package:build_pip_package failed to build
Use --verbose_failures to see the command lines of failed build steps.
ERROR: /home/arno/tensorflow/tensorflow/tools/pip_package/BUILD:134:1 C++ compilation of rule '//tensorflow/contrib/mpi:mpi_rendezvous_mgr' failed: crosstool_wrapper_driver_is_not_gcc failed: error executing command external/local_config_cuda/crosstool/clang/bin/crosstool_wrapper_driver_is_not_gcc -U_FORTIFY_SOURCE '-D_FORTIFY_SOURCE=1' -fstack-protector -fPIE -Wall -Wunused-but-set-parameter ... (remaining 151 argument(s) skipped): com.google.devtools.build.lib.shell.BadExitStatusException: Process exited with status 1.
INFO: Elapsed time: 6.668s, Critical Path: 4.98s

I have tried to manually add the headers' path to GCC's include path with this command:

$ bazel build --config=opt --config=cuda //tensorflow/tools/pip_package:build_pip_package --copt='-I/usr/lib/openmpi/include'

...but then I get errors because the headers included from /usr/lib/openmpi/include/openmpi/ompi/mpi/cxx are not declared in Bazel's configuration files. And I can't declare them to Bazel since it doesn't accept absolute paths.

I can't find the right way to make this build work. I am new to Bazel, and from what I have read, the build directory should be "self-contained", that is, contain all the headers and source files necessary, but the TensorFlow repository violates this by adding symlinks to /usr/lib/... in third_party/mpi. Changing TensorFlow versions is not an option.

How can I build TensorFlow 1.3 with OpenMPI support?

Edit: adding the -s option to the Bazel build command, like suggested in the comments, gives a more detailed output, but I can't discern which compilator is used. I think these are the relevant lines:

>>>>> # //tensorflow/core/kernels:transpose_functor [action 'Compiling tensorflow/core/kernels/transpose_functor_cpu.cc']
(cd /home/arno/.cache/bazel/_bazel_arno/e7d941e3336cbc1a05a122432422a066/execroot/tensorflow && \
  exec env - \
    CUDA_TOOLKIT_PATH=/usr/local/cuda \
    CUDNN_INSTALL_PATH=/usr/local/lib \
    GCC_HOST_COMPILER_PATH=/usr/bin/gcc \
    PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/arno/bin \
    PYTHON_BIN_PATH=/usr/bin/python3 \
    PYTHON_LIB_PATH=/usr/lib/python3/dist-packages \
    TF_CUDA_CLANG=0 \
    TF_CUDA_COMPUTE_CAPABILITIES=6.2 \
    TF_CUDA_VERSION=8.0 \
    TF_CUDNN_VERSION=6 \
    TF_NEED_CUDA=1 \
    TF_NEED_OPENCL=0 \
  external/local_config_cuda/crosstool/clang/bin/crosstool_wrapper_driver_is_not_gcc -U_FORTIFY_SOURCE '-D_FORTIFY_SOURCE=1' -fstack-protector -fPIE -Wall -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-pointer -g0 -O2 -DNDEBUG -ffunction-sections -fdata-sections '-march=native' '-std=c++11' '-march=native' -MD -MF bazel-out/local_linux-py3-opt/bin/tensorflow/core/kernels/_objs/transpose_functor/tensorflow/core/kernels/transpose_functor_cpu.pic.d '-frandom-seed=bazel-out/local_linux-py3-opt/bin/tensorflow/core/kernels/_objs/transpose_functor/tensorflow/core/kernels/transpose_functor_cpu.pic.o' -fPIC -DEIGEN_MPL2_ONLY -DTENSORFLOW_USE_JEMALLOC -DSNAPPY -DTENSORFLOW_USE_MPI -iquote . -iquote bazel-out/local_linux-py3-opt/genfiles -iquote external/jemalloc -iquote bazel-out/local_linux-py3-opt/genfiles/external/jemalloc -iquote external/bazel_tools -iquote bazel-out/local_linux-py3-opt/genfiles/external/bazel_tools -iquote external/protobuf -iquote bazel-out/local_linux-py3-opt/genfiles/external/protobuf -iquote external/eigen_archive -iquote bazel-out/local_linux-py3-opt/genfiles/external/eigen_archive -iquote external/local_config_sycl -iquote bazel-out/local_linux-py3-opt/genfiles/external/local_config_sycl -iquote external/gif_archive -iquote bazel-out/local_linux-py3-opt/genfiles/external/gif_archive -iquote external/jpeg -iquote bazel-out/local_linux-py3-opt/genfiles/external/jpeg -iquote external/com_googlesource_code_re2 -iquote bazel-out/local_linux-py3-opt/genfiles/external/com_googlesource_code_re2 -iquote external/farmhash_archive -iquote bazel-out/local_linux-py3-opt/genfiles/external/farmhash_archive -iquote external/fft2d -iquote bazel-out/local_linux-py3-opt/genfiles/external/fft2d -iquote external/highwayhash -iquote bazel-out/local_linux-py3-opt/genfiles/external/highwayhash -iquote external/png_archive -iquote bazel-out/local_linux-py3-opt/genfiles/external/png_archive -iquote external/zlib_archive -iquote bazel-out/local_linux-py3-opt/genfiles/external/zlib_archive -iquote external/snappy -iquote bazel-out/local_linux-py3-opt/genfiles/external/snappy -iquote external/local_config_cuda -iquote bazel-out/local_linux-py3-opt/genfiles/external/local_config_cuda -isystem external/jemalloc/include -isystem bazel-out/local_linux-py3-opt/genfiles/external/jemalloc/include -isystem external/bazel_tools/tools/cpp/gcc3 -isystem external/protobuf/src -isystem bazel-out/local_linux-py3-opt/genfiles/external/protobuf/src -isystem external/eigen_archive -isystem bazel-out/local_linux-py3-opt/genfiles/external/eigen_archive -isystem external/gif_archive/lib -isystem bazel-out/local_linux-py3-opt/genfiles/external/gif_archive/lib -isystem external/farmhash_archive/src -isystem bazel-out/local_linux-py3-opt/genfiles/external/farmhash_archive/src -isystem external/png_archive -isystem bazel-out/local_linux-py3-opt/genfiles/external/png_archive -isystem external/zlib_archive -isystem bazel-out/local_linux-py3-opt/genfiles/external/zlib_archive -isystem external/local_config_cuda/cuda -isystem bazel-out/local_linux-py3-opt/genfiles/external/local_config_cuda/cuda -isystem external/local_config_cuda/cuda/cuda/include -isystem bazel-out/local_linux-py3-opt/genfiles/external/local_config_cuda/cuda/cuda/include -DEIGEN_AVOID_STL_ARRAY -Iexternal/gemmlowp -Wno-sign-compare -fno-exceptions '-DGOOGLE_CUDA=1' -msse3 -pthread '-DGOOGLE_CUDA=1' -no-canonical-prefixes -Wno-builtin-macro-redefined '-D__DATE__="redacted"' '-D__TIMESTAMP__="redacted"' '-D__TIME__="redacted"' -fno-canonical-system-headers -c tensorflow/core/kernels/transpose_functor_cpu.cc -o bazel-out/local_linux-py3-opt/bin/tensorflow/core/kernels/_objs/transpose_functor/tensorflow/core/kernels/transpose_functor_cpu.pic.o)
ERROR: /home/arno/tensorflow/tensorflow/contrib/mpi/BUILD:48:1: C++ compilation of rule '//tensorflow/contrib/mpi:mpi_utils' failed: crosstool_wrapper_driver_is_n
ot_gcc failed: error executing command external/local_config_cuda/crosstool/clang/bin/crosstool_wrapper_driver_is_not_gcc -U_FORTIFY_SOURCE '-D_FORTIFY_SOURCE=1' -fstack-protector -fPIE -Wall -Wunused-but-set-parameter ... (remaining 131 argument(s) skipped): com.google.devtools.build.lib.shell.BadExitStatusException: Process exited with status 1.
In file included from ./third_party/mpi/mpi.h:2673:0,
                 from ./tensorflow/contrib/mpi/mpi_utils.h:27,
                 from tensorflow/contrib/mpi/mpi_utils.cc:18:
./third_party/mpi/openmpi/ompi/mpi/cxx/mpicxx.h:35:17: fatal error: mpi.h: No such file or directory
compilation terminated.

Solution

  • A workaround is to build and install MVAPICH from source (the MPI toolkit path is then /usr/local). The problem exists only with OpenMPI.