I am learning MPI for parallel programming in C and I am using a processor with 4 cores. I am trying to do an example from a tutorial in which the output should be:
Hello world! I'm process 0 out of 4 processes
Hello world! I'm process 2 out of 4 processes
Hello world! I'm process 1 out of 4 processes
Hello world! I'm process 3 out of 4 processes
In whatever order.
Here is my code:
#include <stdio.h>
#include <mpi.h>
int main(int argc, char** argv)
{
int ierr, num_procs, my_id;
ierr = MPI_Init(&argc, &argv);
ierr = MPI_Comm_rank(MPI_COMM_WORLD, &my_id);
ierr = MPI_Comm_size(MPI_COMM_WORLD, &num_procs);
printf("Hello world! I'm process %i out of %i processes\n", my_id, num_procs);
ierr = MPI_Finalize();
}
I compile it using:
mpicc helloworld.c -o helloworld
And I run it using:
mpirun -np 4 helloworld
This is what is outputted:
Hello world! I'm process 0 out of 1 processes
Hello world! I'm process 0 out of 1 processes
Hello world! I'm process 0 out of 1 processes
Hello world! I'm process 0 out of 1 processes
It's outputting it 4 times which is relatively good news I guess but the program isn't recognising the number of threads and each thread ID.
Is it even running in parallel or is it just running 4 times in serial? How can I get the program to recognise the amount of threads and the thread ID properly?
Thanks in advance!
mpicc helloworld.c -o helloworld
mpirun -np 4 helloworld
Hello world! I'm process 0 out of 1 processes
Hello world! I'm process 0 out of 1 processes
Hello world! I'm process 0 out of 1 processes
Hello world! I'm process 0 out of 1 processes
This sequence clearly shows us that your MPI runtime was unable to detect parallel start, it probably from misconfiguration: your mpicc
is from one MPI implementation and your mpirun
is from other. For example, both MPICH and OpenMPI have mpicc
scripts for compiling MPI programs, but their mpiexec
/ mpirun
programs are incompatible. Compile with MPICH, start with OpenMPI starter and MPICH runtime will not receive needed environment variables to figure out parallel run and its parameters.
You should revisit list of installed packages ( dpkg -l|egrep 'mpich|openmpi'
) and check which file is from which library ( dpkg -L mpich
, dpkg -L openmpi-bin
; dpkg -L libmpich-dev
, dpkg -L libopenmpi-dev
). Ubuntu/debian also have system of "alternatives" which will install symbolic links mpicc
and mpirun
to actual scripts (do ls -l /usr/bin/mpicc /usr/bin/mpirun
to see current state of the links). Check update-alternatives
tool, its man page and docs to learn how to reset all mpi-named scripts to one implementation (and there is galternatives
GUI for it).
According to file lists in packages, mpich and openmpi have variants of mpirun/mpiexec with suffixes http://packages.ubuntu.com/yakkety/amd64/openmpi-bin/filelist http://packages.ubuntu.com/yakkety/amd64/mpich/filelist :
/usr/bin/mpiexec.openmpi
/usr/bin/mpirun.openmpi
/usr/bin/mpiexec.hydra
/usr/bin/mpiexec.mpich
/usr/bin/mpirun.mpich
Same situation for mpicc scripts: http://packages.ubuntu.com/yakkety/amd64/libopenmpi-dev/filelist http://packages.ubuntu.com/yakkety/amd64/libmpich-dev/filelist
/usr/bin/mpicc.openmpi
/usr/bin/mpicc.mpich
Always use mpicc and mpirun (or mpiexec) from the same implementation. You may also use variants with suffix to be sure: mpicc.openmpi
& mpiexec.openmpi
pair or mpicc.mpich
& mpiexec.mpich
pair.
And to use some MPI implementation, you should have it fully installed, both for bin, lib and dev packages.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.