(OpenMP, C++ME, System.Threading.Thread) -> Resource Leak.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • PimpDaddy
    New Member
    • Oct 2007
    • 2

    (OpenMP, C++ME, System.Threading.Thread) -> Resource Leak.

    Here is the source code of my C++/ME app. When I run it I can use "Windows Task Manager" to see that thread HANDLEs are leaking.

    Code:
    #include "stdafx.h"
    #include <omp.h>
    
    public __gc class TestClass
    {
    public:
    	void ThreadProc()
    	{
    		int sum = 0;
    	
    #pragma omp parallel for reduction(+:sum)
    		for ( int i = 0; i < 10000; i++ )
    		{
    			sum += i;
    		}
    
    		System::Console::WriteLine( sum.ToString() );
    	}
    };
    
    int main()
    {
    	for (;;)
    	{
    		TestClass __gc * o = __gc new TestClass();
    		System::Threading::Thread __gc * t = __gc new System::Threading::Thread( 
                        __gc new System::Threading::ThreadStart( o, &TestClass::ThreadProc ) );
    
    		t->Start();
    
    		System::Threading::Thread::get_CurrentThread()->Sleep( 100 );
    	}
    }
    If I put something that is not using Open MP into the ThreadProc no HANDLEs will be leaked.
    I realize that what happens is: Omp creates unmanaged threads inside a managed thread (i.e. System::Threadi ng::Thread). I just have very little idea about what could be wrong with it. If somebody will try to reproduce this, I am currently using VS2005.
    Thanks for all your comments and suggestions.
  • gnanapoongothai
    New Member
    • Jun 2007
    • 62

    #2
    I think i have got ur problem. there are unmanaged threads when using openmp.is it so?

    openmp architecture is like when u specify the openmp pragma, it creates a new thread for ur loop and once finished joins the main thread.this might be ur problem. I am not versatile in openmp. But this is a new concept only less ppl use it.so keep in touch to discuss about openmp

    Comment

    • PimpDaddy
      New Member
      • Oct 2007
      • 2

      #3
      We recently discovered that the thread leak has nothing to do with .NET. The same leak occurs even if we run OpenMP code inside a Win32 thread. So, here is the new example. Suggestions\ide as, please?

      Code:
      #include <windows.h>
      #include <stdio.h>
      #include <omp.h>
      
      int calculateArithmeticalProgressionSumOMP( int a1, int d, int n )
      {
      	int sum = 0;
      	omp_set_num_threads(3);
      #pragma omp parallel 
      	{
      #pragma omp for reduction (+: sum)
      		for( int i = 0; i < n; i++)
      		{
      			int a = a1 + i * d;
      			sum += a;                                                                                                                                                                                      
      		}
      	}
      	return sum;
      }
      
      DWORD threadFunc(LPVOID pParams)
      {
      	int sum = calculateArithmeticalProgressionSumOMP(0, 1, 10000);
      	printf("%d\n", sum);
      	return 0;
      }
      
      int main()
      {
      	//create some threads
      	for(int i = 0; i < 100; i++)
      	{
      		HANDLE handle;
      		handle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE )threadFunc, NULL, 0, NULL);
      		WaitForSingleObject(handle, INFINITE);
      		Sleep(100); //imitating some other work
      	}
      
      	return 0;
      }

      Comment

      Working...