0

I have a C MPI program that runs well but my challenge is to compile it with a Makefile. The C file is named frequencyMPI.c and I have drafted the Make file named makefile.frequencyMPI. The makefile successfully compiled on Linux terminal but I don't know how to run it. My problem is how to run it and whether it is correct. The content of the Makefile is given below:

all: program

program: frequencyMPI.o
    mpicc frequencyMPI.o -o program

frequencyMPI.o: frequencyMPI.c
    mpicc -c frequencyMPI.c -o frequencyMPI.o

clean:
    rm -f frequencyMPI.o program core *~
einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • 1
    1. What do you mean by "The makefile successfully compiled"? You can't compile Makefiles... did you run `make` or `gmake`, specifying your Makefile with `-f makefile.frequencyMPI`? 2. Have you considered using CMake instead, and letting it generate the Makefile for you? [Here](https://stackoverflow.com/a/23166953/1593077) is an explanation on how to do that. It also takes care of locating relevant libraries if they are not in the include path already. 3. Are you sure you want to run the mpicc wrapper rather than compiling directly? – einpoklum Oct 02 '21 at 19:09
  • what I meant by compiling successfully was that when I entered `make -f makefile.frequencyMPI` it did not give any errors – Udeze Leonard Oct 02 '21 at 19:12
  • Please use block code formatting when including examples (e.g., indent all lines by 4+ spaces). As mentioned above you don't "compile makefiles"; you run `make`, which uses the instructions in the makefile to build your software. The above makefile builds a program named `program`. Did you try to run that program? If the problem is that it didn't work then we need to know, what did you type? What errors did you get? – MadScientist Oct 02 '21 at 19:44
  • 1
    After running the makefile with `make -f makefile.frequencyMPI` it generated frequencyMPI.o in the directory. I tried running it with `./frequencyMPI.o` it says permission denied then i tried `sudo ./frequencyMPI.o` then it says command not found. Normally when I compile with `mpicc` on terminal I run with `mpirun -np 3 ./frequencyMPI.o` but for a make file executable, I don't know how to execute it to know if it correct. Secondly, my program is not named `program`, it is named `frequencyMPI.c` – Udeze Leonard Oct 02 '21 at 19:59
  • Thank you @MadScientist, you just clarified me the executable file I should be running is `program`. Hence I tried `mpirun -np 3 ./program` and it worked. I have clarified the matter. The fault is not from the make file. – Udeze Leonard Oct 02 '21 at 20:07
  • 1
    Just for terminology's sake, your program is not `frequencyMPI.c`. That's the _source code_ for your program. Also `frequencyMPI.o` is not a program either and cannot be run from the command line, it's an _object file_ which represents the compiled version of your source code. Then you have to _link_ that object file into a program, or _executable file_, that you have named `program` here. Then you can run the executable file. – MadScientist Oct 02 '21 at 21:12
  • Please [edit] your question instead of adding comments to clarify. Not all comments are initially shown, and they appear in order of votes, not chronologically. Make it easy to help you by putting all information in one place: the question. – Robert Oct 08 '21 at 17:50

1 Answers1

-1

Using open MPI: mpicc to compile and mpirun to run the code

So you would do:

  • make, to compile program
  • mpirun program, to run your parallel code
ACoelho
  • 341
  • 1
  • 11