Instrumenting Your Fortran Code to Check Process/Thread Placement

Article ID: 309
Last updated: 30 Nov, 2021

Pinning, the binding of a process or thread to a specific core, can improve the performance of your code.

You can insert the MPI function mpi_get_processor_name and the Linux C function sched_getcpu into your source code to check process and/or thread placement. The MPI function mpi_get_processor_name returns the hostname an MPI process is running on (to be used for MPI and/or MPI+OpenMP codes only). The Linux C function sched_getcpu returns the processor number the process/thread is running on.

If your source code is written in Fortran, you can use the C code mycpu.c, which allows your Fortran code to call sched_getcpu. The next section describes how to use the mycpu.c code.

C Program mycpu.c

#include <utmpx.h>
int sched_getcpu();

int findmycpu_ ()
{
    int cpu;
    cpu = sched_getcpu();
    return cpu;
}

Compile mycpu.c as follows to produce the object file mycpu.o:

pfe21% module load comp-intel/2020.4.304
pfe21% icc -c mycpu.c

The following example demonstrates how to instrument an MPI+OpenMP source code with the above functions. The added lines are highlighted.

      program your_program
      use omp_lib 
...
      integer :: resultlen, tn, cpu
      integer, external :: findmycpu
      character (len=8) :: name 

      call mpi_init( ierr )
      call mpi_comm_rank( mpi_comm_world, rank, ierr )
      call mpi_comm_size( mpi_comm_world, numprocs, ierr )
      call mpi_get_processor_name(name, resultlen, ierr) 
!$omp parallel

      tn = omp_get_thread_num()
      cpu = findmycpu()  
      write (6,*) 'rank ', rank, ' thread ', tn,
     &  ' hostname ', name, ' cpu ', cpu 
.....
!$omp end parallel
      call mpi_finalize(ierr)
      end

Compile your instrumented code as follows:

pfe21% module load comp-intel/2020.4.304
pfe21% module load mpi-hpe/mpt
pfe21% ifort -o a.out -qopenmp mycpu.o your_program.f -lmpi

Sample PBS script

The following PBS script provides an example of running the hybrid MPI+OPenMP code across two nodes, with 2 MPI processes per node and 4 OpenMP threads per process, and using the mbind tool to pin the processes and threads.

#PBS -lselect=2:ncpus=28:mpiprocs=2:model=bro
#PBS -lwalltime=0:10:00

cd $PBS_O_WORKDIR

module load comp-intel/2020.4.304
module load mpi-hpe/mpt

mpiexec -np 4 mbind.x -cs -t4 -v ./a.out

Here is a sample output:

These 4 lines are generated by mbind only if you have included the -v option:
host: r627i4n1, ncpus: 56, rank: 0 (r0), nthreads: 4, bound to cpus: {0-9:3}
host: r627i4n1, ncpus: 56, rank: 1 (r1), nthreads: 4, bound to cpus: {14-23:3}
host: r627i4n8, ncpus: 56, rank: 2 (r0), nthreads: 4, bound to cpus: {0-9:3}
host: r627i4n8, ncpus: 56, rank: 3 (r1), nthreads: 4, bound to cpus: {14-23:3}

These lines are generated by your instrumented code:
rank    0 thread    3 hostname r627i4n1 cpu    0
rank    0 thread    3 hostname r627i4n1 cpu    3
rank    0 thread    3 hostname r627i4n1 cpu    6
rank    0 thread    3 hostname r627i4n1 cpu    9
rank    1 thread    3 hostname r627i4n1 cpu   14
rank    1 thread    3 hostname r627i4n1 cpu   17
rank    1 thread    3 hostname r627i4n1 cpu   20
rank    1 thread    3 hostname r627i4n1 cpu   23
rank    2 thread    3 hostname r627i4n8 cpu    0
rank    2 thread    3 hostname r627i4n8 cpu    3
rank    2 thread    3 hostname r627i4n8 cpu    6
rank    2 thread    3 hostname r627i4n8 cpu    9
rank    3 thread    3 hostname r627i4n8 cpu   14
rank    3 thread    3 hostname r627i4n8 cpu   17
rank    3 thread    3 hostname r627i4n8 cpu   20
rank    3 thread    3 hostname r627i4n8 cpu   23

Note: In your output, these lines may be listed in a different order.

Contact Us

General User Assistance

Security

  • Report security issues 24x7x365
  • Toll-free: 1-877-NASA-SEC (1-877-627-2732)
  • E-mail: soc@nasa.gov

User Documentation

High-End Computing Capability (HECC) Portfolio Office

NASA High-End Computing Program

Tell Us About It

Our goal is furnish all the information you need to efficiently and effectively use the HECC resources needed for your NASA computational projects.

We welcome your input on features and topics that you would like to see included on this website.

Please send us email with your wish list and other feedback.