Answer the question
In order to leave comments, you need to log in
Is it possible and does it make sense to transfer this code to OpenCL?
Good day!
There is this piece of code:
struct TTour {
QVector<int> tour;
double cost;
};
TTour Tournamet::iter(const TTour ¤tTour)
{
int n = currentTour.tour.count();
TTour bestTour = currentTour;
for(int i=1; i<n-2; ++i){
for(int j=i+1; j<n+1; ++j) {
if(j-i == 1) continue;
QVector<int> swap = currentTour.tour.mid(0, i);
QVector<int> l1 = currentTour.tour.mid(i, j-i);
QVector<int> l2 = currentTour.tour.mid(j);
std::reverse(l1.begin(), l1.end());
swap.append(l1);
swap.append(l2);
TTour newTour = {swap, cost(swap)};
if( newTour.cost < bestTour.cost ) bestTour = newTour;
}
}
}
double Tournamet::cost(const QVector<int> &points)
{
double total = 0;
for(int i=1; i<points.count(); ++i) {
total += _distancer->dist(points[i-1], points[i]); //-- Внутри расстояние отдаётся из QHash, каждый раз не вычисляется.
}
return total;
}
Answer the question
In order to leave comments, you need to log in
Most likely parallelism will give nothing. The matter is that tasks when
1) Shared nothing are paralleled. There are many processes and they work with their data arrays and then merge the result into a certain result.
2) The data is rummaged but at the same time they are immutable.
In your case, operations such as std::reverse, QVector::mid are used. They break the general data snapshot and do not allow point (2) to come out.
In general, you need to seriously break the algorithm in order to get the creep from parallelism.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question