parallel-processingfortrangfortranopenmpigprof

How to compile a Fortran code for profiling with gprof in OpenMPI?


I am able to compile my Openmpi code using gfortran compiler. The compile syntax I give is :

mpif90 -o mycode.exe mycode.f90

mpirun -np 4 ./mycode.exe

It works. And now I want to profile my code using Gprof. I know I should add the -pg flag, but if I put it after mpif90 it crashed. Where should I put the flag?


Solution

  • You can give this a try, see how it yields. Assume you are using openmpi. Create the following as script named mywrapper.sh

    #!/bin/bash
    prefix="gmon_${OMPI_COMM_WORLD_RANK}.out"
    GMON_OUT_PREFIX=$prefix $*
    

    And run your code with

    mpif90 -o mycode.exe -pg mycode.f90
    mpirun -np 4 mywrapper.sh ./mycode.exe
    

    Test code of profiling MPI code (I am using C, but FORTRAN should work without difference) :

    //file x.c
    #include <mpi.h>
    #include <stdio.h>
    int main(int argc, char *argv[])
    {
        int rank=9;
        MPI_Init(&argc, &argv);
        MPI_Comm_rank(MPI_COMM_WORLD, &rank);
        printf("Hello from rank=%d!\n", rank);
        MPI_Finalize();
        return 0;
    }
    

    Command to compile and run the test

    #!/bin/bash
    mpicc -pg x.c
    mpirun --hostfile hostfile mywrapper.sh ./a.out
    

    For simplicity test, hostfile use local host only. The command output:

    Hello from rank=1!
    Hello from rank=2!
    Hello from rank=0!
    

    Then you get those gprof file, if cannot, must be mpi configuration issue.

    .
    ├── a.out
    ├── gmon_0.out.2690
    ├── gmon_1.out.2692
    ├── gmon_2.out.2693