Search

link to homepage

Institute for Advanced Simulation (IAS)

Navigation and service


How to determine the allocated memory of an application at runtime?

The available memory on a JUGENE node in VN mode is: 474 MB (with V1R4M2, June 2010)

Determining memory size/usage in C

Method 1:

#include < sys/resource.h>
#include < common/bgp_personality.h>
#include < common/bgp_personality_inlines.h>
#include < spi/kernel_interface.h&bt
static _BGP_Personality_t mybgp;

/* returns memory per core in MBytes */
unsigned bg_coreMB() {
unsigned procMB, coreMB;
Kernel_GetPersonality(&mybgp, sizeof(_BGP_Personality_t));
procMB = BGP_Personality_DDRSizeMB(&mybgp);
coreMB = procMB/Kernel_ProcessCount();
return coreMB;
}

/* return maximum memory usage of process in kBytes */
unsigned bg_usedKB() {
struct rusage usage;
if (getrusage(RUSAGE_SELF, &usage) != 0)
return 0;
return usage.ru_maxrss;
}

Compile with:

mpicc -O -I/bgsys/drivers/ppcfloor/arch/include

Method 2:

#include < bgp_SPI.h>

uint32_t allocated_memory = 0;
Kernel_GetMemorySize(KERNEL_MEMSIZE_HEAP, &allocated_memory);

Kernel_GetMemorySize along with KERNEL_MEMSIZE_HEAP gives back the allocated memory on heap of the application at runtime.

Example:

#include < mpi.h>
#include < stdlib.h>
#include < bgp_SPI.h>
#include < stdio.h>

const long MB = 1048576;

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

MPI_Init(&argc, &argv);

MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
MPI_Comm_size(MPI_COMM_WORLD, &nprocs);

uint32_t allocated_memory = 0;
Kernel_GetMemorySize(KERNEL_MEMSIZE_HEAP, &allocated_memory);
printf ("Memory allocation on heap: %ld MB\n", allocated_memory/MB);

MPI_Finalize();
return 0;
}

Compile with:

mpixlc_r determine_memory_example.c -o determine_memory_example.x -I/bgsys/drivers/ppcfloor/arch/include/ -I/bgsys/drivers/ppcfloor/arch/include/spi

If you want to obtain the amount of memory allocated on the stack, then replace KERNEL_MEMSIZE_HEAP with KERNEL_MEMSIZE_STACK given in the example above.

Determining memory size/usage in Fortran

Example:

#include < mpi.h>
#include "bgp_SPI.h"
#include < sys/times.h>
#include < sys/resource.h>
#include < assert.h>
#include < stdio.h>
#include < errno.h>
#include < string.h>

void print_memory_usage( void )
{

unsigned int memory_size = 0;

Kernel_GetMemorySize(KERNEL_MEMSIZE_HEAP, &memory_size);

fprintf(stderr, "Memory size HEAP: %10ld \n",
(long) memory_size);

Kernel_GetMemorySize(KERNEL_MEMSIZE_STACK, &memory_size);

fprintf(stderr, "Memory size STACK: %10ld \n",
(long) memory_size);

Kernel_GetMemorySize(KERNEL_MEMSIZE_HEAPAVAIL, &memory_size);

fprintf(stderr, "Memory available HEAP: %10ld \n",
(long) memory_size);

Kernel_GetMemorySize(KERNEL_MEMSIZE_STACKAVAIL, &memory_size);

fprintf(stderr, "Memory available STACK: %10ld \n",
(long) memory_size);

Kernel_GetMemorySize(KERNEL_MEMSIZE_HEAPMAX, &memory_size);

fprintf(stderr, "Maximum memory HEAP: %10ld \n",
(long) memory_size);

}

Compile with:

mpicc -c -I/bgsys/drivers/ppcfloor/arch/include/ -I/bgsys/drivers/ppcfloor/arch/include/spi memory_usage.c

In order to link against the SPI libraries the environment variable MPI_CCLIBS has to be set in the following way:

export MPI_CCLIBS=" -Wl,-rpath,/bgsys/drivers/V1R4M1_460_2009-091110P/ppc/runtime/SPI -lSPI.cna "

The code snippet given below shows how the amount of allocated memory can be obtained for a program at runtime:

program memory_usage

implicit none

integer, parameter :: zeilen=1000
integer, parameter :: spalten=10000
integer :: i

integer,dimension(10000000),parameter :: stack_test=999

integer, dimension(:,:), allocatable :: matrix

write(*,*)('=',i=1,60)
call print_memory_usage()

write(2, '(10i10)' )stack_test

write(*,*)('=',i=1,60)
call print_memory_usage()

allocate(matrix(zeilen,spalten))
matrix=-100
write(1, '(10i10)' )matrix

write(*,*)('=',i=1,60)
call print_memory_usage()

deallocate(matrix)

write(*,*)('=',i=1,60)
call print_memory_usage()
write(*,*)('=',i=1,60)

end program memory_usage

Compile and link with:

mpif90 get_memory.f90 memory_usage.o $MPI_CCLIBS


Environment variables

In case memory runs low in your jobs you can try to increase the memory available for your application on the nodes by using one or more of the environment variables, described in Compiling and Tuning.


Servicemeu

Homepage