Intel® oneAPI Threading Building Blocks
Ask questions and share information about adding parallelism to your applications when using this threading library.

tbb::zip_iterator and std::sort

Timocafé
Beginner
1,423 Views

Hello all,

I was waiting zip_iterator since a long time. I discover there are part of TBB now. Unfortunately, I get issue with GCC 7.4 (but working with my Mac/clang).

The following example work on my Mac with last version of Apple-clang (Apple clang version 11.0.0 (clang-1100.0.33.16)) but it does not compile with GCC 7.4 on my ubuntu station. I get the following error  

/usr/include/c++/7/bits/predefined_ops.h:215:11: note: candidate: bool (*)(const std::tuple<float&, float&>&, const std::tuple<float&, float&>&) <conversion>
/usr/include/c++/7/bits/predefined_ops.h:215:11: note:   candidate expects 3 arguments, 3 provided
main.cpp:30:102: note: candidate: main()::<lambda(const std::tuple<float&, float&>&, const std::tuple<float&, float&>&)>
     std::sort(start, end, [](const std::tuple<float&, float&>& v, const std::tuple<float&, float&>& w) {
                                                                                                      ^
#include <algorithm>
#include <iostream>
#include <tuple>
#include <vector>
#include <random>
#include <tbb/iterators.h>

int main() {
    const int N = 10;
    std::vector<float> a(N), b(N);

 // First create an instance of an engine.
    std::random_device rnd_device;
    // Specify the engine and distribution.
    std::mt19937 mersenne_engine {rnd_device()};  // Generates random integers
    std::uniform_int_distribution<int> dist {1, 52};

    auto gen = [&dist, &mersenne_engine](){
                   return dist(mersenne_engine);
               };

    generate(begin(a), end(a), gen);
    generate(begin(b), end(b), gen);


    auto start = tbb::make_zip_iterator(a.begin(), b.begin());
    auto end   = tbb::make_zip_iterator(a.end(), b.end());

    std::sort(start, end, [](const std::tuple<float&, float&>& v, const std::tuple<float&, float&>& w) {
          return std::get<1>(v) < std::get<1>(w);
    });

    for (int i=0; i< N; ++i)
        std::cout << a <<  " " << b << std::endl;


    return 0;
}

Any suggestions ?

All the best

++T

0 Kudos
1 Solution
Mark_L_Intel
Moderator
1,423 Views

Hi,

    I asked internally developer of this feature to comment why 2019.8 version works vs. 2019.2. 

    Can you work with 2019.8 (in other words, there is no requirement in your case to work with previous version)? 

 

 

View solution in original post

0 Kudos
4 Replies
Timocafé
Beginner
1,423 Views

Hum I have been able to run the example by updated my TBB 2019.2 to TBB 2019.8 and removing the reference into the tupple 

0 Kudos
Mark_L_Intel
Moderator
1,424 Views

Hi,

    I asked internally developer of this feature to comment why 2019.8 version works vs. 2019.2. 

    Can you work with 2019.8 (in other words, there is no requirement in your case to work with previous version)? 

 

 

0 Kudos
Timocafé
Beginner
1,423 Views

Hello,

Thank you for the answer, indeed I have no problem to work with 2019.8. By the way I also remark that the sort is not "stable" following the STL implementation. Currently, GCC 7.4 and Clang-apple provide different sorting. It is not an issue, but good to know. 

Have a good day, and merry xmas 

++t

0 Kudos
Pablo_R_Intel
Employee
1,423 Views

Thanks for your feedback. We improved TBB's zip iterator from 2019 Update 5: https://github.com/intel/tbb/blob/tbb_2020/CHANGES

It works now with algorithms such as sort that swap values via iterators. 

If you're interested in C++ parallel algorithm extensions, you might want to check out Parallel STL: https://github.com/intel/parallelstl. Current version is using TBB's fancy iterators such as zip-iterator, and TBB as a backend for Threading (par and par_unseq policy)

-Pablo

0 Kudos
Reply