user3261475
user3261475

Reputation:

On the installation of mpi.h for C in Ubuntu

I would like to start with MPI/C and I want to compile/execute the standard program mpi_hello. I succeeded regarding mpicc but I got an error message when I compile the file. Here is the program:

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

int main (int argc, char* argv[])

{

int mynode, totalnodes;

  MPI_Init(&argc,&argv);
  MPI_Comm_size(MPI_COMM_WORLD, &totalnodes);
  MPI_Comm_rank(MPI_COMM_WORLD, &mynode);

  printf( "\nHello world from process %d of %d\n", mynode, totalnodes );
  if(totalnodes==1) printf("You have just one processor!\n");
  MPI_Finalize();
  return 0;

}

I got the following:

turb@turb-LIFEBOOK-AH531:~/Desktop/Prog$ mpicc mpi_hello.c
turb@turb-LIFEBOOK-AH531:~/Desktop/Prog$ cc -O3 mpi_hello.c
mpi_hello.c:6:17: fatal error: mpi.h: No such file or directory
 #include <mpi.h>
                 ^
compilation terminated.

I would appreciate your help. Thank you! a

Upvotes: 2

Views: 14479

Answers (3)

user3261475
user3261475

Reputation:

When using MPI you are nor allowed to use cc -O3 name.c to compile your program. The correct command for compilation is: mpicc -O3 name.c. The command mpicc name.c has nothing to do here. Here is what you get for just one proc:

turb@turb-LIFEBOOK-AH531:~/Desktop/Prog$ mpicc -O3 mpi_hello.c
turb@turb-LIFEBOOK-AH531:~/Desktop/Prog$ ./a.out

Hello world from process 0 of 1
You have just one processor!

You may also use for more than one processor (e.g. 2) mpirun -np 2 ./a.out as mentioned above. I hope this could help a bit!

Upvotes: 0

wbt11a
wbt11a

Reputation: 818

Can you try:

module add gcc mpich2
mpicc mpi_hello.c

?

Edit: Oh wow, I completely misread your post. You successfully compiled it by the looks of it with mpicc hello_world.c

Now you should be able to execute a.out with

mpirun -np 2 ./a.out

where 2 = the number of processors. Using your code and a fresh install:

beaty@korriban:~$ mpicc test.c
beaty@korriban:~$ mpirun ./a.out

Hello world from process 0 of 1
You have just one processor!

beaty@korriban:~$ mpirun -np 2 ./a.out

Hello world from process 0 of 2

Hello world from process 1 of 2

Upvotes: 2

Timo Geusch
Timo Geusch

Reputation: 24351

You'll need to make sure your include path is updated so the compiler can find mpi.h if it's not installed in a standard location.

Upvotes: 1

Related Questions