This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Memory leak or incorrect use of '#pragma omp parallel'?


On Tue, 12 May 2009 09:33:34 +0300 Mikolaj Golub wrote:

 MG> On Mon, 11 May 2009 19:00:54 +0300 Mikolaj Golub wrote:

 >> Hello,
 >>
 >> If I run the following program
 >>
 >> ---------------
 >>
 >> #include <omp.h>
 >>
 >> int n = 4, m = 2;
 >>
 >> int main () {
 >>         for (;;) {
 >>                 int i;
 >>
 >> #pragma omp parallel num_threads(m)
 >>                 {
 >>                         int just_to_make_some_code_generated;
 >>                         just_to_make_some_code_generated = 0;
 >>                 }
 >> #pragma omp parallel for num_threads(n)
 >>                 for(i = 0; i < 1; i++) {}
 >>
 >>         }
 >>
 >>         return 0;
 >> }
 >>
 >> ---------------
 >>
 >> on FreeBSD6/7 (i386 and amd64) built with gcc42 or gcc43 -fopenmp (I have not
 >> tried other gcc versions), the memory usage is constantly growing during the
 >> run. The memory growth is observed in case n != m. If n == m, everything works
 >> ok.

 MG> The problem is observed also for constructions like this one:

 MG> #pragma omp parallel for num_threads(m)
 MG> ...
 MG> #pragma omp parallel for num_threads(n)
 MG> ...

 MG> Adding some sleep() code I see in top that during the program run m or n
 MG> threads are run in turn. So it looks like memory leaks when thread is
 MG> removed. It might be FreeBSD problem, not gcc.

The problem is in libgomp/team.c.  gomp_thread_start() does gomp_sem_init()
but gomp_sem_destroy() is never called. FreeBSD implementation of sem_init()
allocates some memory for semaphore. This patch solves the problem for me:

--- gcc-4.4-20090227/libgomp/team.c.orig        2009-05-16 22:57:05.000000000 +0300
+++ gcc-4.4-20090227/libgomp/team.c     2009-05-16 22:57:14.000000000 +0300
@@ -458,6 +458,7 @@ gomp_team_start (void (*fn) (void *), vo
 void
 gomp_team_end (void)
 {
+  int i;
   struct gomp_thread *thr = gomp_thread ();
   struct gomp_team *team = thr->ts.team;
 
@@ -493,6 +494,9 @@ gomp_team_end (void)
        }
       while (ws != NULL);
     }
+
+  for(i = 1; i < team->nthreads; i++)
+    gomp_sem_destroy (team->ordered_release[i]);
   gomp_sem_destroy (&team->master_release);
 #ifndef HAVE_SYNC_BUILTINS
   gomp_mutex_destroy (&team->work_share_list_free_lock);

I am going to register this in bugzilla.

The problem is not observed under Linux, because it looks like sem_init()
implementation for Linux does not do memory allocation in the process' virtual
space. The memory for the test program below grows under FreeBSD and does not
under Linux.

#include <semaphore.h>

int
main(int argc, char *argv[]) {

        sem_t sem;

        for(;;) { sem_init(&sem, 0, 0);}

        return 0;
}

-- 
Mikolaj Golub


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]