1

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?

Shiqi He
  • 95
  • 3
  • 10
  • 2
    Please state the exact error message you are getting and the complete command line you're using. – Robert Harvey Feb 19 '19 at 01:56
  • Yeah, that's the problem. I actually do not know how to compile the code with gprof. And if I write "mpif90 -o -pg mycode.exe mycode.f90", it does not give me the executable file, but create a file named "-pg". – Shiqi He Feb 19 '19 at 02:15
  • 1
    That which follows -o is the executable 's name. Try placing the -pg before -o or after mycode.exe. – jbdv Feb 19 '19 at 05:10
  • 1
    And apparently the profiling output is all in one file except if you define `GMON_OUT_PREFIX` https://stackoverflow.com/a/30085788/3327666 – Pierre de Buyl Feb 19 '19 at 08:11
  • 1
    You say it crashed. What exactly happened? Which exact commands did you use? What was the error message? – Vladimir F Героям слава Feb 19 '19 at 09:33
  • @jbdv. I tried, but it gives: Open MPI tried to fork a new process via the "execve" system call but failed. Open MPI checks many things before attempting to launch a child process, but nothing is perfect. This error may be indicative of another problem on the target host, or even something as silly as having specified a directory for your application. Your job will now abort. – Shiqi He Feb 21 '19 at 13:55
  • My comment was only directed at your statement 'And if I write "mpif90 -o -pg mycode.exe mycode.f90", it does not give me the executable file, but create a file named "-pg"'. If you write `-o -pq` you tell your compiler to name the output file `-pg`. So in that case, it gives you an executable file, just not with the name you _think_ you gave it. – jbdv Feb 22 '19 at 09:37
  • @jbdv OK, I see your point. Thank you. – Shiqi He Feb 22 '19 at 15:39

1 Answers1

1

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
KL-Yang
  • 381
  • 4
  • 8
  • It gives the bug:Open MPI tried to fork a new process via the "execve" system call but failed. Open MPI checks many things before attempting to launch a child process, but nothing is perfect. This error may be indicative of another problem on the target host, or even something as silly as having specified a directory for your application. Your job will now abort. – Shiqi He Feb 21 '19 at 13:52
  • Just tried the script again, edit a typo, removed the ";" in 3rd line of the script, it should work. If continue has issue, I can post my full test suit of this example. – KL-Yang Feb 21 '19 at 15:39
  • I tried again, it still gave me the same error: "Error: Exec format error". If you can post your test suit that would be helpful. Thank you! – Shiqi He Feb 22 '19 at 15:36
  • hostfile has 3 times the localhost name – KL-Yang Feb 22 '19 at 16:32