plaidml / tpp-mlir

TPP experimentation on MLIR for linear algebra
https://arxiv.org/abs/2404.15204
Other
111 stars 31 forks source link

Build issues on Ubuntu 24.04 - incorrect openmp linkage in CMake #959

Open dbabokin opened 2 months ago

dbabokin commented 2 months ago

OpenMP libraries a linked using the library name (i.e. omp): https://github.com/plaidml/tpp-mlir/blob/9c53a05fb56128cda8869a563b999e90b31f2e17/cmake/modules/xsmm-dnn.cmake#L47-L50 https://github.com/plaidml/tpp-mlir/blob/9c53a05fb56128cda8869a563b999e90b31f2e17/tools/tpp-run/CMakeLists.txt#L12-L14

This is not portable, as it assumes the libomp.so is in standard location, which might not be the case. And it seems that what happens on Ubuntu 24.04 with libomp-dev package installed. -lomp5 works, but not -lomp.

The recommended way to handle this is the following:

target_link_libraries(xsmm_dnn_mlp PRIVATE OpenMP::OpenMP_C)

And that fix perfectly fixes the first code snippet.

For the second case it's a bit messy, because of --no-as-needed/--as-needed hack. I suggest using OpenMP_C_LIBRARIES instead of -lomp, though it brings two libraries on my machine: /usr/lib/llvm-18/lib/libomp.so;/lib/aarch64-linux-gnu/libpthread.a

rengolin commented 1 month ago

Coming back to this, I did some digging, and it's not clear to me how to set an actual vendor.

Here's some background information: https://stackoverflow.com/questions/46414660/macos-cmake-and-openmp#48216682

I tried:

  # Force LLVM version of OpenMP
  set(OpenMP_C_LIB_NAMES "omp")
  set(OpenMP_CXX_LIB_NAMES "omp")
  set(OpenMP_omp_LIBRARY ${OpenMP_C_LIB_NAMES})
  set(OpenMP_libomp_LIBRARY ${OpenMP_C_LIB_NAMES})
  set(OpenMP_libgomp_LIBRARY ${OpenMP_C_LIB_NAMES})
  find_package(OpenMP REQUIRED)

Which Clang gets it:

-- Found OpenMP_C: -fopenmp=libomp (found version "5.0") 
-- Found OpenMP_CXX: -fopenmp=libomp (found version "5.0") 

and links correctly.

But GCC still doesn't:

-- Found OpenMP_C: -fopenmp (found version "4.5") 
-- Found OpenMP_CXX: -fopenmp (found version "4.5") 

And emits:

ld.lld: error: unable to find library -lomp
rengolin commented 1 month ago

And it seems that what happens on Ubuntu 24.04 with libomp-dev package installed. -lomp5 works, but not -lomp.

Weird, my build machine runs on Ubuntu 22.04.5 and does not have that problem. We run on Intel, AMD and Arm machines from AWS and did not have that problem either.

The recommended way to handle this is the following:

target_link_libraries(xsmm_dnn_mlp PRIVATE OpenMP::OpenMP_C)

And that fix perfectly fixes the first code snippet.

Not quite. This doesn't fix the issue, just makes GCC chose GOMP instead of OMP, which compiles and links fine, but the execution is really slow on high core count.

Our rationale behind forcing LLVM's OMP is performance. You must install libomp into your system (packages, make install, etc) and CMake must be able to find it. Each distro is different, so adding special rules here misses the point of using CMake in the first place.

I suggest using OpenMP_C_LIBRARIES instead of -lomp, though it brings two libraries on my machine: /usr/lib/llvm-18/lib/libomp.so;/lib/aarch64-linux-gnu/libpthread.a

I agree here. We probably got away with it because libpthread.a is already included elsewhere. But we still need to make sure LLVM's OMP is selected, otherwise, we'd just link GOMP here and not compare apples to apples on benchmarking.

dbabokin commented 1 month ago

Reading CMake sources I don't see a way to specify a vendor. We should probably ping CMake maintainer on https://gitlab.kitware.com/cmake/cmake/-/issues/26263 and live with a hack in the meantime.