Dynamic momory allocation in a parallel region

General OpenMP discussion

Dynamic momory allocation in a parallel region

Postby bluekds » Mon Jul 14, 2008 6:16 am

Hello!

Now, I am converting my old tree construction code into new code using parallel processing.
And I found a problem related with dynamic memory allocation.

the code is like bellow
-------------------------------------------------------------------------------------------------------------------
#pragma omp parallel sections shared( divideBuf, buf_left )
{
#pragma omp section
{
rootNode->setLeftChild( leftNode->construct( divideBuf, buf_left ) ) ;
}
#pragma omp section
{
rootNode->setRightChild( rightNode->construct( divideBuf + buf_left, numTri - buf_left ) ) ;
}
}
-------------------------------------------------------------------------------------------------------------------
In the construct method, it makes two children by using dynamic allocation.
As a result, it shows slower speed rather than using single thread.

So I made a method that does 1000 iteration of dynamic allocation and release and tested parallel processing by using the method.
And get the same situation with my code.

In my opinion, dynamic memory allocating cannot be doing in parallel.

What is your opinion?

Is there someone who had a similar problem before? How can you treat this problem?

Thanks.
bluekds
 

Re: Dynamic momory allocation in a parallel region

Postby djo35 » Fri Jul 18, 2008 8:51 am

I can't quite see what your code is up to (I guess construct allocates memory)

From this
Code: Select all
#pragma omp parallel sections shared( divideBuf, buf_left )

you seem to be allocating shared memory. This has to be done in a serial fashion otherwise you could have all sorts of memory trample issues. If you really want to allocate memory this way you'll probably have to do it in thread private space.

Also, mallocing / freeing memory is slow (expensive). Do you really need to do this so many times that it needs parallelisation? It's often easier to alloc memory at the start and reuse it rather than to create / free it on the fly.

Dan
djo35
 
Posts: 11
Joined: Wed May 28, 2008 8:24 am

Re: Dynamic momory allocation in a parallel region

Postby ejd » Mon Jul 21, 2008 8:22 am

Dan (djo35) is right - you haven't really shown enough to really understand what is going on. Dynamic memory allocation itself can be done in parallel safely. You can look at various write-ups about mtmalloc, ptmalloc, or hoard to find out more about this. The general problem is that a lot of compilers will use the default malloc even with OpenMP and the default version usually protects itself with a fairly high level lock that limits scalability.
ejd
 
Posts: 1025
Joined: Wed Jan 16, 2008 7:21 am

Re: Dynamic momory allocation in a parallel region

Postby bluekds » Wed Aug 13, 2008 11:13 pm

Thanks for your answers.

What I want to do is like bellow.

--------------------------------------------------------------------------
class Node {
public :
Node* leftChild ;
Node* rightChild ;
}
---------------------------------------------------------------------------
#pragma omp parallel sections
{
#pragma omp section
{
rootNode->leftChild = new Node() ;
}
#pragma omp section
{
rootNode->rightChild = new Node() ;
}
}
----------------------------------------------------------------------------

When I excuted the code, I can find that multi-thread excution was slower than single thread excution.
So I guessed dynamic allocation is cannot done at same time.

Your answers are very helpfull for me.
And this site is also helpfull..
http://developers.sun.com/solaris/artic ... iproc.html

I will try to use other library of allocator and memory pool.

Thanks.
Duksu Kim
SGLab, KAIST
bluekds
 
Posts: 8
Joined: Mon Jul 14, 2008 6:18 am


Return to Using OpenMP

Who is online

Users browsing this forum: No registered users and 13 guests