R
Robert Fendt
Hi,
I am currently trying to parallelise an image transformation loop. The transformations are arbitrary and performed consecutively, so they currently are kept in a vector of functor pointers. A much simplified version looks like this (syntax errors and typos notwithstanding):
class FunctorBase
{
virtual void get_coord(double& x, double&y) = 0;
};
class Transform
{
public:
void transform()
{
#pragma omp parallel for
for (size_t j = 0; j < lines; ++j)
{
for (size_t i = 0; i < lines; ++i)
{
...
get_coord(x, y);
...
}
}
}
private:
void get_coord(double& x, double&)
{
for (size_t i = 0; i < n_func_; ++i)
{
func_->get_coord(x, y);
}
}
private:
std::vector<FunctorBase*> func_;
size_t n_func_;
};
If I allow more than one thread, the result is garbled (for OMP_NUM_THREADS==1, everything is fine), though I do not really understand why. The transformation functors do not use ugly stuff like static variables or similar, and I always thought it was save to call a method in the same object from different threads, _provided_ one does not change the object state (i.e., all member variables are untouched).
So: what do I do? I can wrap the transformation list in a separate class and implement a copy constructor for it, which would enable me to put a thread-private instance of it on the stack. But this is quite a lot of overhead, so I was hoping that maybe I am not seeing the wood for the trees here (i.e., overlooking the obvious).
Regards,
Robert
I am currently trying to parallelise an image transformation loop. The transformations are arbitrary and performed consecutively, so they currently are kept in a vector of functor pointers. A much simplified version looks like this (syntax errors and typos notwithstanding):
class FunctorBase
{
virtual void get_coord(double& x, double&y) = 0;
};
class Transform
{
public:
void transform()
{
#pragma omp parallel for
for (size_t j = 0; j < lines; ++j)
{
for (size_t i = 0; i < lines; ++i)
{
...
get_coord(x, y);
...
}
}
}
private:
void get_coord(double& x, double&)
{
for (size_t i = 0; i < n_func_; ++i)
{
func_->get_coord(x, y);
}
}
private:
std::vector<FunctorBase*> func_;
size_t n_func_;
};
If I allow more than one thread, the result is garbled (for OMP_NUM_THREADS==1, everything is fine), though I do not really understand why. The transformation functors do not use ugly stuff like static variables or similar, and I always thought it was save to call a method in the same object from different threads, _provided_ one does not change the object state (i.e., all member variables are untouched).
So: what do I do? I can wrap the transformation list in a separate class and implement a copy constructor for it, which would enable me to put a thread-private instance of it on the stack. But this is quite a lot of overhead, so I was hoping that maybe I am not seeing the wood for the trees here (i.e., overlooking the obvious).
Regards,
Robert