Open MPI logo

Open MPI User's Mailing List Archives

  |   Home   |   Support   |   FAQ   |   all Open MPI User's mailing list

Subject: [OMPI users] Possible memory leak in MPI_Gather
From: Silvio a Beccara (s.abeccara_at_[hidden])
Date: 2012-12-03 05:18:07


Dear users,

I have found a memory leak in an MPI program of mine. Basically the
memory used by the program increases first rapidly, and then linearly,
even if slowly, with time.

I reproduced the error in a very simple program, which I include here:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <mpi.h>

typedef struct {

  double* send_buffer;
  double* receive_buffer;

} my_structure;

my_structure* my_structure_alloc ( int nelems, int nprocs ) {

  my_structure* my_object;
  my_object = ( my_structure* ) malloc ( sizeof ( my_structure ) );

  if ( !my_object ) return ( NULL );

  my_object -> send_buffer = ( double* ) malloc ( nelems * sizeof ( double ) );
  my_object -> receive_buffer = ( double* ) malloc ( nelems * nprocs *
sizeof ( double ) );

  return ( my_object );

}

void my_structure_free ( my_structure* my_object ) {

  free ( my_object -> send_buffer );
  free ( my_object -> receive_buffer );

}

void collect_elements ( my_structure* my_object, int nelems, int
idproc, int istep, int nsteps ) {

  int ielem;

  static double* send_buffer = NULL;
  if ( send_buffer == NULL ) send_buffer = ( double* ) malloc ( nelems
* sizeof ( double ) );

  for ( ielem = 0; ielem < nelems; ielem++ ) {
    send_buffer [ ielem ] = sqrt ( 2.0 * ( double ) idproc * rand() );
  }

  MPI_Gather ( send_buffer, nelems, MPI_DOUBLE, my_object ->
receive_buffer, nelems, MPI_DOUBLE, 0, MPI_COMM_WORLD );

  if ( istep == nsteps - 1 ) free ( send_buffer );

} //end collect_elements

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

  int idproc;
  int nprocs;
  int nelems = 1;
  int istep;
  long nsteps;

  MPI_Init ( &argc, &argv );
  MPI_Comm_rank ( MPI_COMM_WORLD, &idproc );
  MPI_Comm_size ( MPI_COMM_WORLD, &nprocs );

  my_structure* my_object;

  my_object = my_structure_alloc ( nelems, nprocs );

  nsteps = 1000000000;

  for ( istep = 0; istep < nsteps; istep++ ) {
      collect_elements ( my_object, nelems, idproc, istep, nsteps );
    if ( istep % 1000 == 0 ) { fprintf ( stderr, "# ISTEP = %i\n", istep ); }
  } //for istep

 my_structure_free ( my_object );

 MPI_Finalize();

} //end main

I compiled with the following command:
mpicc.openmpi -lm Mpi_Gather_leak.c -o Mpi_Gather_leak

and run the following way:
mpirun.openmpi -np 4 -mca mpi_leave_pinned 0 ./Mpi_Gather_leak

I check memory usage by using the pidof command. Analysis of the
memory with the mtrace() function gives no reference to unfreed memory
in the program, but only
to hexadecimal addresses (which I reckon refer to some library).

I have OpenMPI version 1.4.3, running on a Kubuntu 12.04 Intel 64 system.

Best regards

Silvio a Beccara

FBK Foundation
via Sommarive, 18
I-38123 Povo TN
ITALY
http://www.fbk.eu