stack or heap variables

I have one working solution for parallelization . However, execution time is very very slightly improved by parallelization. I thinks it comes from the fact I new and delete some variable in the loop. I would like it to be stack created, BUT Command class is abstract, and must remain abstract. What can I do to work around that? How to improve time spent on these very long loops???

#pragma omp parallel for  reduction(+:functionEvaluation)
for (int i=rowStart;i<rowEnd+1;i++)
{
    Model model_(varModel_);
    model_.addVariable("i", i);
    model_.addVariable("j", 1);
    Command* command_ = formulaCommand->duplicate(&model_);
    functionEvaluation += command_->execute().toDouble();
    delete command_;
}

The problem may also lie elsewhere! advice welcome!!

thanks and regards.


You may want to play with the private or firstprivate clauses.

Your #pragma would include ...private(varModel, formulaCommand)... or similar, and then each thread would have its own copy of those variables. Using firstprivate will ensure that the thread-specific variable has the initial value copied in, instead of being uninitialized. This would remove the need to new and delete , assuming you can just modify instances for each loop iteration.

This may or may not work as desired, as you haven't provided a lot of detail.


I think you should try to use a mechanism to reuse allocated memory. You probably don't know the size nor the alignment of the Command object coming, so a "big enough" buffer will not suffice. I'd make your duplicate method take two arguments, the second being the reference to a boost::pool . If the pool object is big enough just construct the new Command object inside it, if it's not expand it, and construct into it. boost::pool will handle alignment issues for you, so you don't have to think about it. This way, you'll have to do dynamic memory allocation only a few times per thread.

By the way, it's in general not good practice to return raw pointers in C++. Use smart pointers instead, it's simply better that way without any buts... Well, there's a but in this case :), since with my suggestion you'd be doing some under the hood custom memory management. Still, the bestest practice would be to write a custom smart pointer which handles your special case gracefully, without risking the user to mess up. You could of course do like everyone else and make en exception in this case :) (My advice still holds under normal circumstances though, fx in the question above, you should normally use something like boost::scoped_ptr )

链接地址: http://www.djcxy.com/p/79260.html

上一篇: OpenMP任务中的数据属性

下一篇: 堆栈或堆变量