# makefile for parallel programs i.e. MPICH, OpenMPI, etc  
#       Author: Antonio Carrillo Ledesma 29/12/2010
#       antonio@mmc.geofisica.unam.mx
#       http://www.mmc.geofisica.unam.mx/acl/
#
# usage:
#   First automatic dependency calculation
#     $ make deps
#
#   Compile and link
#     $ make
#
#   Run
#     $ make run
#
#   Clean
#     $ make clean
#
#   Display callable targets
#     $ make help




# Defining the sources
SOURCES = $(wildcard *.cpp)

# Parameters of executable
PARAMETERS = 

# Defining the numbers of CPUs
CPUS = -np 4

# Defining the neme of th executable
EXECUTABLE = ejemplo

# Defining the debug Flags
DEBUG += -O2	        # Optimization

# Defining the compiler Flags
CFLAGS =  $(DEBUG)

# Defining the linker Flags
LDFLAGS = -lm $(DEBUG)

# Defining the compiler:
CC = mpic++



###################################################
# Defining the object files:
OBJECTS = $(SOURCES:.cpp=.o)
# Load the automatic dependency
DEPS = $(OBJECTS:.o=.d)
# target: all - The default rule - compiling our main program:
all: $(SOURCES)  $(EXECUTABLE) 
	echo all: make complete
$(EXECUTABLE): $(OBJECTS)
	$(CC) $(LDFLAGS) $(OBJECTS) -o $@
# Tell make how to build .o files from .cpp files:
$(OBJECTS):
	$(CC) -MD -c $(CFLAGS)  $(@:.o=.cpp) -o $@
#Now make sure that make rebuilds files if included headers change:
-include $(DEPS)
# target: deps - Automatic dependency calculation
deps: $(SOURCES)
	$(CC) -MD -E $(SOURCES) > /dev/null
# The .PHONY rule keeps make from doing something with a file named clean
.PHONY: clean
# target: clean - Removing the executable and the object files
clean:
	rm $(EXECUTABLE) $(OBJECTS) $(DEPS)
	echo clean: make complete
# target: run - Run the application
run:
	mpirun $(CPUS) ./$(EXECUTABLE) $(PARAMETERS)
# target: help - Display callable targets.
help:
	egrep "^# target:" [Mm]akefile
###################################################


