How to use experimental parallel STL in C++1z?

痴心易碎 提交于 2019-12-05 13:45:25

Is this not implemented yet?

That is correct. Neither the parallelism TS (which would live in <experimental/xxx> or the parallel algorithms in the (not yet final) C++1z standard have been implemented in libc++ (yet).

I use the implementation of PSTL in Intel 18 Parallel Studio XE beta, which composes with both libc++ and libstdc++ and is based on TBB. I've tested for_each and transform but nothing else.

Update: The Intel PSTL is open-source (https://github.com/intel/parallelstl) and works with GCC and Clang.

Because support for PSTL is limited, I make the code portable via the preprocessor:

#if defined(USE_PSTL) && defined(__INTEL_COMPILER) && (__INTEL_COMPILER >= 1800)
  std::for_each( pstl::execution::par, std::begin(range), std::end(range), [&] (int i) {
    std::for_each( pstl::execution::par_unseq, std::begin(range), std::end(range), [&] (int j) {
#elif defined(USE_PSTL) && defined(__GNUC__) && defined(__GNUC_MINOR__) \
                        && ( (__GNUC__ == 8) || (__GNUC__ == 7) && (__GNUC_MINOR__ >= 2) )
  __gnu_parallel::for_each( std::begin(range), std::end(range), [&] (int i) {
    __gnu_parallel::for_each( std::begin(range), std::end(range), [&] (int j) {
#else
#warning Parallel STL is NOT being used!
  std::for_each( std::begin(range), std::end(range), [&] (int i) {
    std::for_each( std::begin(range), std::end(range), [&] (int j) {
#endif
        B[i*order+j] += A[j*order+i];
        A[j*order+i] += 1.0;
      });
    });
  }

You can see that this code is based on libc++ on Mac, although the PSTL part itself is from the Intel headers, which in turn use TBB as the runtime.

$ otool -L transpose-vector-pstl
transpose-vector-pstl:
    @rpath/libtbb.dylib (compatibility version 0.0.0, current version 0.0.0)
    /usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 307.5.0)
    @rpath/libiomp5.dylib (compatibility version 5.0.0, current version 5.0.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1238.60.2)

Full disclosure: I work for Intel and have discussed this implementation with the developers, although I am in no way responsible for it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!