ROCm / HIP-CPU

An implementation of HIP that works on CPUs, across OSes.
MIT License
107 stars 19 forks source link

Linking to system TBB considered harmful (primarily on Ubuntu 18.04) #11

Closed MathiasMagnus closed 3 years ago

MathiasMagnus commented 3 years ago

I know that HIP-CPU may not share the support matrix of ROCm, however projects that rely on ROCm often do (and HIP-CPU targets these applications). I'm sure you're aware of the sad situation of the parallel STL and Ubuntu 18.04, the gist of the problem being that while installing GCC 8 and 9 sporting parallel STL is simple enough from the ppa:ubuntu-toolchain-r/test PPA, it requires a version of TBB newer than the one provided by the system.

In this case it is painfully clear that simply adding -ltbb to the linker command line will not fly. There are two issues with this:

Even if HIP-CPU does not intend on supporting this quirky situation, blindly adding -ltbb to the command line (beside being bad form) opens the door to consuming TBB twice, moreover with different versions. (Projects consuming HIP-CPU may want to depend on TBB on their own right as well.)

I would urge trying to detect libstdc++ instead off being on Linux (after all this situation is the consequence of an implementation detail) using CMake's try_run():

Try compiling a <srcfile>. Returns TRUE or FALSE for success or failure in <compileResultVar>. If the compile succeeded, runs the executable and returns its exit code in <runResultVar>.

or using check_cxx_symbol_exists, whichever is easiest. (I don't have a turnkey solution, but we should.) If STL is libc++, only then do

check_cxx_symbol_exists(SOME_LIBCXX_ONLY_SYMBOLNAME "iostream" USING_LIBCXX)
if(USING_LIBCXX)
  if(NOT TARGET TBB::tbb)
    find_package(TBB REQUIRED)
  endif()
  if(NOT TARGET Threads::Threads)
    find_package(Threads REQUIRED)
  endif()
endif(USING_LIBCXX)
target_link_libraries(${PROJECT_NAME}
  INTERFACE
    $<$<BOOL:${USING_LIBCXX}>:
      ${CMAKE_DL_LIBS}
      Threads::Threads
      TBB::tbb
    >
)

_(Note: AFAIK having to find and link to Threads::Threads (and perhaps ${CMAKE_DL_LIBS} too) is yet another quirkiness libc++, but don't call me out on that one. It isn't painful to have on non-Linux systems, it's minimal bloat to configuration time and console output on Windows.)_

AlexVlx commented 3 years ago

This is indeed a significant concern, and there was some measure of doubt before actually adding the convenience hip_cpu_rt target; in one incarnation the onus was fully on the user to provide the additional dependencies. In the end we have something that is not very robust, as you have observed. That being said, I'd be more than happy to take PRs improving the situation. Even fleshing this discussion out and thrashing the details of desirable behaviour in a broad sense would be nice.