Custom iterator works with std::sort but not with tbb::parallel_sort?

前端 未结 4 660
日久生厌
日久生厌 2021-02-15 18:13

I am trying to use tbb::parallel_sort to sort 2 arrays at the same time. Intel\'s documentation here says https://software.intel.com/en-us/node/506167 The req

相关标签:
4条回答
  • 2021-02-15 18:51

    The first error reads "no appropriate default constructor available..", but Iterators must be default constructible.

    Whether the default construction is used in one algorithm or another doesn't matter, the requirement still holds. So compilation success with std::sort and failure with tbb::parallel_sort doesn't mean the The requirements on the iterator and sequence are the same as for std::sort isn't true; it just means that the implementation of std::sort that you are using doesn't rely on that pre-requisite.

    If you implement your iterator to conform to the subset of standard that is relied upon by both algorithms, then it should work, per the documentation.

    0 讨论(0)
  • 2021-02-15 18:57

    The class quick_sort_range in tbb/parallel_sort.h contains RandomAccessIterator begin; member which is copy-initialized in one its constructor and default-initialized and then assigned in the other constructor. Thus it requires iterators which are default&copy-constructable and assignable.

    So, the TBB documentation is not correct claiming the same requirements as std::sort since the later requires just random-access iterators which are not required to be assignable while TBB implementation requires it for versions <= 4.4.

    The default-constructable and assignable requirements can be fixed but move or copy-constructable will likely remain (making the claim in the documentation correct). You can report this issue on TBB Forum.

    You can safely add default&copy constructors and assignment operator to your code to compile it with tbb::parallel_sort as far as I can see.

    Here is the online compiler with your snippet: http://coliru.stacked-crooked.com/a/47dafd091d36a9c4

    0 讨论(0)
  • 2021-02-15 18:58

    I was not able to compile basic use case (i.e. with std::sort). Thus, the code is adapted to compile successfully in one specific compiler case.

    BTW, RandomAccessIterator satisfies ForwardIterator requirements too. And if we look at the ForwardIterator's requirements we find out that it should be DefaultConstructible. (see §24.2.5 Forward iterators, section (1.2) in one of the latest C++ standard's working draft)

    0 讨论(0)
  • 2021-02-15 19:05

    For a RandomAccessIterator, reference must be a reference to value_type. It cannot be a tuple of references.

    As such, your dual iterator is not a valid RandomAccessIterator.

    Many algorithms will still work, but that doesn't make your code valid.

    The requirements being the same does not mean that anything that works with a given implementation of std::sort will also work with tbb::parallel_sort: a given implementation of std::sort does not have to enforce all of the requirements made in the standard.

    Regardless of the documentation, if the implementation won't work with your code, it won't work with your code.

    The easiest approach might be to create an array of pseudo-pairs of indexes (or iterators) into the original arrays, then sort it. You just need to override < properly.

    0 讨论(0)
提交回复
热议问题