none
OpenMP question: How to handle the case more and more threads are created RRS feed

  • Question

  • I want to ask a question about using OpenMP. I hope there are some guys here who can give me some helps on this.
    I've a class which will create a parallel region in the following way. m_LThreadNum is always equal to my core number. And this Run method is actually running in a thread launched by _begin_thread api.

    Network::Run()
    {
    #pragma omp parallel if(bParallel) num_threads(m_lThreadNum)
    #pragma omp for schedule(dynamic)
    for(long lIndex = m_lRDPModuleIndex + 1; lIndex < (long)m_vecModules.size(); lIndex ++)
    {

    m_Error = m_pThisDomain.GetComPtr()->RunModule(lIndex, dStopTime);
    }
    // end of parallel for
    }

    Each time I create this object, and then call its Run method. My core number is 8. Then I noticed each time 8 threads will be created and left there. So, the number of the threads will be more and more. What I'm using is Microsoft VS 2008.

    I don't know how I can control the destroy of the threads in OpenMP. Suppose we don't need to care for this, right?
    Monday, August 30, 2010 10:31 AM

Answers

  • No.  Nor does VS2010.

    Zhang, are you saying that you are seeing constant thread growth with OMP?  Or that you are concerned that you will see continued growth?

    Have you tried setting a max number of threads?  See this documentation: http://msdn.microsoft.com/en-us/library/3aw0bk98(v=VS.80).aspx and http://msdn.microsoft.com/en-us/library/e16bady3(v=VS.80).aspx

    -Dana

    • Marked as answer by Zhang Pengfei Wednesday, September 8, 2010 2:02 AM
    Friday, September 3, 2010 1:28 AM
  • Dana,

    Setting max thread number doesn't work as I guessed. However, I used omp_set_dynamic(1), and it really made the thread leak much lesser than before.

    Orginally, each time after I create a new Network and Run, there will be 8 threads left. After using omp_set_dynamic, in many cases, the thread number will kept as 8. That's to say, these 8 threads are reused! However, there is indeed some light-weight leak. That's to say, if I create a new Network and Run it, and repeat to do so several times, the total thread number might be a little more than before (1~2 threads are more).

    I'm very confused by this fact. However, the situation is much better. So, I will fist use this solution. Thanks!

    Could you help me on these two questions:

    1. why there are still some light leak? how can we eliminate it?

    2. why omp_set_dynamic(1) can work? I cannot understand this API.

    - Pengfei

    • Marked as answer by Zhang Pengfei Wednesday, September 8, 2010 2:02 AM
    Monday, September 6, 2010 2:51 AM

All replies

  • You can try the OpenMP new feature - omp task - which is available in OpenMP 3.0. It schedules existing threads instead of creating new threads everytime the construct is encountered. You may want to checkout this: http://myxman.org/dp/node/183   which uses omp task in parallel quicksort.

    Posted by SpeedGo Computing: http://www.speedgocomputing.com

    • Proposed as answer by myxman Wednesday, September 1, 2010 5:39 AM
    Wednesday, September 1, 2010 5:37 AM
  • But Visual Studio 2008 supports OpenMP 3.0?
    Thursday, September 2, 2010 1:40 AM
  • No.  Nor does VS2010.

    Zhang, are you saying that you are seeing constant thread growth with OMP?  Or that you are concerned that you will see continued growth?

    Have you tried setting a max number of threads?  See this documentation: http://msdn.microsoft.com/en-us/library/3aw0bk98(v=VS.80).aspx and http://msdn.microsoft.com/en-us/library/e16bady3(v=VS.80).aspx

    -Dana

    • Marked as answer by Zhang Pengfei Wednesday, September 8, 2010 2:02 AM
    Friday, September 3, 2010 1:28 AM
  • Dana,

    Setting max thread number doesn't work as I guessed. However, I used omp_set_dynamic(1), and it really made the thread leak much lesser than before.

    Orginally, each time after I create a new Network and Run, there will be 8 threads left. After using omp_set_dynamic, in many cases, the thread number will kept as 8. That's to say, these 8 threads are reused! However, there is indeed some light-weight leak. That's to say, if I create a new Network and Run it, and repeat to do so several times, the total thread number might be a little more than before (1~2 threads are more).

    I'm very confused by this fact. However, the situation is much better. So, I will fist use this solution. Thanks!

    Could you help me on these two questions:

    1. why there are still some light leak? how can we eliminate it?

    2. why omp_set_dynamic(1) can work? I cannot understand this API.

    - Pengfei

    • Marked as answer by Zhang Pengfei Wednesday, September 8, 2010 2:02 AM
    Monday, September 6, 2010 2:51 AM