#
# Makefile for example MPI codes
#
# $Revision: 4795 $
#

#
# utilities
#
MPI_ROOT= /opt/hpmpi

CC	= $(MPI_ROOT)/bin/mpicc
CXX	= $(MPI_ROOT)/bin/mpiCC
F77	= $(MPI_ROOT)/bin/mpif77
F90	= $(MPI_ROOT)/bin/mpif90
MPI_RUN = $(MPI_ROOT)/bin/mpirun
RM	= /bin/rm -f

#
# binaries
#
BINCC	= communicator hello_world ping_pong ping_pong_ring io
BINF77	= compute_pi compute_pi_spawn send_receive
BINCXX	= cart sort
BINF90	= master_worker

BIN	= $(BINCC) $(BINF77) $(BINCXX) $(BINF90)
BINMT	= $(BIN) multi_par thread_safe

RUN	= $(BIN:=.run)
RUNMT	= $(BINMT:=.run)

#
# targets
#

default:
	@ if [ -x /lib/libcps.1 ]; then \
	    make all_mt ; \
	else \
	    make all ; \
	fi

all: $(RUN)                      # compile and run

all_mt: $(RUNMT)		 # compile and run with threads

compile: $(BIN)                  # compile only

compile_mt: $(BINMT)		 # compile only with threads

clean:
	$(RM) $(BINMT) *.o

#
# dependencies
#
sort.run: sort
	$(MPI_RUN) -np 4 ./$?  # any number of processes
sort: sort.o
	$(CXX) -o $@ sort.o

cart.run: cart
	$(MPI_RUN) -np 4 ./$?  # any number of processes
cart: cart.o
	$(CXX) -o $@ cart.o

thread_safe.run: thread_safe
	$(MPI_RUN) -np 2 ./$?  # must be 2 processes

thread_safe: thread_safe.o
	$(CC) -o $@ thread_safe.o -lmtmpi

communicator.run: communicator
	$(MPI_RUN) -np 2 ./$?  # must be 2 processes
communicator: communicator.o
	$(CC) -o $@ communicator.o

hello_world.run: hello_world
	$(MPI_RUN) -np 4 ./$?  # any number of processes
hello_world: hello_world.o
	$(CC) -o $@ hello_world.o

ping_pong.run: ping_pong
	$(MPI_RUN) -np 2 ./$? 0  # must be 2 processes, takes message length
ping_pong: ping_pong.o
	$(CC) -o $@ ping_pong.o

ping_pong_ring.run: ping_pong_ring
	$(MPI_RUN) -np 4 ./$? 0  # at least 2 processes
ping_pong_ring: ping_pong_ring.o
	$(CC) -o $@ ping_pong_ring.o

compute_pi.run: compute_pi
	$(MPI_RUN) -np 4 ./$?  # any number of processes
compute_pi: compute_pi.o
	$(F90) -o $@ compute_pi.o

compute_pi_spawn.run: compute_pi_spawn
	$(MPI_RUN) -commd -np 1 ./$?  # any number of processes
compute_pi_spawn: compute_pi_spawn.o
	$(F90) -o $@ compute_pi_spawn.o

send_receive.run: send_receive
	$(MPI_RUN) -np 4 ./$?  # at least 2 processes
send_receive: send_receive.o
	$(F90) -o $@ send_receive.o

master_worker.run: master_worker
	$(MPI_RUN) -np 4 ./$?  # at least 2 processes
master_worker: master_worker.f90
	$(F90) -o $@ master_worker.f90

io.run: io
	$(MPI_RUN) -np 4 ./$?  # any number of processes
io: io.o
	$(CC) -o $@ io.o

multi_par.run: multi_par
	@( \
		echo "Running multi_par on a single host.  This example" ; \
		echo "can also be run with mpirun -f appfile with an" ; \
		echo "appfile of the form" ; \
		echo "  -h host1 -np 1 -e MP_GANG=ON /dir1/multi_par" ; \
		echo "  -h host2 -np 1 -e MP_GANG=ON /dir2/multi_par" ; \
		$(MPI_RUN) -e MP_GANG=ON -np 1 ./multi_par ; \
	)
multi_par: multi_par.o
	$(F90) -o $@ multi_par.o +Oopenmp +O3 +Oparallel
multi_par.o: multi_par.f
	$(F90) -c multi_par.f +Oopenmp +O3 +Oparallel

.C.o:
	$(CXX) -I. -c $<

.c.o:
	$(CC) -c $<

.f.o:
	$(F90) -c $<
