我已经使用 parallel_for 实现了该算法。但大多数情况下我使用同步部分,所以我没有利润。也许有更好的选择?
tbb::parallel_for (tbb::blocked_range<int>(1, m * n), apply_transform(d, j, this, m, n));
void apply_transformation(int * d, int i, int j, int n){
int elem1 = (*v1)[i];
int elem2 = (*v2)[j];
if(elem1 == elem2){
dLock.acquire(dMutex);
d[i*n + j] = d[(i-1)*n + j-1]; // no operation required
dLock.release();
} else {
dLock.acquire(dMutex);
d[i*n + j] = std::min(std::min(d[(i-1)*n + j] + 1, //deletion
d[i*n + j-1] + 1), //insertion
d[(i-1)*n + j-1] + 1); //substitution
dLock.release();
}
}
class apply_transform{
int * array;
int m_j;
Levenstein * m_l;
int m, n;
public:
apply_transform (int* a, int j, Levenstein * l, int width, int height):
array(a), m_j(j), m_l(l), m(width), n(height) {}
void operator()(const tbb::blocked_range<int>& r ) const {
for (int i=r.begin(); i!=r.end(); i++ ){
m_l->apply_transformation(array, i, m_j, n);
}
}
};