tbb::zip_iterator and std::sort

TCE Options

Nivel de TCE: 

Fecha de apertura de TCE: 

Martes, 17 diciembre, 2019 - 22:17

tbb::zip_iterator and std::sort

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[i] <<  " " << b[i] << std::endl;

    return 0;

Any suggestions ?

All the best


publicaciones de 5 / 0 nuevos

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 

Best Reply


    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)? 




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 


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)


Deje un comentario

Por favor inicie sesión para agregar un comentario. ¿No es socio? Únase ya