Hi

The problem I am having is as follows,

1) I have arrays A and B of size N.

2) I have to sort A and also make B in the same sorted permutation as that of A.

This is how currently I have implemented in my code using TBB sort. But I find that the in-place re-ordering algorithm is a serial implementation and is a major bottleneck in my code. How can I do this more efficiently using only TBB sort and possibly avoid the use of permutation array ?

std::vector<unsigned> order(N);

std::vector<double> A(N), B(N);

for( unsigned i = 0; i < N; ++i ) order[i] = i;

CompFunctor compare(A);

tbb::parallel_sort( order.begin(), order.end(), compare ); /// Runs in parallel

InPlacePermute( &A[0], &B[0], &order[0], N ); /// Runs in serial

/// Comparison functor definition

struct CompFunctor {

CompFunctor( std::vector<double> &a )

: _data(a) { }

inline bool operator()( uintT i, uintT j ) const {

return _data[i] < _data[j];

}

private:

std::vector<double> &_data;

}

/// Inplace permute

template < typename T1, typename T2 >

void InplacePermutation( T1 *data1, T2 *data2, unsigned *perm, size_t size ) {

T1 temp1;

T2 temp2;

unsigned j, k;

for( unsigned i = 0; i < size; ++i ) {

if( i != perm[i] ) {

temp1 = data1[i];

temp2 = data2[i];

j = i;

while( i != perm[j] ) {

k = perm[j];

data1[j] = data1[k];

data2[j] = data2[k];

perm[j] = j;

j = k;

}

data1[j] = temp1;

data2[j] = temp2;

perm[j] = j;

}

}

}